Last active
August 29, 2015 14:08
-
-
Save jirkapenzes/c026baaa0d9c807462ef to your computer and use it in GitHub Desktop.
Project Euler problem 01 - Java vs Clojure (https://projecteuler.net/problem=1)
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
(defn mod35 [number] | |
(or | |
(zero? (mod number 3)) | |
(zero? (mod number 5)))) | |
(reduce + (filter mod35 (range 1 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
(reduce + ( | |
for [number (range 1 1000) | |
:when (or | |
(zero? (mod number 3)) | |
(zero? (mod number 5))) | |
] number)) |
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
import java.util.*; | |
class ProjectEuler_01 | |
{ | |
public static void main (String[] args) throws java.lang.Exception | |
{ | |
int sum = 0; | |
for (int number = 0; number < 1000; number++) { | |
if (number % 3 == 0 || number % 5 == 0) { | |
sum += number; | |
} | |
} | |
System.out.println(sum); | |
} | |
} |
for je hrozně neidiomatický. Použij filter nebo map:
(defn mod-3-and-5 [x]
(or
(zero? (mod x 3))
(zero? (mod x 5))))
(reduce + (filter mod-3-and-5 (range 1 1000)))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Transduce ;)