Hi
this is my solution for the euler 12 problem on projecteuler.net:
What is the value of the first triangle number to have over five hundred divisors?
this is my solution, but it's wrong:
#include <iostream>
using namespace std;
int main() {
unsigned int i=1, y=0, ii=1, counter=0;
for (i=1 ; i<100000; ++i) {
y+=i; // y= triangle number
for(ii=1 ; ii<100000; ++ii) {
if(y%ii == 0) // check if y can be divided by ii
++counter; // if yes, increase counter
}
I get "236215980" as a solution.
I experimented a bit with the counter varibale. If it set "if(counter>420)" instead of 500, I get the right solution ("76576500")
But I don't understand why.
First of all, good that you're doing Project Euler. Do enough of them, and it will really make you a better programmer. Secondly, use the [code] tags. Third, in your second for loop, you don't need to have ii go up that high. It can be ii<y instead. In fact, if you were clever, it could be even lower. Fourth, I have no clue why it would do that with 420. It's gotta be lucky or something.
Besides, you're brute forcing the program. The point of PE is to find the "minute solution".