Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

BACKGROUND: Let's consider three rules: If a number is even , divide it by 2. If a number is odd , triple it and then

BACKGROUND: Let's consider three rules:

If a number is even, divide it by 2.

If a number is odd, triple it and then add 1.

Stop if you ever get to 1.

For example, starting with 7:

 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 

or, starting with 12:

 12 6 3 10 5 16 8 4 2 1 

It has been conjectured (I suppose by someone named Ulam) that no matter what positive number we begin with, the pattern will always reach 1. This has been tested on trillions of numbers, and it has always worked, but no one has been able to prove that it works for all numbers.

TASK: Write a Python program that given some number determines how many numbers are in the sequence, or stated another way, determines how many "steps" (plus one) it took to get to one. For example, if given the number 12 (that is, reads a "12" from standard input), the program should determine that there are 10 numbers in the sequence (see second example above) and should print that result. Similarly, if given the number 7, the program should print that the sequence length is 17. The program should then read another number and repeat this process until a sentinel value of zero is detected.

IMPLEMENTATION NOTES: Define an integer value-returning function, named "ulam_sequence_length()", that when invoked with a single (positive) integer argument returns the Ulam sequence length of that integer. For example,

 number = 7 x = ulam_sequence_length(number) 

will assign a value of 17 to x.

Do not use any global variables. Read your data from standard input (stdin) using the input() function. Don't bother prompting the user for input as you will be using the UNIX input file redirection mechanism to obtain the input values from a file. Use as your source file name "ulam.py". Your output should consist of two columns of (right-justified) numbers: the first column is 12 characters wide and contains the input number; the second column is 6 characters wide and contains the number's corresponding Ulam sequence length.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M. Ricardo, Susan D. Urban, Karen C. Davis

4th Edition

1284231585, 978-1284231588

Students also viewed these Databases questions

Question

Write a Python program to check an input number is prime or not.

Answered: 1 week ago

Question

Write a program to check an input year is leap or not.

Answered: 1 week ago