プロジェクト

全般

プロフィール

操作

ホーム - Project Euler

Problem 3

Largest Prime Factor

The prime factors of $13195$ are $5, 7, 13$ and $29$.
What is the largest prime factor of the number $600851475143$?

最大の素因数

13195 の素因数は 5, 7, 13, 29 である.
600851475143 の素因数のうち最大のものを求めよ.

#!r6rs
#!chezscheme

(import (chezscheme))

(define (prime-factor num)
  (let ([check-max (isqrt num)])
    (let loop ([current 2] [rest num] [result '()])
      (cond
        [(= rest 1) result]
        [(< check-max current) (cons rest result)]
        [(zero? (mod rest current))
         (loop current (div rest current) (cons current result))]
        [else (loop (add1 current) rest result)]))))

(define answer-3
  (apply max (prime-factor 600851475143)))

(printf "3: ~D~%" answer-3)

Noppi2023/12/28に更新 · 4件の履歴