Add Python solution for problem 808
This commit is contained in:
parent
b2c2fb2e8e
commit
adca45e24f
47
Python/p808.py
Normal file
47
Python/p808.py
Normal file
@ -0,0 +1,47 @@
|
||||
# Both 169 and 961 are the square of a prime. 169 is the reverse of 961.
|
||||
#
|
||||
# We call a number a reversible prime square if:
|
||||
#
|
||||
# 1. It is not a palindrome, and
|
||||
# 2. It is the square of a prime, and
|
||||
# 3. Its reverse is also the square of a prime.
|
||||
#
|
||||
# 169 and 961 are not palindromes, so both are reversible prime squares.
|
||||
#
|
||||
# Find the sum of the first 50 reversible prime squares.
|
||||
|
||||
from math import sqrt
|
||||
|
||||
from projecteuler import is_palindrome, sieve, timing
|
||||
|
||||
|
||||
@timing
|
||||
def p808():
|
||||
n = 1
|
||||
primes = sieve(50000000)
|
||||
reversible_prime_squares = []
|
||||
|
||||
for n, p in enumerate(primes):
|
||||
if not p:
|
||||
continue
|
||||
|
||||
n_squared = n * n
|
||||
|
||||
if not is_palindrome(n_squared):
|
||||
n_reverse = int(str(n_squared)[::-1])
|
||||
n_reverse_sqrt = sqrt(n_reverse)
|
||||
|
||||
if n_reverse_sqrt.is_integer() and primes[int(n_reverse_sqrt)]:
|
||||
reversible_prime_squares.append(n_squared)
|
||||
|
||||
if len(reversible_prime_squares) == 50:
|
||||
break
|
||||
|
||||
res = sum(reversible_prime_squares)
|
||||
|
||||
print('Project Euler, Problem 808')
|
||||
print(f'Answer: {res}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
p808()
|
@ -10,20 +10,19 @@ from numpy import zeros
|
||||
def is_prime(num):
|
||||
if num < 4:
|
||||
# If num is 2 or 3 then it's prime.
|
||||
return num == 2 or num == 3
|
||||
return num in (2, 3)
|
||||
|
||||
# If num is divisible by 2 or 3 then it's not prime.
|
||||
if num % 2 == 0 or num % 3 == 0:
|
||||
return False
|
||||
|
||||
# Any number can have only one prime factor greater than its
|
||||
# square root. If we reach the square root and we haven't found
|
||||
# any smaller prime factors, then the number is prime.
|
||||
limit = floor(sqrt(num)) + 1
|
||||
|
||||
# Every prime other than 2 and 3 is in the form 6k+1 or 6k-1.
|
||||
# If I check all those value no prime factors of the number
|
||||
# will be missed. If a factor is found, the number is not prime
|
||||
# and the function returns 0.
|
||||
# Every prime other than 2 and 3 is in the form 6k+1 or 6k-1. If I check all those values no prime factors of the number
|
||||
# will be missed. If a factor is found, the number is not prime and the function returns False.
|
||||
for i in range(5, limit, 6):
|
||||
if num % i == 0 or num % (i + 2) == 0:
|
||||
return False
|
||||
@ -32,7 +31,7 @@ def is_prime(num):
|
||||
return True
|
||||
|
||||
|
||||
def is_palindrome(num, base):
|
||||
def is_palindrome(num, base=10):
|
||||
reverse = 0
|
||||
|
||||
tmp = num
|
||||
@ -426,6 +425,7 @@ def partition_fn(n, partitions, mod=-1):
|
||||
# The partition function for zero is 1 by definition.
|
||||
if n == 0:
|
||||
partitions[n] = 1
|
||||
|
||||
return 1
|
||||
|
||||
# If the partition for the current n has already been calculated, return the value.
|
||||
|
@ -4,4 +4,4 @@ These are my solutions in C and Python, not necessarily the best solutions. I've
|
||||
# Notes
|
||||
- Solutions for problems 82, 86, 95, and 145 in Python are quite slow.
|
||||
- Solutions for problems 84, 89, 102, and 124 have been implemented in C but not in Python.
|
||||
- Solutions for problems 88, 90 and 91 have been implemented in Python but not in C.
|
||||
- Solutions for problems 88, 90, 91, and 808 have been implemented in Python but not in C.
|
||||
|
Loading…
x
Reference in New Issue
Block a user