Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Complexity problem

  1. #1
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default 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:
    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.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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.

Similar Threads

  1. complexity of code??
    By navxxxx in forum Java Theory & Questions
    Replies: 8
    Last Post: April 18th, 2013, 01:35 PM
  2. complexity of code??
    By navxxxx in forum Algorithms & Recursion
    Replies: 0
    Last Post: April 18th, 2013, 12:57 PM
  3. time complexity qustion
    By romavolman in forum Java Theory & Questions
    Replies: 1
    Last Post: September 18th, 2012, 07:45 PM
  4. [SOLVED] time complexity qustion
    By romavolman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 18th, 2012, 06:26 PM
  5. time complexity qustion
    By romavolman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 18th, 2012, 06:24 PM