What is the easiest way to initialize an array of N numbers after a simple pattern?

Let, say, the first N integers divisible by 3, starting from 9.

I'm sure there is one linear solution using lambdas, I just don't know what the area of โ€‹โ€‹the language is good enough.

+4
source share
7 answers

Just to be different (and to avoid using the where clause), you can also:

var numbers = Enumerable.Range(0, n).Select(i => i * 3 + 9); 

Update . It also has an advantage when the number does not end.

+5
source

Using Linq:

 int[] numbers = Enumerable.Range(9,10000) .Where(x => x % 3 == 0) .Take(20) .ToArray(); 

You can also easily parallelize using PLinq if you need to:

 int[] numbers = Enumerable.Range(9,10000) .AsParallel() //added this line .Where(x => x % 3 == 0) .Take(20) .ToArray(); 
+5
source
 const int __N = 100; const int __start = 9; const int __divisibleBy = 3; var array = Enumerable.Range(__start, __N * __divisibleBy).Where(x => x % __divisibleBy == 0).Take(__N).ToArray(); 
+1
source
 int n = 10; // Take first 10 that meet criteria int[] ia = Enumerable .Range(0,999) .Where(a => a % 3 == 0 && a.ToString()[0] == '9') .Take(n) .ToArray(); 
+1
source

I canโ€™t say that itโ€™s good, I am not an expert in C # and I just hit it, but I think this is probably a canonical example of using yield .

 internal IEnumerable Answer(N) { int n=0; int i=9; while (true) { if (i % 3 == 0) { n++; yield return i; } if (n>=N) return; i++; } } 
0
source

You need to go through 0 or 1 to N and add them manually. Or you can simply create your own function f (int n), and in this function you will cache the results inside the session or the global hash table or dictionary.

Pseudo-code, where ht is the global Hashtable or Dictionary (highly recommended later because it is strongly typed.

 public int f(int n) { if(ht[n].containsValue) return ht[n]; else { //do calculation ht[n] = result; return result; } } 

Just a note. If you do this type of functional programming all the time, you can check out F # or maybe even Iron Ruby or Python.

0
source

I want to see how this solution stacks with the above Linq solutions. The trick here is changing the predicate, using the fact that the set (q % m) starting with s is (s + (s % m) + m*n) (where n represents the nth value in the set). In our case, s=q .

The only problem with this solution is that it has a side effect of making your implementation dependent on the template you choose (and not all templates have a suitable predicate). But this has the advantage that:

  • There are always exactly n iterations
  • Never work out like the solutions suggested above (refer to the limited Range ).

In addition, no matter which template you choose, you will always need to change the predicate so that you can make it mathematically efficient:

  static int[] givemeN(int n) { const int baseVal = 9; const int modVal = 3; int i = 0; return Array.ConvertAll<int, int>( new int[n], new Converter<int, int>( x => baseVal + (baseVal % modVal) + ((i++) * modVal) )); } 

edit: I just want to illustrate how you can use this method with delegate to improve code reuse:

  static int[] givemeN(int n, Func<int, int> func) { int i = 0; return Array.ConvertAll<int, int>(new int[n], new Converter<int, int>(a => func(i++))); } 

You can use it with givemeN(5, i => 9 + 3 * i) . Note again that I changed the predicate, but you can do this with most simple templates as well.

0
source

Source: https://habr.com/ru/post/1276717/


All Articles