プロジェクト

全般

プロフィール

Problem 3 » 履歴 » バージョン 4

Noppi, 2023/12/28 01:53

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 4 Noppi
(define (prime-factor num)
19
  (let ([check-max (isqrt num)])
20
    (let loop ([current 2] [rest num] [result '()])
21 1 Noppi
      (cond
22 4 Noppi
        [(= rest 1) result]
23
        [(< check-max current) (cons rest result)]
24 2 Noppi
        [(zero? (mod rest current))
25 4 Noppi
         (loop current (div rest current) (cons current result))]
26
        [else (loop (add1 current) rest result)]))))
27
28
(define answer-3
29
  (apply max (prime-factor 600851475143)))
30 1 Noppi
31
(printf "3: ~D~%" answer-3)
32
```