I always figure one of the best ways to learn a new language is to try out a couple of Project Euler exercises using it. I had solved this one with JavaScript previously, but using CoffeeScript let me use some more Python-style comprehensions to get to the same solution: ", "content" : "

\nI always figure one of the best ways to learn a new language is to try out a couple of Project Euler exercises using it. I had solved this one with JavaScript previously, but using CoffeeScript let me use some more Python-style comprehensions to get to the same solution:

\n\n`:::coffeescript\n# Project Euler #3 -> http://projecteuler.net/index.php?section=problems&id=3\n# Q: What is the largest prime factor of the number 600851475143 ?\n\n# Generate a Sieve of Eratosthenes\n# (an array loaded with prime numbers)\nprimes = []\nsieve = (set, pos) ->\n return false if pos >= set.length\n if set[pos] * set[pos] < set[set.length-1]\n primes = (x for x in set when x == set[pos] or x % set[pos] != 0)\n return sieve(primes,pos+1)\nsieve([2..200000],0)\n\n# Factor a number into its primes\n# we use our Sieve above to quickly check if number is prime\nfactors = []\nfactorial = (primes, target, count) ->\n if primes[count] < target\n if target % primes[count] == 0\n factors.push(primes[count])\n target = target / primes[count]\n else\n count++\n\n if (num for num in primes when num == target).length > 0\n factors.push(target)\n else\n factorial(primes, target, count)\n\nfactorial(primes, 600851475143, 0)\nconsole.log(factors)\n`

\n",
"title" : "Project Euler #3 in CoffeeScript",
"updated" : "2010-12-21T11:43:00+00:00"
}
}
}