Created
April 24, 2012 11:58
-
-
Save ponkore/2479096 to your computer and use it in GitHub Desktop.
Project Euler Problem 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;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 の素因数のうち最大のものを求めよ。 | |
;;; 素数のリストを求める。あまり巨大なリストにすると、計算が大変なので、 | |
;;; 1000000 位にアタリをつける。 | |
(defn primes [n] (primes- '(2) (take (dec n) (iterate inc 2)))) | |
(defn primes- [prime-list search-list] | |
(let [prime-list-max (first prime-list) | |
search-list-max (apply max search-list) | |
search-list-next (filter #(not (zero? (mod % prime-list-max))) search-list)] | |
(if (< search-list-max (* prime-list-max prime-list-max)) | |
(sort (concat prime-list search-list-next)) | |
(primes- (conj prime-list (first search-list-next)) search-list-next)))) | |
(def primes-1000000 (primes 1000000)) | |
(count primes-1000000) | |
;;;=>78498 | |
;;; Problem 3 の値。 | |
(def a 600851475143) | |
;;; コレを素因数分解するのだが、単純にこの中で割り切れる素数を調べてみる。 | |
(filter #(zero? (mod a %)) primes-1000000) | |
;;; => (71 839 1471 6857) | |
;;; たった4つしかない。あとは掛け算して検算、推測するに答えは... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment