Jump to content

Magnanimous numbers: Difference between revisions

Added C# version
(→‎{{header|Raku}}: various style / efficiency tweaks)
(Added C# version)
Line 341:
486685 488489 515116 533176 551558 559952 595592 595598 600881 602081
</pre>
 
=={{header|C#|CSharp}}==
<lang csharp>using System; using static System.Console;
 
class Program {
 
static bool[] np; // not-prime array
 
static void ms(long lmt) { // populates array, a not-prime is true
np = new bool[lmt]; np[0] = np[1] = true;
for (long n = 2, j = 1; n < lmt; n += j, j = 2) if (!np[n])
for (long k = n * n; k < lmt; k += n) np[k] = true; }
 
static bool is_Mag(long n) { long res, rem;
for (long p = 10; n >= p; p *= 10) {
res = Math.DivRem (n, p, out rem);
if (np[res + rem]) return false; } return true; }
 
static void Main(string[] args) { ms(100_009); string mn;
WriteLine("First 45{0}", mn = " magnanimous numbers:");
for (long l = 0, c = 0; c < 400; l++) if (is_Mag(l)) {
if (c++ < 45 || (c > 240 && c <= 250) || c > 390)
Write(c <= 45 ? "{0,4} " : "{0,8:n0} ", l);
if (c < 45 && c % 15 == 0) WriteLine();
if (c == 240) WriteLine ("\n\n241st through 250th{0}", mn);
if (c == 390) WriteLine ("\n\n391st through 400th{0}", mn); } }
}</lang>
 
{{out}}
<pre>First 45 magnanimous numbers:
0 1 2 3 4 5 6 7 8 9 11 12 14 16 20
21 23 25 29 30 32 34 38 41 43 47 49 50 52 56
58 61 65 67 70 74 76 83 85 89 92 94 98 101 110
 
241st through 250th magnanimous numbers:
17,992 19,972 20,209 20,261 20,861 22,061 22,201 22,801 22,885 24,407
 
391st through 400th magnanimous numbers:
486,685 488,489 515,116 533,176 551,558 559,952 595,592 595,598 600,881 602,081</pre>
 
=={{header|C++}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.