EulerProject刷题记录 - Mathematica
Problem 23
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of $28$ would be $1 + 2 + 4 + 7 + 14 = 28$, which means that $28$ is a perfect number.
A number $n$ is called deficient if the sum of its proper divisors is less than $n$ and it is called abundant if this sum exceeds $n$.
As $12$ is the smallest abundant number, $1 + 2 + 3 + 4 + 6 = 16$, the smallest number that can be written as the sum of two abundant numbers is $24$. By mathematical analysis, it can be shown that all integers greater than $28123$ can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.
Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
1 | abundant = Select[Range[28123], Total@Divisors[#] > 2 # &]; |
其他解答:
1 | n = 28123; s = {}; |
Problem 24
A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits $1, 2, 3$ and $4$. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of $0, 1$ and $2$ are:
012 021 102 120 201 210
What is the millionth lexicographic permutation of the digits $0, 1, 2, 3, 4, 5, 6, 7, 8$ and $9$?
1 | lists = Permutations[Range[10] - 1, {10}]; |
Problem 25
The Fibonacci sequence is defined by the recurrence relation:
$F_n = F_{n−1} + F_{n−2}$, where $F_1 = 1$ and $F_2 = 1$.
Hence the first $12$ terms will be:
$F_1 = 1$
$F_2 = 1$
$F_3 = 2$
$F_4 = 3$
$F_5 = 5$
$F_6 = 8$
$F_7 = 13$
$F_8 = 21$
$F_9 = 34$
$F_{10} = 55$
$F_{11} = 89$
$F_{12} = 144$
The $12$th term, $F_{12}$, is the first term to contain three digits.
What is the index of the first term in the Fibonacci sequence to contain $1000$ digits?
1 | n = 12; |
Problem 27
Euler discovered the remarkable quadratic formula:
It turns out that the formula will produce 40 primes for the consecutive integer values $0 \leq n \leq 39$. However, when $n=40$, $40^2+40+41=40(40+1)+41$ is divisible by $41$, and certainly when $n=41$, $41^2+41+41$ is clearly divisible by $41$.
The incredible formula $n^2-79 n+1601$ was discovered, which produces $80$ primes for the consecutive values $0 \leq n \leq 79$. The product of the coefficients, $-79$ and $1601$, is $-126479$.
Considering quadratics of the form: $n^2+a n+b$, where $|a|<1000$ and $|b| \leq 1000$ where $|n|$ is the modulus/absolute value of $n$ e.g. $|11|=11$ and $|-4|=4$
Find the product of the coefficients, $a$ and $b$, for the quadratic expression that produces the maximum number of primes for consecutive values of $n$, starting with $n=0$.
1 | len[a_, b_] := Module[{n = 0}, While[PrimeQ[n^2 + a n + b], n++]; n]; |
1 | f[{a_, b_}] := (For[n = 0, PrimeQ[n^2 + a n + b], n++]; {n, a b}) |
1 | lengthOfQuadraticPrimes[a_Integer, b_Integer] := |
1 | First[Times @@@ |
Problem 28
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:
It can be verified that the sum of the numbers on the diagonals is $101$. What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?
1 | 1 + 4 \!\( |
Problem 29
Consider all integer combinations of $a^b$ for $2 \leq a \leq 5$ and $2 \leq b \leq 5$:
If they are then placed in numerical order, with any repeats removed, we get the following sequence of $15$ distinct terms:
How many distinct terms are in the sequence generated by $a^b$ for $2 \leq a \leq 100$ and $2 \leq b \leq 100$?
1 | Length@Union@Flatten@Table[a^b, {a, 2, 100}, {b, 2, 100}] |
Problem 30
Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
As $1=1^4$ is not a sum it is not included. The sum of these numbers is $1634+8208+9474=19316$. Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
1 | n = 2; res = {}; |
Problem 31
In the United Kingdom the currency is made up of pound $(£)$ and pence $(p)$. There are eight coins in general circulation:
It is possible to make $£ 2$ in the following way:
How many different ways can $£ 2$ be made using any number of coins?
1 | count[n_, lst__] := Module[{numoflast = Floor[n/lst[[-1]]]}, |
Problem 32
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 \times 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.
1 | alldigits = Range[9]; |
其它结果
1 | test = MemberQ[ |
Problem 33
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 $9$s.
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.
1 | test[mn__] := |
Problem 34
1 | test[n_] := Plus @@ (Factorial[#] & /@ IntegerDigits[n]) == n; |
其它解法
1 | Sum[Boole[n == Tr[IntegerDigits[n]!]] n, {n, 3, 1*^5}] // Timing |
1 | Total@(Transpose@ |
Problem 35
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?
1 | test[num_] := |
Problem 36
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.)
1 | test[n_] := (IntegerDigits[n] == |
Problem 37
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.
1 | primes = Table[Prime[n], {n, 5, 1000000/Log[1000000]}]; |
其他解法
1 | f[a_, b_] := Select[Plus @@@ Tuples@{# a, b^#2 Range@9}, PrimeQ] & |
1 | Total[Intersection[ |
Problem 38
Take the number $192$ and multiply it by each of $1, 2$, and $3$:
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, \ldots, n)$ where $n>1$?
1 | test[num_] := Module[{digitslength = 0, n = 0, products}, |
其他解法
1 | f = Join @@ IntegerDigits[# {1, 2}] &; |
Problem 39
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 \le 1000$, is the number of solutions maximised?
1 | pairs = Flatten[ |
Problem 40
An irrational decimal fraction is created by concatenating the positive integers:
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.
1 | lst = Join @@ (IntegerDigits /@ Range[1000000]); |
其他解法
1 | ChampernowneDigit[n_] := Block[{m = n}, |
一行代码的 bruteforce 解决1
Times @@ Flatten[IntegerDigits@Range@2*^5][[10^Range@6]]
Problem 41
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?
1 | For[n = 9, (lst = |
其他解法
1 | Select[FromDigits/@Permutations@Range@#,PrimeQ]&~Array~9//Max |
Problem 42
The $n$-th term of the sequence of triangle numbers is given by, $t_n=\frac{1}{2} n(n+1)$; so the first ten triangle numbers are:
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?
1 | olddata = |
其他解答1
2codes = ToCharacterCode@Import["Y:\\tmp\\0042_words.csv"][[1]] - 64;
Sqrt[1 + 8 Tr /@ codes]~Count~_Integer