Created
November 16, 2011 17:06
-
-
Save jakedobkin/1370671 to your computer and use it in GitHub Desktop.
Euler 11
This file contains hidden or 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
| #!/usr/local/bin/node | |
| // according to wikipedia, formula for triangle numbers is Tn=n(n+1)/2 | |
| // so we can just generate triangle numbers with a while loop, and then check for factors with a for loop | |
| // everytime it finds a factor, add 1 to the limit, end for loop when factors > 500 | |
| biggesti = 0; | |
| biggesttriangle = 0; | |
| biggestfactors = 0; | |
| for (i=10000;i<=15000;i++) | |
| { | |
| triangle = (i * (i+1))/2; | |
| factors = 0; | |
| if (triangle%2==0 && triangle%5==0 && triangle%10 == 0) | |
| { | |
| for(j=1;j<=triangle;j++) | |
| { | |
| if (triangle%j==0) | |
| { | |
| factors+=1; | |
| } | |
| } | |
| } | |
| if (factors > biggestfactors) | |
| { | |
| biggestfactors=factors; | |
| biggesti = i; | |
| biggesttriangle = triangle; | |
| console.log("our current champion is triangle number " + biggesti + " with value " + biggesttriangle + " which has " + biggestfactors + " factors"); | |
| } | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I hate a few problems here. The first is that my while statement borked- or maybe it didn't- it might have just been running really slow, because this takes forever, and I wasn't sure if it was working at all. So I switched to a for statement so I could control which triangle range we searched. Second, I looked at the triangles which were the high scores in each range, and it seemed like they were all divisible by 0, 5, and 10, so I took a chance and limited the search to that- it helped a little, but not that much. According to http://projecteuler.net/project/resources/012_a86c72e34d4ddac1da5871011962ecba/012_overview.pdf - you could basically count all factors up to the square root of the number you're looking at- and then double it, since factors are symmetrical- the problem is you then have to correct for perfect squares. You could also sieve primes, and then follow a slightly complex formula for deriving the number of divisors from the number of primes.