Not Yet Answered # hailstone algorithm...

Discussion Starter shadows09 scru 909 Discussion Starter shadows09 scru 909 scru 909 leegeorg07 23 leegeorg07 23 Need some help with this Array. I am trying to get the sum of the even numbers and the sum of the odd numbers using a for each loop. I know the answers to what I am trying to achive are sum of even = 84 and the sum of ...

0

Here's what I did so far.

Obviously, there are syntax errors and I don't know the parity code.

numStr = raw_input("Please enter an integer: ")

number = int(numStr)

while number !=1:

if number == even:

number = number/2

elif if number == odd:

number = (number * 3) + 1

elif if number == 1

break

print 'Done'

0

Please wrap you code in [ code=python ] [ /code ] tags next time.

Your algorithm seems fine according to the pseudo code you gave above. So I'm ging to show you how to determine if an integer is odd or even.

```
numStr = raw_input("Please enter an integer: ")
number = int(numStr)
while number > 0 and number != 1:
if number % 2:
number = number * 3 + 1
else:
number =/= 2
print "done"
```

Things to note:

I removed the last elif because the while loop already checks for that. I also now check that number is not negative.

The % operator means modulus. It divides a number by its operand and returns the remainder. Using % 2 will be 0 when a number is even (evaluates as False) and 1 when it is odd (evaluates as True)

0

huh....I just get 'done' for any integers. I'm not sure if that's what's supposed to happen

0

From the pseudo code in the first post, it looks like the algorithm is supposed to reduce any integer to 1. If that is happening then it will indeed print "done" at the end.

If you need to print the amount of steps it took, create a variable named steps and assign zero to it, then increment it by one in the while loop.

Unless there's something else you want it to do that I missed?

0

So you want to print number out everytime you enter the loop. You can use a comma to get them to appear on the same line.

You want to keep prompting until a negative number is entered?

Take out the > 0 check i entered, wrap the first while loop in a new while loop that does this check. Then prompt for number again at the end of that new while loop.

0

after looking at your code i made my own i a function which i feel is faster, i also added in a print for a counter, the number and the final number. I used Python30.

```
def hailstone(num):
i = 0
while num > 1:
if num <= 0:
print("please enter a new number")
elif num %2:
num = (num * 3) + 1
else:
num /=2
print(num)
i+=1
print()
print(i)
print()
print("done")
print()
print(num)
```

im going to time it to make sure but thats my first idea.

0

no i was wrong, it turns out that scru's program is 0.0010001659393000014 seconds faster than mine with the number 10000000003. but i thought it was a good exercise

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

When I execute this progammatically, I get a table with row heights much larger than when I do this manually.

Note : Sel is the Word.Selection object and the Clipboard contains an Excel Table.

```
public void AddClipboard()
{
Sel.PasteExcelTable(false,false, false);
var t = Sel.Tables[Sel.Tables.Count];
t.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
}
```

the function that I created to find the ...