FrankKair / polyglot-euler

šŸ“œ Project Euler solutions in various programming languages
MIT License
74 stars 14 forks source link

Clojure 009 #79

Closed yvern closed 6 years ago

yvern commented 6 years ago

Hello! Thank you for your contribution to polyglot-euler.

How the solution works

Clojure doesn't have its own numeric tower, so we have to define square and sqrt (the later returns a float)

sum-1000? is a predicate that checks if a group of 3 numbers sums 1000 (using == instead of = ignores "type"; (= 1 1.0) is false, but (== 1 1.0) is true)

candidates is a lazy sequence of 3-vectors of numbers, built from ranges. 5 < b <= 700 ; heuristics alert: 700 is a reasonable number to start our search, and we know b can't be smaller than say, 5. in the code its reverses so we start looking from 700. 4 <= a < b ; and c is defined as āˆš(a^2 + b^2)

then we keep only the vectors that pass our sum-1000? test, turn them to ints (to keep sure we don't print anything in "scientific notation" and get their product.

Performance

Try it online!

Real time: 3.021 s
User time: 5.468 s
Sys. time: 0.206 s
CPU share: 187.78 %
FrankKair commented 6 years ago

Yeah, I think that separating the solve function into smaller functions that are called inside solve is more readable.

yvern commented 6 years ago

to be fair, for no actual reason. I will fix that