Write programs to find (using recursions) 1. Factorial of a number n (This is an example of using recursion) 2. Fibonacci numbers The program demonstrates the concept of Fibonacci numbers. You must convert it into a recursive function 3. Xn 4. Consecutive sum of n numbers 5. Consecutive sum of n odd numbers 6. The sum of the following sequence: 1 2 + 22 + 32 + ... +n2 7. Find GCD of two numbers
Question
Write programs to find (using recursions)
- Factorial of a number n (This is an example of using recursion)
- Fibonacci numbers The program demonstrates the concept of Fibonacci numbers. You must convert it into a recursive function
- Xn
- Consecutive sum of n numbers
- Consecutive sum of n odd numbers
- The sum of the following sequence: 1 2 + 22
- 32
- ... +n2
- Find GCD of two numbers
Solution
Sure, here are the Python programs for each of your requests:
- Factorial of a number n:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
- Fibonacci numbers:
def fibonacci(n):
if n <= 1:
return n
else:
return(fibonacci(n-1) + fibonacci(n-2))
- X^n:
def power(x, n):
if n == 0:
return 1
else:
return x * power(x, n-1)
- Consecutive sum of n numbers:
def sum_n(n):
if n == 0:
return 0
else:
return n + sum_n(n-1)
- Consecutive sum of n odd numbers:
def sum_odd(n):
if n == 0:
return 0
else:
return (2*n-1) + sum_odd(n-1)
- The sum of the following sequence: 1^2, 2^2, 3^2, ..., n^2:
def sum_squares(n):
if n == 0:
return 0
else:
return n**2 + sum_squares(n-1)
- Find GCD of two numbers:
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
Please note that these recursive functions are not the most efficient way to solve these problems, especially for large inputs, due to the overhead of function calls and the risk of exceeding the maximum recursion depth. Iterative solutions would be more efficient in such cases.
Similar Questions
Write a program to calculate the factorial of N.
Write a program in C to find the GCD of two numbers using recursio
You have been given a series that follows a specific pattern. The odd-indexed terms, i.e. the 1st, 3rd, 5th, and so on, form a Fibonacci sequence. The even-indexed terms, i.e. the 2nd, 4th, 6th, and so on, are prime numbers arranged in ascending order. The series begins with the numbers 1, 2, 1, 3, 2, 5, 3, 7, 5, 11, 8, 13, 17, and so on.Your task is to write a program that can find the nth term in this series. The program should use control flow statements to alternately generate Fibonacci and prime numbers, depending on whether the term's index is odd or even.
Problem StatementHelen is developing a program for a gaming application that involves generating a sequence of mystical numbers based on the Tribonacci series. She needs to implement a recursive function tribonacci to determine the Tribonacci numbers for various stages of the quest. Write a program to achieve her task.The Tribonacci series is a sequence of numbers defined as the sum of the three preceding terms. 0, 1, 1, 2, 4, 7, 13, and so on.Input format :The input consists of a positive integer n.Output format :The output displays the n terms in the Tribonacci series, separated by a space.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:1 ≤ n ≤ 20Sample test cases :Input 1 :2Output 1 :0 1 Input 2 :9Output 2 :0 1 1 2 4 7 13 24 44 Input 3 :18Output 3 :0 1 1 2 4 7 13 24 44 81 149 27
Write a program that evaluates thefactorials of the integers from 1 to 5.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.