CS377: Database Design - Iteration in Python (3 Points)

Developed by Professor Tralie and Professor Mongan.

Exercise Goals

The goals of this exercise are:
  1. To use iteration to compute a discrete value
  2. To use conditionals to make choices during each loop iteration
Modify the ThreeXPlusOne.py file to solve the 3x+1 problem using a loop and conditional.

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
Clicking Run below will check your work and, if it passes, will submit your work automatically. You must be connected to the VPN for submission to be successful! You will receive a copy of your code via e-mail, so you'll know that it was submitted if you receive that e-mail! VPN access requires Multi-Factor Authentication, which sends you a code when you log into the network. Instructions on configuring these for your account can be found here.

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!