Skip to content swizec.com

## Project euler is a fun way to become a better geek By 1772, Leonhard Euler had proved that 2,147,...

Yesterday I was a bit bored at the Theoretical Basis Of Computer Science class and the obvious solution was to try doing a bit of coding on an algorithm I'm trying to develop that aims to learn the proper syllabification for a language by reading it. Because I'm all sorts of cool I want to develop this thing in Clojure.

Naturally, the moment I started my severe lack of proficiency in clojure started showing through and I found myself spending more time online than coding. Somehow I ended up on stackoverflow where some guy suggested to another guy they go through the Project Euler problem set to get better at Clojure.

And so I did just that.

Have only managed to solve the first three problems so far, but by god this crap is fun. Maybe I'm just being way too dorky, but it's #WINNING!

Me being me, I've decided to motivate myself to solving these by posting a solution on this blog every time I solve a problem.

Here are the first three, probably less than elegant, but they're mine and I love them!

`.css-1baulvz{display:inline-block;};If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.;Find the sum of all the multiples of 3 or 5 below 1000.(defn nums [max n]  (loop [cnt (+ n n) acc [n]]    (if (>= cnt max) acc    (recur (+ cnt n) (concat acc [cnt])))))(defn answer [max]  (println (reduce + (set (concat (nums max 3) (nums max 5))))))(answer 1000)`
`;Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:;1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...;By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.(defn term [p-2 p-1]  (+ p-2 p-1))(defn fib [max p-2 p-1 acc]  (if (>= p-1 max) acc      (fib max p-1 (term p-2 p-1) (concat acc [p-1]))))(defn fibonacci [max p-2 p-1]  (fib max p-2 p-1 ))(defn bla [a]  (if (even? a) a 0))(defn answer [max]  (reduce + (map #(if (even? %1) %1 0) (fibonacci max 1 2))))(println (answer 4000000))`
`; The prime factors of 13195 are 5, 7, 13 and 29.; What is the largest prime factor of the number 600851475143 ?(defn any? [l]  (reduce #(or %1 %2) l))(defn prime? [n known]  (loop [cnt (dec (count known)) acc []]    (if (< cnt 0) (not (any? acc))  (recur (dec cnt) (concat acc [(zero? (mod n (nth known cnt)))]))))) (defn next-prime [primes]   (let [n (inc (count primes))]     (let [lk (if (even? (inc (last primes))) (+ 2 (last primes)) (inc (last primes)))]       (loop [cnt lk p primes]  (if (>= (count p) n) (last p)        (recur (+ cnt 2) (if (prime? cnt p) (concat p [cnt]) p)))))))(memoize next-prime)(defn n-primes [n]  (loop [cnt 1 p ]    (if (>= cnt n) p    (recur (inc cnt) (concat p [(next-prime p)])))))(defn factor [n factors primes]  (if (== n 1) factors      (loop [p primes]    (if (== 0 (mod n (last p))) (factor                     (/ n (last p))                     (concat [(last p)] factors)                     p)        (recur (concat p [(next-prime p)]))))))(println (factor 600851475143 [] (n-primes 1)))`

The third one took particularly long to figure out because I was going about it all wrong. What I later realized was that I don't at all have to spend time finding the next prime for the sequence of already known primes because factorization doesn't have to be that complicated. Could just have gone through the number and anything it would divide by would already be a guaranteed prime. ## Did you enjoy this article?

Published on March 8th, 2011 in Clojure, Computer Science, Fibonacci number, Mathematics, Prime factor, Prime number, Project Euler, Technical

## Learned something new? Want to become a high value JavaScript expert?

Here's how it works 👇

Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet 📖right away. After that you'll get thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over my 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

### Start with an interactive cheatsheet 📖

Then get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career.

"Man, love your simple writing! Yours is the only email I open from marketers and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

Join over 10,000 engineers just like you already improving their careers with my letters, workshops, courses, and talks. ✌️

Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

Ready to Stop copy pasting D3 examples and create data visualizations of your own?  Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️

Created bySwizecwith ❤️swizec.com