Frobenius numbers: Difference between revisions
m
→{{header|C#|CSharp}}: marked (with an asterisk) the non-primes among the numbers
(Added C# entry) |
m (→{{header|C#|CSharp}}: marked (with an asterisk) the non-primes among the numbers) |
||
Line 7:
=={{header|C#|CSharp}}==
Asterisks mark the non-primes among the numbers. Perhaps the draft task creator ought to edit the title to reflect that not all the results are prime numbers...
<lang csharp>using System.Collections.Generic; using System.Linq; using static System.Console; using static System.Math;
class Program
static bool ispr(int x) { int lim = (int)Sqrt((double)x);
if (x < 2) return false; if ((x % 3) == 0) return x == 0; bool odd = false;
if (x % d == 0) return false; } return true; }
static void Main() {
int c = 0, d = 0, f, lim = 1000000, l2 = lim / 100; var Frob = PG.Primes((int)Sqrt(lim) + 1).ToArray();
for (int n = 0, m = 1; m < Frob.Length; n = m++) {
if ((f = Frob[n] * Frob[m] - Frob[n] - Frob[m]) < l2) d++;
Write("{0,7:n0}
"of which {2} were under {3:n0}", c, lim, d, l2); } }
class PG { public static IEnumerable<int> Primes(int lim) {
var flags = new bool[lim + 1]; int j = 3; yield return 2;
▲ for (j = 4; j <= lim; j += 2) flags[j] = true; j = 3;
for (int d = 8, sq = 9; sq <= lim; j += 2, sq += d += 8)
if (!flags[j]) { yield return j;
Line 26 ⟶ 33:
{{out}}
<pre> 1
1,079
5,615
16,379
32,039
55,215
82,343
126,015
175,559
222,747
303,575
370,871 376,991
443,519
547,595
664,199 674,039
770,879
900,591
=={{header|REXX}}==
|