<p>Let's say the first N integers divisible by 3 starting with 9.</p>

<p>I'm sure there is some one line solution using lambdas, I just don't know it that area of the language well enough yet.</p>

[{'answer_id': 67552, 'author': '1800 INFORMATION', 'author_id': 3146, 'author_profile': 'https://Stackoverflow.com/users/3146', 'pm_score': 0, 'selected': False, 'text': "<p>I can't say this is any good, I'm not a C# expert and I just whacked it out, but I think it's probably a canonical example of the use of <code>yield</code>.</p>\n\n<pre><code>internal IEnumerable Answer(N)\n{\n  int n=0;\n  int i=9;\n  while (true)\n  {\n    if (i % 3 == 0)\n    {\n      n++;\n      yield return i;\n    }\n\n    if (n&gt;=N) return;\n    i++;\n  }\n}\n</code></pre>\n"}, {'answer_id': 67562, 'author': 'Charles Graham', 'author_id': 7705, 'author_profile': 'https://Stackoverflow.com/users/7705', 'pm_score': 0, 'selected': False, 'text': '<p>You have to iterate through 0 or 1 to N and add them by hand.  Or, you could just create your function f(int n), and in that function, you cache the results inside session or a global hashtable or dictionary.</p>\n\n<p>Pseudocode, where ht is a global Hashtable or Dictionary (strongly recommend the later, because it is strongly typed.</p>\n\n<pre><code>public int f(int n)\n{\n  if(ht[n].containsValue)\n    return ht[n];\n  else\n  {\n    //do calculation\n    ht[n] = result;\n    return result;\n   }\n}\n</code></pre>\n\n<p>Just a side note.  If you do this type of functional programming all the time, you might want to check out F#, or maybe even Iron Ruby or Python.</p>\n'}, {'answer_id': 67677, 'author': 'cfeduke', 'author_id': 5645, 'author_profile': 'https://Stackoverflow.com/users/5645', 'pm_score': 1, 'selected': False, 'text': '<pre><code>const int __N = 100;\nconst int __start = 9;\nconst int __divisibleBy = 3;\n\n\nvar array = Enumerable.Range(__start, __N * __divisibleBy).Where(x =&gt; x % __divisibleBy == 0).Take(__N).ToArray();\n</code></pre>\n'}, {'answer_id': 67695, 'author': 'porges', 'author_id': 10311, 'author_profile': 'https://Stackoverflow.com/users/10311', 'pm_score': 3, 'selected': False, 'text': '<p>Using Linq:</p>\n\n<pre><code>int[] numbers =\n    Enumerable.Range(9,10000)\n    .Where(x =&gt; x % 3 == 0)\n    .Take(20)\n    .ToArray();\n</code></pre>\n\n<p>Also easily parallelizeable using PLinq if you need:</p>\n\n<pre><code>int[] numbers =\n    Enumerable.Range(9,10000)\n    .AsParallel()             //added this line\n    .Where(x =&gt; x % 3 == 0)\n    .Take(20)\n    .ToArray();\n</code></pre>\n'}, {'answer_id': 67721, 'author': 'Guy', 'author_id': 1463, 'author_profile': 'https://Stackoverflow.com/users/1463', 'pm_score': 1, 'selected': False, 'text': "<pre><code>int n = 10; // Take first 10 that meet criteria\nint[] ia = Enumerable\n              .Range(0,999)\n              .Where(a =&gt; a % 3 == 0 &amp;&amp; a.ToString()[0] == '9')\n              .Take(n)\n              .ToArray();\n</code></pre>\n"}, {'answer_id': 67933, 'author': 'Loren Segal', 'author_id': 6436, 'author_profile': 'https://Stackoverflow.com/users/6436', 'pm_score': 1, 'selected': False, 'text': "<p>I want to see how this solution stacks up to the above Linq solutions. The trick here is modifying the predicate using the fact that the set of <code>(q % m)</code> starting from <code>s</code> is <code>(s + (s % m) + m*n)</code> (where n represent's the nth value in the set). In our case <code>s=q</code>. </p>\n\n<p>The only problem with this solution is that it has the side effect of making your implementation depend on the specific pattern you choose (and not all patterns have a suitable predicate). But it has the <em>advantage</em> of:</p>\n\n<ol>\n<li>Always running in exactly n iterations </li>\n<li>Never failing like the above proposed solutions (wrt to the limited <code>Range</code>). </li>\n</ol>\n\n<p>Besides, no matter what pattern you choose, you will always need to modify the predicate, so you might as well make it mathematically efficient: </p>\n\n<pre><code>static int[] givemeN(int n)\n{\n    const int baseVal = 9;\n    const int modVal = 3;\n\n    int i = 0;\n    return Array.ConvertAll&lt;int, int&gt;(\n        new int[n],\n        new Converter&lt;int, int&gt;(\n            x =&gt; baseVal + (baseVal % modVal) + \n                ((i++) * modVal)\n        ));\n}\n</code></pre>\n\n<p>edit: I just want to illustrate how you could use this method with a <code>delegate</code> to improve code re-use:</p>\n\n<pre><code>static int[] givemeN(int n, Func&lt;int, int&gt; func)\n{\n    int i = 0;\n    return Array.ConvertAll&lt;int, int&gt;(new int[n], new Converter&lt;int, int&gt;(a =&gt; func(i++)));\n}\n</code></pre>\n\n<p>You can use it with <code>givemeN(5, i =&gt; 9 + 3 * i)</code>. Again note that I modified the predicate, but you can do this with most simple patterns too.</p>\n"}, {'answer_id': 68036, 'author': 'Cameron MacFarland', 'author_id': 3820, 'author_profile': 'https://Stackoverflow.com/users/3820', 'pm_score': 4, 'selected': True, 'text': '<p>Just to be different (and to avoid using a where statement) you could also do:</p>\n\n<pre><code>var numbers = Enumerable.Range(0, n).Select(i =&gt; i * 3 + 9);\n</code></pre>\n\n<p><strong>Update</strong> This also has the benefit of not running out of numbers.</p>\n'}]
['https://Stackoverflow.com/questions/67492', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5056/']