31. Coin sums
Problem Description
In the United Kingdom the currency is made up of pound (£) and pence (p). There are eight coins in general circulation:
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p), and £2 (200p).
It is possible to make £2 in the following way:
1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
How many different ways can £2 be made using any number of coins?
Solution
题目本质为经典的找零问题,可以通过动态规划的方法解决。
以本题给出的条件为例进行详细说明。题目给出1,2,5,10,20,50,100,200这八种面额的便士类型,要求通过组合获得200便士并求出所有组合的数量。首先将问题进行分解,结果为200便士的组合可分为以下三种:
- 不引入100便士而是使用其他便士进行组合(单体最多为200便士)
- 引入一个100便士,剩余的100便士由其他便士组合(单体最多为100便士)
- 引入两个100便士完成组合
故可得以下递推公式:
\1.PNG)
各字符含义如下:
- t表示目标量;
- c表示最大可用的硬币面值;
- s(c)表示除c外更小的硬币面值;
- w(t, c)表示使用面值为c或更小的硬币组成目标量的所有方法数。
根据以上递推公式采用动态规划的方法,将大问题分割成多个小问题并进行分类讨论处理,该方法为第一类动态规划,遵循自顶向下的问题划分原则。同时引入二维数组记录不同(t, c)组对应的方法数,以提高程序运行效率。代码如下:
1 | # How many different ways can 2 pound be made using any number of coins? |
以上算法还可以进一步优化。首先我们观察(t, c)取值不同时w(t, c)的运算结果,对应如下表:
\2.PNG)
根据以上对应关系归纳递推公式如下:
\3.PNG)
经过深入分析,我们可以得出w’(t, c)函数不同参数对应结果之间的关系图如下所示:
\4.PNG)
可以看出第一行和第一列的数值1达到最终目的地需要较长的加和路径。除此之外其他所有元素都可由最多另外两个元素相加得到。从而每个w’(t, c)的值最多决定于另外两个数值,即:
- 同列c的前一个元素;
- 同行t,前一列c-1对应的元素;
该方法为第二种动态规划方法,遵循自底向上的问题划分原则,从较小的问题开始解决,逐步解决更大的子问题。此类方法在处理以下问题时具有更高的效率:
- 每个元素可以通过至多两个其他元素相加获得;
- 要求较低的时间复杂度及空间复杂度;
- 使用循环而非函数调用解决问题;
结合以上分析,对应代码如下:
1 | # How many different ways can 2 pound be made using any number of coins? |
找零问题参考资料
Euler-Project-Problem31-Overview:https://projecteuler.net/overview=031
动态规划2:https://www.jianshu.com/p/e515efee2310
硬币找零问题:https://www.cnblogs.com/anzhengyu/p/11176134.html
32. Pandigital products
Problem Description
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, the 5-digit number, 15234, is 1 through 5 pandigital.
The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.
Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.
HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.
Solution
1 | # Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital. |
33. Digit cancelling fractions
Problem Description
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.
Solution
1 | # If the product of these four fractions is given in its lowest common terms, find the value of the denominator. |
34. Digit factorials
Problem Description
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
Find the sum of all numbers which are equal to the sum of the factorial of their digits.
Note: As 1! = 1 and 2! = 2 are not sums they are not included.
Solution
1 | # Find the sum of all numbers which are equal to the sum of the factorial of their digits |
35. Circular primes
Problem Description
The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.
There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.
How many circular primes are there below one million?
Solution
首先引入python的sympy库筛选出10到1000000之间的所有素数,随后对其进行第二次筛选操作。由于需要对每个素数进行顺序位移,故第一批剔除任意数位为偶数的素数。第三次筛选题目要求进行顺序位移并判断结果是否在第一步获得的素数池内。最后计算长度并作差即可。
1 | # How many circular primes are there below one million |
36. Double-base palindromes
Problem Description
The decimal number, $ 585 = 1001001001_{2}$ (binary), is palindromic in both bases.
Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
(Please note that the palindromic number, in either base, may not include leading zeros.)
Solution1
1 | # Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2 |
Solution2
除借助python特有的字符串逆序特性方便地解决问题外,我们设计任意进制下判定某个数字是否为回文数字的函数isPalindromes()
如下:
1 | def isPalindrome(input_number, base): |
注意在二进制运算中,可以使用&1
代替模运算,使用<<1
代替乘法运算。
由于题目需要在二进制下保持回文状态且不考虑前导零的存在,故排除所有的偶数,即循环过程中以奇数起始,步长选择为2:
1 | if __name__ == '__main__': |
Solution3
在题目给出的限制范围内,解法二可保持较高的效率,但随着数字上限的扩大,程序运行时间会大大增长。故考虑”生成+判定”的方法:假设在b进制下存在回文数字xyzzyx,取前三个数字xyz为回文结,则该六位数字由三位回文结xyz定义。同时xyz亦可定义五位回文数字xyzyx。故我们可得出如下结论:
对于任意b进制,任意小于$b^{n}$的整数可作为回文结生成两个小于$b^{2n}$的回文数字,这两个回文数字的位数一奇一偶。
应用以上结论,我们选择在二进制下生成两个回文数字并检验十进制下该数字是否为回文数字。如果需要得到奇数位数的回文数字,则设置标志位为true,并进行运算生成元n/进制b
,代码如下:
1 | # Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2 |
37. Truncatable primes
Problem Description
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.
Solution
1 | # Find the sum of the only eleven primes that are both truncatable from left to right and right to left |
38. Pandigital multiples
Problem Description
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?
Solution
1 | # 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? |
39. Integer right triangles
Problem Description
If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.
{20,48,52}, {24,45,51}, {30,40,50}
For which value of p ≤ 1000, is the number of solutions maximised?
Solution
1 | # For which value of p <= 1000, is the numnber of solutions maximised? |
40. Champernowne’s constant
Probelm Description
An irrational decimal fraction is created by concatenating the positive integers:
0.123456789101112131415161718192021…
It can be seen that the $12^{th}$ digit of the fractional part is 1.
If $d_{n}$ represents the $n^{th}$ digit of the fractional part, find the value of the following expression.
Solution
1 | # Find the value of the following expression |