PROGRAMMING     CLIMBING

  minte9
reminder




Factorial

p 110 The definition of the factorial number is that 0! is 1, and n! = n(n-1)!
 
# Factorial definition: 
# 
# 0! is 1
# n! = n(n-1)!
#
# 3! is 3 times 2!, which is 2 times 1!, which is 1 times 0!.
# 3! = 3 * 2! = 3 * 2 * 1! = 3 * 2 * 1 * 0! = 6

def factorial(n):
    
    if (n == 0): return 1

    n = n * factorial(n-1)
    return n

assert factorial(3) == 6
assert factorial(0) == 1
assert factorial(4) == 24

Fibonacci

p 113 Each number is the sum of the two preceding ones.
 
# Fibonacci sequence:
#
#    fb(0) = 0
#    fb(1) = 1
#    fb(n) = fb(n-1) + fb(n-2)
#
# Each number is the sum of the two preceding ones.

def fibonacci(n):

    if (n==0): return 0
    if (n==1): return 1

    return fibonacci(n-1) + fibonacci(n-2)

assert fibonacci(0) == 0
assert fibonacci(1) == 1
assert fibonacci(2) == 1
assert fibonacci(3) == 2
assert fibonacci(4) == 3
assert fibonacci(5) == 5

Questions     github Functions
Modulus
        A B C D E F
🔔
1/1