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:

I need to replace the 5 with a 500 to find the solution.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); } }