You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

12.2 Simple math recursion

2 min readjune 24, 2024

Recursive algorithms in mathematics break problems into smaller subproblems, solving them recursively until reaching a . This approach is key to and efficient problem-solving in computer science.

From factorials to sequences, recursion offers elegant solutions to complex mathematical problems. Understanding vs. , and considering efficiency, are crucial for implementing effective recursive algorithms in programming.

Recursive Algorithms in Mathematics

Recursive case vs base case

Top images from around the web for Recursive case vs base case
Top images from around the web for Recursive case vs base case
  • Recursive algorithms break down problems into smaller subproblems solved recursively until reaching a base case
  • Base case is the condition where recursion stops, typically the smallest possible input or a case with a known solution (0! = 1)
  • Prevents infinite recursion by providing a termination condition
  • Recursive case is the condition where the function calls itself with a modified input
  • Breaks down the problem into smaller subproblems progressing towards the base case (n! = n × (n-1)! for n > 0)
  • This approach is fundamental to mathematical induction, where a property is proven for a base case and then shown to hold for all cases

Factorial calculation with recursion

  • of a non-negative integer nn is the product of all positive integers less than or equal to nn (5!=5×4×3×2×1=1205! = 5 \times 4 \times 3 \times 2 \times 1 = 120)
  • Denoted as n!n!
  • Recursive definition of factorial:
    1. Base case: 0!=10! = 1
    2. Recursive case: n!=n×(n1)!n! = n \times (n-1)! for n>0n > 0
  • Recursive function to calculate factorial:
    [def](https://www.fiveableKeyTerm:def) factorial(n):
        if n == 0:
            [return](https://www.fiveableKeyTerm:Return) 1
        else:
            return n * factorial(n - 1)
    

Recursive functions for math problems

  • Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones (0, 1, 1, 2, 3, 5, 8, 13, ...)
  • Recursive definition:
    1. Base cases: F(0)=0F(0) = 0 and F(1)=1F(1) = 1
    2. Recursive case: F(n)=F(n1)+F(n2)F(n) = F(n-1) + F(n-2) for n>1n > 1
  • Recursive function to calculate the nth Fibonacci number:
    def fibonacci(n):
        if n <= 1:
            return n
        else:
            return fibonacci(n - 1) + fibonacci(n - 2)
    
  • Greatest common divisor (GCD) is the largest positive integer that divides each of the integers (gcd(48, 18) = 6)
  • Euclidean algorithm:
    1. Base case: gcd(a,0)=agcd(a, 0) = a
    2. Recursive case: gcd(a,b)=gcd(b,amodb)gcd(a, b) = gcd(b, a \mod b) for b0b \neq 0
  • Recursive function to calculate GCD:
    def gcd(a, b):
        if b == 0:
            return a
        else:
            return gcd(b, a % b)
    

Efficiency considerations in recursive algorithms

  • : Measure of how the execution time of an algorithm increases with input size
  • : Measure of the memory usage of an algorithm as input size grows
  • : A strategy where a problem is broken down into smaller subproblems, solved recursively, and then combined to solve the original problem
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary