1
0
Fork 0

Problem 37

This commit is contained in:
Jonathan Chan 2016-12-05 19:04:17 -08:00
parent a994d0c558
commit 990fbe1cca
2 changed files with 43 additions and 0 deletions

BIN
37 Executable file

Binary file not shown.

43
37.c Normal file
View File

@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include "primes.h"
bool* primesTable;
bool isLeftTrunc (int n) {
int digits = floor (log10 (n)) + 1;
for (int i = 1; i <= digits; i++) {
if (!primesTable[n % (int) pow (10, i)])
return false;
}
return true;
}
bool isRightTrunc (int n) {
while (n > 0) {
if (!primesTable[n])
return false;
n /= 10;
}
return true;
}
int main () {
int* primes;
int numOfPrimes = listOfPrimes (1000000, &primes, &primesTable);
// unfortunately I couldn't find a deterministic way to find
// the upper-bound of all truncatable primes... 1 mil will do
int sum = 0;
for (int i = 0; i < numOfPrimes; i++) {
if (isLeftTrunc (primes[i]) &&
isRightTrunc (primes[i]) &&
primes[i] > 10) {
printf ("%d\n", primes[i]);
sum += primes[i];
}
}
printf ("sum: %d\n", sum);
}