Skip to content

Instantly share code, notes, and snippets.

@sebm
Last active January 17, 2017 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebm/6c407e2154a7617bc991686f50f420ae to your computer and use it in GitHub Desktop.
Save sebm/6c407e2154a7617bc991686f50f420ae to your computer and use it in GitHub Desktop.
Solutions to Project Euler 001 in multiple languages, with performance stats
#include <iostream>
using namespace std;
int main() {
int running_sum = 0;
for (int i = 2; i < 1000; i++) {
if (i % 3 == 0 || i % 5 == 0) {
running_sum += i;
}
}
cout << running_sum;
return 0;
}
list35sBelow n = [x | x <- [1..n-1],
3 `divides` x || 5 `divides` x]
sum35sBelow n = sum (list35sBelow n)
d `divides` n = n `mod` d == 0
main = do {
print (sum35sBelow 1000)
}
let runningSum = 0;
for (i = 3; i < 1000; i++) {
if (i % 3 === 0 || i % 5 === 0) {
runningSum += i;
}
}
console.log(runningSum);
fn main() {
let mut running_sum = 0;
for x in 2..1000 {
if x % 3 == 0 || x % 5 == 0 {
running_sum += x;
}
}
println!("{}", running_sum);
}
object Euler001 extends App {
var runningSum = 0
for (i <- 1 until 1000) {
if (i % 3 == 0 || i % 5 == 0) {
runningSum += i
}
}
println(runningSum)
}
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.
$ time node javascript/001.js
233168
real 0m0.064s
user 0m0.045s
sys 0m0.016s
$ ghc -O2 -dynamic /Users/smotraghi/japes/project-euler/haskell/001.hs
$ du -h haskell/001
20K haskell/001
$ time ./haskell/001
233168
real 0m0.012s
user 0m0.002s
sys 0m0.006s
$ g++ cpp/001/001.cpp -o cpp/001/001
$ du -h cpp/001/001
12K cpp/001/001
$ time cpp/001/001
233168
real 0m0.005s
user 0m0.001s
sys 0m0.002s
$ rustc rust/001/src/main.rs -o 001.rust.out
$ du -h ./001.rust.out
344K ./001.rust.out
$ time ./001.rust.out
233168
real 0m0.004s
user 0m0.001s
sys 0m0.002s
$ scalac scala/001.scala
$ time scala Euler001 2> /dev/null
233168
real 0m0.614s
user 0m0.668s
sys 0m0.094s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment