Section 3.17 ~ 3.18
Vocab
Collatz
- 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.
Hailstone numbers
- 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.
def Collatz(n):
print(n)
if n == 1:
return 1
elif n%2:
Collatz(3*n+1)
else:
Collatz(n//2)
x = int(input())
Collatz(x)
Notes
Problem had been posed by L.Collatz in 1937. There are some cycles that repeat forever. For example, there are (4, 2, 1), (-2. -1), (-17, -50, -25, -74, -37, -110, -55, -164, -82, -41, -122, -61, -182, -91, -272, -136, -68, -34). Numbers that produced by Collatz are called hailstone number. Conway proved that the length of the cycle must be less than 400 and he also demonstrate that Collatz-type problems can be formally undecidable. Collatz algorithm has been tested and found that all number of range(1, 19*2^(58)) will reach to 1. The Collatz problem can be implemented as an 8-register machine (Wolfram 2002, p. 100), quasi-cellular automaton (Cloney et al. 1987, Bruschi 2005), or 6-color one-dimensional quasi-cellular automaton with local rules but which wraps first and last digits around (Zeleny).
Algorithm efficiency is the aspect of programming that measure the step of codes. It is more useful when the codes are small, but have a more function. The shorter code seems to be better to see and pace of calculation of computer will be way more faster than the long code. That's why people made procedure for not repeating codes.
def collatz1(i):
while i > 1:
print(i, end=' ')
if (i % 2):
# i is odd
i = 3*i + 1
else:
# i is even
i = i//2
print(1, end='')
def collatz2(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('Enter i: '))
list_ = [i]
break
except ValueError:
print('Invaid selection, try again: ', end='')
print('Sequence: ', end='')
collatz1(i)
l = collatz2(i)
print('')
print('Number of iterations:', len(l) - 1)
numList = []
b = ""
while True:
try:
i = int(input("put your number: "))
break
except ValueError:
print("Please enter your number again: ")
def collatz1(a):
while True:
numList.append(int(a))
if a%2 == 0:
a = a/2
elif a == 1:
break
elif a%2 == 1:
a = 3*a+1
collatz1(i)
for i in range(len(numList)):
b += str(numList[i])
b += ", "
print(b)
print(len(numList))
def wu(x):
print(x)
if x == 1:
return 1
if x % 2 == 1:
return wu(3*x+1)
else:
return wu(x//2)
n = int(input())
wu(n)
import random
sel = ['scissor', 'rock', 'paper']
result = {0: 'win.', 1: 'lose.', 2: 'draw.'}
def checkWin(user, com):
if not user in sel:
print('you wrote wrong. type it again')
return False
print(f'player 1 ( {user} vs {com} ) com')
if user == com:
state = 2
elif user == 'scissor' and com == 'rock':
state = 1
elif user == 'rock' and com == 'paper':
state = 1
elif user == 'paper' and com == 'scissor':
state = 1
else:
state = 0
print(result[state])
return True
print('\n-------------------------------------------')
while True:
user = input("rock, scissor, paper : ")
com = sel[random.randint(0, 2)]
if checkWin(user, com):
break
print('-------------------------------------------\n')
import random
game = ["rock", "scissor", "paper"]
winning = ["paper", "rock", "scissor"]
i = 0
def gameStart(a):
randomNumber = random.randrange(0,2)
randomOne = game[randomNumber]
gamer = str(input("what will you do"))
print(gamer)
print(randomOne)
while True:
if winning[a] == gamer:
break
else:
a += 1
if randomNumber == a:
print("You win")
else:
if randomNumber == (a+1)%3:
print("Lose")
elif randomNumber == (a+2)%3:
print("Draw")
pre = input("Do you want a game?[yes/no]")
if pre == "yes":
gameStart(i)
randomNumber = random.randrange(0,2)
else:
print("Goodbye")
gameStart(i)
Reason
The second code is more effective because I use list instead of comparing rock scissors paper. rock wins scissors, paper wins rock, and scissor wins paper. It looks like pattern to me. So I change it to the list. If there are A list represented ["rock", "scissor", "paper"] and B list represented ["paper", "scissor", "rock"], A[i] always win B[i]. Also I used the loops so that player can play rock scissor paper with several times. The code are similar, but second one brings up more effect. Therefore, I think the effective one is the second code.
Hack 3
Explain Algorithm effectivity
An algorithm is more efficient when its complexity is low. That is, algorithms that take a short time to run or use a small amount of memory are efficient. For example, there are code that multiply 5 at 6 and code that add 6 five times. It will be effective when we use 6*3. There are several codes which lead to same result. However, the most simple and shortest one will be the best code between it. There is no answer in these days. Sometime this is right or sometime this way is wrong.
from datetime import datetime
tasks = ["wake up", "go to school"]
def things_to_do(tasks):
for task in tasks:
if task == "wake up":
print("Waking up now!")
elif task == "go to school":
print("Going to school now!")
print(datetime.today().strftime("%A"))
if datetime.today().weekday() == 5 | 6:
print("Today we have no school, I will sleep all day")
else:
things_to_do(tasks)