The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... Let us list the factors of the first seven triangle numbers: 1: 1 3: 1,3 6: 1,2,3,6 10: 1,2,5,10 15: 1,3,5,15 21: 1,3,7,21 28: 1,2,4,7,14,28 We can see that 28 is the first triangle number to have over five divisors. What is the value of the first triangle number to have over five hundred divisors?
// find the nth triangular number and return it var f = function(n) { var g; g = n * (n + 1) / 2; return g; }; // find a prime number that is >= m and return it var prime = function(m) { for (var j = 2; j < m; j++) { if (m % j === 0) { j = 1; m++; } } return m; }; var user = prompt("How many multiples should the triangular number have? At least ..."); var n = 2; var puppy = true; while(puppy) { var triangular = f(n); if (triangular >= 1000000000) { console.log("Pyramidal number is too big."); puppy = false; } //if var powers = []; var product = 1; for (var i = 2; i < triangular + 1; i++) { var p = prime(i); if (triangular % p === 0) { powers.push(1); triangular /= p; while (triangular % p === 0) { powers[(powers.length - 1)]++; triangular /= p; } } //if else { powers.push(0); } //else if (p > i) { i = p; } } //for for (var l = 0; l < powers.length; l++) { if (powers[l] > 0) { product *= (powers[l] + 1); } //if } //for if (product >= user) { console.log("n is " + n); console.log("Triangular number is " + f(n)); console.log("Number of factors is " + product); console.log(powers); puppy = false; } //if else { n++; } // else } //while
he first triangular number with over 500 factors is: 76,576,500
The*
I wish I could give myself a meddle.
anyone who is interested in this problem can find it online here at: http://projecteuler.net/problem=12
that code is for javascript and yes i did write it myself "oink oink"
figures out the same using perl :- ``` my @triangle_numbers = (); foreach my $number (1 .. 99999) { my $tn = $number*($number+1)/2; push(@triangle_numbers, $tn); } foreach my $number (@triangle_numbers) { @divisors = grep { $number % $_ == 0 } 1 .. sqrt($number); push @divisors, map {$number == $_*$_ ? () : $number/$_} reverse @divisors; print "$number $#divisors\n" if $#divisors > 498; } ```
too cool ganeshie8!
ty :) your code looks great ! mine took < 6 seconds.... thinking how to optimize it further...
i have not learned to code in pearl yet. there are some clever math tricks to speed up the program.
yea most of the time is taken while grabbing all factors of a number
this guy can do it in under 3 seconds: http://code.jasonbhill.com/sage/project-euler-problem-12/
or, rather, his program can do it in under 3 seconds by remembering the previous iteration's factor
I wish i knew coding.
hez using the factors of (n+1) of previous number for n in current number... thats a brilliant idea to reduce run time !!!
thanks for sharing @cebroski :)
thank you for replying to the post @ganeshie8
Join our real-time social learning platform and learn together with your friends!