mirror of
https://github.com/jlengrand/project_euler.git
synced 2026-03-10 00:31:21 +00:00
Solves problem 38 in less than one second.
Code is not that beautiful though.
This commit is contained in:
@@ -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? <br />
|
||||
36 - Find the sum of all numbers less than one million, which are palindromic in base 10 and base 2. - 0.933 <br />
|
||||
37 - Find the sum of all eleven primes that are both truncatable from left to right and right to left. < 1 min <br />
|
||||
38 - What is the largest 1 to 9 pandigital that can be formed by multiplying a fixed number by 1, 2, 3, ... ? < 1 sec <br />
|
||||
39 - If p is the perimeter of a right angle triangle, {a, b, c}, which value, for p <= 1000, has the most solutions? - 1min<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 />
|
||||
@@ -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. <br />
|
||||
33 - Discover all the fractions with an unorthodox cancelling method. <br />
|
||||
38 - What is the largest 1 to 9 pandigital that can be formed by multiplying a fixed number by 1, 2, 3, ... ? <br />
|
||||
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. <br />
|
||||
46 - What is the smallest odd composite that cannot be written as the sum of a prime and twice a square? <br />
|
||||
47 - Find the first four consecutive integers to have four distinct primes factors. <br />
|
||||
97 - Find the last ten digits of the non-Mersenne prime: 28433 × 2^7830457 + 1. <br />
|
||||
|
||||
**WARNING : Spoil inside for those who want to solve problems by themselves :)**
|
||||
|
||||
|
||||
1
e_33.py
1
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())
|
||||
34
e_38.py
34
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())
|
||||
print "Answer : %d " % (concat_pandigital())
|
||||
24
e_43.py
Normal file
24
e_43.py
Normal file
@@ -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())
|
||||
24
e_46.py
Normal file
24
e_46.py
Normal file
@@ -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())
|
||||
24
e_47.py
Normal file
24
e_47.py
Normal file
@@ -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())
|
||||
Reference in New Issue
Block a user