Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Euler7 #3

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
25 changes: 25 additions & 0 deletions .lein-repl-history
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(partition 3 1 `(1 2 3 4 5 6 7 8))
(partition 4 1 `(1 2 3 4 5 6 7 8))
(partition 3 1 `(1 2 3 4 5 ))
(Math/sqrt 5)
(Math/sqrt 4)
(def n (Math/sqrt 5))
(def p (MAth/sqrt 4))
(def p (Math/sqrt 4))
(str n)
(first (str n))
(second (str n))
(nth (str n) 3)
(nth (str p) 3)
(nth (str n) 2)
(nth (str p) 2)
(defn tenth-zero? [n] (= \0 (nth (str n) 2)))
(tenth-zero? n)
(tenth-zero? p)
(tenth-zero? 113484)
(tenth-zero? 10.2)
(tenth-zero? 10.0)
(tenth-zero? 100.7)
(defn whole-num? [n] (zero? (- n (int n))))
(whole-num? 2)
(whole-num? 2.1)
1 change: 1 addition & 0 deletions .nrepl-port
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
62615
3 changes: 2 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(defproject euler "1.0.0-SNAPSHOT"
:description "Solutions to https://projecteuler.net"
:dependencies [[org.clojure/clojure "1.10.1"]
[org.clojure/math.combinatorics "0.1.6"]]
[org.clojure/math.combinatorics "0.1.6"]
[org.clojure/math.numeric-tower "0.0.4"]]
:profiles {:dev {:dependencies [[speclj "3.3.2"]]}}
:plugins [[speclj "3.3.2"]]
:test-paths ["spec/"]
Expand Down
17 changes: 15 additions & 2 deletions spec/euler/level1/problem001_spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@

; http://projecteuler.net/index.php?section=problems&id=1

;; nonambiguous
(describe "Euler Problem #1"

(it "Multiple of 3 or 5"
(should= false (multiple-3-5? 1))
(should= false (multiple-3-5? 2))
(should= true (multiple-3-5? 3))
(should= false (multiple-3-5? 4))
(should= true (multiple-3-5? 5))
(should= true (multiple-3-5? 6))
(should= false (multiple-3-5? 7))
(should= false (multiple-3-5? 8))
(should= true (multiple-3-5? 9))
(should= true (multiple-3-5? 10))
)
(it "Solves #1"
(should= 23 (euler-1 10))
(should= -1 (euler-1 1000)))
(should= 233168 (euler-1 1000))
)
)

(run-specs)
34 changes: 34 additions & 0 deletions spec/euler/level1/problem002_spec.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(ns euler.level1.problem002-spec
(:use
[speclj.core]
[euler.level1.problem002])
)

; https://projecteuler.net/problem=2

(describe "Euler Problem #2"
(it "Gets next fibonacci term"
(should= 1 (get-next-fib-term 1))
(should= 2 (get-next-fib-term 2))
(should= 3 (get-next-fib-term 3))
(should= 5 (get-next-fib-term 4))
)
(it "Generates fibonacci sequence of length n"
(should= '(1) (get-fib-seq 1))
(should= '(1 2) (get-fib-seq 2))
(should= '(1 2 3) (get-fib-seq 3))
(should= '(1 2 3 5) (get-fib-seq 4))
)
(it "Checks if number is even"
(should= false (is-even? 1))
(should= true (is-even? 2))
(should= false (is-even? 3))
(should= true (is-even? 4))
)
(it "Solves #2"
(should= 10 (euler-2 5))
(should= 4613732 (euler-2 33))
)
)

(run-specs)
22 changes: 22 additions & 0 deletions spec/euler/level1/problem003_spec.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(ns euler.level1.problem003-spec
(:require
[speclj.core :refer :all]
[euler.level1.problem003 :refer :all]))

; https://projecteuler.net/problem=3

(describe "Euler Problem #3"
(it "Gets the next factor greater than 1"
(should= 2 (find-factor 10)))

(it "Gets largest factor of n"
(should= 29 (get-max #{nil 7 13 29 5})))

(it "Gets a list of all the factors > 1 for n "
(should= #{nil 7 13 29 5} (factorialize 13195)))

(it "Solves #3"
(should= 6857 (euler-3 600851475143)))
)

(run-specs)
16 changes: 16 additions & 0 deletions spec/euler/level1/problem004_spec.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(ns euler.level1.problem004-spec
(:require
[speclj.core :refer :all]
[euler.level1.problem004 :refer :all]))

; https://projecteuler.net/problem=4

(describe "Euler Problem #4"

(it "Solves #4"
(should= -1 (euler-4 -1)))
(it "returns true if number is a palindrome"
(should= true (is-palindrome? 5005)))
)

(run-specs)
14 changes: 14 additions & 0 deletions spec/euler/level1/problem005_spec.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(ns euler.level1.problem005-spec
(:require
[speclj.core :refer :all]
[euler.level1.problem005 :refer :all]))

; https://projecteuler.net/problem=5

(describe "Euler Problem #5"

(it "Solves #5"
(should= -1 (euler-5 -1)))
)

(run-specs)
20 changes: 20 additions & 0 deletions spec/euler/level1/problem006_spec.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(ns euler.level1.problem006-spec
(:require
[speclj.core :refer :all]
[euler.level1.problem006 :refer :all]))

; https://projecteuler.net/problem=6

(describe "Euler Problem #6"

(it "Gets sum of the squares of the first n numbers"
(should= 385 (get-sum-squares 10))
)
(it "Gets square of the sum of the first n numbers"
(should= 3025 (get-square-sum 10))
)
(it "Solves #6"
(should= 25164150 (euler-6 100)))
)

(run-specs)
28 changes: 28 additions & 0 deletions spec/euler/level1/problem007_spec.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(ns euler.level1.problem007-spec
(:require
[speclj.core :refer :all]
[euler.level1.problem007 :refer :all]))

; https://projecteuler.net/problem=7

(describe "euler 7"

(it "check if n is not divisble"
(should (non-divisible? 3 2))
(should-not (non-divisible? 4 2))
(should (non-divisible? 4 3))
)
(it "checks if n is prime"
(should (prime? 2))
(should-not (prime? 4))
)
(it "solve euler 7"
(should= 2 (euler-7 1))
(should= 5 (euler-7 3))
(should= 7927 (euler-7 1001))
(should= 104743 (euler-7 10001))
)

)

(run-specs)
26 changes: 26 additions & 0 deletions spec/euler/level1/problem008_spec.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(ns euler.level1.problem008-spec
(:require
[speclj.core :refer :all]
[euler.level1.problem008 :refer :all]))

; https://projecteuler.net/problem=8

(describe "Euler Problem #8"

(it "converts n to vector of alphanumeric characters"
(should= `([\1 \2 \3] [\2 \3 \4] [\3 \4 \5] [\4 \5 \6]) (get-groupings 123456 3))
(should= `([\1 \2 \3 \4] [\2 \3 \4 \5] [\3 \4 \5 \6] [\4 \5 \6 \7] [\5 \6 \7 \8]) (get-groupings 12345678 4))
)
(it "converts char groupings to int groupings"
(should= `([1 2 3] [2 3 4] [3 4 5]) (convert-to-int `([\1 \2 \3] [\2 \3 \4] [\3 \4 \5])))
(should= `([1 2 3] [2 3 4] [3 4 5] [4 5 6]) (convert-to-int `([\1 \2 \3] [\2 \3 \4] [\3 \4 \5] [\4 \5 \6])))
)
(it "Gets the sorted set of products"
(should= #{6 24 60} (get-product-set `([1 2 3] [2 3 4] [3 4 5])))
)

(it "Solves #8"
(should= 23514624000 (euler-8 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450)))
)

(run-specs)
51 changes: 51 additions & 0 deletions spec/euler/level1/problem009_spec.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
(ns euler.level1.problem009-spec
(:require
[speclj.core :refer :all]
[euler.level1.problem009 :refer :all]))

; https://projecteuler.net/problem=9

(describe "euler 9"

(it "is pythagorean triplet?"
(should (pythagorean-triplet? [3 4 5]))
(should-not (pythagorean-triplet? [0 1 2])))

(it "check if c is a perfect square"
(should (perfect-square? 0))
(should (perfect-square? 1))
(should-not (perfect-square? 2))
(should-not (perfect-square? 3))
(should (perfect-square? 4))
(should-not (perfect-square? 5))
(should (perfect-square? 9))
)

(it "sums to n"
(should-not (sums-to-n? 12 [1 2 3]))
(should (sums-to-n? 12 [3 4 5])))

(it "maybe returns a pythagorean triplet"
(should-be-nil (maybe-p-triplet [1 2]))
(should= [3 4 5] (maybe-p-triplet [3 4]))
(should= [6 8 10] (maybe-p-triplet [6 8]))
)

(it "finds all triplets below n"
(should= [] (find-pythagorean-triplets-below-n 0))
(should= [] (find-pythagorean-triplets-below-n 1))
(should= [] (find-pythagorean-triplets-below-n 2))
(should= [] (find-pythagorean-triplets-below-n 3))
(should= [] (find-pythagorean-triplets-below-n 4))
(should= [[3 4 5]] (find-pythagorean-triplets-below-n 5))
(should= [[3 4 5] [6 8 10]] (find-pythagorean-triplets-below-n 10))
)

(it "solve euler #9"
(should= 60 (euler-9 12))
(should= 480 (euler-9 24))
(should= 31875000 (euler-9 1000))
)
)

(run-specs)
14 changes: 14 additions & 0 deletions spec/euler/level1/problem010_spec.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(ns euler.level1.problem010-spec
(:require
[speclj.core :refer :all]
[euler.level1.problem010 :refer :all]))

; https://projecteuler.net/problem=10

(describe "Euler Problem #10"

(it "Solves #10"
(should= -1 (euler-10 -1)))
)

(run-specs)
Binary file added src/.DS_Store
Binary file not shown.
10 changes: 10 additions & 0 deletions src/euler/level1/problem001.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
(ns euler.level1.problem001)

(defn multiple-3-5? [n]
(cond
(= (mod n 3) 0) true
(= (mod n 5) 0) true
:else false)
)

(defn euler-1 [n]
(reduce + (filter multiple-3-5? (range n)))
)


23 changes: 23 additions & 0 deletions src/euler/level1/problem002.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(ns euler.level1.problem002)



(defn get-next-fib-term [n]
(if (< n 4)
n
(+ (get-next-fib-term (- n 1)) (get-next-fib-term (- n 2)))
))

(defn get-fib-seq [n]
(map get-next-fib-term (range 1 (+ n 1)))
)

(defn is-even? [n]
(if(= (mod n 2) 0)
true
false)
)

(defn euler-2 [n]
(reduce + (filter is-even? (get-fib-seq n)))
)
23 changes: 23 additions & 0 deletions src/euler/level1/problem003.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(ns euler.level1.problem003)

(defn find-factor [n]
(->> (range 2 (inc n))
(drop-while #(not(= 0 (mod n %))))
(first)))

(defn get-max [factors]
(->> (clojure.set/difference factors #{nil} )
(apply max)))

(defn factorialize [n]
(loop [n n
factors #{}]
(if (nil? n)
factors
(let [next (find-factor n)
quotient (if (nil? next) nil (/ n next))]
(recur quotient (conj factors next))))))

(defn euler-3 [n]
(->> (factorialize n)
(get-max)))
24 changes: 24 additions & 0 deletions src/euler/level1/problem004.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(ns euler.level1.problem004)

(def prod (for [num-1 (vec (range 1 1000))
num-2 (vec (range 1 1000))]
[num-1 num-2]))

(def six-digit-products
(sort(set (map #(reduce * %) prod)))
)

(defn is-palindrome? [num]
(let [num (vec (str num))
rev-num (vec (reverse num))]
(if (= 0 (compare num rev-num))
true
false)
)
)

(defn euler-4 [n]
(last (filter is-palindrome? six-digit-products))
)


Loading