Problem 3 » 履歴 » バージョン 3
  Noppi, 2023/12/27 12:14 
  
| 1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] | 
|---|---|---|---|
| 2 | # [[Problem 3]] | ||
| 3 | |||
| 4 | 3 | Noppi | ## Largest Prime Factor | 
| 5 | The prime factors of $13195$ are $5, 7, 13$ and $29$. | ||
| 6 | What is the largest prime factor of the number $600851475143$? | ||
| 7 | |||
| 8 | ## 最大の素因数 | ||
| 9 | 13195 の素因数は 5, 7, 13, 29 である. | ||
| 10 | 600851475143 の素因数のうち最大のものを求めよ. | ||
| 11 | |||
| 12 | 1 | Noppi | ```scheme | 
| 13 | #!r6rs | ||
| 14 | #!chezscheme | ||
| 15 | |||
| 16 | (import (chezscheme)) | ||
| 17 | |||
| 18 | (define answer-3 | ||
| 19 | 2 | Noppi | (let* ([n 600851475143] | 
| 20 | [check-max (isqrt n)]) | ||
| 21 | (let loop ([current 3] [rest n] [max-prime 1]) | ||
| 22 | 1 | Noppi | (cond | 
| 23 | [(< check-max current) max-prime] | ||
| 24 | [(zero? (mod rest current)) | ||
| 25 | (loop current (div rest current) current)] | ||
| 26 | [else (loop (+ current 2) rest max-prime)])))) | ||
| 27 | |||
| 28 | (printf "3: ~D~%" answer-3) | ||
| 29 | ``` |