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

# 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. ## 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. ## 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?

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

Originally Posted by Norm
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. ## 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. ## 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

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

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:

```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. ## 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.

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

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

9. ## 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.

10. ## 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. ## 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);

}

}```