Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Pentagonal numbers are generated by the formula, Pn=n(3n-1)/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 are pentagonal and D = |Pk - Pj| is minimised;
- #what is the value of D?
- import time
- def pentagonal(n):
- if n >= 1:
- return (n * (3 * n - 1) )/2
- def isPentagonal(n):
- if (((24*n+1)**0.5)+1)%6 == 0:
- return True
- else:
- return False
- start = time.time()
- pentagonals = []
- n = 0
- flag = True
- while flag:
- n += 1
- newPentagonal = pentagonal(n)
- pentagonals.append(newPentagonal)
- for i in pentagonals:
- if isPentagonal(newPentagonal - i)\
- and isPentagonal(newPentagonal + i):
- print newPentagonal - i
- flag = False
- end = time.time()
- print end - start
Advertisement
Add Comment
Please, Sign In to add comment