6 Replies - 224 Views - Last Post: 05 October 2013 - 07:36 PM Rate Topic: -----

#1 luigi10011  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-March 13

Question on how to make this run faster

Posted 05 October 2013 - 11:26 AM

the program i am writing, user input a number in which each variable can be in the formula "0<A<=B<=C<=D<=E<=F" and to display the solutions. The current issue as of now is that the program takes too long due to the method I am doing. I would like to make this run faster as well as maybe even lighten the amount of code I have.

     static int num;
	static ArrayList<String> LArrays =  new ArrayList<String>();
	static ArrayList<String> RArrays = new ArrayList<String>();
	static ArrayList<String> Solutions = new ArrayList<String>();

	public Solve(int chosenNumber){
		num = chosenNumber;
	}

	public void createArray(){
		long a, b, c, d, e, f;
		long[] L = new long[4];
		long[] R = new long[4];
		for(a = 1; a <= num; a++){
			for(b = 1; b <=num; b++){
				for(c = 1; c <= num; c++){
					if( a <= b && b <= c){
						L[0] = (long) Math.pow(a, 5) + (long) Math.pow(b, 5) +(long) Math.pow(c, 5);
						L[1] = a;
						L[2] = b;
						L[3] = c;
						LArrays.add(Arrays.toString(L));
					}
				}
			}
		}

		for(d = 1; d <= num; d++){
			for(e = 1; e <=num; e++){
				for(f = 1; f <= num; f++){
					if( d <= e && e <= f){
						R[0] = (long) Math.pow(f, 5) - ( (long) Math.pow(d, 5) + (long) Math.pow(e, 5) );;
						R[1] = d;
						R[2] = e;
						R[3] = f;
						RArrays.add(Arrays.toString(R));
					}
				}
			}
		}
		LRCompare();
	}

	public void dispSolutions(){
		if(Solutions.isEmpty()){
			System.out.println("no solution");
		}else{
			for(int i = 0; i < Solutions.size(); i++){
				System.out.println(Solutions.get(i));
			}
		}
	}

	public void LRCompare(){
		int LCount = 0;
		int RCount = 0;
		int count = 0;

		while(count < LArrays.toArray().length){
			long L = getAnswer(LArrays.get(LCount));
			long R = getAnswer(RArrays.get(RCount));
			if( L == R){
				String LEquation = getEquation(LArrays.get(LCount));
				String REquation = getEquation(RArrays.get(RCount));
				String ans = LEquation+" "+REquation;
				if(Solutions.contains(ans) == false){
					Solutions.add(ans);
				};
				LCount+=1;
				RCount=0;
				count+=1;
			}else if(RCount == RArrays.toArray().length-1){
				LCount+=1;
				RCount=0;
				count+=1;
			}else if(LCount == LArrays.toArray().length-1){
				break;
			}else{
				RCount+=1;
			}
		}
		dispSolutions();
	}

	public long getAnswer(String s){
		s = s.replace("[", "");
		s = s.replace("]", "");
		String[] numbers = s.split("\\s*,\\s*");
		return Long.parseLong(numbers[0]);
	}

	public String getEquation(String s){
		s = s.replace("[", "");
		s = s.replace("]", "");
		String[] numbers = s.split("\\s*,\\s*");
		String ans = numbers[1]+" "+numbers[2]+" "+numbers[3];
		return ans;
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Question on how to make this run faster

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,109
  • Joined: 19-March 11

Re: Question on how to make this run faster

Posted 05 October 2013 - 12:16 PM

If you want to learn how sorts work, that's usually one of the first topics covered in any basic algorithms course. There are about half a dozen standard sorts to learn, and you can look them up: bubble sort, insertion sort, selection sort, shell sort, merge sort, and quick sort, with radix sort as a sort of curiosity on the side.

If you just want to get these things sorted, use Arrays.sort()
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Question on how to make this run faster

Posted 05 October 2013 - 05:59 PM

View Postluigi10011, on 05 October 2013 - 02:26 PM, said:

The current issue as of now is that the program takes too long due to the method I am doing.

what do you mean ? It takes 2 nanoseconds and you want it to run in 1 ?
Was This Post Helpful? 0
  • +
  • -

#4 luigi10011  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-March 13

Re: Question on how to make this run faster

Posted 05 October 2013 - 06:59 PM

View Postpbl, on 05 October 2013 - 05:59 PM, said:

View Postluigi10011, on 05 October 2013 - 02:26 PM, said:

The current issue as of now is that the program takes too long due to the method I am doing.

what do you mean ? It takes 2 nanoseconds and you want it to run in 1 ?

If you provide a value for num such as 10, it is fast (yes), but I want to increase performance for the situations of 100, 200, 300, etc.( huge numbers)
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,109
  • Joined: 19-March 11

Re: Question on how to make this run faster

Posted 05 October 2013 - 07:11 PM

Okay, so even a bubble sort will get you into thousands of numbers before it starts crapping out on you - start there. There are plenty of tutorials out there which will show you those basic sorts, and how to understand them.
Was This Post Helpful? 0
  • +
  • -

#6 luigi10011  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-March 13

Re: Question on how to make this run faster

Posted 05 October 2013 - 07:34 PM

View Postjon.kiparsky, on 05 October 2013 - 07:11 PM, said:

Okay, so even a bubble sort will get you into thousands of numbers before it starts crapping out on you - start there. There are plenty of tutorials out there which will show you those basic sorts, and how to understand them.

Yes, thank you for the multiple sorting options! I have been looking into the sorting options and bubble sort seems the most appropriate to me. Thank you very much!
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,109
  • Joined: 19-March 11

Re: Question on how to make this run faster

Posted 05 October 2013 - 07:36 PM

Have fun!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1