Solves problem 44. a bit long but first guess was the right one

This commit is contained in:
Julien Lengrand-Lambert
2012-10-21 19:55:59 +02:00
parent 11aa1ef61e
commit 995bdf13b7
3 changed files with 70 additions and 5 deletions

View File

@@ -60,6 +60,7 @@ So you may find some of the code here quite ugly. And this is the case :). Why o
41 - What is the largest n-digit pandigital prime that exists? < 5 min <br /> 41 - What is the largest n-digit pandigital prime that exists? < 5 min <br />
42 - Using words.txt, a 16K text file containing nearly two-thousand common English words, how many are triangle words? - < 1 sec <br /> 42 - Using words.txt, a 16K text file containing nearly two-thousand common English words, how many are triangle words? - < 1 sec <br />
43 - Find the sum of all pandigital numbers with an unusual sub-string +divisibility property. - 28sec <br /> 43 - Find the sum of all pandigital numbers with an unusual sub-string +divisibility property. - 28sec <br />
44 - Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal. **~= 19 min** <br />
45 - Find the next triangle number that is also pentagonal and hexagonal. - < 1 sec <br /> 45 - Find the next triangle number that is also pentagonal and hexagonal. - < 1 sec <br />
46 - What is the smallest odd composite that cannot be written as the sum of a prime and twice a square? - < 6 sec <br /> 46 - What is the smallest odd composite that cannot be written as the sum of a prime and twice a square? - < 6 sec <br />
48 - Find the last ten digits of 1^1 + 2^2 + ... + 1000^1000. - 0.053 <br /> 48 - Find the last ten digits of 1^1 + 2^2 + ... + 1000^1000. - 0.053 <br />
@@ -82,4 +83,4 @@ Feel free to mail me for any comment or request.
You can contact me at julien at lengrand dot fr, or on my [current website](http://www.lengrand.fr) You can contact me at julien at lengrand dot fr, or on my [current website](http://www.lengrand.fr)
Last update : 16/10/2012 Last update : 21/10/2012

64
e_44.py Normal file
View File

@@ -0,0 +1,64 @@
#!/usr/bin/env python
'''
Created on 21 oct. 2012
@author: Julien Lengrand-Lambert
DESCRIPTION: Solves problem 44 of Project Euler
Pentagonal numbers are generated by the formula, Pn=n(3n1)/2.
The first ten pentagonal numbers are:
1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...
It can be seen that P4 + P7 = 22 + 70 = 92 = P8.
However, their difference, 70 22 = 48, is not pentagonal.
Find the pair of pentagonal numbers, Pj and Pk,
for which their sum and difference is pentagonal
and D = |Pk - Pj| is minimised; what is the value of D?
'''
penta_vals = []
def is_pentagonal(val):
"""
Returns True if val is a pentagonal number
"""
global penta_vals
return (val in penta_vals)
def pent(val):
"""
Returns the pentagonal number created using val
Pentagonal numbers are defined by Pn=n(3n-1)/2
"""
return (val * (3 * val - 1) / 2)
def create_pentagonal(ite=1000):
"""
Returns a list of ite pentagonal numbers
default is 1000
Pentagonal numbers are defined by Pn=n(3n1)/2
"""
temp = range(1, ite + 1)
return [pent(val) for val in temp]
def find_pair(ite=1000):
"""
Finds the pair of pentagonal numbers such as
Pj + Pk and Pj - Pk are also pentagonal
Returns minimised |Pk - Pj|
"""
global penta_vals
penta_vals = create_pentagonal(ite)
for val_j in penta_vals:
for val_k in penta_vals:
if is_pentagonal(val_k + val_j) and is_pentagonal(val_k - val_j):
return min(abs(val_j - val_k), abs(val_k - val_j))
if __name__ == '__main__':
print "Answer : %d " % (find_pair(10000)) # 10000 is just a rude guess

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python
''' '''
Created on 10 feb. 2012 Created on 10 feb. 2012
@@ -12,19 +12,19 @@ The first two consecutive numbers to have two distinct prime factors are:
The first three consecutive numbers to have three distinct prime factors are: The first three consecutive numbers to have three distinct prime factors are:
644 = 2^2 7 * 23 644 = 2^2 * 7 * 23
645 = 3 * 5 * 43 645 = 3 * 5 * 43
646 = 2 * 17 * 19. 646 = 2 * 17 * 19.
Find the first four consecutive integers to have four distinct primes factors. What is the first of these numbers? Find the first four consecutive integers to have four distinct primes factors. What is the first of these numbers?
''' '''
import pickle import pickle
def consecutive_primes(num): def consecutive_primes(num):
""" """
Returns the first of the firs num consecutive primes. Returns the first of the firs num consecutive primes.
""" """
if __name__ == '__main__': if __name__ == '__main__':
print 1 print 1