Problem 12 » 履歴 » バージョン 2
Noppi, 2023/12/29 15:07
| 1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
|---|---|---|---|
| 2 | # [[Problem 12]] |
||
| 3 | |||
| 4 | ## Highly Divisible Triangular Number |
||
| 5 | The sequence of triangle numbers is generated by adding the natural numbers. So the $7$<sup>th</sup> triangle number would be $1 + 2 + 3 + 4 + 5 + 6 + 7 = 28$. The first ten terms would be: |
||
| 6 | $$1, 3, 6, 10, 15, 21, 28, 36, 45, 55, \dots$$ |
||
| 7 | Let us list the factors of the first seven triangle numbers: |
||
| 8 | |||
| 9 | **1:** 1 |
||
| 10 | **3:** 1,3 |
||
| 11 | **6:** 1,2,3,6 |
||
| 12 | **10:** 1,2,5,10 |
||
| 13 | **15:** 1,3,5,15 |
||
| 14 | **21:** 1,3,7,21 |
||
| 15 | **28:** 1,2,4,7,14,28 |
||
| 16 | |||
| 17 | We can see that $28$ is the first triangle number to have over five divisors. |
||
| 18 | What is the value of the first triangle number to have over five hundred divisors? |
||
| 19 | |||
| 20 | ## 高度整除三角数 |
||
| 21 | 三角数の数列は自然数の和で表わされ, 7番目の三角数は 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28 である. 三角数の最初の10項は: |
||
| 22 | $$1, 3, 6, 10, 15, 21, 28, 36, 45, 55, \dots$$ |
||
| 23 | となる. |
||
| 24 | |||
| 25 | 最初の7項について, その約数を列挙すると, 以下のとおり. |
||
| 26 | |||
| 27 | **1:** 1 |
||
| 28 | **3:** 1,3 |
||
| 29 | **6:** 1,2,3,6 |
||
| 30 | **10:** 1,2,5,10 |
||
| 31 | **15:** 1,3,5,15 |
||
| 32 | **21:** 1,3,7,21 |
||
| 33 | **28:** 1,2,4,7,14,28 |
||
| 34 | |||
| 35 | これから, 7番目の三角数である28は, 5個より多く約数をもつ最初の三角数であることが分かる. |
||
| 36 | では, 500個より多く約数をもつ最初の三角数はいくつか. |
||
| 37 | |||
| 38 | ```scheme |
||
| 39 | 2 | Noppi | #!r6rs |
| 40 | #!chezscheme |
||
| 41 | |||
| 42 | (import (chezscheme)) |
||
| 43 | |||
| 44 | (define (triangular-numbers end-condition) |
||
| 45 | (let loop ([i 1] [current 0] [result '()]) |
||
| 46 | (if (and |
||
| 47 | (not (null? result)) |
||
| 48 | (end-condition result)) |
||
| 49 | result |
||
| 50 | (let ([next (+ current i)]) |
||
| 51 | (loop (add1 i) next (cons next result)))))) |
||
| 52 | |||
| 53 | (define (divisors num) |
||
| 54 | (let ([check-max (isqrt num)]) |
||
| 55 | (let loop ([i 1] [result '()]) |
||
| 56 | (cond |
||
| 57 | [(< check-max i) result] |
||
| 58 | [(zero? (mod num i)) |
||
| 59 | (loop (add1 i) (cons* i (div num i) result))] |
||
| 60 | [else (loop (add1 i) result)])))) |
||
| 61 | |||
| 62 | (define over500-divisors-triangular-number |
||
| 63 | (triangular-numbers |
||
| 64 | (lambda (lis) |
||
| 65 | (< 500 (length (divisors (car lis))))))) |
||
| 66 | |||
| 67 | (define answer-12 |
||
| 68 | (car over500-divisors-triangular-number)) |
||
| 69 | |||
| 70 | (printf "12: ~D~%" answer-12) |
||
| 71 | 1 | Noppi | ``` |