Home > euler_node

euler_node

Euler_node is a project mainly written in JavaScript, it's free.

my attempts to solve the problems from project euler (projecteuler.net) in node.js

This project is intended to document my efforts to learn Node.js, and to maintain my numerical math skills.

Problem 1:

  • Tempted to brute force it (either look at each number less that 1000 and check whether it was divisible), but decided to get a bit fancier.
  • Essentially this problem is looking for the sum of all numbers less than 1000 divisible by 3 plus the sum of all numbers less than 1000 divisible by 5 minus the sum of all numbers less than 1000 divisible by 15.
  • OR ... 3SUM(1..FLOOR(1000/3)) + 5SUM(1..FLOOR(1000/5)) - 15*SUM(1..FLOOR(1000/15))
  • SUM(1..N) inclusive = N * ( N + 1 ) / 2... and we're done.

Problem 2:

  • A quick browse of wikipedia gives the closed form solution for the sequence: F(n) = ( phi ^ n - (-phi) ^ (-n) ) / sqrt(5)... phi = 1/2 + SQRT(5)/2
  • It turns out ever third element is even, so I only need to sum every third element
  • Can't see a clever way to solve this, so I'll just run a loop... feels like there should be a better way though

Problem 3:

  • Can't think of anything fancy here... I'll hack something together

Problem 4:

  • Once again, can't think of anything fancy... I'll at least pass around a function...

Problem 5:

  • This one seems easier to solve by hand than with a computer...
  • I'll solve for 1..n where n is arbitrary
  • Start with the largest number, work downwards.
  • Factorize each, and remove the factors from the list, until you get to the smallest element in the list.
  • Multiply remaining numbers together, and you're done
  • Ouch! This approach is wrong... lets think some more.
  • Need to take the prime factors of each number. For each prime factor, need as many of them as the largest requirement
  • Multiply them all together (some prime factors more than once)
  • After another incorrect submission (!), and a javascript bug is hunted down, this technique seems to work

Problem 6:

  • This one seems too easy. I wonder if there is some elegant solution

Problem 7:

  • also pretty simple

Problem 8:

  • no problem

Problem 9:

  • this one looks like fun

Problem 10:

  • need an efficient way to get a sequence of primes... 2000000 is largish
  • keep a list of all the primes i know of so far, test anything new against those
  • wrong answer !?! what have I done wrong?
  • whoops, index off by 1

Problem 11:

  • Brute force. Dull

Problem 12:

  • I like this one. Brute force is NOT going to cut it.
  • I can't do a logarithmic search because numDivisors is NOT monotonic in the triangle numbers sequence
  • Means I need to do a linear search, which means I need to be VERY efficient (500 divisors will be a large number)
  • I already have a reasonably efficient prime factorization function, need to make a function that takes a list of prime factors gives number of divisors

Problem 13:

  • Another brute force problem