Wednesday, July 18, 2012

Project Euler Problem 12

Owh. This time, it is about triangle number. The question gave good enough background information, and little good wikipedia show this formula: n*(n+1)/2. If you already read it, you can see a brute strength way of solving this. But, project euler was made to be solved within 1 minute. We may calculate the number of divisors for each triangle number. In essence, two parts are needed. Function to make triangle number from the formula and calculating the number of divisors. We may calculate it by dividing the triangle number to 1 until itself, while counting everytime it's modulus are zero, which in programming term, going for it divided perfectly without fractional part.

It may become very big number. Thus, calculation can be in an hour....

Shortcut. For each factor/divisor, there is another that paired with it. So, we only needed to calculate until square root of that number. But, instead of counting iteration of 1, it will be iteration of 2.

Although, there are some forum posts and websites said something about square number won't work with this short-cut. I create the function suitable for it. But... I tried without it anyway, and answers appeared few seconds later. D:

Uh, I think maybe the square number function are kinda not accurate anyway. But, what's with the square number condition? Oh well.

No comments:

Post a Comment