41. Pandigital prime
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, 2143 is a 4-digit pandigital and is also prime.
What is the largest n-digit pandigital prime that exists?
Solution
1 | # What is the largest n-digit pandigital prime that exists? |
42. Coded triangle numbers
Problem Description
The $ n^{th} $ 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 words.txt (right click and ‘Save Link/Target As…’), a 16K text file containing nearly two-thousand common English words, how many are triangle words?
Solution
1 | # How many are triangle words? |
43. Sub-string divisibility
Problem Description
The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property.
Let $ d_{1} $ be the $ 1^{st} $ digit,$ d_{2} $ be the $ 2^{nd} $ digit, and so on. In this way, we note the following:
- $ d_{2}*d_{3}*d_{4} = 406 $ is divisible by 2
- $ d_{3}*d_{4}*d_{5} = 063 $ is divisible by 3
- $ d_{4}*d_{5}*d_{6}=635 $ is divisible by 5
- $ d_{5}*d_{6}*d_{7}=357 $ is divisible by 7
- $ d_{6}*d_{7}*d_{8}=572 $ is divisible by 11
- $ d_{7}*d_{8}*d_{9} = 728 $ is divisible by 13
- $ d_{8}*d_{9}*d_{10} = 289 $ is divisible by 17
Find the sum of all 0 to 9 pandigital numbers with this property.
Solution
题解引入sympy.nextprime(n)
函数以持续不断地获取素数,从而用于性质判断。该函数返回大于参数n的第一个素数。
1 | # Find the sum of all 0 to 9 pandigital numbers with this property |
44. Pentagon numbers
Problem Description
Pentagonal numbers are generated by the formula,$ P_{n} = n(3n−1)/2 $. The first ten pentagonal numbers are:
1, 5, 12, 22, 35, 51, 70, 92, 117, 145, …
It can be seen that $ P_{4} + P_{7} = 22 + 70 = 92 = P_{8} $. However, their difference, 70 − 22 = 48, is not pentagonal.
Find the pair of pentagonal numbers, $ P_{j} $ and$ P_{k} $, for which their sum and difference are pentagonal and $ D = |P_{k} − P_{j}| $ is minimised; what is the value of D?
Solution
题解引入operator函数库,运用其中的底层函数提高运算效率,该库内函数说明如下:
operator库函数说明:https://docs.python.org/zh-cn/3/library/operator.html。
1 | # Find the pair of pentagonal numbers, Pj and Pk |
45. Triangular, pentagonal, and hexagonal
Problem Description
Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:
NumberType | Formulae | Example |
---|---|---|
Triangle | $ T_{n} = n*(n+1) / 2 $ | 1, 3, 6, 10, 15, … |
Pentagonal | $ P_{n} = n*(3*n-1) / 2 $ | 1, 5, 12, 22, 35, … |
Hexagonal | $ H_{n} = n*(2*n-1) $ | 1, 6, 15, 28, 45 |
It can be verified that $ T_{285} = P_{165} = H_{143} = 40755 $.
Find the next triangle number that is also pentagonal and hexagonal.
Solution1
根据题意,以1—60000为生成元构造三角形数、五边形数和六边形数对应的集合,随后求交集并输出大于40755的第一个数字即可。
1 | # Find the next triangle number bigger than 40755 that is also pentagonal and hexagonal |
Solution2
通过观察这三类数字的生成表达式可以获得三角形数、五边形数和六边形数的判断方法:
- 若数字x是一个三角形数,则$ (\sqrt{(8*x+1)}-1) / 2 $必为一个整数;
- 若数字x是一个五边形数,则$ (\sqrt{(24*x+1)}+1) / 6 $必为一个整数;
- 若数字x是一个六边形数,则$ (\sqrt{(8*x+1)}+1) / 4 $必为一个整数
同时,这三类数的生成过程满足性质:$ H(n) = T(2n-1) $,即生成元n得到的六边形数一定对应更大生成元得到的三角形数。于是可以从第144个六边形数开始逐个验证其是否可表示为五边形数即可。暴力代码如下:
1 | # Find the next triangle number bigger than 40755 that is also pentagonal and hexagonal |
46. Goldbach’s other conjecture
Problem Description
It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.
It turns out that the conjecture was false.
What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?
Solution
1 | # What is the smallest odd composite that cannot be written as the sum of a prime and twice a square? |
47. Distinct primes factors
Problem Description
The first two consecutive numbers to have two distinct prime factors are:
The first three consecutive numbers to have three distinct prime factors are:
Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?
Solution
1 | # Find the first four consecutive integers to have four distinct prime factors each. |
48. Self powers
Problem Description
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} $.
Solution
1 | # Find the last ten digits of the series, sum(pow(n, n) for n in range(1, 1000)) |
49. Prime permutations
Problem Description
The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.
There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.
What 12-digit number do you form by concatenating the three terms in this sequence?
Solution
1 | # What 12-digit number do you form by concatenating the three terms in this sequence? |
50. Consecutive prime sum
Problem Description
The prime 41, can be written as the sum of six consecutive primes:
41 = 2 + 3 + 5 + 7 + 11 + 13
This is the longest sum of consecutive primes that adds to a prime below one-hundred.
The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of the most consecutive primes?
Solution
1 | # Which prime, below one-million, can be written as the sum of the most consecutive primes? |