Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added program for collatz conjecture #598

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions projects/Collatz Conjecture/collatz_conjecture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

current_num = int(input("Enter the number to start from: ")) #accept user input
checker = False
num_list = []

even = lambda current_num: current_num/2 #lambda function for the even results
odd = lambda current_num: 3*current_num + 1 #lambda function for the odd results

while (checker == False):
if (current_num != 1):#check if the number equals 1
if (current_num%2 == 0): #check if the number is even
num_list.append(int(even(current_num))) #add the operation value to the tree
current_num = even(current_num) #set the new value of the current_num

elif (current_num%2 == 1): #check if the number is odd
num_list.append(int(odd(current_num))) #add the operation value to the tree
current_num = odd(current_num) #set the new value of the current_num

else: #if the number equals 1
num_list.append(int(current_num))
checker = True #loop breaker
print(f"\nTree: {num_list}") #print tree
print(f"The operation took {len(num_list)} steps to complete and the peak was {max(num_list)}.\n") #operation summary