Use a for loop to find the smallest integer n such that n^3 is greater than 1000

So I came out with below code

public class Chapter4 {
static int n = 0;
public static void main(String[] args) {
for (int i = 1; Math.pow(i, 3) <= 1000; i++) {
n = i;
}
System.out.println("Smallest i is " + n);
}
}

I get value answer 21 which looked like not the smallest integer. What is wrong with my logic ? I thought the value n should be 10 ?

Not sure where 21 came from. I get 10 as well. Which is wrong, yes?

find the smallest integer n such that n^3 is greater than 1000

10 to the 3rd power EQUALS 1000. It is not GREATER THAN 1000.

PS. This works too.

    public class Chapter4 {
static int n = 0;
static int i = 0;
public static void main(String[] args) {
for (i = 1; Math.pow(i, 3) <= 1000; i++) {
n = i;
}
System.out.println(i);
}
}

Why? The loop stops at the value which was equal but due to how for() works, your answer is i.

PS. This works too.

Yes, it works and is a good thing to study as far as tracing through the values. However, considering that the OP is a newbie, it might put him down a road he's not ready to go down just yet (variable scope issues and static class variables). I imagine the OP is a newbie and the professor is teaching the class like a C/C++ professor would in the beginning (no classes, no constructors, so make everything static, make everything global, then later we'll get to variable scoping, but for now stick them all at the top). All fine.

Except that in for-loops, the OP already knows to declare them in the for-loop, but he likely does not know how the ramifications of that. I see an inevitable problem of the OP coding on auto-pilot and declaring i in the for-loop SOMETIMES and sometimes not. When that happens, the OP needs an error message, he doesn't need two separate i variables, each at different scopes.

So to sum up, my general rule of thumb for newbies is that i, j, and k are never declared outside of a function, are never static, and are ONLY to be declared and used in a for-loop. OR if you don't like that rule, then NEVER declare them in the for-loop.

When they are no longer newbies, they can bend these rules.

YMMV.

I'm already adding "except for" clauses to the above. I'm fine with i, j, and k being used for while and do-while loops as well. A loop is a loop. The for-loop, however, is a potential trap.

    for (int i = 1; Math.pow(i, 3) <= 1000; i++) {
n = i;
}

is this...

{
int i = 1;
while(Math.pow(i, 3) <= 1000)
{
n = i;
i++;
}
}
// i no longer exists here

Thus I prefer teaching while loops, THEN for loops.

To the OP, if this is confusing, don't worry too much about it. You'll get there in good time.

I get value answer 21 which looked like not the smallest integer

If you used 10000 instead of 1000 you would get 21.

commented: Jackpot or Blackjack. +14

If you used 10000 instead of 1000 you would get 21.

Yes, you're right ! Just realize I wrote 10000 instead of 1000.

10 to the 3rd power EQUALS 1000. It is not GREATER THAN 1000.

I also removed the EQUALS sign so can get value GREATER THAN 1000.

Thanks guys!

Odd, when I removed the EQUAL sign I got 9.

To address concerns about the variable i, I spiffed up the code to look like this.

        public class Chapter4 {
static int n = 0;
public static void main(String[] args) {
for (n = 1; Math.pow(n, 3) <= 1000; n++) {
}
System.out.println(n);
}
}

I get the expected result of 11 (Spinal Tap reference) and without the EQUAL sign, ten.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.