Created
May 5, 2012 09:03
-
-
Save ponkore/2601115 to your computer and use it in GitHub Desktop.
Project Euler Problem 12
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 sequence of triangle numbers is generated by adding the natural numbers. | |
;;; So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: | |
;;; | |
;;; 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... | |
;;; | |
;;; Let us list the factors of the first seven triangle numbers: | |
;;; | |
;;; 1: 1 | |
;;; 3: 1,3 | |
;;; 6: 1,2,3,6 | |
;;; 10: 1,2,5,10 | |
;;; 15: 1,3,5,15 | |
;;; 21: 1,3,7,21 | |
;;; 28: 1,2,4,7,14,28 | |
;;; We can see that 28 is the first triangle number to have over five divisors. | |
;;; | |
;;; What is the value of the first triangle number to have over five hundred divisors? | |
(defn triangle-num [n] (apply + (take n (iterate inc 1)))) | |
;;; (map #(triangle-num %) (range 1 11)) | |
;;; => (1 3 6 10 15 21 28 36 45 55) | |
(defn divisors [n] | |
(let [limit (int (Math/floor (Math/sqrt n))) | |
seq (filter #(zero? (mod n %)) (range 1 (inc limit)))] | |
(sort (concat seq (map #(quot n %) seq))))) | |
;;; (divisors 28) | |
;;; => (1 2 4 7 14 28) | |
;;; blute force attack!!! | |
(defn bfa [counts] | |
(let [tnseq (map triangle-num (iterate inc 1)) | |
dv (map divisors tnseq)] | |
(filter #(> (count %) counts) dv))) | |
;;; 試しに動かしてみる。 | |
;;; (take 2 (bfa 100)) | |
;;; => ((1 2 3 4 5 6 7 8 10 11 12 14 15 16 20 21 22 24 28 30 32 33 35 40 42 44 48 55 56 60 64 66 70 77 80 84 88 96 105 110 112 120 132 140 154 160 165 168 176 192 210 220 224 231 240 264 280 308 320 330 336 352 385 420 440 448 462 480 528 560 616 660 672 704 770 840 880 924 960 1056 1120 1155 1232 1320 1344 1540 1680 1760 1848 2112 2240 2310 2464 2640 3080 3360 3520 3696 4620 4928 5280 6160 6720 7392 9240 10560 12320 14784 18480 24640 36960 73920) (1 2 3 4 5 6 7 9 10 11 12 14 15 18 20 21 22 28 30 33 35 36 42 44 45 49 55 60 63 66 70 77 84 90 98 99 105 110 126 132 140 147 154 165 180 196 198 210 220 231 245 252 294 308 315 330 385 396 420 441 462 490 495 539 588 630 660 693 735 770 882 924 980 990 1078 1155 1260 1386 1470 1540 1617 1764 1980 2156 2205 2310 2695 2772 2940 3234 3465 4410 4620 4851 5390 6468 6930 8085 8820 9702 10780 13860 16170 19404 24255 32340 48510 97020)) | |
;;; (take 1 (bfa 500)) | |
;;; => .... | |
;;; このリストの最後の値が答え。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment