From a00fe0fab3232d7c0feed0ecbd8bd1ce13ee2594 Mon Sep 17 00:00:00 2001 From: julien Lengrand-Lambert Date: Wed, 2 May 2012 09:05:08 +0200 Subject: [PATCH] Solves Problem 33 (coin change). Adds some new problems --- README.markdown | 8 ++++++-- e_31_2.py | 40 ++++++++++++++++++++++++++++++++++++++++ e_33.py | 16 ++++++++++++++++ e_37.py | 17 +++++++++++++++++ e_38.py | 22 ++++++++++++++++++++++ e_41.py | 15 +++++++++++++++ 6 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 e_31_2.py create mode 100644 e_33.py create mode 100644 e_37.py create mode 100644 e_38.py create mode 100644 e_41.py diff --git a/README.markdown b/README.markdown index ba85258..50a4517 100644 --- a/README.markdown +++ b/README.markdown @@ -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
29 - How many distinct terms are in the sequence generated by ab for 2 a 100 and 2 b 100? - < 1 sec
30 - Find the sum of all the numbers that can be written as the sum of fifth powers of their digits. - < 3 sec
+31 - Investigating combinations of English currency denominations. - < 1 sec
34 - Find the sum of all numbers which are equal to the sum of the factorial of their digits. - 30 sec - 16 sec
+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
39 - If p is the perimeter of a right angle triangle, {a, b, c}, which value, for p <= 1000, has the most solutions? - 1min
42 - Using words.txt, a 16K text file containing nearly two-thousand common English words, how many are triangle words? - < 1 sec
@@ -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.
-31 - Investigating combinations of English currency denominations.
-35 - How many circular primes are there below one million?
+33 - Discover all the fractions with an unorthodox cancelling method.
+37 - Find the sum of all eleven primes that are both truncatable from left to right and right to left.
+38 - What is the largest 1 to 9 pandigital that can be formed by multiplying a fixed number by 1, 2, 3, ... ?
+41 - What is the largest n-digit pandigital prime that exists?
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_31_2.py b/e_31_2.py new file mode 100644 index 0000000..9381f15 --- /dev/null +++ b/e_31_2.py @@ -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) + \ No newline at end of file diff --git a/e_33.py b/e_33.py new file mode 100644 index 0000000..48b1725 --- /dev/null +++ b/e_33.py @@ -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()) \ No newline at end of file diff --git a/e_37.py b/e_37.py new file mode 100644 index 0000000..2051f62 --- /dev/null +++ b/e_37.py @@ -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()) \ No newline at end of file diff --git a/e_38.py b/e_38.py new file mode 100644 index 0000000..b302f24 --- /dev/null +++ b/e_38.py @@ -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()) \ No newline at end of file diff --git a/e_41.py b/e_41.py new file mode 100644 index 0000000..4439939 --- /dev/null +++ b/e_41.py @@ -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()) \ No newline at end of file