📖 Scenario: Imagine you want to calculate the factorial of a number. Factorial means multiplying a number by all the numbers below it down to 1. For example, 5 factorial (written as 5!) is 5 x 4 x 3 x 2 x 1 = 120.We will learn how to write a recursive function in R to calculate factorials. Recursive means the function calls itself with a smaller number until it reaches 1.
🎯 Goal: Build a recursive function in R called factorial that calculates the factorial of a given number.
📋 What You'll Learn
Create a variable with a number to calculate factorial for
Create a base case variable to stop recursion
Write a recursive function named factorial that calls itself
Print the factorial result
💡 Why This Matters
🌍 Real World
Recursive functions are useful in many areas like calculating factorials, navigating file systems, or solving puzzles like the Tower of Hanoi.
💼 Career
Understanding recursion helps in programming jobs that require problem-solving skills and working with algorithms, especially in software development and data science.
Progress0 / 4 steps
1
Create a variable with the number to calculate factorial
Create a variable called num and set it to 5.
R Programming
Hint
Use the assignment operator <- to set num to 5.
2
Create a base case variable for recursion
Create a variable called base_case and set it to 1 to stop the recursion when the number reaches 1.
R Programming
Hint
The base case is the smallest number where recursion stops. For factorial, it is 1.
3
Write the recursive factorial function
Write a function called factorial that takes a parameter n. If n equals base_case, return 1. Otherwise, return n multiplied by factorial(n - 1).
R Programming
Hint
Use an if statement to check if n is the base case. Use recursion by calling factorial(n - 1).
4
Print the factorial of the number
Use print() to display the result of factorial(num).
R Programming
Hint
Call factorial(num) inside print() to show the result.
Practice
(1/5)
1. What is the main purpose of a base case in a recursive function?
easy
A. To stop the recursion and prevent infinite calls
B. To call the function again with the same input
C. To increase the input size for the next call
D. To print the result of the recursion
Solution
Step 1: Understand recursion basics
A recursive function calls itself repeatedly with smaller inputs.
Step 2: Identify the role of base case
The base case stops the recursion to avoid infinite loops and eventual crash.
Final Answer:
To stop the recursion and prevent infinite calls -> Option A
Quick Check:
Base case = stop recursion [OK]
Hint: Base case always stops recursion to avoid infinite loops [OK]
Common Mistakes:
Forgetting the base case causes infinite recursion
Thinking base case calls function again
Confusing base case with recursive step
2. Which of the following is the correct syntax to define a recursive function fact in R that calculates factorial?
easy
A. fact <- function(n) { repeat n * fact(n - 1) }
B. fact <- function(n) { while(n > 0) n * fact(n - 1) }
C. fact <- function(n) { if(n == 1) n + fact(n - 1) }
D. fact <- function(n) { if(n == 0) 1 else n * fact(n - 1) }
Solution
Step 1: Check base case and recursive call
fact <- function(n) { if(n == 0) 1 else n * fact(n - 1) } correctly uses if condition for base case (n == 0) and recursive call with n-1.
Step 2: Identify syntax errors in other options
Options B, C, and D misuse loops or have wrong base case or missing else.
Final Answer:
fact <- function(n) { if(n == 0) 1 else n * fact(n - 1) } -> Option D
Quick Check:
Correct base case and recursive call syntax [OK]
Hint: Base case uses if, recursive call uses else with smaller input [OK]
A. The recursive call uses the same n, causing infinite recursion
B. The function should use a loop instead
C. The base case is missing
D. The return statement is outside the function
Solution
Step 1: Check recursive call argument
The function calls sum_to_n(n) instead of sum_to_n(n-1), so input never decreases.
Step 2: Understand effect of error
This causes infinite recursion because n stays the same and base case is never reached.
Final Answer:
The recursive call uses the same n, causing infinite recursion -> Option A
Quick Check:
Recursive call must reduce input [OK]
Hint: Recursive call must use smaller input to reach base case [OK]
Common Mistakes:
Calling function with same argument
Missing base case
Confusing recursion with loops
5. Write a recursive R function flatten_list that takes a nested list of numbers and returns a flat vector of all numbers. What is the correct approach?
hard
A. Use recursion but forget to combine results from recursive calls
B. Check if input is a list; if yes, recursively flatten each element and combine; else return the element as vector
C. Return the input directly without any processing
D. Use a for loop to iterate and append elements without recursion
Solution
Step 1: Identify recursive structure
Nested lists require checking each element; if it's a list, call flatten_list recursively.
Step 2: Combine results properly
Combine all flattened elements into one vector to get a flat list of numbers.
Final Answer:
Check if input is a list; if yes, recursively flatten each element and combine; else return the element as vector -> Option B
Quick Check:
Recursion + combine results = flatten list [OK]
Hint: Recursively flatten each list element and combine results [OK]
Common Mistakes:
Not combining recursive results
Forgetting to check if element is a list
Using loops only without recursion
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.