logo-MindMajix
PostPythonPython
p
persis crystal

Posted on 23rd July 2024|25 views

0
votes

Collatz Sequence Python

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

1 answers
Answers
P
sumana reddy

Posted on 23rd July 2024| views

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
Name
Course *
Email *
Phone Number