Create a gist now

Instantly share code, notes, and snippets.

@abitdodgy /javascript.js Secret
Last active Feb 9, 2016

What would you like to do?
Project Euler #6 solution code samples using Swift, Ruby, and JavaScript. Find full article here: http://mohamad.im
//
// Using MapReduce
//
function arrayOfNumbers(n, square) {
return Array.apply(null, Array(n)).map(function (e, i) {
i += 1;
return i * (square ? i : 1);
});
}
function difference(n) {
var sumOfSquares = arrayOfNumbers(n, true).reduce(function (a, b) {
return a + b;
});
var squareOfSum = arrayOfNumbers(n).reduce(function (a, b) {
return a + b;
});
squareOfSum *= squareOfSum;
return squareOfSum - sumOfSquares;
}
var start = performance.now();
console.log(difference(125000));
var finish = performance.now();
console.log("MapReduce 125,000 finished in " + (finish - start) / 1000)
//
// Using while Loop
//
function diff(n) {
var sumOfSquares = squareOfSum = 0;
while(n) {
sumOfSquares += n * n, squareOfSum += n, n--;
}
squareOfSum *= squareOfSum;
return squareOfSum - sumOfSquares;
}
var start = performance.now();
console.log(diff(1000000000));
var finish = performance.now();
console.log("Loop 1,000,000,000 finished in " + (finish - start) / 1000)
require 'benchmark'
def map_reduce(n)
(1..n).reduce(:+)**2 - (1..n).map { |n| n**2 }.reduce(:+)
end
def enum(n)
sumOfSquares = squareOfSum = 0
n.downto 1 do |i|
squareOfSum += i; sumOfSquares += i * i
end
(squareOfSum *= squareOfSum) - sumOfSquares
end
def while_loop(n)
sumOfSquares = squareOfSum = 0
while n > 0
squareOfSum += n; sumOfSquares += n * n; n -= 1
end
(squareOfSum *= squareOfSum) - sumOfSquares
end
def induction(n)
(n * (n + 1) / 2)**2 - (n * (n + 1)) * ((n * 2) + 1) / 6
end
iterations = 10_000_000
puts "Benchmarking with #{iterations} iterations"
Benchmark.bm do |bm|
bm.report "MapReduce" do
map_reduce(iterations)
end
bm.report "Enum " do
enum(iterations)
end
bm.report "Loop " do
while_loop(iterations)
end
bm.report "Induction" do
induction(1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)
end
end
# Benchmarking with 10000000 iterations
# user system total real
# MapReduce 2.890000 0.030000 2.920000 ( 2.928501)
# Enum 1.610000 0.010000 1.620000 ( 1.611081)
# Loop 1.200000 0.000000 1.200000 ( 1.201676)
# Induction 0.000000 0.000000 0.000000 ( 0.000010)
//
// Functional approach
//
func diff(n: Int) -> Int {
let array = [Int](1...n)
let sumOfSquares = array.map({ $0 * $0 }).reduce(0, +)
let sum = array.reduce(0, +)
let squareOfSum = sum * sum
return squareOfSum - sumOfSquares
}
//
// Using a loop
//
func loopDiff(var i: Int) -> Int {
var sumOfSquares = 0; var squareOfSum = 0
while i > 0 {
sumOfSquares += i * i; squareOfSum += i; i--
}
squareOfSum *= squareOfSum
return squareOfSum - sumOfSquares
}
let startTime = CFAbsoluteTimeGetCurrent()
let result = loopDiff(10000)
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
println("Time elapsed : \(timeElapsed) s")
println(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment