CS377: Database Design - Iteration in Python (3 Points)
Developed by Professor Tralie and Professor Mongan.Exercise Goals
The goals of this exercise are:- To use iteration to compute a discrete value
- To use conditionals to make choices during each loop iteration
Enter your Ursinus netid before clicking run. This is not your ID number or your email. For example, my netid is wmongan
(non Ursinus students can simply enter their name to get this to run, but they won't get an e-mail record or any form of credit).
Netid |
ThreeXPlusOne.py
def threeXPlusOne(x):
# TODO - solve the 3x+1 problem here
# while x is not 1, set x to half of itself if val is even, and to 3x+1 if it is odd
# count and return the number of iterations you needed until x became 1
main.py
ans1 = threeXPlusOne(5)
ans2 = threeXPlusOne(27)
ans3 = threeXPlusOne(17)
print(str(ans1) + "-" + str(ans2) + "-" + str(ans3))
Output
Trivia
This problem is part of the Collatz Conjecture which suggests that any value will eventually converge back to 1 after a finite number of iterations. We don’t know if this is true!