From a48b0ea435f13ede3164fce60ea3d9ee9db1c9e7 Mon Sep 17 00:00:00 2001 From: julien Lengrand-Lambert Date: Thu, 3 May 2012 12:45:02 +0200 Subject: [PATCH] Solves problem 38 in less than one second. Code is not that beautiful though. --- README.markdown | 7 +++++-- e_33.py | 1 + e_38.py | 34 +++++++++++++++++++++++++++++++--- e_43.py | 24 ++++++++++++++++++++++++ e_46.py | 24 ++++++++++++++++++++++++ e_47.py | 24 ++++++++++++++++++++++++ 6 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 e_43.py create mode 100644 e_46.py create mode 100644 e_47.py diff --git a/README.markdown b/README.markdown index 0655658..ba8eca9 100644 --- a/README.markdown +++ b/README.markdown @@ -47,6 +47,7 @@ Should be used in order to help future reuse of code :) 35 - How many circular primes are there below one million?
36 - Find the sum of all numbers less than one million, which are palindromic in base 10 and base 2. - 0.933
37 - Find the sum of all eleven primes that are both truncatable from left to right and right to left. < 1 min
+38 - What is the largest 1 to 9 pandigital that can be formed by multiplying a fixed number by 1, 2, 3, ... ? < 1 sec
39 - If p is the perimeter of a right angle triangle, {a, b, c}, which value, for p <= 1000, has the most solutions? - 1min
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
@@ -60,8 +61,10 @@ Should be used in order to help future reuse of code :) 26 - Find the value of d < 1000 for which 1/d contains the longest recurring cycle.
33 - Discover all the fractions with an unorthodox cancelling method.
-38 - What is the largest 1 to 9 pandigital that can be formed by multiplying a fixed number by 1, 2, 3, ... ?
-97 - Find the last ten digits of the non-Mersenne prime: 28433 × 2^7830457 + 1. +43 - Find the sum of all pandigital numbers with an unusual sub-string divisibility property.
+46 - What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?
+47 - Find the first four consecutive integers to have four distinct primes factors.
+97 - Find the last ten digits of the non-Mersenne prime: 28433 × 2^7830457 + 1.
**WARNING : Spoil inside for those who want to solve problems by themselves :)** diff --git a/e_33.py b/e_33.py index 48b1725..7006450 100644 --- a/e_33.py +++ b/e_33.py @@ -13,4 +13,5 @@ If the product of these four fractions is given in its lowest common terms, find ''' if __name__ == '__main__': + print 1 #print "Answer : %d " % (last_ten()) \ No newline at end of file diff --git a/e_38.py b/e_38.py index b302f24..6532488 100644 --- a/e_38.py +++ b/e_38.py @@ -1,6 +1,6 @@ #!/usr/bin/env python ''' -Created on 10 feb. 2012 +Created on 2 may 2012 @author: Julien Lengrand-Lambert @@ -16,7 +16,35 @@ The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5 What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, ... , n) where n >1? ''' +def has_duplicates(mylist): + """ + Returns True if the list contains at least one duplicate + """ + return (len(mylist)!=len(set(mylist))) + +def concat_pandigital(): + """ + Returns the largest 1 to 9 pandigital number formed as the concatened product of an integer with (1, 2, ..., n) + """ + pand_list = [] + # max_val is number for which sum(len(max_val * 1) + len(max_val * 2) ) > 9 = 10000 + for x in range(1, 10000): + got = "" # list of all numbers we already have + mul = 1 + doit = 1 + while doit: + cur_val = x * mul + if (("0" in str(cur_val)) or (has_duplicates(got + str(cur_val)))): + doit = 0 + else: + got += str(cur_val) + mul += 1 + + if len(got) == 9: # we have a pandigital number in output + print x + pand_list.append(int(got)) # should put got back in a correct way + + return max(pand_list) if __name__ == '__main__': - print 1 - #print "Answer : %d " % (last_ten()) \ No newline at end of file + print "Answer : %d " % (concat_pandigital()) \ No newline at end of file diff --git a/e_43.py b/e_43.py new file mode 100644 index 0000000..d52405d --- /dev/null +++ b/e_43.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python +''' +Created on 10 feb. 2012 + +@author: Julien Lengrand-Lambert + +DESCRIPTION: Solves problem 43 of Project Euler +The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property. + +Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way, we note the following: + +d2d3d4=406 is divisible by 2 +d3d4d5=063 is divisible by 3 +d4d5d6=635 is divisible by 5 +d5d6d7=357 is divisible by 7 +d6d7d8=572 is divisible by 11 +d7d8d9=728 is divisible by 13 +d8d9d10=289 is divisible by 17 +Find the sum of all 0 to 9 pandigital numbers with this property. +''' + +if __name__ == '__main__': + print 1 + #print "Answer : %d " % (last_ten()) \ No newline at end of file diff --git a/e_46.py b/e_46.py new file mode 100644 index 0000000..47fc8da --- /dev/null +++ b/e_46.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python +''' +Created on 10 feb. 2012 + +@author: Julien Lengrand-Lambert + +DESCRIPTION: Solves problem 46 of Project Euler +It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. + +9 = 7 + 2*1^2 +15 = 7 + 2*2^2 +21 = 3 + 2*3^2 +25 = 7 + 2*3^2 +27 = 19 + 2*2^2 +33 = 31 + 2*1^2 + +It turns out that the conjecture was false. + +What is the smallest odd composite that cannot be written as the sum of a prime and twice a square? +''' + +if __name__ == '__main__': + print 1 + #print "Answer : %d " % (last_ten()) \ No newline at end of file diff --git a/e_47.py b/e_47.py new file mode 100644 index 0000000..7423c75 --- /dev/null +++ b/e_47.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python +''' +Created on 10 feb. 2012 + +@author: Julien Lengrand-Lambert + +DESCRIPTION: Solves problem 47 of Project Euler +The first two consecutive numbers to have two distinct prime factors are: + +14 = 2*7 +15 = 3*5 + +The first three consecutive numbers to have three distinct prime factors are: + +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? +''' + +if __name__ == '__main__': + print 1 + #print "Answer : %d " % (last_ten()) \ No newline at end of file