Phase 3 - Level 2: Advanced Recursion
Master advanced recursive patterns - digit operations, GCD, binary conversion.
10 programs · open one, then the next
Start with Count Digits Recursively- 01Count Digits RecursivelyIntermediateCount integer digits using recursive floor division. Students trace one count per decimal position and separate zero input from the helper's empty remainder.Open example →pydef count_digits(n): # Base case if n == 0: return 0
- 02Reverse Number RecursivelyIntermediateReverse an integer by carrying a partial result through recursive calls. Students trace a shrinking argument, an expanding accumulator, and sign restoration.Open example →pydef reverse_number(n, rev=0): # Base case if n == 0: return rev
- 03Check Palindrome RecursivelyIntermediateCheck numeric palindromes with a recursive reverse helper. Students compare an unchanged magnitude with an accumulator result and trace the stopping case.Open example →pydef reverse_number(n, rev=0): if n == 0: return rev last_digit = n % 10
- 04Product of Digits RecursivelyIntermediateMultiply digits with a recursive helper. Students distinguish the empty-product base case from a literal zero digit and trace short-circuiting on internal zeros.Open example →pydef product_digits(n): # Base case if n == 0: return 1
- 05GCD RecursivelyIntermediateFind a GCD with Euclid's recursive algorithm. Students trace remainders, argument swapping, absolute values, and a zero second argument as the base case.Open example →pydef gcd(a, b): # Base case if b == 0: return a
- 06Convert Number to Binary RecursivelyIntermediateBuild a binary representation recursively. Students divide by two, append remainder bits on return, and supply a visible zero for an empty base result.Open example →pydef to_binary(n): # Base case if n == 0: return ""
- 07Print Digits in WordsIntermediatePrint each decimal digit as an English word using recursion. Students delay output until smaller prefixes return and handle zero as a complete input separately.Open example →pydef print_digit_word(digit): words = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"] return words[digit]
- 08Sum of First N Even Numbers RecursivelyIntermediateSum the first n positive even numbers with recursion. Students distinguish a term count from an upper limit and trace two-times-n additions to the base case.Open example →pydef sum_even(n): # Base case if n == 0: return 0
- 09Sum of First N Odd Numbers RecursivelyIntermediateSum the first n positive odd numbers through recursion. Students trace the term formula, a zero-count base case, and unfinished additions on the call stack.Open example →pydef sum_odd(n): # Base case if n == 0: return 0
- 10nCr Recursively (Pascal Formula)AdvancedCompute combinations with Pascal's recursive formula. Students trace two smaller subproblems, edge entries equal to one, and repeated work in the call tree.Open example →pydef ncr(n, r): # Base cases if r == 0 or r == n: return 1