Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By superhaNds

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

  1. #1
    jamgor is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default 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. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    263
    Rep Power
    2

    Default 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. #3
    jamgor is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

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

    Quote Originally Posted by superhaNds View Post
    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. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    263
    Rep Power
    2

    Default 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. #5
    jamgor is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

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

    Quote Originally Posted by superhaNds View Post
    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. #6
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    263
    Rep Power
    2

    Default 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. #7
    jamgor is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default 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. #8
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    263
    Rep Power
    2

    Default 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.
    jamgor likes this.

  9. #9
    jamgor is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

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

    Quote Originally Posted by superhaNds View Post
    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. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

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

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

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. What is wrong with my code?
    By Denis2k11 in forum New To Java
    Replies: 3
    Last Post: 03-28-2013, 03:26 PM
  2. what's wrong with this code?
    By worldchamp in forum New To Java
    Replies: 6
    Last Post: 11-30-2012, 09:49 AM
  3. What is wrong with this code?
    By Mythreadings in forum New To Java
    Replies: 38
    Last Post: 11-19-2010, 12:43 AM
  4. Need help! something wrong in my code
    By novak100 in forum New To Java
    Replies: 2
    Last Post: 11-18-2009, 11:59 PM
  5. What's going wrong with this code?
    By Suurisa in forum New To Java
    Replies: 5
    Last Post: 10-19-2009, 11:59 PM

Posting Permissions

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