## 21. Amicable numbers

Let

d(n) be defined as the sum of proper divisors ofn(numbers less thannwhich divide evenly inton).If

d(a) =bandd(b) =a, wherea≠b, thenaandbare an amicable pair and each ofaandbare called amicable numbers.For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore

d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; sod(284) = 220.Evaluate the sum of all the amicable numbers under 10000.

本题要寻找 10000 一下所有「相亲数」之和。可以用 `DivisorSigma`

计算所有因数的和。这里用了 *O*(*N*²) 的方法，比较笨，虽然编译到 C 以尽可能地加速但还是要跑两分钟：

```
(* Too slow! *)
findAmicablePairs = Compile[{{n, _Integer}},
Module[{sum = 0, x},
Do[
x = i + j; If[DivisorSigma[1, i] == x && DivisorSigma[1, j] == x, sum += x],
{j, n}, {i, j - 1}
];
sum],
CompilationTarget -> "C"];
findAmicablePairs[10000] // AbsoluteTiming
(* {119.510203, 31626} *)
```

实际上明明就有 *O*(*N*) 的解法，果然是我失了智了：

```
With[{f = DivisorSigma[1, #] - # &},
Total @ DeleteCases[Select[Range[10000], f[f[#]] == # &], _?PerfectNumberQ]]
(* 31626 *)
```

## 22. Names scores

Using

names.txt(right click and ‘Save Link/Target As…’), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?

数据拿到之后先清洗一下（没事加 `"`

干什么……），切成单词用 `ToCharacterCode`

转成 ASCII 码，再减掉 `64`

（`A`

是 65）得到字母顺序值。`MapIndexed`

类似 Python 的 `enumerate`

，可以同时作用在值和下标上。

```
With[{data = Import["https://projecteuler.net/project/resources/p022_names.txt", "String"]},
Total @ Flatten @ MapIndexed[Total[ToCharacterCode[#1] - 64] * #2 &] @
Sort @ StringSplit[StringDelete[data, "\""], ","]]
(* 871198282 *)
```

## 23. Non-abundant sums

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

nis called deficient if the sum of its proper divisors is less thannand it is called abundant if this sum exceedsn.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.

「过剩数」同样用 `DivisorSigma`

函数检查。找到过剩数的列表生成出全部可能的和，再做一下补集运算：

```
With[{n = 28123},
Total @ Complement[Range[n], Union[Total /@ Subsets[#, {2}], 2#]] & @
Select[Range[n], DivisorSigma[1, #] > 2# &]]
(* 4179871 *)
```

## 24. Lexicographic permutations

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?

默认的 `Permutations`

其实就是按字典序产生的（虽然文档没说，但试几下就可以确定了）。

```
FromDigits @ Part[Permutations[Range[0, 9]], 1*^6]
(* 2783915460 *)
```

## 25. 1000-digit Fibonacci number

The Fibonacci sequence is defined by the recurrence relation:

F_{n}=F_{n-1}+F_{n-2}, whereF_{1}= 1 andF_{2}= 1.Hence the first 12 terms will be:

F_{1}= 1F_{2}= 1F_{3}= 2F_{4}= 3F_{5}= 5F_{6}= 8F_{7}= 13F_{8}= 21F_{9}= 34F_{10}= 55F_{11}= 89F_{12}= 144The 12th 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?

Fibonacci 序列和数字位数都有默认函数。不定长的循环可以用 `NestWhile`

实现。

```
NestWhile[# + 1 &, 1, IntegerLength[Fibonacci[#]] < 1000 &]
(* 4782 *)
```

## 26. Reciprocal cycles

A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:

1/2 = 0.5

1/3 = 0.(3)

1/4 = 0.25

1/5 = 0.2

1/6 = 0.1(6)

1/7 = 0.(142857)

1/8 = 0.125

1/9 = 0.(1)

1/10 = 0.1Where 0.1(6) means 0.166666…, and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.

Find the value of

d< 1000 for which 1/dcontains the longest recurring cycle in its decimal fraction part.

`RealDigits`

中包含了循环节的信息。有限小数不包含这个循环节的列表，长度为 0，因此可以统一处理。

```
First @ MaximalBy[Range[1000], Length @ RealDigits[1 / #][[1, 1]] &]
(* 983 *)
```

## 27. Quadratic primes

Euler discovered the remarkable quadratic formula:

n² +n+ 41It turns out that the formula will produce 40 primes for the consecutive integer values 0 ≤

n≤ 39. However, whenn= 40, 40² + 40 + 41 = 40 (40 + 1) is divisible by 41, and certainly whenn= 41, 41² + 41 + 41 is clearly divisible by 41.The incredible formula

n² − 79n+ 1601 was discovered, which produces 80 primes for the consecutive values 0 ≤n≤ 79. The product of the coefficients, −79 and 1601, is −126479.Considering quadratics of the form:

n² +an+b, where |a| < 1000 and |b| ≤ 1000where |

n| is the modulus/absolute value ofn. e.g. |11| = 11 and |−4| = 4Find the product of the coefficients,

aandb, for the quadratic expression that produces the maximum number of primes for consecutive values ofn, starting withn= 0.

给定 `a`

、`b`

之后，`qPrimesCount`

函数给出对应二次函数所能生成的最大素数。剩下来的事情便是枚举了。若要 *n* = 0 时为素数，必然要求 `b`

为素数。稍微检查一下即可知道 2 不可能，因此 `b`

为奇素数。继而又可以推出 `a`

也为奇数。这样枚举范围就可以大大缩小。

```
qPrimesCount[a_, b_] := NestWhile[# + 1 &, 0, PrimeQ[#^2 + a # + b] &]
Times @@ First @ MaximalBy[Apply[qPrimesCount]] @ Catenate @
Table[{a, b}, {a, Range[-999, 999, 2]}, {b, Prime @ Range[2, PrimePi[1000]]}]
(* -59231 *)
```

## 28. Number spiral diagonals

Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:

2122 23 2425

2078910

19 612 11

18543121716 15 1413It 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?

易见右上角的数字为 *n*²，进而可以推出四个角对应的公式。代值计算，最后补上中心位置的 1 即可。

```
Total[Total[n^2 - (n-1) * Range[0, 3]] /. n -> Range[3, 1001, 2]] + 1
(* 669171001 *)
```

## 29. Distinct powers

Consider all integer combinations of

abfor 2 ≤a≤ 5 and 2 ≤b≤ 5:2² = 4, 2³ = 8, 2⁴ = 16, 2⁵ = 32

3² = 9, 3³ = 27, 3⁴ = 81, 3⁵ = 243

4² = 16, 4³ = 64, 4⁴ = 256, 4⁵ = 1024

5² = 25, 5³ = 125, 5⁴ = 625, 5⁵ = 3125If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?

直接打表计算，用 `Union`

移除重复项。

```
Union @@ Table[a^b, {a, 2, 100}, {b, 2, 100}] // Length
(* 9183 *)
```

## 30. Digit fifth powers

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 1⁴ + 6⁴ + 3⁴ + 4⁴

8208 = 8⁴ + 2⁴ + 0⁴ + 8⁴

9474 = 9⁴ + 4⁴ + 7⁴ + 4⁴As 1 = 1⁴ 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.

先用 `IntegerDigits`

得到各位数字（它和上面的 `FromDigits`

互逆），算 5 次方求和再和原数字相比较。别忘了最后扔掉 1。乘方这样的基本操作都是可以直接作用在列表上的，不需要额外用 `/@`

。

这里选择的上界 `2*^5`

是盲猜的，但其实也很容易找到比较确切的值。

```
Total @ Select[{#, Total[IntegerDigits[#]^5]} & /@ Range[2*^5], Apply[#1 == #2 &]][[2;;, 1]]
(* 443839 *)
```