homework 4
homework for the fourth class lesson
First Idea, make a recursive loop, where if the value that's being looped squared is equal to the value from the list, it stops. A primative solution, but one that would work, except it wouldn't. The numbers given are far too large for that too work, and the loop reaches it's limit before getting the fourth number.
My second idea was to add a large number like 1,000 after the loop passes 5. This way, I can get farther before reaching the limit. I didn't even attempt this, the gap between the numbers is far too big to work.
I thought about using binary search, as the lesson was about this, but binary search is only used on sorted lists, here I'm working with a singe number.
My first loop didn't work because a the end of the code, I had it run the function again, resulting in the maximum recursion depth being reached. I'm gonna use a while loop instead so it's not stupid.
It wasn't stupid
def sqrt(N):
i = 0
while i < N:
v = i * i
if v == N:
break
i += 1 # Make an extra variable that is i times itself
print(i)
return i
from math import sqrt as sq
test_cases = [0,1,4,85248289,22297284,18939904,91107025,69122596,9721924,37810201,1893294144,8722812816,644398225]
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()
def collatzConjecture(N):
print(N)
while N != 1:
if N % 2 == 0:
N *= 0.5
print(N)
else:
N *= 3
N += 1
print(N)
collatzConjecture(20)