Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Janiczek
Created May 31, 2011 21:57
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Janiczek/1001364 to your computer and use it in GitHub Desktop.
Save Janiczek/1001364 to your computer and use it in GitHub Desktop.
First few Project Euler problems solved in CoffeeScript. Comments on how to make it more beautiful (in terms of CoffeeScript, not algorithm) highly appreciated!
@_1 = ->
# sum of multiples of 3 or 5 (not both) under 1K
answer = 0
for n in [1...1000]
if n % 3 == 0 or n % 5 == 0
answer += n
answer
@_2 = ->
# sum of even fibonacci numbers up to 4M
answer = 0
[f,s,t] = [0,1,1]
while t <= 4000000
[f,s,t] = [s,t,s+t]
if t % 2 == 0
answer += t
answer
@_3 = ->
# highest factor of a large number
factor(600851475143).max()
@_4 = ->
# highest palindromic number that is product of two 3-digit numbers
max = 0
isP = (n) ->
n.toString() == n.toString().reverse()
for x in [100..999]
for y in [100..999]
tmp = x*y
if isP(tmp) and max < tmp
max = tmp
max
@_5 = ->
# lowest number divisible by numbers 1..20
answer = 1
for prime in primes_upto 20
tmp = prime
while tmp < 20
answer *= prime
tmp *= prime
answer
@_6 = ->
# difference between squared sum of 1..100 and sum of squares of 1..100
sum = (x for x in [1..100])
sqs = (x*x for x in sum)
sqsum = Math.pow sum.sum(), 2
sumsqs = sqs.sum()
answer = sqsum - sumsqs
@_7 = ->
# 10001th prime
prime 10001
@_8 = ->
# highest product of 5 consecutive digits of 1000-digit number
max = 0
num = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
for i in [0..num.length-5]
tmp = 1
for char in num.substr i,5
tmp *= parseInt char
if tmp > max
max = tmp
max
@_9 = ->
# pythagorean triple such that a+b+c = 1K
for a in [1...1000]
aa = a*a
for b in [a...1000-a]
bb = b*b
c = 1000-a-b
if aa + bb == c*c
return a*b*c
@_10 = ->
# sum of primes up to 2M
primes_upto(2000000).sum()
# ==============
# helper methods
# ==============
String::reverse = ->
# "abc" -> "cba"
this.split('').reverse().join('')
Array::max = ->
# [1,3,2] -> 3
Math.max.apply Math, this
Array::min = ->
# [1,3,2] -> 1
Math.min.apply Math, this
Array::sum = ->
# [1,3,5] -> 9
this.reduce (x,y) -> x+y
@isPrime = (n) ->
# is n prime?
if n == 1
return false
if n < 4
return true
if n % 2 == 0
return false
if n < 9
return true
if n % 3 == 0
return false
r = Math.floor Math.sqrt n
f = 5
while f <= r
if n % f == 0
return false
if n % (f+2) == 0
return false
f += 6
return true
@primes_upto = (n) ->
# all primes up to n
(x for x in [1..n] when isPrime x)
@primes = (n) ->
# n primes
rank = 0
i = 1
ps = []
while rank < n
if isPrime ++i
ps.push i
rank++
ps
@prime = (n) ->
# nth prime
rank = 1
i = 2
while rank < n
if isPrime ++i
rank++
i
@factor = (n) ->
# 60 -> [2,2,3,5]
facs = []
divisor = 2
while n > 1
while n % divisor == 0
facs.push divisor
n /= divisor
divisor++
facs
@DarrenN
Copy link

DarrenN commented Jun 1, 2011

I was doing something similar a while back - https://gist.github.com/750315

@ricardobeat
Copy link

Related: I've taken the time to solve a few problems on rosettacode.org, but there are still plenty left. Javascript is lacking too.

@Janiczek
Copy link
Author

Janiczek commented Jun 1, 2011

@DarrenN: Thanks for the link, interesting implementation of the sieve! This is the first time I see it done recursively... :)
@ricardobeat: Wow! Didn't know about that site. Looks AMAZING. Thanks!

@viperfx
Copy link

viperfx commented Jun 5, 2011

Nice Solutions, however is the factor() function gone ? I cannot seem to make it work.

@Janiczek
Copy link
Author

Janiczek commented Jun 5, 2011

it's the one at the bottom of the code, I didn't change it ... maybe there are scope issues? I prefixed the functions with @, so I can call them from Chrome dev console (you know, other than window.someFunction() ) ...

does the _3() work for you? it uses the factor() and should find it without problems ...

@viperfx
Copy link

viperfx commented Jun 5, 2011

oh im such an idiot :) Yes that looks much better, what is the purpose of the /= operator?

@Janiczek
Copy link
Author

Janiczek commented Jun 5, 2011

it's okay, happens to me too :)

x /= y gets translated to: x = x / y, similar to += etc.
edit: you probably asked because of the weird syntax highlighting here, didn't you? :)

what's I especially like are these:
a? # is 'a' defined?
a ?= b # if 'a' isn't defined, a = b
a or= b # a or (a = b), uses lazy evaluation - the (a=b) block doesn't get executed if a is true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment