Recursion
Recursion is a very important concept in computer science.
Programs:
Factorial
The factorial of a positive number n is given by:
The factorial of a negative number doesn't exist.
The factorial of 0 is 1.
Fibonacci
The Fibonacci sequence is a sequence of numbers where the first two numbers are 0 and 1.
The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..
In mathematical terms,
the sequence Fn of Fibonacci numbers is defined by the recurrence relation
with seed values
G.C.D
Greatest Common Divisor (G.C.D) is the largest number that divides both the given numbers without a remainder.
A simple and old approach is Euclidean algorithm by subtraction
It is is a process of repeat subtraction, carrying the result forward each time until the result is equal to the any one number being subtracted. If the answer is greater than 1, there is a GCD (besides 1). If the answer is 1, there is no common divisor (besides 1), and so both numbers are coprimes
pseudo code for above approach:
At some point one number becomes factor of the other so instead of repeatedly subtracting till both become equal , we check if it is factor of the other .
Tower of Hanoi
Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
Last updated
Was this helpful?