# Complexity problem

• July 16th, 2013, 12:38 PM
keepStriving
Complexity problem
I'm trying solve a problem which I have been able to code on a smaller scale though as soon as I start imputing larger values the time it takes increases considerably to the point where the final solution may take me a lifetime to find out.

The problem is Project Euler | Problem 12, I don't want the solution rather just guidance on a considerably more efficient collection type which may aid me.

The following is my own code:
Code :

```public class abc{ public static void main(String[] args){ for(int j=1;j<1000;j++){ List<Integer>ar = new ArrayList<Integer>(); for(int i=1;i<triangle(j)+1;i++){ if(triangle(j)%i==0){ ar.add(i); } }   if(ar.size()>5){ System.out.println(triangle(j)); break; } } }     public static int triangle(int num){ if(num==1){ return 1; } return num +triangle(num-1); } }```
I need to replace the 5 with a 500 to find the solution.
• July 16th, 2013, 05:23 PM
GregBrannon
Re: Complexity problem
Do only what is necessary to answer the question. Simplify your program to something like:

1. Find the next triangle number
2. Find the number of factors in the triangle number
3 Report the first triangle number to have over 500 factors and stop, OR
4. Goto 1

My solution is simpler than yours and could be simplified some, but finding the answer takes a looooong time.