Truncatable primes: Difference between revisions
Content deleted Content added
m →{{header|PARI/GP}}: lang tag |
→{{header|C}}: replace code: number method; non-portable function; speed |
||
Line 155:
=={{header|C}}==
<lang C>#include <stdio.h>
#include <
#include <
#define MAX_PRIME 1000000
int isprime(int n){▼
if(n<10)return isprm[n];▼
if(!(n&1)||!(n%3))return 0;▼
while(a<h){▼
}▼
return 1;▼
}▼
/* Sieve. If we were to handle 10^9 range, use bit field. Regardless,
* if a large amount of prime numbers need to be tested, sieve is fast.
*/
void init_primes()
{
int j;
primes = malloc(sizeof(char) * MAX_PRIME);
memset(primes, 1, MAX_PRIME);
primes[0] = primes[1] = 0;
int i = 2;
while (i * i < MAX_PRIME) {
for (j = i * 2; j < MAX_PRIME; j += i)
primes[j] = 0;
while (++i < MAX_PRIME && !primes[i]);
▲ }
}
{
int tens = 1;
while (tens < n) tens *= 10;
▲ char s[32];
tens /= 10;
while(isprime(n)){▼
n %= tens;
▲ }
return 0;▼
}
{
if (!primes[n]) return 0;
n /=
▲ return 1;
}
int main()
{
int n;
int max_left = 0, max_right = 0;
init_primes();
for (n = MAX_PRIME - 1; !max_left; n -= 2)
if (left_trunc(n)) max_left = n;
for (n = MAX_PRIME - 1; !max_right; n -= 2)
if (right_trunc(n)) max_right = n;
printf("Left: %d; right: %d\n", max_left, max_right);
▲ return 0;
}</lang>output<lang>Left: 998443; right: 739399</lang>
=={{header|C sharp|C#}}==
|