Solution to Project Euler Problem 37: Truncatable primes - The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3. Find the sum of the only eleven primes that are both truncatable from left to right and right to left. NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
Updated: June 1, 2023 — Training Time: 3 minutes
Overseen by: Archangel Macsika
All Training Resources
Scroll for more menu list
Topic: Project Euler Problem 37: Truncatable primes.
Difficulty: Easy.
Objective: The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.
Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
Input: None.
Expected Output: 748317.
Sikademy Solution in Java Programming Language
package sikademy;
/**
*
* @author Archangel Macsika
* Copyright (c) Sikademy. All rights reserved
*/
public class SikademyEulerSolution {
public String run() {
long sum = 0;
for (int count = 0, n = 10; count < 11; n++) {
if (isTruncatablePrime(n)) {
sum += n;
count++;
}
}
return Long.toString(sum);
}
private static boolean isTruncatablePrime(int n) {
// Test if left-truncatable
for (long i = 10; i <= n; i *= 10) {
if (!Library.isPrime(n % (int)i))
return false;
}
// Test if right-truncatable
for (; n != 0; n /= 10) {
if (!isPrime(n))
return false;
}
return true;
}
public static boolean isPrime(int x) {
if (x < 0)
throw new IllegalArgumentException("Negative number");
if (x == 0 || x == 1)
return false;
else if (x == 2)
return true;
else {
if (x % 2 == 0)
return false;
for (int i = 3, end = sqrt(x); i <= end; i += 2) {
if (x % i == 0)
return false;
}
return true;
}
}
public static int sqrt(int x) {
if (x < 0)
throw new IllegalArgumentException("Square root of negative number");
int y = 0;
for (int i = 1 << 15; i != 0; i >>>= 1) {
y |= i;
if (y > 46340 || y * y > x)
y ^= i;
}
return y;
}
public static void main(String[] args) {
SikademyEulerSolution solution = new SikademyEulerSolution();
System.out.println(solution.run());
}
}
Sikademy Solution in Python Programming Language
#
# @author Archangel Macsika
# Copyright (c) Sikademy. All rights reserved.
#
import itertools
def compute():
ans = sum(itertools.islice(filter(is_truncatable_prime, itertools.count(10)), 11))
return str(ans)
def is_truncatable_prime(n):
# Test if left-truncatable
i = 10
while i <= n:
if not is_prime(n % i):
return False
i *= 10
# Test if right-truncatable
while n > 0:
if not is_prime(n):
return False
n //= 10
return True
def sqrt(x):
assert x >= 0
i = 1
while i * i <= x:
i *= 2
y = 0
while i > 0:
if (y + i)**2 <= x:
y += i
i //= 2
return y
# Tests whether the given integer is a prime number.
def is_prime(x):
if x <= 1:
return False
elif x <= 3:
return True
elif x % 2 == 0:
return False
else:
for i in range(3, sqrt(x) + 1, 2):
if x % i == 0:
return False
return True
if __name__ == "__main__":
print(compute())