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 11 of 11

Thread: Arrays, BigInteger's and a bunch of for-loops. Welcome to a huge mess! [Do not view if you do not want any hints on Project Euler]

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Arrays, BigInteger's and a bunch of for-loops. Welcome to a huge mess! [Do not view if you do not want any hints on Project Euler]

    Alright, I am currently working on Euler Problem 29. The problem may be found in its full length here:

    Problem 29 - Project Euler

    This is the code I was confident would solve the problem:

    import java.math.*;
    import java.util.Arrays;
    public class EulerProblem29 {
    	public static void main(String[] args) {
    		BigInteger[] tabell = new BigInteger[9801]; //9801 is the amount of numbers that can exist
    		BigInteger hundre = new BigInteger("100");
    		int i = 0;
     
    		//I fill the array with the numbers.
    		for (BigInteger a = BigInteger.valueOf(2);a.compareTo(hundre) <= 0;a = a.add(BigInteger.ONE)){
    			for (int b = 2;b<=100;b++){
    				tabell[i] = a.pow(b);
    				i++;
    			}
    		}
    		//I print out the first to check if it is correct.
    		System.out.println(tabell[0]);
    		//I sort the array.
    		Arrays.sort(tabell);
     
    		for (int j = 0;j<9801;j++){
    			for (int y = 0;y<=100;y++){
    				if (j + y < 9801){ //To avoid moving out of bounds.
    				if (tabell[j].equals(tabell[j+y])){ //If the numbers are equal
    					tabell[j+y].equals(BigInteger.ZERO); //I give one of them the value 0.
    				}
     
    			}
    		}
    	}
     
    		int antall = 0;
    		BigInteger zero = new BigInteger("0");
     
    		for (int z = 0;z<9801;z++){
    			if (!tabell[z].equals(zero));{ //If the number is not 0
    				antall++; //it should count
    			}
    		}
     
    		System.out.println(antall); //9801, for some reason.
     
    	}
     
    }

    Now, what baffles me is the fact that the number printed at the end is 9801, the same number of elements in the array. What should be printed, according to my logic, is the number of elements minus the number of elements that are 0.

    Please do not make any remarks regarding my code being inefficient or slow. The only thing I want to know is why the wrong number is printed, not how slow it is printed.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Arrays, BigInteger's and a bunch of for-loops. Welcome to a huge mess! [Do not view if you do not want any hints on Project Euler]

    Have you done any tests with a smaller number of elements in the array (say 10) and done some debugging by adding lots of println statements to show you the values of the array as the code executes?

    How many elements are 0?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Arrays, BigInteger's and a bunch of for-loops. Welcome to a huge mess! [Do not view if you do not want any hints on Project Euler]

    Quote Originally Posted by Norm View Post
    How many elements are 0?
    Excellent question, I will check that...

    when running:

    for (int z = 0;z<9801;z++){
    			if (tabell[z].equals("0"));
    			teller++;
    		}
     
    		System.out.println(teller);

    it gives me 9801, thus the program claims, given that there is no error in my logic, that all elements both are and are not equal to 0. On one hand, I have recreated Schrödingers experiment, on the other, I have no idea what's going on. I have checked my array several times and confirmed that all values are not equal to 0.

  4. #4
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Arrays, BigInteger's and a bunch of for-loops. Welcome to a huge mess! [Do not view if you do not want any hints on Project Euler]

    There are two issues with your program. One is a simple dumb mistake. The other is a logic problem. I'll give you the dumb mistake and let you mull over the logic problem before i give it to you.

    On your line "//I give one of them the value 0." You're not giving it a value of 0. The line you have is doing a boolean check. .equals doesnt set anything. You obviously know how to set array values, you did it on this line "tabell[i] = a.pow(b);". Fix that and you'll get some progress. Your next error is that you're going to be setting every value to zero. See if you can figure out why!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Arrays, BigInteger's and a bunch of for-loops. Welcome to a huge mess! [Do not view if you do not want any hints on Project Euler]

    There is a problem in the code that the compiler will tell you about if you use the javac program's -Xlint option.
    Here is the command line I use to compile with:
    D:\Java\jdk1.7.0.7\bin\javac.exe -cp . -Xlint EulerProblem29.java
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Arrays, BigInteger's and a bunch of for-loops. Welcome to a huge mess! [Do not view if you do not want any hints on Project Euler]

    Quote Originally Posted by Chris.Brown.SPE View Post
    There are two issues with your program. One is a simple dumb mistake. The other is a logic problem. I'll give you the dumb mistake and let you mull over the logic problem before i give it to you.

    On your line "//I give one of them the value 0." You're not giving it a value of 0. The line you have is doing a boolean check. .equals doesnt set anything. You obviously know how to set array values, you did it on this line "tabell[i] = a.pow(b);". Fix that and you'll get some progress. Your next error is that you're going to be setting every value to zero. See if you can figure out why!
    Thanks for the help, I got the big mistake fixed. I still don't get why all of the numbers are set to 0, though. Wouldn't the actions done by this code:

    for (int j = 0;j<9801;j++){
    			for (int y = 0;y<=100;y++){
    				if (j + y < 9801){ //To avoid moving out of bounds.
    				if (tabell[j].equals(tabell[j+y])){ //If the numbers are equal
    					tabell[j+y] = BigInteger.ZERO; //I give one of them the value 0.
    				}
     
    			}
    		}
    	}

    be:

    1. The first if-statement ensure that we do not move out of bounds.
    2. The second if-statement checks if tabell[j] is equal to tabell[j+y], if it is:
    3. tabell[j+y], and, as far as I can see, ONLY tabell[j+y], will be set to 0.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Arrays, BigInteger's and a bunch of for-loops. Welcome to a huge mess! [Do not view if you do not want any hints on Project Euler]

    Add a println to display when an element is set to 0.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Arrays, BigInteger's and a bunch of for-loops. Welcome to a huge mess! [Do not view if you do not want any hints on Project Euler]

    Quote Originally Posted by Norm View Post
    Add a println to display when an element is set to 0.
    Only 0's when printing tabell[j]

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Arrays, BigInteger's and a bunch of for-loops. Welcome to a huge mess! [Do not view if you do not want any hints on Project Euler]

    How many times did the message print? That would tell you if there were any elements with a value of 0.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Arrays, BigInteger's and a bunch of for-loops. Welcome to a huge mess! [Do not view if you do not want any hints on Project Euler]

    I would recommend using an ide like eclipse that lets you debug and step through the program. Keep an eye on your counters and what they are when you get to your comparisons. That should help some.

  11. #11
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Arrays, BigInteger's and a bunch of for-loops. Welcome to a huge mess! [Do not view if you do not want any hints on Project Euler]

    I can finally report that I got it right. While working on a different problem, I saw how easy it was to scan through a sorted array. I did a bunch of stupid things with that code, using the BigInteger-class itself was a weird decision, as a double can handle those numbers. Anyway, I started from scratch after realizing that and wrote this in two minutes:

    import java.math.*;
    import java.util.Arrays;
    public class EulerProblem29 {
    	public static void main(String[] args) {
    		double[] tall = new double[10000];
    		int teller = 0;
    		for (double a = 2;a<=100;a++){
    			for (int b = 2;b<=100;b++){
    				tall[teller] = Math.pow(a,b);
    				teller++;
    			}
    		}
     
    		Arrays.sort(tall);
    		for (int c = 0;c<tall.length;c++){
    			for (int d = c + 1;d<tall.length;d++){
    				if (tall[c] == tall[d]){
    					tall[d] = 0;
    				}
    			}
    		}
     
    		int teller1 = 0;
    		for (int e = 0;e<tall.length;e++){
    			if (tall[e] != 0){
    				teller1++;
    			}
    		}
     
    		System.out.println(teller1);
     
     
    	}
     
    }

Similar Threads

  1. [SOLVED] Project Euler problem one
    By lanmonster in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2013, 05:10 PM
  2. [SOLVED] Project Euler Questio 2
    By goldenpsycho in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 12th, 2012, 07:25 AM
  3. [SOLVED] Project Euler: Truncatable Primes
    By Staticity in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 10th, 2011, 12:27 PM
  4. Grades project/arrays, loops
    By rochla16 in forum Collections and Generics
    Replies: 1
    Last Post: March 29th, 2011, 12:26 AM
  5. Project Euler
    By helloworld922 in forum The Cafe
    Replies: 5
    Last Post: September 9th, 2009, 02:51 PM