Concept explainers
Ackermann’s Function
Ackermann’s Function is a recursive mathematical
If m = 0 then return n + 1
If n = 0 then return A(m−1, 1)
Otherwise, return A(m−1, A(m, n−1))
Test your function in a driver
A(0, 0) A(0, 1) A(1, 1) A(1, 2) A(1, 3) A(2, 2) A(3, 2)
Want to see the full answer?
Check out a sample textbook solutionChapter 20 Solutions
MyLab Programming with Pearson eText -- Access Card -- for Starting Out with C++ from Control Structures to Objects (My Programming Lab)
Additional Engineering Textbook Solutions
Database Concepts (7th Edition)
Software Engineering (10th Edition)
Introduction to Programming Using Visual Basic (10th Edition)
Starting Out with Java: From Control Structures through Objects (6th Edition)
Starting Out with Python (4th Edition)
Starting Out with C++: Early Objects (9th Edition)
- is it correct ? Write a recursive function that returns the nth number in a fibonacci sequence when n is passed to function. The fibonacci sequence is like 0,1,1,2,3,5,8,13...... Answer: #include <iostream> using namespace std; int getFibonacci(int n) { if (n == 0 || n == 1) return n; else return getFibonacci(n - 1) + getFibonacci(n - 2); } int main() { int n = 7; int result = getFibonacci(n); cout << result; }arrow_forwardWrite a recursive function to implement the recursive algorithm (multiplying two positive integers using repeated addition). Also, write a program to test your function.arrow_forwardThe first examples of recursion are the mathematical functions factorial and fibonacci. These functions are defined for non-negative integers using the following recursive formulas:factorial(0) = 1factorial(N) = N*factorial(N-1) for N > 0fibonacci(0) = 1fibonacci(1) = 1fibonacci(N) = fibonacci(N-1) + fibonacci(N-2) for N > 1Write recursive functions to compute factorial(N) and fibonacci(N) for a given non-negative integer N, and write a main() routine to test your functions.(In fact, factorial and fibonacci are really not very good examples of recursion, since the most natural way to compute them is to use simple for loops. Furthermore, fibonacci is a particularly bad example, since the natural recursive approach to computing this function is extremely inefficient.)arrow_forward
- Write a recursive fibonacci (n) function with an expression body. The function should return an Int; it will be too slow to deal with inputs whose fibonacci numbers are too large anyway. You do *not* need to use memoization. Note that: fibonacci(0) = 0 fibonacci(1) = 1 fibonacci(n, where n is greater than 1) = fibonacci(n-2) + fibonacci(n - 1)arrow_forwardWrite a recursive function to implement the recursive algorithm (determining the number of ways to select a set of things from a given set of things). Also, write a program to test your function.arrow_forwardWhat type of recursion is used in the following function? int f(int n){ if (n==1) return 1; else return n+f(n-1); } Tail recursion Multiple recursion Indirect recursion Non-tail recursionarrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage