-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHailstoneProblem.py
48 lines (39 loc) · 1.75 KB
/
HailstoneProblem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from os import close
import random
def checkAndWrite(line):
if storingFile == 'y':
file = open(fileName, 'at')
file.write("\n" + line)
file.close()
print('This script is licensed under the MIT license and can be found as LICENSE in folder this script came from or at https://github.com/EscWasTaken/Python-Hailstone-Problem/blob/main/LICENSE')
baseNum = int(input('Enter any integer or leave blank for a random number between 2 & 10 Million: '))
if baseNum == "":
baseNum = random.randint(2,10000000)
print("Your number will be: " + str(baseNum))
workNum = baseNum
counter = 0
fileName = 'Hailstone ' + str(baseNum) + '.txt'
storingFile = input('Would you like to store the output to a text file? (y/n) ')
while workNum > 1:
if workNum % 2 == 0:
print('Even Number, Dividing '+ str(workNum) + ' by 2.')
checkAndWrite('Even Number, Dividing ' + str(workNum) + ' by 2.')
workNum = workNum / 2
print("Result is " + str(workNum))
checkAndWrite("Result is " + str(workNum))
else:
print('Odd number, Multiplying ' + str(workNum) + ' by 3 and adding 1.')
checkAndWrite('Odd number, Multiplying ' + str(workNum) + ' by 3 and adding 1.')
workNum = workNum * 3
workNum += 1
print('Result is ' + str(workNum))
checkAndWrite('Result is ' + str(workNum))
counter += 1
print('Not solved, going again.')
checkAndWrite('Not solved, going again.')
print('Your base Number was: ' + str(baseNum))
checkAndWrite('Your base Number was: ' + str(baseNum))
print('It resulted in: ' + str(workNum))
checkAndWrite('It resulted in: ' + str(workNum))
print('It took ' + str(counter) + ' steps to reach 1')
checkAndWrite('It took ' + str(counter) + ' steps to reach 1')