C# Program to Generate Odd Numbers in Parallel using LINQ


Introduction

This article will teach you how to create a C# program that uses LINQ to generate odd numbers in parallel. Let's take a quick look at the language. The creation of desktop, online, and mobile apps generally uses the C# programming language. One of the strengths of C# is Linguistic Integrated Query, or LINQ. Data from many sources, including arrays, collections, and databases, can be rapidly accessed by developers. It provides a common syntax for data queries, regardless of the data source. As the syntax of LINQ and SQL is similar, developers can quickly understand and use them.

Problem Explanation

To further understand the LINQ, let's write a program to generate odd numbers in parallel. As the first step of any program, the problem description and needs must be determined, and in this problem, it offers a selection of techniques for querying objects that use ParallelQuery {TSource} to generate odd numbers in parallel using LINQ. Also, in order to obtain the odd numbers, we must use the where and select clauses

Two LINQ methods will be used to complete this task, as follows −

  • Select()

  • Where()

Let's now briefly discuss both methods.

Select()

When a query is executed, the select clause of the query expression specifies the kind of data that will be generated. Each of the previous clauses and any expressions found inside the select clause itself are evaluated to determine the result. A query statement must have a select clause at the end.

Where()

Use the where clause in the query expression to select the elements from the data source that will be returned. It applies a Boolean condition (predicate) to each source element (referred to by the range variable) and then returns any source elements for which the condition is true. A single query expression may have several where clauses, just as it may contain multiple predicate subexpressions.

Syntax Using Where and Select

IEnumerable<int> variable = ((ParallelQuery<int>)ParallelEnumerable.Range(start, stop)).Where(x => x % 2 != 0).Select(i => i); 

Let’s suppose that we have to generate the odd numbers in parallel within some specific range. So we will have to mention the range, i.e., (10, 21), and have to find all the odd numbers between them.

Input

Range(start, stop)= Range(10, 21) 

We currently have numbers with a defined range, and we have to generate all the odd numbers within this specific range. So our output will be something like this −

Output

13
17
19
11
15

Let’s see the algorithms in the next section.

Algorithm

The steps below to generate odd numbers in parallel using LINQ in a C# program.

Step 1  Firstly we will include the range, i.e., (10, 21).

Step 2  We will now write the code for the Parallel Query in the range 10 to 21.

Step 3  Take a variable x to check the condition for odd numbers.

Step 4  Use the Where clause to verify that the modulus of x by two is not equal to zero.

Step 5  Now take another variable, y.

Step 6  Use the Select clause to determine whether the value of the y variable is greater than or equal to the value of the variable (i.e., y).

Step 7  Use a foreach loop to iterate the odd numbers.

Step 8  Display all the odd numbers.

The steps of the algorithm are very simple; now let’s have a look at the code.

Example

Let's use an example to understand the problem −

// C# program to generate odd numbers in parallel using LINQ
using System;
using System.Linq;
using System.Collections.Generic; 
class TutorialPoint{
   static void Main(string[] args){
      // Execute a parallel query within range 10 to 11.
      IEnumerable<int> odd = ((ParallelQuery<int>)ParallelEnumerable.Range(10, 11))
   
      // Check the condition for odd numbers using the Where clause.
      .Where(x => x % 2 != 0)
   
      //Use the Select clause to select those odd numbers.
      .Select(y => y);
      
      // Display the odd numbers
      foreach (int n in odd){
         Console.WriteLine(n);
      }
      Console.ReadLine();
   }
}

Output

17
19
11
13
15

Time Complexity

The time complexity of a C# a program to generate odd numbers in parallel using LINQ depends on the size of the input range.

Assuming that the program generates the odd numbers using a LINQ query, the time complexity of the query will be O(n), where n is the number of odd numbers to be generated . This is because the query needs to iterate over the sequence of numbers to determine which ones are odd. However, when the program runs the query in parallel, the time complexity will depend on the number of processors or cores available in the system, as well as the efficiency of the parallelization implementation. If the program uses a good parallelization algorithm, it can divide the task of generating the odd numbers into smaller sub-tasks and distribute them across multiple processors or cores. This can reduce the overall execution time of the program.

So if we talk about the time complexity of a C# a program to generate odd numbers in parallel using LINQ, it is O(n) for the LINQ query. As we are traversing in the loop once, and the times we traverse depends on the numbers given in input, the time complexity derives O(n).

Conclusion

In this article, we have discussed extensively the C# program to generate odd numbers in parallel. We have made sure not to jump directly to code, but we have discussed the syntax, especially the where clause. We have also talked about the algorithm. We have talked about the code in depth. We have generated the relevant output of the code. We hope that this article has been useful in enhancing your knowledge in C#.

Updated on: 31-Mar-2023

254 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements