From 995bdf13b77f9c35f5ef8bcf3e764d2f5bfd35ca Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Sun, 21 Oct 2012 19:55:59 +0200 Subject: [PATCH] Solves problem 44. a bit long but first guess was the right one --- README.markdown | 3 ++- e_44.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ e_47.py | 8 +++---- 3 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 e_44.py diff --git a/README.markdown b/README.markdown index 64d3c44..b3d0b4a 100644 --- a/README.markdown +++ b/README.markdown @@ -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
42 - Using words.txt, a 16K text file containing nearly two-thousand common English words, how many are triangle words? - < 1 sec
43 - Find the sum of all pandigital numbers with an unusual sub-string +divisibility property. - 28sec
+44 - Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal. **~= 19 min**
45 - Find the next triangle number that is also pentagonal and hexagonal. - < 1 sec
46 - What is the smallest odd composite that cannot be written as the sum of a prime and twice a square? - < 6 sec
48 - Find the last ten digits of 1^1 + 2^2 + ... + 1000^1000. - 0.053
@@ -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) -Last update : 16/10/2012 +Last update : 21/10/2012 diff --git a/e_44.py b/e_44.py new file mode 100644 index 0000000..61ae0f7 --- /dev/null +++ b/e_44.py @@ -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 diff --git a/e_47.py b/e_47.py index 1be8084..d543ff4 100644 --- a/e_47.py +++ b/e_47.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python ''' 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: -644 = 2^2 7 * 23 +644 = 2^2 * 7 * 23 645 = 3 * 5 * 43 646 = 2 * 17 * 19. Find the first four consecutive integers to have four distinct primes factors. What is the first of these numbers? -''' +''' import pickle def consecutive_primes(num): """ Returns the first of the firs num consecutive primes. """ - + if __name__ == '__main__': print 1