Post

PythonPython

p
persis crystal

Posted on 27th February 2024|21 views

0
votes

Collatz Sequence Python

Can anyone give an example python program to test Collatz Conjecture for a given number?

Answers
P
sumana reddy

Posted on 27th February 2024

Here is the example.

def collatz(x):

    while x > 1:

        print(x, end=' ')

        if (x % 2):

        x = 3*x + 1

        else:

       x = x//2

    print(1, end='')

 x = int(input('Enter x: '))

print('Sequence: ', end='')

collatz(x)

 

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo
  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos