1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need some help with my code, if you're able.

Discussion in 'Programming' started by misery89, Oct 1, 2007.

  1. #1
    Hey everyone, I've been working hard on this, and I don't know if I'm making progress. How do I get the number, (i) to go through the corresponding formula and THEN returning with the next number (i+1).
    If you know, Please help me out a bit.

    Also, if know how - how to determine the maximum number of interations, and the number that generated it.

    public class Collatz
    {
    public static void main(String [] args)
    {
    for (int count =1; count <= 65535; count++)
    {

    for (int i = 1; i <=65535; i++)
    {
    if( i% 2 == 0)
    {
    i = i / 2;
    }
    else
    {
    i = 3 * i + 1;
    }

    System.out.println(i);
    }

    }

    }
    }


    Thank you so much.
     
    misery89, Oct 1, 2007 IP
  2. it career

    it career Notable Member

    Messages:
    3,562
    Likes Received:
    155
    Best Answers:
    0
    Trophy Points:
    270
    #2
    Can you elaborate what problem are you try to solve ?
    You are going in an infinite loop possibly.
     
    it career, Oct 2, 2007 IP
  3. webmaster_TSU

    webmaster_TSU Peon

    Messages:
    449
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yep, I noticed I was trying to answer your question in the other thread, and I gave this infinite loop, too (can't check the code atm, just going off the top of my head). I think this would stop the infinite loop, but I'm still unsure the question 100%.

    public class Collatz
    {
    public static void main(String [] args)
    {
    for (int count =1; count <= 65535; count++)
    {

    for (int i = 1; i <=65535; i++)
    {
    temp_i = i;
    if( temp_i% 2 == 0)
    {
    temp_i = temp_i / 2;
    }
    else
    {
    temp_i = 3 * temp_i + 1;
    }

    System.out.println(temp_i);
    }

    }

    }
    }
     
    webmaster_TSU, Oct 2, 2007 IP
  4. arcx33

    arcx33 Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Now those two bits of code do very different things.

    The first is a nested loop of 64k iterations with each iteration going into a loop that runs about 10 times since I is never even during the if-else statement. so About 640K iterations total

    The second is a nested loop that does 64K iterations with each iteration doing 64K inner iterations. A total of about 4 billion iterations.

    Did you want to use that count variable somewhere? What is the codes intention?
     
    arcx33, Oct 3, 2007 IP