Homework 9,11
Create an algorithm that will start with any positive integer n and display the full sequence of numbers that result from the Collatz Conjecture. The COllatz Conjecture is as follows:
- start with any positive integer
- if the number is even, divide by 2
- if the number is odd, multiply by 3 and add 1
- repeat steps 2 and 3 until you reach 1
NOTE:
1. My javascript kernel is broken I dont know why.
2. To see outputs highlight the text. My fastpages theme is stuck between darkmode and lightmode so its black text on a black background.
def collatzthis(invalue):
while invalue != 1:
print(invalue)
if invalue % 2 == 0:
invalue = invalue / 2
else:
invalue = invalue * 3
invalue = invalue + 1
return(invalue)
values = [6,1,7,23,8,12]
for i in range(len(values)):
goesin = values[i]
out = collatzthis(goesin)
print(int(out))
print("----------------------------")
def sqrt(x):
z = x
y = x
while y**2 != x:
if y**2 > x:
z = y
y = y//2
#print('Low')
else:
y = (y+z)//2
#print('high')
return y
# theres a surprise in the unformatted markdown for the heading if you look hard enough
return None
from math import sqrt as sq
test_cases = [0,1,4,85248289,22297284,18939904,91107025,69122596,9721924,37810201,1893294144,8722812816,644398225]
#these were for personal debug please disregard \/
#test_cases = [0,1,4,9,16,25,36,49,64,81,100,121,144]
answers = [int(sq(x)) for x in test_cases]
def checkValid():
for i in range(len(test_cases)):
if sqrt(test_cases[i]) == answers[i]:
print("Check number {} passed".format(i+1))
else:
print("Check number {} failed".format(i+1))
checkValid()