Possible Duplicate:
Project Euler Problem 12 – C++
I am trying to get the first triangle number with more than 400 divisors (Triangle Number eg: 1,3,6,10). For an example, triangle number 6 has four divisors 1,2,3,6. The following is my attempt to get the triangle number with 400 divisors
import java.math.BigInteger;
public class IQ3
{
static int num1 = 1;
static int devideResult = 0;
public static void main(String[]args)
{
while(true)
{
int triangle = num1*(num1+1)/2;
if(devide(triangle))
{
break;
}
num1++;
}
}
static boolean devide(int num)
{
boolean result = false;
int devideCounter = 2;
for(int i=1;i<=num/2;i++)
{
if(num%i == 0)
{
devideCounter++;
System.out.println("Devide Counter: "+devideCounter);
//System.out.println("i number: "+i);
//System.out.println("input number: "+num);
if(devideCounter>400)
{
System.out.println("Number: "+num);
result = true;
break;
}
}
}
return result;
}
}
But this takes a huge time, and some times it crashes.
However, since the answer could be really big, I thought of using BigInteger.
import java.math.BigInteger;
public class IQ2P2
{
static BigInteger num1 = new BigInteger("1");
static BigInteger two = new BigInteger("2");
static BigInteger one = new BigInteger("1");
static BigInteger i = new BigInteger("1");
static BigInteger zero = new BigInteger("0");
static int devideResult = 0;
// static int devideCounter = 0;
public static void main(String[]args)
{
while(true)
{
BigInteger triangle = num1.multiply(num1.add(one)).divide(two);
if(devide(triangle))
{
break;
}
num1.add(one);
}
}
static boolean devide(BigInteger num)
{
boolean result = false;
int devideCounter = 2;
while((i.compareTo(num))<(num.divide(two).intValue()))
{
if(num.remainder(i) == zero)
{
devideCounter++;
System.out.println("Devide Counter: "+devideCounter);
//System.out.println("i number: "+i);
//System.out.println("input number: "+num);
if(devideCounter>400)
{
System.out.println("Number: "+num);
result = true;
break;
}
}
i.add(one);
}
return result;
}
}
But the biginteger one never returned anything.
Please help me to get first Triangle number with more than 400 divisors.
Note: This is not a homework. I am not a student.
The following is a response to an answer
import java.math.BigInteger;
public class IQ2
{
static long num1 = 1;
static long numberToAdd = 0;
static long devideResult = 0;
static long triangleNum = 1;
static long incrementer = 2;
// static int devideCounter = 0;
public static void main(String[]args)
{
while(true)
{
triangleNum += incrementer++;
if(devide(triangleNum))
{
break;
}
num1++;
}
}
static boolean devide(long num)
{
boolean result = false;
int devideCounter = 2;
for(long i=1;i<=num/2;i++)
{
if(num%i == 0)
{
devideCounter++;
System.out.println("Devide Counter: "+devideCounter);
//System.out.println("i number: "+i);
//System.out.println("input number: "+num);
if(devideCounter>400)
{
System.out.println("Number: "+num);
result = true;
break;
}
}
}
return result;
}
}
you need to optimize the way you find out the amount of divisors for a given number. First, for every
d <= sqrt(n)such thatn%d==0, there ism=n/dsuch thatn%m==0andm >= sqrt(n). That means you can count both of them at once, stopping atsqrt(n).But the real optimization is to calculate the prime factorization of a number instead, and find out the amount of divisors from there.