Solves Problem 33 (coin change). Adds some new problems

This commit is contained in:
julien Lengrand-Lambert
2012-05-02 09:05:08 +02:00
parent f09f83e0a4
commit a00fe0fab3
6 changed files with 116 additions and 2 deletions

View File

@@ -42,7 +42,9 @@ Should be used in order to help future reuse of code :)
28 - What is the sum of both diagonals in a 1001 by 1001 spiral? - < 1 sec <br />
29 - How many distinct terms are in the sequence generated by ab for 2 a 100 and 2 b 100? - < 1 sec <br />
30 - Find the sum of all the numbers that can be written as the sum of fifth powers of their digits. - < 3 sec <br />
31 - Investigating combinations of English currency denominations. - < 1 sec <br />
34 - Find the sum of all numbers which are equal to the sum of the factorial of their digits. - 30 sec - 16 sec <br />
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 />
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 />
42 - Using words.txt, a 16K text file containing nearly two-thousand common English words, how many are triangle words? - < 1 sec <br />
@@ -55,8 +57,10 @@ Should be used in order to help future reuse of code :)
**In progress: **
26 - Find the value of d < 1000 for which 1/d contains the longest recurring cycle. <br />
31 - Investigating combinations of English currency denominations. <br />
35 - How many circular primes are there below one million? <br />
33 - Discover all the fractions with an unorthodox cancelling method. <br />
37 - Find the sum of all eleven primes that are both truncatable from left to right and right to left. <br />
38 - What is the largest 1 to 9 pandigital that can be formed by multiplying a fixed number by 1, 2, 3, ... ? <br />
41 - What is the largest n-digit pandigital prime that exists? <br />
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 :)**

40
e_31_2.py Normal file
View File

@@ -0,0 +1,40 @@
#!/usr/bin/env python
'''
Created on 10 feb. 2012
@author: Julien Lengrand-Lambert
DESCRIPTION: Solves problem 31 of Project Euler
In England the currency is made up of pound and pence, p, and there are eight coins in general circulation:
1p, 2p, 5p, 10p, 20p, 50p, L1 (100p) and L2 (200p).
It is possible to make L2 in the following way:
1 * L1 + 1 * 50p + 2 * 20p + 1 * 5p + 1 * 2p + 3 * 1p
How many different ways can L2 be made using any number of coins?
This problem is known as the coin change problem.
'''
S = [1, 2, 5, 10, 20, 50, 100, 200] # set of coins
def coin_change(n, m):
"""
Solves the coin change problem for a given set of coins and a desired value.
Recursive way
"""
# initial conditions
if n == 0 :
return 1
if n < 0:
return 0
if m <= 0 and n >= 1 :
return 1
# either a coin is not in the solution, and can be removed from the set
part1 = coin_change(n, m - 1)
# or it is part of it, and the final wanted value can be reduced (n - value of the coin)
part2 = coin_change(n - S[m], m)
return part1 + part2
if __name__ == '__main__':
answer = coin_change(200, len(S) - 1)
print "Answer is : %d" % (answer)

16
e_33.py Normal file
View File

@@ -0,0 +1,16 @@
#!/usr/bin/env python
'''
Created on 10 feb. 2012
@author: Julien Lengrand-Lambert
DESCRIPTION: Solves problem 33 of Project Euler
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.
We shall consider fractions like, 30/50 = 3/5, to be trivial examples.
There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.
If the product of these four fractions is given in its lowest common terms, find the value of the denominator.
'''
if __name__ == '__main__':
#print "Answer : %d " % (last_ten())

17
e_37.py Normal file
View File

@@ -0,0 +1,17 @@
#!/usr/bin/env python
'''
Created on 10 feb. 2012
@author: Julien Lengrand-Lambert
DESCRIPTION: Solves problem 37 of Project Euler
The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.
Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
'''
if __name__ == '__main__':
print 1
#print "Answer : %d " % (last_ten())

22
e_38.py Normal file
View File

@@ -0,0 +1,22 @@
#!/usr/bin/env python
'''
Created on 10 feb. 2012
@author: Julien Lengrand-Lambert
DESCRIPTION: Solves problem 38 of Project Euler
Take the number 192 and multiply it by each of 1, 2, and 3:
192 * 1 = 192
192 * 2 = 384
192 * 3 = 576
By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3)
The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1,2,3,4,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?
'''
if __name__ == '__main__':
print 1
#print "Answer : %d " % (last_ten())

15
e_41.py Normal file
View File

@@ -0,0 +1,15 @@
#!/usr/bin/env python
'''
Created on 10 feb. 2012
@author: Julien Lengrand-Lambert
DESCRIPTION: Solves problem 41 of Project Euler
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
What is the largest n-digit pandigital prime that exists?
'''
if __name__ == '__main__':
print 1
#print "Answer : %d " % (last_ten())