Primes which contain only one odd digit: Difference between revisions
Content added Content deleted
(→{{header|ALGOL 68}}: Use Algol 68-primes) |
(Added C# entry) |
||
Line 109: | Line 109: | ||
Primes which contain only one even number 1-999: 78 |
Primes which contain only one even number 1-999: 78 |
||
</pre> |
</pre> |
||
=={{header|C#|CSharp}}== |
|||
Modifies a conventional prime sieve to cull the items with more than one odd digit. |
|||
<lang csharp>using System; |
|||
using System.Collections.Generic; |
|||
class Program |
|||
{ |
|||
// starts as an ordinary odds-only prime sieve, but becomes |
|||
// extraordinary after the else statement... |
|||
static List<uint> sieve(uint max, bool ordinary = false) |
|||
{ |
|||
uint k = ((max - 3) >> 1) + 1, |
|||
lmt = ((uint)(Math.Sqrt(max++) - 3) >> 1) + 1; |
|||
var pl = new List<uint> { }; |
|||
var ic = new bool[k]; |
|||
for (uint i = 0, p = 3; i < lmt; i++, p += 2) if (!ic[i]) |
|||
for (uint j = (p * p - 3) >> 1; j < k; j += p) ic[j] = true; |
|||
if (ordinary) |
|||
{ |
|||
pl.Add(2); |
|||
for (uint i = 0, j = 3; i < k; i++, j += 2) |
|||
if (!ic[i]) pl.Add(j); |
|||
} |
|||
else |
|||
for (uint i = 0, j = 3, t = j; i < k; i++, t = j += 2) |
|||
if (!ic[i]) |
|||
{ |
|||
while ((t /= 10) > 0) |
|||
if (((t % 10) & 1) == 1) goto skip; |
|||
pl.Add(j); |
|||
skip:; |
|||
} |
|||
return pl; |
|||
} |
|||
static void Main(string[] args) |
|||
{ |
|||
var pl = sieve((uint)1e9); |
|||
uint c = 0, l = 10, p = 1; |
|||
Console.WriteLine("List of one-odd-digit primes < 1,000:"); |
|||
for (int i = 0; pl[i] < 1000; i++) |
|||
Console.Write("{0,3}{1}", pl[i], i % 9 == 8 ? "\n" : " "); |
|||
string fmt = "\nFound {0:n0} one-odd-digit primes < 10^{1} ({2:n0})"; |
|||
foreach (var itm in pl) |
|||
if (itm < l) c++; |
|||
else Console.Write(fmt, c++, p++, l, l *= 10); |
|||
Console.Write(fmt, c++, p++, l); |
|||
} |
|||
} |
|||
</lang> |
|||
{{out}} |
|||
<pre>List of one-odd-digit primes < 1,000: |
|||
3 5 7 23 29 41 43 47 61 |
|||
67 83 89 223 227 229 241 263 269 |
|||
281 283 401 409 421 443 449 461 463 |
|||
467 487 601 607 641 643 647 661 683 |
|||
809 821 823 827 829 863 881 883 887 |
|||
Found 3 one-odd-digit primes < 10^1 (10) |
|||
Found 12 one-odd-digit primes < 10^2 (100) |
|||
Found 45 one-odd-digit primes < 10^3 (1,000) |
|||
Found 171 one-odd-digit primes < 10^4 (10,000) |
|||
Found 619 one-odd-digit primes < 10^5 (100,000) |
|||
Found 2,560 one-odd-digit primes < 10^6 (1,000,000) |
|||
Found 10,774 one-odd-digit primes < 10^7 (10,000,000) |
|||
Found 46,708 one-odd-digit primes < 10^8 (100,000,000) |
|||
Found 202,635 one-odd-digit primes < 10^9 (1,000,000,000)</pre> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |