Frobenius numbers: Difference between revisions

Content added Content deleted
(Added C# entry)
m (→‎{{header|C#|CSharp}}: marked (with an asterisk) the non-primes among the numbers)
Line 7: Line 7:


=={{header|C#|CSharp}}==
=={{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;
<lang csharp>using System.Collections.Generic; using System.Linq; using static System.Console; using static System.Math;


class Program { static void Main() {
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;
for (int d = 5; d <= lim; d += (odd = !odd) ? 2 : 4) {
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();
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++) {
for (int n = 0, m = 1; m < Frob.Length; n = m++) {
if ((f = Frob[n] * Frob[m] - Frob[n] - Frob[m]) < l2) d++;
if ((f = Frob[n] * Frob[m] - Frob[n] - Frob[m]) < l2) d++;
Write("{0,7:n0} {1}", f , ++c % 10 == 0 ? "\n" : ""); }
Write("{0,7:n0}{2} {1}", f , ++c % 10 == 0 ? "\n" : "", ispr(f) ? " " : "*"); }
WriteLine("\n\nFound {0} Frobenius numbers of consecutive primes under {1:n0}," +
Write("\n\nCalculated {0} Frobenius numbers of consecutive primes under {1:n0}," +
"of which {2} were under {3:n0}", c, lim, d, l2); } }
"of which {2} were under {3:n0}", c, lim, d, l2); } }


class PG { public static IEnumerable<int> Primes(int lim) {
class PG { public static IEnumerable<int> Primes(int lim) {
var flags = new bool[lim + 1]; int j; yield return 2;
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)
for (int d = 8, sq = 9; sq <= lim; j += 2, sq += d += 8)
if (!flags[j]) { yield return j;
if (!flags[j]) { yield return j;
Line 26: Line 33:


{{out}}
{{out}}
<pre> 1 7 23 59 119 191 287 395 615 839
<pre> 1* 7 23 59 119* 191 287* 395* 615* 839
1,079 1,439 1,679 1,931 2,391 3,015 3,479 3,959 4,619 5,039
1,079* 1,439 1,679* 1,931 2,391* 3,015* 3,479* 3,959* 4,619* 5,039
5,615 6,395 7,215 8,447 9,599 10,199 10,811 11,447 12,095 14,111
5,615* 6,395* 7,215* 8,447 9,599* 10,199* 10,811* 11,447 12,095* 14,111*
16,379 17,679 18,767 20,423 22,199 23,399 25,271 26,891 28,551 30,615
16,379* 17,679* 18,767* 20,423* 22,199* 23,399 25,271* 26,891 28,551* 30,615*
32,039 34,199 36,479 37,631 38,807 41,579 46,619 50,171 51,527 52,895
32,039* 34,199* 36,479 37,631* 38,807* 41,579 46,619 50,171* 51,527* 52,895*
55,215 57,119 59,999 63,999 67,071 70,215 72,359 74,519 77,279 78,959
55,215* 57,119 59,999 63,999* 67,071* 70,215* 72,359* 74,519* 77,279 78,959*
82,343 89,351 94,859 96,719 98,591 104,279 110,879 116,255 120,407 122,495
82,343* 89,351* 94,859* 96,719* 98,591* 104,279* 110,879 116,255* 120,407* 122,495*
126,015 131,027 136,151 140,615 144,395 148,215 153,647 158,399 163,199 170,543
126,015* 131,027* 136,151* 140,615* 144,395* 148,215* 153,647* 158,399* 163,199 170,543*
175,559 180,599 185,759 189,215 193,595 198,015 204,287 209,759 212,519 215,291
175,559* 180,599* 185,759* 189,215* 193,595* 198,015* 204,287* 209,759* 212,519* 215,291*
222,747 232,307 238,139 244,019 249,995 255,015 264,159 271,439 281,879 294,839
222,747* 232,307 238,139* 244,019* 249,995* 255,015* 264,159* 271,439* 281,879* 294,839*
303,575 312,471 319,215 323,759 328,319 337,535 346,911 354,015 358,799 363,599
303,575* 312,471* 319,215* 323,759 328,319* 337,535* 346,911* 354,015* 358,799* 363,599*
370,871 376,991 380,687 389,339 403,199 410,879 414,731 421,191 429,015 434,279
370,871 376,991* 380,687* 389,339* 403,199* 410,879* 414,731 421,191* 429,015* 434,279*
443,519 454,271 461,031 470,579 482,999 495,599 508,343 521,267 531,431 540,215
443,519* 454,271* 461,031* 470,579 482,999* 495,599* 508,343* 521,267 531,431* 540,215*
547,595 556,499 566,999 574,559 583,679 592,895 606,791 625,655 643,167 654,479
547,595* 556,499* 566,999 574,559* 583,679* 592,895* 606,791 625,655* 643,167* 654,479*
664,199 674,039 678,971 683,927 693,863 713,975 729,311 734,447 739,595 755,111
664,199 674,039* 678,971 683,927* 693,863* 713,975* 729,311* 734,447* 739,595* 755,111*
770,879 776,159 781,451 802,715 824,459 835,379 851,903 868,607 879,839 889,239
770,879* 776,159 781,451* 802,715* 824,459 835,379 851,903* 868,607* 879,839 889,239*
900,591 919,631 937,019 946,719 958,431 972,179 986,039
900,591* 919,631 937,019* 946,719* 958,431* 972,179* 986,039*


Found 167 Frobenius numbers of consecutive primes under 1,000,000, of which 25 were under 10,000</pre>
Calculated 167 Frobenius numbers of consecutive primes under 1,000,000,of which 25 were under 10,000</pre>


=={{header|REXX}}==
=={{header|REXX}}==