Question

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def collatz(n):
    # Looping till n becomes 1
    while(not n==1):
        # Checking if n is even
        if(n%2==0):
            # Making n to its half
            n=n//2
        # Checking if n is even
        else:
            # Changing value of n to 2n+1
            n=3*n+1
        # Printing value of n
        print(n)

# Testing
def main():
    # Reading input from user
    n = int(input("Please enter a positive integer: "))
    # Making call to collatz function
    collatz(n)
    
main()

Output

positive integer: 10 Please enter a 16 2 1 Process fini shed with exit code o LC

Add a comment
Know the answer?
Add Answer to:
Language is Python 3, please include comments Question 1 Write function collatz, that takes a positive...
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
  • 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...

  • 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...

  • 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....

  • 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...

  • IN PYTHON 1.Choose a positive integer 2. To get the next number in the sequence we...

    IN PYTHON 1.Choose a positive integer 2. To get the next number in the sequence we do the following: If the integer is odd, we multiply by 3 and add 1. If the integer is even, we divide by 2. It is hypothesized that the above sequence will always converge to the value of 1, regardless of any valid initial choice. This hypothesis is known as the Collatz Conjecture. For example, if we start at 5, the numbers generated by...

  • Write code in Python: explain with comments Starting with two one-digit positive integers a and b,...

    Write code in Python: explain with comments Starting with two one-digit positive integers a and b, consider the sequence in which the next number is the digit in the ones place of the sum of the previous two numbers. For example, if a = 1 and b = 1, the sequence is 1, 1, 2, 3, 5, 8, 3, 1, 4, 5, 9, 4, 3, 7, 0, … Write a function mystery(a, b) that returns the length of the sequence...

  • Write code in Python: explain with comments Starting with two one-digit positive integers a and b,...

    Write code in Python: explain with comments Starting with two one-digit positive integers a and b, consider the sequence in which the next number is the digit in the ones place of the sum of the previous two numbers. For example, if a = 1 and b = 1, the sequence is 1, 1, 2, 3, 5, 8, 3, 1, 4, 5, 9, 4, 3, 7, 0, … Write a function mystery(a, b) that returns the length of the sequence...

  • Question 3 Program Language C++ Problem 3 Fibonacci Numbers 10 points Fibonacci numbers are a sequence...

    Question 3 Program Language C++ Problem 3 Fibonacci Numbers 10 points Fibonacci numbers are a sequence of numbers where each number is represented by the sum of the two preceding numbers, starting with 0 and 1: 0, 1, 1, 2, 3, 5, 8, etc Write a program that repeatedly prompts the user for a positive integer and prints out whether or not that integer is a Fibonacci number. The program terminates when-I is entered. Create a method with the following...

  • Please use python 3 programming language Write a function that gets a string representing a file...

    Please use python 3 programming language Write a function that gets a string representing a file name and a list. The function writes the content of the list to the file. Each item in the list is written on one line. Name the function WriteList. If all goes well the function returns true, otherwise it returns false. Write another function, RandomRange that takes an integer then it returns a list of length n, where n is an integer passed as...

  • The Collatz conjecture concerns what happens when we take any positive integer n and apply the...

    The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorithm: if n is even 3 xn+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 1S 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4,2,'1 Write a C program using the fork) system call that generates this sequence in the...

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