Skip to content

Commit

Permalink
re-write using Underscore.js
Browse files Browse the repository at this point in the history
  • Loading branch information
bitsai committed Aug 31, 2011
1 parent e848e99 commit 46dd1d9
Show file tree
Hide file tree
Showing 7 changed files with 893 additions and 51 deletions.
39 changes: 39 additions & 0 deletions coffeescript/euler.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
_ = require './underscore.js'

exports.factors = (x) ->
output = []
root = Math.sqrt x
for y in [0...root]
if x % y == 0
output.push y
output.push x / y
if x % root == 0 then output.push root
output

exports.fibs = (max) ->
if max == 0 then return [0]
output = [0, 1]
while (x = output[output.length - 1] + output[output.length - 2]) <= max
output.push x
output

exports.gcd = (x, y) ->
if y == 0 then x
else exports.gcd y, x % y

exports.isPalindrome = (x) ->
s = x.toString()
for i in [0...s.length / 2]
if s[i] != s[s.length - 1 - i]
return false
true

exports.isPrime = (x) ->
(exports.factors x).length == 2

exports.lcm = (x, y) ->
(Math.abs (x * y)) / (exports.gcd x, y)

exports.sum = (xs) ->
add = (x, y) -> x + y
_.reduce xs, add, 0
8 changes: 3 additions & 5 deletions coffeescript/p1.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
sum = (xs...) ->
output = 0
output += x for x in xs
output
euler = require './euler.coffee'
_ = require './underscore.js'

alert sum (x for x in [0...1000] when x % 3 == 0 or x % 5 == 0)...
console.log euler.sum _.select [0...1000], (x) -> x % 3 == 0 or x % 5 == 0
16 changes: 3 additions & 13 deletions coffeescript/p2.coffee
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
sum = (xs...) ->
output = 0
output += x for x in xs
output
euler = require './euler.coffee'
_ = require './underscore.js'

fibs = (max) ->
if max < 0 then return []
if max == 0 then return [0]
output = [0, 1]
while (n = output[output.length - 2] + output[output.length - 1]) <= max
output.push n
output

alert sum (x for x in fibs 4000000 when x % 2 == 0)...
console.log euler.sum _.select (euler.fibs 4000000), (x) -> x % 2 == 0
16 changes: 3 additions & 13 deletions coffeescript/p3.coffee
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
factors = (x) ->
output = []
root = Math.sqrt x
for y in [0...root]
if x % y == 0
output.push y
output.push x / y
if x % root == 0 then output.push root
output
euler = require './euler.coffee'
_ = require './underscore.js'

isPrime = (x) ->
2 == (factors x).length

alert Math.max (x for x in factors 600851475143 when isPrime x)...
console.log _.max _.select (euler.factors 600851475143), euler.isPrime
10 changes: 3 additions & 7 deletions coffeescript/p4.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
isPalindrome = (x) ->
s = x.toString()
for i in [0...s.length / 2]
if s[i] != s[s.length - 1 - i]
return false
return true
euler = require './euler.coffee'
_ = require './underscore.js'

products = () ->
output = []
Expand All @@ -12,4 +8,4 @@ products = () ->
output.push (x * y)
output

alert Math.max (x for x in products() when isPalindrome x)...
console.log _.max _.select products(), euler.isPalindrome
16 changes: 3 additions & 13 deletions coffeescript/p5.coffee
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
gcd = (x, y) ->
if y == 0 then x
else gcd y, x % y
euler = require './euler.coffee'
_ = require './underscore.js'

lcm = (x, y) ->
(Math.abs (x * y)) / (gcd x, y)

lcmAll = (x, y, zs...) ->
output = lcm x, y
for z in zs
output = lcm output, z
output

alert lcmAll [1...21]...
console.log _.reduce [1...21], euler.lcm, 1
Loading

0 comments on commit 46dd1d9

Please sign in to comment.