Truncatable primes: Difference between revisions
Content deleted Content added
→{{header|C}}: Adding C# |
Shorter D version |
||
Line 272: | Line 272: | ||
} |
} |
||
bool |
bool isTruncatablePrime(bool left)(int n) { |
||
string s = to!string(n); |
string s = to!string(n); |
||
if (indexOf(s, '0') != -1) |
if (indexOf(s, '0') != -1) |
||
return false; |
return false; |
||
foreach (i; 0 .. s.length) |
foreach (i; 0 .. s.length) |
||
if ( |
static if (left) { |
||
⚫ | |||
⚫ | |||
return |
return false; |
||
} else { |
|||
} |
|||
if (!isPrime(to!int(s[0 .. i+1]))) |
|||
⚫ | |||
bool isRightTruncatablePrime(int n) { |
|||
} |
|||
string s = to!string(n); |
|||
if (indexOf(s, '0') != -1) |
|||
return false; |
|||
foreach (i; 0 .. s.length) |
|||
⚫ | |||
return false; |
|||
return true; |
return true; |
||
} |
} |
||
Line 295: | Line 290: | ||
enum int n = 1_000_000; |
enum int n = 1_000_000; |
||
foreach_reverse (i; 2 .. n) |
foreach_reverse (i; 2 .. n) |
||
if ( |
if (isTruncatablePrime!true(i)) { |
||
writeln("Largest left-truncatable prime in 2 .. ", n, ": ", i); |
writeln("Largest left-truncatable prime in 2 .. ", n, ": ", i); |
||
break; |
break; |
||
} |
} |
||
foreach_reverse (i; 2 .. n) |
foreach_reverse (i; 2 .. n) |
||
if ( |
if (isTruncatablePrime!false(i)) { |
||
writeln("Largest right-truncatable prime in 2 .. ", n, ": ", i); |
writeln("Largest right-truncatable prime in 2 .. ", n, ": ", i); |
||
break; |
break; |