1. Member
Join Date
Jun 2013
Posts
12
Rep Power
0

## Help please, what's wrong with my code?

The problem:

n is an element of 1 to 100,000,000 inclusive. print the number and the largest value reached in the hailstone sequence for n only if that largest value exceeds n^2

the 1st 4 lines of OP are
3 16
7 52
27 9232
31 9232

hailstone sequence: if the 1st # is odd, the following # is 3n+1
if it is even, the following # is n\2
the sequence stops at 1

MY CODE:
Java Code:
```public class Main
{

public static void findMax(int i)
{
int	p=i;
int max = 0;

while (i<1)
{

if (i%2==0)
{
i=i/2;

}
else
{
i=3*i+1;

}
if (max<i &&max < Math.pow(i,2))
{

max=i;
System.out.println(p + "\t" + max);
}

}

}
public static void main (String[] args)
{
for (int d = 1; d<=7; d++)
{
findMax(d);
}
}
}```
Last edited by jamgor; 06-11-2013 at 10:33 PM.

2. Senior Member
Join Date
Apr 2013
Location
Sweden
Posts
272
Rep Power
4

## Re: Help please, what's wrong with my code?

while (i<1) why do you have this condition? in your main you start from 1 and the loop is never entered.

3. Member
Join Date
Jun 2013
Posts
12
Rep Power
0

## Re: Help please, what's wrong with my code?

Originally Posted by superhaNds
while (i<1) why do you have this condition? in your main you start from 1 and the loop is never entered.
well it doesnt matter if i start at 1 in the main loop since it increments...

4. Senior Member
Join Date
Apr 2013
Location
Sweden
Posts
272
Rep Power
4

## Re: Help please, what's wrong with my code?

so what is the problem , you haven't asked a question.
yeah it increments up to 7, but findMax(int i) does nothing.

5. Member
Join Date
Jun 2013
Posts
12
Rep Power
0

## Re: Help please, what's wrong with my code?

Originally Posted by superhaNds
so what is the problem , you haven't asked a question.
yeah it increments up to 7, but findMax(int i) does nothing.
there is no output when i run the program(my problem)...so i guess you're right but can you explain why findMax(i) does nothing?

6. Senior Member
Join Date
Apr 2013
Location
Sweden
Posts
272
Rep Power
4

## Re: Help please, what's wrong with my code?

in your main method you start your for loop from 1 and go up to 7 and you pass the counter as an argument to findMax(int i)

so 1,2,3,4,5,6,7 these numbers are going to be passed to findMax.

in the findMax(int i) , you say while(i < 1) do the things you do. So that loop never enters because i will not be less than 1 and so your
program will never reach the print statement

7. Member
Join Date
Jun 2013
Posts
12
Rep Power
0

## Re: Help please, what's wrong with my code?

ok i changed it to != and got some output....theoretically the while will never get executed according to what you said but it did...
thanks btw ^.^

edit:
uhh my program actually does 100% what it is supposed to do now but all i chaanged was the < into the != (which i still dont understand)

and moved the part where i check the if max is greater than the number squared and the print stsatement outside the loop
Last edited by jamgor; 06-12-2013 at 12:58 AM.

8. Senior Member
Join Date
Apr 2013
Location
Sweden
Posts
272
Rep Power
4

## Re: Help please, what's wrong with my code?

Yes, the for loop will increment to 2 and it WILL be passed into findMax, but inside findMax, it will not enter the while loop that it has, can't you see it?
the first time 1 was being passed so 1 < 1 is false, so don't enter the loop, then 2, 2 < 1 is false, so don't enter the loop and so on until 7 and nothing was happening.

9. Member
Join Date
Jun 2013
Posts
12
Rep Power
0

## Re: Help please, what's wrong with my code?

Originally Posted by superhaNds
Yes, the for loop will increment to 2 and it WILL be passed into findMax, but inside findMax, it will not enter the while loop that it has, can't you see it?
the first time 1 was being passed so 1 < 1 is false, so don't enter the loop, then 2, 2 < 1 is false, so don't enter the loop and so on until 7 and nothing was happening.
I am facepalming so hard right now...sorry you had to deal with ma stupidity :d
anyways, thanks so much!

10. ## Re: Help please, what's wrong with my code?

Originally Posted by jamgor
Help please, what's wrong with my code?
Please go through the Forum Rules, particularly the third paragraph.

db

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•