Created
December 19, 2010 19:21
-
-
Save DarrenN/747609 to your computer and use it in GitHub Desktop.
Uses CoffeeScript and Underscore.js to solve Project Euler #3 http://projecteuler.net/index.php?section=problems&id=3
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
# Sieve of Eratosthenes | |
# (an array loaded with prime numbers) | |
base = [2..10000] | |
primes = [] | |
sieve = (set, pos) -> | |
return false if pos >= set.length | |
if set[pos] * set[pos] < set[set.length-1] | |
primes = _.select set, ((num) -> | |
return num if num == set[pos] | |
return num % set[pos] != 0 | |
) | |
return sieve(primes,pos+1) | |
sieve(base,0) | |
# Factor a number into its primes using out Sieve from above | |
factors = [] | |
factorial = (primes, target, count) -> | |
if primes[count] < target | |
if target % primes[count] == 0 | |
factors.push(primes[count]) | |
target = target / primes[count] | |
else | |
count++ | |
if _.include primes, target | |
factors.push(target) | |
else | |
factorial(primes, target, count) | |
factorial(primes, 600851475143, 0) | |
console.log(factors) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment