Bottom Page

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)
Quote
#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.
Quote
#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)
Quote
#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         
Quote
#5
Thank you.

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

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  The Collatz Sequence Truman 11 1,340 Dec-27-2018, 11:28 PM
Last Post: Truman
  Basic Python Program for Sequence patycanes 7 2,967 Jul-15-2018, 08:53 PM
Last Post: ichabod801
  ValueError: setting an array element with a sequence ying9309 2 2,152 Jul-15-2018, 07:04 AM
Last Post: ying9309
  TypeError: can't multiply sequence by non-int of type 'complex' FeverDream 2 695 Jul-12-2018, 11:08 PM
Last Post: DeaD_EyE
  [split] [split] regular expression kerzol81 1 413 Jun-24-2018, 09:18 AM
Last Post: volcano63
  TypeError: can't multiply sequence by non-int of type 'str' rmpadilla73 4 8,914 May-28-2018, 12:21 AM
Last Post: py_learner
  Linear Fibonacci sequence Tawnwen 1 558 Feb-27-2018, 08:01 AM
Last Post: nilamo
  TypeError: can't multiply sequence by non-int of type 'str' Beatenberg 12 4,018 Oct-10-2017, 10:14 PM
Last Post: Beatenberg
  Help with fibonacci sequence masonmoore93 2 2,523 Oct-01-2017, 12:56 AM
Last Post: masonmoore93
  Fibonacci sequence logic 3 1,440 Jun-11-2017, 08:23 PM
Last Post: Ofnuts

Forum Jump:


Users browsing this thread: 1 Guest(s)