19.3k views
5 votes
The Simplest Impossible Math Problem

In this problem, you are going to program a sequence of steps for a mathematical conjecture whose proof has eluded mathematicians for almost a century.

Here is the procedure:

pick a number X
repeat until X equals 1:
if the number is even, divide it by 2
if the number is odd, multiply it by 3 and add 1
The claim is that this procedure will always converge regardless of the initial value of X.

Your task is to prove this conjecture… just kidding! Your task is to program this procedure using a command line argument as input. For example, if your script is called main.py, you can assume we called your script with python main.py X where X is the input positive integer. Specifically, fill in the code for the converge(n) function which implements this procedure. It received a positive integer n, and returns the number of iterations the procedure took to complete. Also fill out the code under if __name__ == "__main__": to retrieve the first command line argument and print out text to match the following format:

With an input of X we converged to 1 after Y iterations.

where X is the input to the program and Y is the number of iterations it took to converge to 1. You can see most test cases but we've hidden a couple from you.

Given Code:

import sys

# conjecture() take a positive integer n and returns the number of executions of the loop
def conjecture(n):


if __name__ == "__main__":
# retrieve the input, it is passed as the first argument when calling the script
n =
# conjecture takes a positive integer n as input and returns
# the number of loop iterations the loop took to converge
print("With an input of",,"we converged to 1 after",,"iterations.") # fill this in!

User Majdi
by
4.7k points

2 Answers

5 votes

Answer:

Check the explanation

Step-by-step explanation:

Just the way it is specified in the question above, I have taken the input x from the command line as well as making use of that input I calculated iterations that are required to converge to 1 in conjecture function.

Going to the conjecture function, I utilized the count variable to store the number of iterations and returned at the end of the function and at the end printed the output.

The Code:-

#importing sys

import sys

#function

def conjecture(n):

#count to store no of iterations

count=0

#loop run until n is equal to 1

while n!=1:

#if n is even

if n%2==0:

n=n/2

else: #if n is odd

n=3*n+1

#increment counter

count=count+1

return count

if __name__=="__main__":

#command line argument

x=int(sys.argv[1])

#calling function

n=conjecture(x)

#printing output

print("with an input of",x,"we converged to 1 after",n,"iterations.")

User Muhammad Rayhan
by
4.0k points
5 votes

Answer:

  1. import sys
  2. def conjecture(n):
  3. count = 0
  4. while(n != 1):
  5. if(n % 2 == 0):
  6. n = n//2
  7. else:
  8. n = (n * 3) + 1
  9. count += 1
  10. return count
  11. if __name__ == "__main__":
  12. n = sys.argv
  13. c = conjecture(int(n[1]))
  14. print("With an input of",n[1],"we converged to 1 after",c,"iterations.")

Step-by-step explanation:

Firstly, we work on conjecture function. Create a counter variable, count to track the number of iteration needed to converge n to 1 (Line 4).

Create a while loop and set the condition so long as n not equal to 1 the loop will keep running (Line 5). In the loop, if the n is divisible by 2 (even), divide the n by 2 (Line 6-7). Otherwise, multiply n by 3 and add 1 (Line 8-9). Increment count by one before proceeding to next loop (Line 10).

At last return the count as output (Line 11).

In the main program, we use sys.argv to get the input value from command line (Line 14). Pass the second element of the input from command line to the function and create a variable c to capture the function output (Line 15 ). At last, print the input number and output from the function (Line 16).

User Oleg Kyrylchuk
by
4.9k points