# Kelvin的胡言乱语

==============> 重剑无锋，大巧不工。

# Project Euler的解题笔记

Project Euler exists to encourage, challenge, and develop the skills and enjoyment of anyone with an interest in the fascinating world of mathematics.

I learned so much solving problem XXX so is it okay to publish my solution elsewhere?

It appears that you have answered your own question. There is nothing quite like that "Aha!" moment when you finally beat a problem which you have been working on for some time. It is often through the best of intentions in wishing to share our insights so that others can enjoy that moment too. Sadly, however, that will not be the case for your readers. Real learning is an active process and seeing how it is done is a long way from experiencing that epiphany of discovery. Please do not deny others what you have so richly valued yourself.

## Problem 1

Multiples of 3 and 5

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

(funcall
(lambda (max)
(let ((m3 3) (m5 5) (sum 0))
(while (< m3 max)
(setq sum (+ sum
(if (= (% m3 5) 0) 0 m3)
(if (< m5 max) m5 0)))
(setq m3 (+ m3 3))
(setq m5 (+ m5 5)))
sum))
1000)


sum_multi(1000) = sum_multi_3(1000) + sum_multi_5(1000) - sum_multi_15(1000)
sum_multi_3(1000) = 3 + 6 + 9 + ... + 999
= 3 × (1 + 2 + ... + 333)
= 3 × (1 + 333) × 333 ÷ 2
sum_multi_5(1000) 和 sum_multi_15(1000) 类推


(defun sum(factor max)
(let ((count (/ max factor)))
(* factor
(/ (* count
(+ 1 count))
2))))

(funcall
(lambda (max)
(- (+ (sum 3 max)
(sum 5 max))
(sum 15 max)))
999)


## Problem 2

Even Fibonacci numbers

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

(funcall
(lambda (max)
(let ((3i 2) (3i+1 3) (3i+2 5) (sum 0))  ;; 这里的3i+1, 3i+2是变量，不是运算
(while (<= 3i max)
(setq sum (+ sum 3i))
(setq 3i (+ 3i+1 3i+2))
(setq 3i+1 (+ 3i 3i+2))
(setq 3i+2 (+ 3i 3i+1)))
sum))
4000000)


PS：因为这个解法就是“标准”答案的思路，所以这里就不分析“标准”答案了。 :-p

## Problem 3

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

(defun even? (n)
(= (% n 2) 0))

(defun square (n)
(* n n))

(defun expmod (base exp m)
(cond ((= exp 0) 1)
((even? exp) (% (square (expmod base (/ exp 2) m)) m))
(t (% (* base (expmod base (- exp 1) m)) m))))

(defun fermat-test (n)
(defun try-it (a)
(= (expmod a n n) a))
(try-it (+ 1 (random (- n 1)))))

(defun fast-prime? (n times)
(cond ((= times 0) t)
((fermat-test n) (fast-prime? n (- times 1)))
(t nil)))


fast-prime? 函数接收两个参数，一个要用来测试的数n，一个是执行费马检查的次数。整个检查的耗时部分在 expmod 函数，但是它的复杂度也只有O(log n)。

(funcall
(lambda (n test-times)
(let ((small-factor 1) (large-factor n)
(found nil) result)
(when (= (% n small-factor) 0)
(when (fast-prime? small-factor test-times)
(setq result small-factor))
(setq large-factor (/ n small-factor))
(when (fast-prime? large-factor test-times)
(setq result large-factor)
(setq found t)))
(setq small-factor (+ 1 small-factor)))
result))
600851475143 2)


n = "the evil big number"

if n mod 2 = 0 then
lastFactor = 2
n = n div 2
while n mod 2 = 0
n = n div 2
else
lastFactor = 1

factor = 3
maxFactor = sqrt(n)
while n > 1 and factor <= maxFactor
if n mod factor = 0 then
n = n div factor
lastFactor = factor
while n mod factor = 0
n = n div factor
maxFactor = sqrt(n)
factor=factor+2
if n = 1 then
output lastFactor
else
output n


“标准答案”基于两个事实：1. 任何正整数都可以被分解为多个素数因子的的积（如果把1也当作素数的话）；2. 一个正整数，最多只能有一个大于其平方根的素数因子。

## Problem 4

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.

(defun check-palindromic (n)
(let (numbers)
(setq numbers (list (% n 10)))
(setq n (/ n 10))
(while (> n 0)
(nconc numbers (,(% n 10)))
(setq n (/ n 10)))
(equal numbers (reverse numbers))))

(funcall
(lambda (min max)
(let ((s max) (l max) (result 0) temp)
(while (>= s min)
(setq l s)
(while (>= l min)
(setq temp (* s l))
(when (and (< result temp)
(check-palindromic temp))
(setq result temp))
(setq l (1- l)))
(setq s (1- s)))
result))
100 999)


## Problem 5

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

(defun prime-factors (num)
(let ((i 2) (factors '(1)))
(while (>= num i)
(when (= (% num i) 0)
(setq num (/ num i))
(while (= (% num i) 0)
(setq num (/ num i))))
(setq i (+ i (if (= i 2) 1 2))))
factors))

(funcall
(lambda (max)
(let ((i 2) (primes '(1)) (result 1) factors)
(while (<= i max)
(setq factors (prime-factors i))
(dolist (f (cdr factors))
(when (or (not (memq f primes))
(= (length factors) 2)) ;; 长度为2表明这是一个幂类型的数
(setq result (* result f))
(setq i (1+ i)))
result))
20)


## Problem 6

… Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

$S\left(n\right)=\left(1+2+3+...+n{\right)}^{2}-\left({1}^{2}+{2}^{2}+{3}^{2}+...+{n}^{2}\right)=\left[\frac{n\left(1+n\right)}{2}{\right]}^{2}-\frac{n\left(n+1\right)\left(2n+1\right)}{6}=...=\frac{{n}^{4}}{4}+\frac{{n}^{3}}{6}-\frac{{n}^{2}}{4}-\frac{n}{6}$

(funcall
(lambda (n)
(-
(+ (/ (expt n 4) 4)
(/ (expt n 3) 6))
(+ (/ (expt n 2) 4)
(/ n 6))))
100)


“标准答案”就不介绍了，没有这个简单。:-p

## Problem 7

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

(defun solve-problem7 (n)
(let ((i 2)
(prime 3))
(while (< i n)
(setq prime (+ prime 2))
(when (fast-prime? prime 2) ;; 这里的fast-prime?的定义在问题3里面可以看到
(setq i (+ i 1))))
prime))

(solve-problem7 10001)


561, 1105, 1729, 2465, 2821, 6601, 8911, 10585, 15841, 29341, 41041, 46657, 52633, 62745, 63973, 75361, 101101

“标准答案”的素数判定原理和普通的循环求模判定差不多，只是稍做了一些优化，这里就不再介绍了。

## Problem 8

Find the greatest product of five consecutive digits in the 1000-digit number.

73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450

## Problem 9

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2

For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

$a=\frac{s}{2}×\frac{s-2b}{s-b}$

(funcall
(lambda (sum)
(let ((b 1) (found nil) (half (/ sum 2))
a1 a2)
(setq a1 (* half (- sum (* 2 b))))
(setq a2 (- sum b))
(if (= (% a1 a2) 0)
(setq found t)
(setq b (1+ b))))
(* (/ a1 a2) b (- sum (/ a1 a2) b))))
1000)


“标准答案”咱就不看了，没看太懂。。

## Problem 10

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

(define (find-prime-sum max)
(cond ((> max 2)
(+ (if (miller-rabin-prime? max 5) max 0)
(find-prime-sum (- max (if (even? max) 1 2)))))
(else 2)))

(find-prime-sum 2000000)


1