Problem 53 » 履歴 » バージョン 1
Noppi, 2024/01/18 13:41
1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
---|---|---|---|
2 | # [[Problem 53]] |
||
3 | |||
4 | ## Combinatoric Selections |
||
5 | There are exactly ten ways of selecting three from five, 12345: |
||
6 | |||
7 | <div style="text-align:center">123, 124, 125, 134, 135, 145, 234, 235, 245, and 345</div> |
||
8 | |||
9 | In combinatorics, we use the notation, $\displaystyle \binom 5 3 = 10$. |
||
10 | |||
11 | In general, $\displaystyle \binom n r = \dfrac{n!}{r!(n-r)!}$, where $r \le n$, $n! = n \times (n-1) \times ... \times 3 \times 2 \times 1$, and $0! = 1$. |
||
12 | |||
13 | It is not until $n = 23$, that a value exceeds one-million: $\displaystyle \binom {23} {10} = 1144066$. |
||
14 | |||
15 | How many, not necessarily distinct, values of $\displaystyle \binom n r$ for $1 \le n \le 100$, are greater than one-million? |
||
16 | |||
17 | ## 組み合わせ選択 |
||
18 | 12345から3つ選ぶ選び方は10通りである. |
||
19 | |||
20 | <div style="text-align:center">123, 124, 125, 134, 135, 145, 234, 235, 245, 345.</div> |
||
21 | |||
22 | 組み合わせでは, 以下の記法を用いてこのことを表す: $\displaystyle \binom 5 3 = 10$. |
||
23 | |||
24 | 一般に, $r \le n$ について $\displaystyle \binom n r = \dfrac{n!}{r!(n-r)!}$ である. ここで, $n! = n \times (n-1) \times ... \times 3 \times 2 \times 1$, $0! = 1$ と階乗を定義する. |
||
25 | |||
26 | $n = 23$ になるまで, これらの値が100万を超えることはない: $\displaystyle \binom {23} {10} = 1144066$. |
||
27 | |||
28 | $1 \le n \le 100$ について, 100万を超える $\displaystyle \binom n r$ は何通りあるか? |
||
29 | |||
30 | ```scheme |
||
31 | (import (scheme base) |
||
32 | (gauche base)) |
||
33 | |||
34 | (define (combination-num n r) |
||
35 | (assume (exact-integer? n)) |
||
36 | (assume (exact-integer? r)) |
||
37 | (assume (<= 1 r n 100)) |
||
38 | (/ (apply * (iota r n -1)) |
||
39 | (apply * (iota r r -1)))) |
||
40 | |||
41 | (define combination-list |
||
42 | (fold-right |
||
43 | (^[i lis] |
||
44 | (append |
||
45 | (map (^j `(,i ,j)) |
||
46 | (iota i 1)) |
||
47 | lis)) |
||
48 | '() |
||
49 | (iota 100 1))) |
||
50 | |||
51 | (define over100_0000list |
||
52 | (filter (cut < 100_0000 <>) |
||
53 | (map (^[lis] |
||
54 | (combination-num (car lis) |
||
55 | (cadr lis))) |
||
56 | combination-list))) |
||
57 | |||
58 | (define answer-53 |
||
59 | (length over100_0000list)) |
||
60 | |||
61 | (format #t "53: ~d~%" answer-53) |
||
62 | ``` |