mirror of
https://github.com/jlengrand/project_euler.git
synced 2026-03-10 08:41:20 +00:00
Solves 4 problems.
Eclipse integration for simpler use with Windows. Still have to perform some good work on prime numbers! Signed-off-by: Julien Lengrand-Lambert <julien@lengrand.fr>
This commit is contained in:
@@ -39,13 +39,18 @@ Should be used in order to help future reuse of code :)
|
||||
24 - What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? - too long <br />
|
||||
25 - What is the first term in the Fibonacci sequence to contain 1000 digits? - 0.741 <br />
|
||||
27 - Find a quadratic formula that produces the maximum number of primes for consecutive values of n. - too long <br />
|
||||
29 - How many distinct terms are in the sequence generated by ab for 2 a 100 and 2 b 100? - < 1 sec <br />
|
||||
36 - Find the sum of all numbers less than one million, which are palindromic in base 10 and base 2. - 0.933 <br />
|
||||
42 - Using words.txt, a 16K text file containing nearly two-thousand common English words, how many are triangle words? - < 1 sec <br />
|
||||
45 - Find the next triangle number that is also pentagonal and hexagonal. - < 1 sec <br />
|
||||
48 - Find the last ten digits of 1^1 + 2^2 + ... + 1000^1000. - 0.053 <br />
|
||||
52 - Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits. - 2min <br />
|
||||
67 - Using an efficient algorithm find the maximal sum in the triangle? - 0.027 <br />
|
||||
|
||||
**In progress: **
|
||||
|
||||
26 - Find the value of d < 1000 for which 1/d contains the longest recurring cycle. <br />
|
||||
30 - Find the sum of all the numbers that can be written as the sum of fifth powers of their digits. <br />
|
||||
35 - How many circular primes are there below one million? <br />
|
||||
|
||||
**WARNING : Spoil inside for those who want to solve problems by themselves :)**
|
||||
|
||||
38
e_29.py
Normal file
38
e_29.py
Normal file
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env python
|
||||
'''
|
||||
Created on 7 feb. 2012
|
||||
|
||||
@author: Julien Lengrand-Lambert
|
||||
|
||||
DESCRIPTION: Solves problem 29 of Project Euler
|
||||
Consider all integer combinations of a^b for 2<=a<=5 and 2<=b<=5:
|
||||
|
||||
2^2=4, 2^3=8,2^4=16, 2^5=32
|
||||
3^2=9, 3^3=27, 3^4=81,3^5=243
|
||||
4^2=16, 4^3=64, 4^4=256, 4^5=1024
|
||||
5^2=25, 5^3=125, 5^4=625, 5^5=3125
|
||||
If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:
|
||||
|
||||
4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
|
||||
|
||||
How many distinct terms are in the sequence generated by a^b for 2<=a<=100 and 2<=b<=100?
|
||||
'''
|
||||
def integer_combinations(a_max, b_max):
|
||||
"""
|
||||
Returns a list of all integer combinations of a^b for 2<=a<=5 and 2<=b<=5
|
||||
"""
|
||||
olist = []
|
||||
for a in range(2, a_max + 1):
|
||||
for b in range(2, b_max + 1):
|
||||
olist.append(a**b)
|
||||
|
||||
return olist
|
||||
|
||||
def sort_n_short(olist):
|
||||
"""
|
||||
Sort elements of olist and remove all duplicates.
|
||||
"""
|
||||
return sorted(set(olist))
|
||||
|
||||
if __name__ == '__main__':
|
||||
print "Answer : %d " % (len(sort_n_short(integer_combinations(100, 100))))
|
||||
29
e_30.py
Normal file
29
e_30.py
Normal file
@@ -0,0 +1,29 @@
|
||||
#!/usr/bin/env python
|
||||
'''
|
||||
Created on 7 feb. 2012
|
||||
|
||||
@author: Julien Lengrand-Lambert
|
||||
|
||||
DESCRIPTION: Solves problem 30 of Project Euler
|
||||
Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
|
||||
|
||||
1634 = 1^4 + 6^4 + 3^4 + 4^4
|
||||
8208 = 8^4 + 24 + 0^4 + 8^4
|
||||
9474 = 9^4 + 4^4 + 7^4 + 4^4
|
||||
As 1 = 1^4 is not a sum it is not included.
|
||||
|
||||
The sum of these numbers is 1634 + 8208 + 9474 = 19316.
|
||||
|
||||
Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
|
||||
'''
|
||||
|
||||
|
||||
def largest():
|
||||
"""
|
||||
Returns
|
||||
"""
|
||||
|
||||
return 1
|
||||
|
||||
if __name__ == '__main__':
|
||||
print "Answer : %d " % (largest())
|
||||
53
e_35.py
53
e_35.py
@@ -11,7 +11,8 @@
|
||||
How many circular primes are there below one million?
|
||||
##---
|
||||
"""
|
||||
|
||||
import pickle
|
||||
|
||||
def all_permutations(seq):
|
||||
"""permutate a sequence and return a list of the permutations"""
|
||||
if not seq:
|
||||
@@ -52,20 +53,23 @@ def prime_list(max_val):
|
||||
|
||||
return plist
|
||||
|
||||
def perm_primes(max_val):
|
||||
def perm_primes_old(max_val):
|
||||
"""
|
||||
Returns the number of circular primes below max_val
|
||||
"""
|
||||
circulars = 0
|
||||
circl = []
|
||||
|
||||
# to avoid calculating primes
|
||||
# plist = pickle.load(open("primes_list.dup", "rb"))
|
||||
# print plist
|
||||
plist = prime_list(max_val)
|
||||
print "###"
|
||||
# stuff here
|
||||
cpt = 0
|
||||
for prime in plist:
|
||||
cpt += 1
|
||||
print "%d/%d" % ( cpt, len(list))
|
||||
print "%d/%d" % ( cpt, len(plist))
|
||||
print circulars, circl
|
||||
perms = all_permutations(str(prime)) # finds all permutations
|
||||
|
||||
@@ -82,8 +86,47 @@ def perm_primes(max_val):
|
||||
|
||||
return circulars, circl
|
||||
|
||||
def perm_primes(max_val):
|
||||
"""
|
||||
Returns the number of circular primes below max_val
|
||||
"""
|
||||
circulars = 0
|
||||
circl = []
|
||||
|
||||
# to avoid calculating primes
|
||||
plist = pickle.load(open("primes_list.dup", "rb"))
|
||||
#plist = prime_list(max_val)
|
||||
checks_list = [1] * len(plist) # all primes to be tested
|
||||
|
||||
for ii in range(len(plist)):
|
||||
if (ii % 99 == 0):
|
||||
print "%d/%d" % ( ii + 1, len(plist))
|
||||
|
||||
if checks_list[ii]: # if prime still to be checked
|
||||
prime = plist[ii]
|
||||
perms = all_permutations(str(prime)) # finds all permutations
|
||||
|
||||
# counts how many permutations are in prime list given one prime
|
||||
p_cpt = 0
|
||||
for perm in perms:
|
||||
if int(perm) in plist:
|
||||
p_cpt += 1
|
||||
|
||||
# if prime is circular
|
||||
if p_cpt == len(perms):
|
||||
# updating checks_list
|
||||
for perm in perms:
|
||||
if not(int(perm) in circl): # avoiding duplicates
|
||||
circulars += 1
|
||||
circl.append(int(perm))
|
||||
checks_list[plist.index(int(perm))] = 0
|
||||
|
||||
else:
|
||||
checks_list[ii] = 0
|
||||
|
||||
return circulars, circl
|
||||
|
||||
if __name__ == '__main__' :
|
||||
answer, plist = perm_primes(1000000)
|
||||
print "Answer is : %d" % (answer)
|
||||
print plist
|
||||
raw_input()
|
||||
print plist
|
||||
100
e_42.py
Normal file
100
e_42.py
Normal file
@@ -0,0 +1,100 @@
|
||||
#!/usr/bin/env python
|
||||
'''
|
||||
Created on 7 feb. 2012
|
||||
|
||||
@author: Julien Lengrand-Lambert
|
||||
|
||||
DESCRIPTION: Solves problem 42 of Project Euler
|
||||
The nth term of the sequence of triangle numbers is given by, t_n = 1/2 * n * (n+1);
|
||||
so the first ten triangle numbers are:
|
||||
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
|
||||
|
||||
By converting each letter in a word to a number corresponding
|
||||
to its alphabetical position and adding these values we form a word value.
|
||||
For example, the word value for SKY is 19 + 11 + 25 = 55 = t_10.
|
||||
If the word value is a triangle number then we shall call the word a triangle word.
|
||||
|
||||
Using e_42.data (right click and 'Save Link/Target As...'),
|
||||
a 16K text file containing nearly two-thousand common English words,
|
||||
how many are triangle words?
|
||||
'''
|
||||
import string
|
||||
|
||||
def load_data(filename):
|
||||
"""
|
||||
Loads the data from a name file into a table
|
||||
"""
|
||||
my_file = open(filename, "r")
|
||||
line = my_file.readline()
|
||||
data = [el for el in line.split("\"")][1::2]
|
||||
my_file.close()
|
||||
return data
|
||||
|
||||
def dict_alphabet():
|
||||
"""
|
||||
Returns a dict of the alphabet, with value for each letter.
|
||||
"""
|
||||
alpb= dict()
|
||||
letters = string.ascii_uppercase
|
||||
inc = 1
|
||||
for j in range(len(letters)):
|
||||
alpb[letters[j]] = inc
|
||||
inc += 1
|
||||
|
||||
return alpb
|
||||
|
||||
def names2scores(data):
|
||||
"""
|
||||
Returns a list of scores given a list of names.
|
||||
Scores are calculated with word values, (A = 1, B = 2, ...)
|
||||
"""
|
||||
scores = []
|
||||
alpb = dict_alphabet()
|
||||
for name in data:
|
||||
score = 0
|
||||
for jj in range(len(name)):
|
||||
score += (alpb[name[jj]])
|
||||
|
||||
scores.append(score)
|
||||
|
||||
return scores
|
||||
|
||||
def triangle_list(max_val):
|
||||
"""
|
||||
Returns a list of triangle numbers up to max_val.
|
||||
triangle_numbers is an increasing function.
|
||||
"""
|
||||
trilist = []
|
||||
ptr = 1
|
||||
val = 1
|
||||
while val < max_val:
|
||||
val = trinum(ptr)
|
||||
trilist.append(val)
|
||||
ptr += 1
|
||||
|
||||
return trilist
|
||||
|
||||
def trinum(val):
|
||||
"""
|
||||
Returns the triangle number for a given value
|
||||
"""
|
||||
return val * (val + 1) / 2
|
||||
|
||||
def triangle_words(filename):
|
||||
"""
|
||||
Returns the number of triangle words in filename
|
||||
"""
|
||||
data = load_data(filename)
|
||||
scores = names2scores(data)
|
||||
trilist = triangle_list(max(scores))
|
||||
|
||||
# checking if words are triangle
|
||||
cpt = 0
|
||||
for score in scores:
|
||||
if score in trilist:
|
||||
cpt += 1
|
||||
|
||||
return cpt
|
||||
|
||||
if __name__ == '__main__':
|
||||
print "Answer : %d " % (triangle_words("e_42.data"))
|
||||
75
e_45.py
Normal file
75
e_45.py
Normal file
@@ -0,0 +1,75 @@
|
||||
'''
|
||||
Created on 7 feb. 2012
|
||||
|
||||
@author: Julien Lengrand-Lambert
|
||||
DESCRIPTION : Solves problem 45 of Project Euler
|
||||
Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:
|
||||
|
||||
Triangle Tn=n(n+1)/2 1, 3, 6, 10, 15, ...
|
||||
Pentagonal Pn=n(3(n-1))/2 1, 5, 12, 22, 35, ...
|
||||
Hexagonal Hn=n(2(n-1)) 1, 6, 15, 28, 45, ...
|
||||
It can be verified that T285 = P165 = H143 = 40755.
|
||||
|
||||
Find the next triangle number that is also pentagonal and hexagonal.
|
||||
'''
|
||||
|
||||
def tn(value):
|
||||
"""
|
||||
Returns the triangle number of the given value
|
||||
"""
|
||||
return value * (value + 1) / 2
|
||||
|
||||
def pn(value):
|
||||
"""
|
||||
Returns the Pentagonal number of the given value
|
||||
"""
|
||||
return value * ((3 * value) - 1) / 2
|
||||
|
||||
def hn(value):
|
||||
"""
|
||||
Returns the Hexagonal number of the given value
|
||||
"""
|
||||
return value * ((2 * value) - 1)
|
||||
|
||||
def tripenhex(value):
|
||||
"""
|
||||
Returns True of the triangle, pentagonal and hexagonal number of value are the same
|
||||
"""
|
||||
return (tn(value) == pn(value) == hn(value))
|
||||
|
||||
def big_while():
|
||||
"""
|
||||
Returns the value for which tm = pn = ho
|
||||
"""
|
||||
cpt_t = 2
|
||||
cpt_p = 2
|
||||
cpt_h = 2
|
||||
val_t = tn(cpt_t)
|
||||
val_p = pn(cpt_p)
|
||||
val_h = hn(cpt_h)
|
||||
|
||||
res = []
|
||||
while len(res) < 2: # to get the two first values
|
||||
while not (val_t == val_p == val_h):
|
||||
|
||||
cpt_t += 1
|
||||
val_t = tn(cpt_t)
|
||||
|
||||
# updating val_p
|
||||
while val_p < val_t:
|
||||
cpt_p += 1
|
||||
val_p = pn(cpt_p)
|
||||
|
||||
# updating val_h
|
||||
while val_h < val_t:
|
||||
cpt_h += 1
|
||||
val_h = hn(cpt_h)
|
||||
res.append(val_t)
|
||||
cpt_t += 1
|
||||
val_t = tn(cpt_t)
|
||||
|
||||
return res[1] # outputs the second value
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
print "Answer : %d " % (big_while())
|
||||
64
e_52.py
Normal file
64
e_52.py
Normal file
@@ -0,0 +1,64 @@
|
||||
'''
|
||||
Created on 7 feb. 2012
|
||||
|
||||
@author: Julien Lengrand-Lambert
|
||||
DESCRIPTION : Solves problem 52 of Project Euler
|
||||
It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.
|
||||
|
||||
Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.
|
||||
'''
|
||||
def smallest_match(max_mul):
|
||||
"""
|
||||
Find the smallest positive integer, x, such that ..., till max_mul*x contain the same digits.
|
||||
"""
|
||||
cpt = 2
|
||||
nlist = [(i + 1) * cpt for i in range(max_mul)]
|
||||
|
||||
while(1):
|
||||
if cpt % 10000 == 0:
|
||||
print cpt
|
||||
if same_size(nlist):
|
||||
if same_digits(nlist):
|
||||
print cpt, nlist
|
||||
return cpt
|
||||
|
||||
cpt += 1
|
||||
nlist = [(i + 1) * cpt for i in range(max_mul)]
|
||||
|
||||
def all_permutations(seq):
|
||||
"""permutate a sequence and return a list of the permutations"""
|
||||
if not seq:
|
||||
return [seq] # is an empty sequence
|
||||
else:
|
||||
temp = []
|
||||
for k in range(len(seq)):
|
||||
part = seq[:k] + seq[k+1:]
|
||||
for m in all_permutations(part):
|
||||
temp.append(seq[k:k+1] + m)
|
||||
return temp
|
||||
|
||||
|
||||
def same_digits(nlist):
|
||||
"""
|
||||
Returns True if all numbers in nlist contain the same digits.
|
||||
Which means they are all permutations of the first value
|
||||
"""
|
||||
perms = all_permutations(str(nlist[0])) # finds all permutations
|
||||
for el in nlist:
|
||||
if not(str(el) in perms):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def same_size(nlist):
|
||||
"""
|
||||
Returns True if all numbers in nlist have the same number of digits
|
||||
"""
|
||||
nlen = len(str(nlist[0]))
|
||||
for el in nlist :
|
||||
if len(str(el)) != nlen:
|
||||
return False
|
||||
return True
|
||||
|
||||
if __name__ == '__main__':
|
||||
print "Answer : %d " % (smallest_match(6))
|
||||
Reference in New Issue
Block a user