Python Forum
[split] The Collatz Sequence
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] The Collatz Sequence
#1
Hello,

This is my first time posting. Hope my question is clear and thank you in advance for everyone's advise.

I'm newbie to python and never expose to any programming whatsoever.

Took a while to figure out this collatz exercise - based on Automate the Boring Stuff (ABS).

Questions:
1.The code below gives me the result I want - based on what the output should be per ABS and after doing many hours of research on how others solve this (and tweak it to my style).
When the code prompt me to 'enter a number', I type 3 and i get the expected output.
However, I don't understand why the WHILE statements has to be !=1?
Since I type 3 anyway (assume user only type 3), I tried to change the statement to >2 or =3, but it gives me different result. I'm very confused. Huh Cry

2. Is it possible to use FOR loop instead of WHILE loop?
Try to do it myself, (just replace WHILE with FOR), and I got error. I also can't seem to find anyone else who use FOR loop in this exercise.

3. What's the difference between print and return? I thought I was just duplicating the code until I accidentally switch the order of print/return (i.e.return THEN print, instead of print THEN return), and the code doesn't work. Tried to do my own google research but i don't see anything very clear. Maybe it will help if you can please advise based on my code below (so i can relate to it)?

Again, thank you kindly.

print('enter a number')
def collatz(number):
    if int(number) %2==0:
        print(int(number)//2)
        return(int(number)//2)    
    else:
        print(3*int(number)+1)
        return(3*int(number)+1)
        
        
number=input()
[b]while int(number)!=1:[/b]
    number=collatz(number)
Reply
#2
You don't need to convert 'number' to integer every time you're using it. You only need to convert the user input to integer once when it is read
def collatz(number):
    if number %2==0:
        print(number//2)
        return(number//2)    
    else:
        print(3*number+1)
        return(3*number+1)
         
         
number=input('enter a number ')
number = int(number)
while number!=1:
    number=collatz(number)
valencia Wrote:I don't understand why the WHILE statements has to be !=1?
This is based on the conjecture that the Collatz sequence eventually becomes 1 whatever the initial value. We want to stop the program once it reaches this value. If you replace !=1 by >2 then the program will stop if number is equal to 1 or 2. If you replace by !=3, the program may never stop, for example the sequence 8 -> 4 -> 2 -> 1 -> 4… never reaches 3.
valencia Wrote:Is it possible to use FOR loop instead of WHILE loop?
The for loop is used to iterate over the elements of a python sequence such as a list, a tuple or any other iterable object. The syntax explicitly needs such an object: for number in some_sequence. You can't simply replace while by for, you need a python sequence. It can be done here by using a more advanced code that creates the Collatz sequence as a python object with the yield keyword
def collatz_sequence(number):
    if number == 1:
        return
    elif number %2==0:
        number = (number//2)    
    else:
        number = (3*number+1)         
    yield number

number=input('enter a number ')
number = int(number)
for number in collatz_sequence(number):
    print(number)
valencia Wrote:What's the difference between print and return?
print() sends information to the user by writing something in the console while return sends information to the program by terminating the current function's execution and returning a value that can be used at the place where the function was called. In your code this is line 13. The value returned by collatz() is used as the value in number=value at line 13.
Reply
#3
thank you kindly for the explanation.
it's clear now.

But the FOR function is not very clear. The suggested code using FOR do not return the result of the original code (using WHILE).
when i type 3, it only return 10 (instead of returning a sequence of number).
Does it mean it is not possible using FOR?

def collatz_sequence(number):
    if number == 1:
        return
    elif number %2==0:
        number = (number//2)    
    else:
        number = (3*number+1)         
    yield number
 
number=input('enter a number ')
number = int(number)
for number in collatz_sequence(number):
    print(number)
Reply
#4
Yes sorry I made a mistake, it should be
def collatz_sequence(number):
    while number != 1:
        if number %2==0:
            number = (number//2)    
        else:
            number = (3*number+1)
        yield number         
Reply
#5
Thank you.

Now i know FOR is not doable for this case.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Collatz-problem CaptainNeemo 2 2,158 Dec-07-2020, 04:09 PM
Last Post: deanhystad
  How can I found how many numbers are there in a Collatz Sequence that I found? cananb 5 3,715 Nov-23-2020, 05:15 PM
Last Post: cananb
  The Collatz Sequence Truman 11 14,034 Dec-27-2018, 11:28 PM
Last Post: Truman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020