Lesson Notes 3.17 and 3.18
Lesson Notes
Notes
-
Hailstone numbers are basically numbers that appear in a sequence of an iteration
-
The Collatz conjecture is one of the most famous unsolved problems in mathematics. The conjecture asks whether repeating two simple arithmetic operations will eventually transform every positive integer into 1.
-
The sequence of integers generated by Collatz conjecture are called Hailstone Numbers. Examples: Input : N = 7 Output : Hailstone Numbers: 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 No.
-
The action or a process of iterating or repeating: such as. : a procedure in which repetition of a sequence of operations yields results successively closer to a desired result.
-
An undecidable problem is one that should give a "yes" or "no" answer, but yet no algorithm exists that can answer correctly on all inputs.
-
An unsolvable problem is one for which no algorithm can ever be written to find the solution.
def collatz(i):
while i != 1:
if i % 2 > 0:
i =((3 * i) + 1)
list_.append(i)
else:
i = (i / 2)
list_.append(i)
return list_
print('Please enter a number: ', end='')
while True:
try:
i = int(input())
list_ = [i]
break
except ValueError:
print('Invaid selection, try again: ', end='')
l = collatz(i)
print('')
print('Number of iterations:', len(l) - 1)
print("Histone Numbers", list_)
This is one of the hacks where we combined two pieces of code and we were able to print both the hail stone numbers and the number of iterations it takes the program to get to the number.
The next two codes are for algorithm efficiency. The first code is inefficient because the programer has to right the same code over and over again, only with using different numbers, which gets messy and annoying. The second code, however, puts all the values in a list and takes the square root all at once. So that way, all you have to do is add numbers, not functions, which is way more convenient.
import math
num1 = 4
num2 = 9
num3 = 16
num4 = 25
print(math.sqrt(num1))
print(math.sqrt(num2))
print(math.sqrt(num3))
print(math.sqrt(num4))
import math
values = [4, 9, 16, 25, 36, 49]
squareroots = [math.sqrt(number) for number in values]
print("The Original Values:\n", values)
print("The Square Root Values:\n", squareroots)