# 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]

• March 19th, 2013, 02:41 PM
EatMyBible
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:

Code :

```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.
• March 19th, 2013, 02:48 PM
Norm
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?
• March 19th, 2013, 02:55 PM
EatMyBible
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
How many elements are 0?

Excellent question, I will check that...

when running:

Code :

```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.
• March 19th, 2013, 03:03 PM
Chris.Brown.SPE
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!
• March 19th, 2013, 03:09 PM
Norm
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
• March 19th, 2013, 03:17 PM
EatMyBible
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
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:

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.
• March 19th, 2013, 03:26 PM
Norm
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.
• March 19th, 2013, 03:28 PM
EatMyBible
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
Add a println to display when an element is set to 0.

Only 0's when printing tabell[j]
• March 19th, 2013, 03:46 PM
Norm
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.
• March 19th, 2013, 05:14 PM
Chris.Brown.SPE
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.
• March 26th, 2013, 04:46 PM
EatMyBible
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:

Code :

```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);     }   }```