Registry Synced

Jan 2026 - Python - Week 9 - WEEK9-GrPA 3

862 words
4 min read

Graded Assignment

Course: Jan 2026 - Python
WEEK9-GrPA 3
Submission deadline has passed for this assignment
Due Apr 17, 2026 at 11:59 PM IST
Instructions
Use "Test Run" to verify your code with public test cases.
Press "Submit" to have your assignment evaluated.
You can submit your assignment multiple times up until the deadline.
Make sure to submit your final code by the deadline to receive your score.
Summary
100 out of100
Score
Public Tests
3/3 Passed
Submitted on Apr 17, 2026 at 5:36 AM IST
Private Tests
5/5 Passed
Submitted on Apr 17, 2026 at 5:36 AM IST

The Collatz function is defined for a positive integer nn as follows.
f(n)={3n+1if n is oddn2if n is evenf(n) = \begin{cases} 3n + 1 &\text{if } n \text{ is odd} \\ \cfrac{n}{2} &\text{if } n \text{ is even} \end{cases}
We consider the repeated application of the Collatz function starting with a given integer nn, which results in the following sequence:
f(n),f(f(n)),f(f(f(n))),f(n), f(f(n)), f(f(f(n))),\dots
It is conjectured that no matter which positive integer nn you start from, the sequence will always reach 11. For example, if n=10n=10, the sequence is:
.tg {border-collapse:collapse;border-spacing:0;} .tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;} .tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;} .tg .tg-0lax{text-align:left;vertical-align:top}
<table class="tg" style="color: rgb(28, 28, 40); font-size: 14px; border-collapse: collapse;" border="1"><thead style="color: rgb(28, 28, 40); font-size: 14px;"><tr style="color: rgb(28, 28, 40); font-size: 14px;"><th class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;"><span style="font-weight: normal; color: rgb(28, 28, 40); font-size: 14px;">Seq No.</span></th><th class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;">n</th><th class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;">f(n)</th></tr></thead><tbody style="color: rgb(28, 28, 40); font-size: 14px;"><tr style="color: rgb(28, 28, 40); font-size: 14px;"><td class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;">1</td><td class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;">10</td><td class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;">5</td></tr><tr style="color: rgb(28, 28, 40); font-size: 14px;"><td class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;">2</td><td class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;">5</td><td class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;">16</td></tr><tr style="color: rgb(28, 28, 40); font-size: 14px;"><td class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;">3</td><td class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;">16</td><td class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;">8</td></tr><tr style="color: rgb(28, 28, 40); font-size: 14px;"><td class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;">4</td><td class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;">8</td><td class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;">4</td></tr><tr style="color: rgb(28, 28, 40); font-size: 14px;"><td class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;">5</td><td class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;">4</td><td class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;">2</td></tr><tr style="color: rgb(28, 28, 40); font-size: 14px;"><td class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;">6</td><td class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;">2</td><td class="tg-0lax" style="color: rgb(28, 28, 40); font-size: 14px;">1</td></tr></tbody></table>
Thus, if you start from n=10n=10, you need to apply the function ff six times in order to first reach 1.

Write a recursive function named collatz that accepts a positive integer nn as argument, where 1<n32,0001 < n \leq 32,000, and returns the number of times ff has to be applied repeatedly in order to first reach 1.

You do not have to accept input from the user or print output to the console. You just have to write the function definition.

Public Tests ( 3/3 )

Case 1

Input:
text
public
7
Expected Output:
text
16
Actual Output:
text
16

Case 2

Input:
text
public
10
Expected Output:
text
6
Actual Output:
text
6

Case 3

Input:
text
public
101
Expected Output:
text
25
Actual Output:
text
25

Private Tests ( 5/5 )

Case 1

Input:
text
private
30000
Expected Output:
text
178

Good, your function is recursive
Actual Output:
text
178

Good, your function is recursive

Case 2

Input:
text
private
31999
Expected Output:
text
98

Good, your function is recursive
Actual Output:
text
98

Good, your function is recursive

Case 3

Input:
text
private
2463
Expected Output:
text
208

Good, your function is recursive
Actual Output:
text
208

Good, your function is recursive

Case 4

Input:
text
private
999
Expected Output:
text
49

Good, your function is recursive
Actual Output:
text
49

Good, your function is recursive

Case 5

Input:
text
private
125
Expected Output:
text
108

Good, your function is recursive
Actual Output:
text
108

Good, your function is recursive

💻 IITM Official Solution

python
def collatz(n):
    # base case of the recursion
    # if n = 1, you don't need to call the function at all
    if n == 1:
        return 0
    # simple application of the piecewise defn of the
    # Collatz function
    if n % 2 == 0:
        return 1 + collatz(n // 2)
    else:
        return 1 + collatz(3 * n + 1)


💻 My Submitted Code

python
def collatz(n):
    """
    A recursive function to compute the number of calls to the Collatz function of n

    Argument:
        n: integer
        Assume that 1 < n <= 32,000
    Returns:
        result: integer
    """
    # Base case
    if n == 1:
        return 0

    # Recursive case
    if n % 2 == 0:
        return 1 + collatz(n // 2)
    else:
        return 1 + collatz(3 * n + 1)

Document Outline
Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.