Unit 3 Lesson 9 & 11 HW
Notebook that compiles all the tangibles required for Unit 3 Lesson 9 and 11
def mystery(num, num2):
if (num % num2 == 0):
print("True")
else:
print("False")
mystery(20, 4)
- What does the algorithm do? Please explain in words.
An algorithm checks whether or not the remainder of the expression above = 0. If that's the case, it prints True, if it isn't, it prints False.
- What if I put in 30 as num and 4 as num2. What would be the output?
The output would be False, as 30 divided by 4 does not equal 0.
What is the output of this algorithm?
it is too hot outside
temp = 95
if (temp >= 90):
print("it is too hot outside")
if (temp >= 65):
print("I will go outside")
else:
print("it is too cold outside")
What is the output of this algorithm? it looks similar but the output is different!
it is too hot outside i will go outside
temp = 95
if (temp >= 65):
if (temp >= 90):
print("it is too hot outside")
else:
print("i will go outside")
if (temp < 65):
print("it is too cold outside")
sum = 0
counter = 1
sum = 0
for i in range (1, 10, 2):
sum = sum + i
print(sum)
def collatz(N):
if N == 1: # If the variable = 1, simply print it
print(N)
return
elif N % 2 == 0: # However, if this number is not one and an even number, print it and divide it by 2
print(N)
collatz(N // 2)
else: # If it's not 1 or even, it must be an odd number that isn't 1 and in that case, multiply by 3 and add 1
print(N)
collatz(N * 3 + 1)
collatz(6) # Calls the function with the argument 6
def sqrt(N):
# Checking to see if the number is 1 or 0, as the sqrt of 1 = 1 and the sqrt 0 = 0.
if N < 2:
return N
# If the number is not 1 or 0, it will proceed to do the following:
else:
y = N # Creating a variable y, and setting that = N, which is a variable that represents whatever integer that is put into the function
z = (y + (N/y)) // 2 # creating the equation that divides N down
while y - z >= 0.00001: # this while loop continues this process until we get our desired answer
y = z
z = (y + (N/y)) / 2
return z # returns our answer
if __name__ == '__main__': # setting N to a placeholder value of 25 in this cell to test
N = 25
answer = round(sqrt(N)) # set final answer to the rounded product of the function.
print(answer)
test_cases = [0,1,4,85248289,22297284,18939904,91107025,69122596,9721924,37810201,1893294144,8722812816,644398225]
answers = [int(sqrt(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()