Truncatable primes: Difference between revisions
Content deleted Content added
Added C code |
|||
Line 82:
Largest RTP from 1..1000000: 739399
</pre>
=={{header|C}}==
<lang C>#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
int isprime(int n){
int isprm[]={0,0,1,1,0,1,0,1,0,0};
int a=7,h;
if(n<10)return isprm[n];
if(!(n&1)||!(n%3))return 0;
h=sqrt(n)+3;
while(a<h){
if(!(n%a)||!(n%(a-2)))return 0;
a+=6;
}
return 1;
}
void leftTrunc(char*s,int l){
memmove(s,s+1,l);
}
void rightTrunc(char*s,int l){
s[l-1]=0;
}
int isTruncPrime(int n,void(*func)(char*,int)){
char s[32];
while(isprime(n)){
ltoa(n,s,10);
if(strchr(s,'0'))break;
func(s,strlen(s));
if(!(n=atol(s)))return 1;
}
return 0;
}
void getHiTruncPrime(int n,void(*func)(char*,int)){
while(n--){
if(isTruncPrime(n,func)){
printf("%d\n",n);
break;
}
}
}
int main(){
puts("Highest left- and right-truncatable primes under 1e6:");
getHiTruncPrime(1000000,leftTrunc);
getHiTruncPrime(1000000,rightTrunc);
puts("Press Enter");
getchar();
return 0;
}</lang>
Output:
<pre>Highest left- and right-truncatable primes under 1e6:
998443
739399
Press Enter</pre>
=={{header|D}}==
<lang d>import std.stdio, std.math, std.string, std.conv;
|