This file contains 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
; Problem 1: | |
; Find the sum of all the multiples of 3 or 5 below 1000. | |
(apply + (filter #(or (zero? (mod % 5)) | |
(zero? (mod % 3))) | |
(range 1000))) |
This file contains 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
; Problem 2: | |
; Find the sum of all the even-valued terms in the Fibonacci | |
; sequence which do not exceed four million. | |
(defn nextfib [[x y]] | |
(cond (zero? x) [1 1] | |
:else [y (+ x y)])) | |
(def fibs (map first (iterate nextfib [0 nil]))) | |
(def even-fibs-lt-4mil | |
(filter even? |
This file contains 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
; Problem 3 | |
; The prime factors of 13195 are 5, 7, 13 and 29. | |
; What is the largest prime factor of the number 600851475143 ? | |
(defn prime? [x] | |
(cond (< x 2) false | |
(= x 2) true | |
(even? x) false | |
:else (let [boundary (inc (int (Math/sqrt x))) | |
candidates (range 3 boundary 2)] |
This file contains 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
; Problem 4 | |
; A palindromic number reads the same both ways. The largest | |
; palindrome made from the product of two 2-digit numbers | |
; is 9009 = 91 99. | |
; | |
; Find the largest palindrome made from the product of two 3-digit numbers. | |
(defn palindromic? [x] | |
(let [strx (str x)] | |
(= (seq strx) (reverse strx)))) |
This file contains 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
; Problem 5 | |
; 2520 is the smallest number that can be divided by each of the | |
; numbers from 1 to 10 without any remainder. | |
; | |
; What is the smallest number that is evenly divisible by all of | |
; the numbers from 1 to 20? | |
(defn divisible-by? [x y] | |
(zero? (mod y x))) |
This file contains 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
; Problem 6 | |
; The sum of the squares of the first ten natural numbers is, | |
; | |
; 1^2 + 2^2 + ... + 10^2 = 385 | |
(def natural-numbers (iterate inc 1)) | |
(def squares (map #(* % %) natural-numbers)) | |
(def sum-of-squares | |
(reduce + (take 10 squares))) |
This file contains 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
; Problem 7 | |
; | |
; By listing the first six prime numbers: 2, 3, 5, 7, 11, | |
; and 13, we can see that the 6th prime is 13. | |
; | |
; What is the 10001st prime number? | |
(defn prime? [x] | |
(cond (< x 2) false | |
(= x 2) true |
This file contains 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
; Problem 8 | |
; | |
; Find the greatest product of five consecutive digits in the 1000-digit number. | |
(def digits | |
(map #(Integer/parseInt %) | |
(rest (seq (.split | |
(str | |
"73167176531330624919225119674426574742355349194934" | |
"96983520312774506326239578318016984801869478851843" |
This file contains 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
; Problem 9 | |
; A Pythagorean triplet is a set of three natural numbers, a < b < c, | |
; for which, | |
; | |
; a^2 + b^2 = c^2 | |
; For example, 3^2 + 4^2 = 9 + 16 = 25 = 52. | |
; | |
; There exists exactly one Pythagorean triplet for which a + b + c = 1000. | |
; Find the product abc. |
This file contains 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
; Problem 10 | |
; | |
; The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. | |
; | |
; Find the sum of all the primes below two million. | |
(defn prime? [x] | |
(cond (< x 2) false | |
(= x 2) true | |
(even? x) false |
OlderNewer