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

• 06-11-2013, 11:28 PM
jamgor
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:
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);         } } }```
• 06-12-2013, 12:10 AM
superhaNds
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.
• 06-12-2013, 01:00 AM
jamgor
Re: Help please, what's wrong with my code?
Quote:

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...
• 06-12-2013, 01:36 AM
superhaNds
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.
• 06-12-2013, 01:43 AM
jamgor
Re: Help please, what's wrong with my code?
Quote:

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?
• 06-12-2013, 01:46 AM
superhaNds
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
• 06-12-2013, 01:49 AM
jamgor
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
• 06-12-2013, 01:57 AM
superhaNds
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.
• 06-12-2013, 01:59 AM
jamgor
Re: Help please, what's wrong with my code?
Quote:

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!
• 06-12-2013, 04:35 AM
DarrylBurke
Re: Help please, what's wrong with my code?
Quote:

Originally Posted by jamgor
Help please, what's wrong with my code?

Please go through the Forum Rules, particularly the third paragraph.

db