I should be getting this as my output, but it keeps going through each result:

10  -> 5  -> 16  -> 8  -> 4  -> 2  -> 1 ; length = 7

11  -> 34  -> 17  -> 52  -> 26  -> 13  -> 40  -> 20  -> 10  -> 5  -> 16  -> 8  -
> 4  -> 2  -> 1 ; length = 15

12  -> 6  -> 3  -> 10  -> 5  -> 16  -> 8  -> 4  -> 2  -> 1 ; length = 10

This is the code I have come up with:

start = input("Enter the starting number ( 1 - 10000 ) ")
    print "Enter the ending number (",start+1,"- 10000 )",
    end = input()

    number = start
    number >= start
    number <= end
    length = 1
    
    print number, '->',
    while number != 1:
        for i in range (start,end):
            if number %2 == 0:
                number = number/2
                print number, '->',
                
            else:
                number = (number*3)+1
                print number, '->',
            length= length+1
            print '  length = ',length,'\n',

Can somebody please help me with this, I feel like there is one line missing or something.

Thanks in advance!

This is a modified version of your program that prints what you expect. I am not sure if it is what you want or not.

def tryit(start, end):
    number = start
##    number >= start
##    number <= end
    length = 1

    print number, '->',

    ## end+1 because length starts at 1, not zero.
    while (number != 1) and (length < end+1):
##    for i in range (start,end):
            if number %2 == 0:
                number = number/2

            else:
                number = (number*3)+1
            length= length+1

            print number, '->',

    print ';  length = ',length,

start = input("Enter the starting number ( 1 - 10000 ) ")
print "Enter maximum number of repetitions ",
end = input()

tryit(start, end)

The problem with your original code was that the for() loop could end before number == 1, which resulted in and infinite loop. Whenever you use a while() loop, it is a good idea to limit the number of cycles, at least when testing, to avoid an infinite loop.

Thats actually the first part of the program where it's only for 1 hailstorm, but the second part of the program should have multiple hailstorms where the user defines the start and end, so it should be in lines, so if the user enters like, 11 and 12, it would display

hailstorm for 11 -> length

hailstorm for 12 -> length

like that and so on if the user enters a bigger range.

Thanks for the reply, but it still doesnt give the output needed, it is still going through the result

Member Avatar for masterofpuppets

try this:

def hailstorms( start ): #This is basically using your algorithm with small changes

    number = start
    length = 1
    print number, "->",

    while number != 1:
        if number % 2 == 0:
            number /= 2
            print number,
            if number != 1:
                print "->",
        else:
            number = ( number * 3 ) + 1
            print number,
            if number != 1:
                print "->",
        length += 1

    print " ; length =", length
    print

start = input( "Enter a number ( 1 - 10000 ) " )
end = input( "Enter the number of repetitions ( %d - 10000 ) " % ( start + 1 ) )

for i in range( end ):
    hailstorms( start )
    #Do the same but increase the start value
    start += 1

That last code doesn't work either, thanks for the input though. Also, I'm not looking for number of repititions, I'm looking for the numbers between and the numbers itself to hailstorm.

it should be like (making these numbers up):
start=12
end=14
12-154-15-2-1
13-153-151-61-3
14-15-1234-6 ##notice how the beginning numbers go up in increments of 1, starts at 12, ends at 14 but displays all between as well

Member Avatar for masterofpuppets

sorry i messed up at the last loop :) the condition should be for i in range ( ( end - start ) + 1 ):

def hailstorms( start ): #This is basically using your algorithm with small changes
 
    number = start
    length = 1
    print number, "->",
 
    while number != 1:
        if number % 2 == 0:
            number /= 2
            print number,
            if number != 1:
                print "->",
        else:
            number = ( number * 3 ) + 1
            print number,
            if number != 1:
                print "->",
        length += 1
 
    print " ; length =", length
    print
 
start = input( "Enter a number ( 1 - 10000 ) " )
end = input( "Enter the number of repetitions ( %d - 10000 ) " % ( start + 1 ) )
 
for i in range( ( end - start ) + 1 ):
    hailstorms( start )
    #Do the same but increase the start value
    start += 1

# here's my result with sample test data:
>>> 
Enter a number ( 1 - 10000 ) 12
Enter the number of repetitions ( 13 - 10000 ) 15
12 -> 6 -> 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1  ; length = 10

13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1  ; length = 10

14 -> 7 -> 22 -> 11 -> 34 -> 17 -> 52 -> 26 -> 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1  ; length = 18

15 -> 46 -> 23 -> 70 -> 35 -> 106 -> 53 -> 160 -> 80 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1  ; length = 18

>>>

Not sure if this is what you want.

P.S sorry if this doesn't help either. I made a mistake in the loop so i decided to correct it :)

Thank you so much! That worked!!! I will post my final program here when finished, thank you again!

You'll have to post your code as no one can tell what to do without code (Doh). If you are using a function, the function would return the length which would then be compared with the existing longest length, and if larger, would become the new longest length.

I have no idea how to start this, here is the example my professor gave me:

Enter the starting number (1-10000): 1
Enter the ending number (2-10000): 100
97 had the longest chain 119

I assume all I have to do is modify the code from the hailstorm

Member Avatar for masterofpuppets

here's a hint:

l = [ 1, 5, 3, 7, 9, 2, 5, 6 ]
longest = l[ 0 ]
for i in l:
     if i > longest:
          longest = i
print longest

just modify the hailstorm function so it returns the length instead of printing the actual results and in every cycle compare it to the previous length :) hope this helps you

hmm, but where did you come up with the numbers in the set?

Member Avatar for masterofpuppets

well, in the hailstorm function just change the last two lines - instead of printing the length return the length. i.e.

def hailstorms( start ):
      ... #here remove all print statements
      return length

...
longest, chain = -1, -1 #since the length can't be < 0
for i in range( ( end - start ) + 1 ):
    if hailstorms( start ) > longest: #compares the current length with the previous one
        longest = start
        chain = hailstorms( start ) #if there is a longest chain, save its length
        
    #Do the same but increase the start value
    start += 1

print longest, "had the longest chain", chain

Thanks a bunch! worked as usual. You can see my complete source code or download it here:

http://pedrumgolriz.com/index/?p=52

You should wrap you code in tags at your web site to preserve indentation.

I did, it didn't work :-/

Hi guys, in case you're interested, I did the second part to this program which displays a GUI of a graph of the range of numbers you enter, again you can view/download on my website.. http://pedrumgolriz.com/index/?p=62

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.