Question

In python: Not sure how to do a Collatz function. Remember the Collatz function? If x...

In python:

Not sure how to do a Collatz function.

Remember the Collatz function? If x is odd, then collatz(x) = 3 ⋅ x + 1 but if x is even
then collatz(x) = x/2. (remember to use // to divide by 2)
Start by writing a Collatz() function that returns the value for a given input. Test
this function for all numbers from 1 to 10.
Next write a Collatz2() method. This method should take in a single number and
repeatedly use the Collatz() function printing out the result while the number is not
one.
For example:
If I say Collatz2(5), Then the output should be :
5
16
8
4
2
1

If I say Collatz2(12), Then the output should be :
12
6
3
10
5
16
8
4
2
1

Once you know it is working, run your method on the number n = 27 and see how long
it takes to get to 1.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

here is the python code in python 3:

===============================================================

Program:

===============================================================

def Collartz(n):

    print(n)

    while n != 1:

        if n%2 != 0:

            n = 3*n+1

            print(n)

        if n%2 == 0:

            n = n//2

            print(n)


n = int(input("Enter n: "))

Collartz(n)


==============================================================

Screen shot of the code..

def Collartz(n): print(n) while n!1: print(n) print(n) nint(input(Enter n: ) Collartz(n)

===============================================================

Output:

def Collartz(n): print(n) while n!1: print(n) print(n) nint(input(Enter n: ) Collartz(n)

Enter n: 27
27
82
41
124
62
31
94
47
142
71
214
107
322
161
484
242
121
364
182
91
274
137
412
206
103
310
155
466
233
700
350
175
526
263
790
395
1186
593
1780
890
445
1336
668
334
167
502
251
754
377
1132
566
283
850
425
1276
638
319
958
479
1438
719
2158
1079
3238
1619
4858
2429
7288
3644
1822
911
2734
1367
4102
2051
6154
3077
9232
4616
2308
1154
577
1732
866
433
1300
650
325
976
488
244
122
61
184
92
46
23
70
35
106
53
160
80
40
20
10
5
16
8
4
2
1

================================================================

Kindly Check and Verify Thanks..!!!

Add a comment
Know the answer?
Add Answer to:
In python: Not sure how to do a Collatz function. Remember the Collatz function? If x...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I want to know how to do this assignment!!! Help me please~ The first part of...

    I want to know how to do this assignment!!! Help me please~ The first part of your program should do the following: • Ask the user type of test they took. o (ACT or SAT) • If they said ACT then ask them their score o If their ACT score was between 0 and 7 say "Needs Work" o If their ACT score was between 10 and 20 say "Acceptable" o If they ACT score was above 20 say "Above...

  • Language is Python 3, please include comments Question 1 Write function collatz, that takes a positive...

    Language is Python 3, please include comments Question 1 Write function collatz, that takes a positive integer x as input and prints the Collatz sequence starting at x. A Collatz sequence is obtained by repeatedly applying this rule to the previous number x in the sequence: 3/2 if x is even 3.2 +1 if x is odd. Your function should stop when the sequence gets to number 1. Note: It is an open question whether the Collatz sequence of every...

  • CODE NEEDS TO BE IN PYTHON. OLA 5: Collatz Sequence Function 12 17 34 Due: Fri...

    CODE NEEDS TO BE IN PYTHON. OLA 5: Collatz Sequence Function 12 17 34 Due: Fri Oct 19, 2018 by 11:59 PM-may be tumed in until Oct 26 by 1159 PM with reduced points (per Open Lab- Project guidance found in the course syllabus) Assignment id: ola5 Assignment type: Project Required Files: ola5.py, myout.log Lab description: In this you will explore the Collatz mathematical sequence. This sequence eventually converges to the value 1, regardless of the initial input Requirements: 1....

  • Please answere using python language codes. Write a program to print out Collatz sequence for a...

    Please answere using python language codes. Write a program to print out Collatz sequence for a user-supplied number. Prompt the user for a positive integer which will become the first number in the sequence. Next number in the sequence is derived as follows: If previous number is odd, the next number is 3 times the previous, plus 1. If previous number is even, the next number is half of the previous According to Collatz proposition, the sequence ultimately reaches 1...

  • use python please #The Joyner Conjecture is a not-at-all famous mathematical #series inspired by the Collatz...

    use python please #The Joyner Conjecture is a not-at-all famous mathematical #series inspired by the Collatz Conjecture for use in this #class. # #The Joyner Conjecture proceeds as follows: # #Start with any number. If the number is divisible by 3, #divide it by 3. Otherwise, add 2 to the number. Eventually, #no matter what number you begin with, this series will run #into 1 or 2. If it runs into 1, it will repeat 1-3 forever. #If it runs...

  • python la ab X, X Lab 7 Design and implement a Python program that uses a...

    python la ab X, X Lab 7 Design and implement a Python program that uses a while loop and selection statements to print the sum of the even numbers and the sum of the odd numbers (all integers) read from the keyboard. The sequence of numbers ends with a negative number: So, all numbers considered for printing the two sums are greater than or equal to 0. The sequence may be empty. Here is a possible interaction. Enter an integer:...

  • python programming X Canvas 4 → XCO Question 26 10 pts (SHORT ANSWER) Define a function...

    python programming X Canvas 4 → XCO Question 26 10 pts (SHORT ANSWER) Define a function named find_even_odd that has 1 parameter: num. When the function is called, it should use an if/else statement to determine whether the variable num is an even or odd number and display appropriate message as follows: • If the value in num is an even number, the progr will output the message: "XX is an EVEN number", where XX is the value in the...

  • Assignment: Using the Fork System Call The Collatz conjecture concerns what happens when we take any...

    Assignment: Using the Fork System Call The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorthm: n={n / 2 , if n is even 3 * n + 1 , if n is odd The conjecture states that when this algorithm is continually applied, all positive integers will eventually reach 1. For example, if n = 35, the sequence is:  35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2,...

  • From previous homework you are already familiar with the math function f defined on positive integers as f(x)=(3x+1)/2 if x is odd and f(x)=x/2 if x is even. Given any integer var, iteratively applying this function f allows you to produce a list of integ

    From previous homework you are already familiar with the math function f defined on positive integers as f(x)=(3x+1)/2 if x is odd and f(x)=x/2 if x is even. Given any integer var, iteratively applying this function f allows you to produce a list of integers starting from var and ending with 1. For example, when var is 6, this list of integers is 6,3,5,8,4,2,1, which has a length of 7 because this list contains 7 integers (call this list the Collatz list for 6). Write a C or C++...

  • c++ The Collatz Conjecture is a conjecture in mathematics that concerns a sequence sometimes known as...

    c++ The Collatz Conjecture is a conjecture in mathematics that concerns a sequence sometimes known as hailstone numbers. Given any positive integer n, the following term, n+1 is calculated as follows: If n is even, then n+1 is defined as n/2. If n is odd, then n+1 is defined as 3n + 1 The Collatz Conjecture states that, for any value of n, the sequence will always reach 1. Once the pattern reaches 1, it repeats indefinitely (3 * 1...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT