Same commit as before. Forgotten to include new files

This commit is contained in:
Julien Lengrand-Lambert
2012-01-16 17:03:38 +01:00
parent 66a8d4abaa
commit 025c58eaa1
5 changed files with 174 additions and 0 deletions

39
e_15.py Executable file
View File

@@ -0,0 +1,39 @@
#!/usr/bin/env python
"""
##---
# jlengrand
#Created on : Mon Jan 16 13:53:07 CET 2012
#
# DESCRIPTION : Solves problem 15 of Project Euler
Starting in the top left corner of a 2x2 grid, there are 6 routes (without backtracking) to the bottom right corner.
How many routes are there through a 20x20 grid?
This problem is linked with the catalan number.
To get to the extreme right, 2xn moves have to be performed
Moves are either R or D.
There is the same the same number of R and D in the sequence.
Problem is finally : How many strings of size 2xn are there consisting of nxR and nxD?
-> number of ways in which we can choose n positions from 2xn available.
binomial :
(2n n) = (2n)! / (n! x n!)
##---
"""
def square_grid_path(n):
"""
Returns the number of paths from top left to bottom right of a
nxn grid.
"""
return (fact(2*n) / (fact(n) * fact(n)))
def fact(value):
"""
Returns value!
"""
if value < 2:
return value
else:
return value * fact(value - 1)
if __name__ == '__main__':
print "Answer : %d" % (square_grid_path(20))

40
e_18.py Executable file
View File

@@ -0,0 +1,40 @@
#!/usr/bin/env python
"""
##---
# jlengrand
#Created on : Mon Jan 16 15:34:46 CET 2012
#
# DESCRIPTION : Solves problem 18 of Project Euler
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
3
7 4
2 4 6
8 5 9 3
That is, 3 + 7 + 4 + 9 = 23.
Find the maximum total from top to bottom of the triangle below:
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
##---
"""
def fun():
"""
"""
return 1
if __name__ == '__main__':
print "Answer : %d" % (fun())

39
e_21.py Executable file
View File

@@ -0,0 +1,39 @@
#!/usr/bin/env python
"""
##---
# jlengrand
#Created on : Mon Jan 16 16:22:29 CET 2012
#
# DESCRIPTION : Solves problem 21 of Project Euler
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a b, then a and b are an amicable pair and each of a and b are called amicable numbers.
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
Evaluate the sum of all the amicable numbers under 10000.
##---
"""
def amicable_numbers(max_val):
"""
Returns the sum of amicable numbers under max_val
"""
am_list = []
for val in range(1, max_val):
am = sum_divisors(val)
if am != val and sum_divisors(am) == val :
am_list.append(val)
return sum(am_list)
def sum_divisors(value):
"""
Returns the sum of the list of proper divisors of value
"""
div = []
for val in range(1, value ):
if (value % val == 0):
div.append(val)
return sum(div)
if __name__ == '__main__':
print "Answer : %d" % (amicable_numbers(10000))

33
e_25.py Executable file
View File

@@ -0,0 +1,33 @@
#!/usr/bin/env python
"""
##---
# jlengrand
#Created on : Mon Jan 16 14:01:09 CET 2012
#
# DESCRIPTION : Solves problem 25 of Project Euler
The Fibonacci sequence is defined by the recurrence relation:
Fn = Fn1 + Fn2, where F1 = 1 and F2 = 1.
The 12th term, F12, is the first term to contain three digits.
What is the first term in the Fibonacci sequence to contain 1000 digits?
##---
"""
def fibo_dig_count(count):
"""
Returns the first term of Fibonacci sequence to contain count digits
"""
last_val = 1
val = 1 # Fibonacci starts with 1
inc = 0
while (len(str(val)) < count):
temp = last_val
last_val = val
val += temp
inc += 1
return inc + 2
if __name__ == '__main__':
print "Answer : %d" % (fibo_dig_count(1000))

23
e_48.py Executable file
View File

@@ -0,0 +1,23 @@
#!/usr/bin/env python
"""
##---
# jlengrand
#Created on : Mon Jan 16 15:47:00 CET 2012
#
# DESCRIPTION : Solves problem 48 of Project Euler
The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317.
Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000.
##---
"""
def sum_pow(max_val):
"""
Returns the last 10 digits of 1^1 + ... + max_val^max_val
"""
summ = 0
for j in range(1, max_val + 1):
summ += pow(j, j)
return int(str(summ)[-10:])
if __name__ == '__main__':
print "Answer : %d" % (sum_pow(1000))