Using Python Recursion is the concept of a function calling itself until the problem is solved when the Base Case is met. Study Recursion: See slides in Modules, Practice Slides, lab12 and also Recursion is in chapter 9 of the textbook. Some examples of recursion are: compute [ factorial of a number, towers of Hanoi, fractals (as shown in the textbook), and many more]. Example: See slides Page 2 For this assignment we'll use Collatz Cojecture (see Wikipedia). Collatz Conjecture algorithm: Given a number n, first call to the function: f(n): In the function: if n == 1 return 1: if n is even then f(n/2), i.e. call self with the new value. else (n is odd) call self with the new value, f((n*3)+1) and repeat. All numbers eventually end up with 1. The program should test for the base case which is: if n == 1, in which case it returns to the caller with 1. This problem is perfect to demonstrate Recursion. Create a list with random numbers (you can just do this part manually) in the list as 1, 2, 3, 4, 5, 6 digit numbers, one or two of each. Call the function from a loop going through the list with the numbers. Program should print each new number and at the end print the numbers of recursive calls. Example: Please enter an integer: 11 11 34 17.0 52.0 26.0 13.0 40.0 20.0 10.0 5.0 16.0 8.0 4.0 2.0 1.0 num visits: 15 If you try this with 9, you'll see it will take more recursions.
Using Python Recursion is the concept of a function calling itself until the problem is solved when the Base Case is met. Study Recursion: See slides in Modules, Practice Slides, lab12 and also Recursion is in chapter 9 of the textbook. Some examples of recursion are: compute [ factorial of a number, towers of Hanoi, fractals (as shown in the textbook), and many more]. Example: See slides Page 2 For this assignment we'll use Collatz Cojecture (see Wikipedia). Collatz Conjecture algorithm: Given a number n, first call to the function: f(n): In the function: if n == 1 return 1: if n is even then f(n/2), i.e. call self with the new value. else (n is odd) call self with the new value, f((n*3)+1) and repeat. All numbers eventually end up with 1. The program should test for the base case which is: if n == 1, in which case it returns to the caller with 1. This problem is perfect to demonstrate Recursion. Create a list with random numbers (you can just do this part manually) in the list as 1, 2, 3, 4, 5, 6 digit numbers, one or two of each. Call the function from a loop going through the list with the numbers. Program should print each new number and at the end print the numbers of recursive calls. Example: Please enter an integer: 11 11 34 17.0 52.0 26.0 13.0 40.0 20.0 10.0 5.0 16.0 8.0 4.0 2.0 1.0 num visits: 15 If you try this with 9, you'll see it will take more recursions.
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
- Using Python
- Recursion is the concept of a function calling itself until the problem is solved when the Base Case is met.
- Study Recursion: See slides in Modules, Practice Slides, lab12 and also Recursion is in chapter 9 of the textbook.
- Some examples of recursion are: compute [ factorial of a number, towers of Hanoi, fractals (as shown in the textbook), and many more].
- Example: See slides Page 2
- For this assignment we'll use Collatz Cojecture (see Wikipedia).
- Collatz Conjecture
algorithm : - Given a number n, first call to the function: f(n):
- In the function:
- if n == 1 return 1:
- if n is even then f(n/2), i.e. call self with the new value.
- else (n is odd) call self with the new value, f((n*3)+1) and repeat. All numbers eventually end up with 1.
- The program should test for the base case which is: if n == 1, in which case it returns to the caller with 1.
- This problem is perfect to demonstrate Recursion.
Create a list with random numbers (you can just do this part manually) in the list as 1, 2, 3, 4, 5, 6 digit numbers, one or two of each. - Call the function from a loop going through the list with the numbers.
- Program should print each new number and at the end print the numbers of recursive calls.
- Example:
- Please enter an integer: 11
11
34
17.0
52.0
26.0
13.0
40.0
20.0
10.0
5.0
16.0
8.0
4.0
2.0
1.0 num visits: 15 - If you try this with 9, you'll see it will take more recursions.
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education