How to resolve the algorithm Sudan function step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Sudan function step by step in the C++ programming language

Table of Contents

Problem Statement

The Sudan function is a classic example of a recursive function, notable especially because it is not a primitive recursive function. This is also true of the better-known Ackermann function. The Sudan function was the first function having this property to be published. The Sudan function is usually defined as follows (svg):

F

0

( x , y )

= x + y

F

n + 1

( x , 0 )

= x

if

n ≥ 0

F

n + 1

( x , y + 1 )

=

F

n

(

F

n + 1

( x , y ) ,

F

n + 1

( x , y ) + y + 1 )

if

n ≥ 0

{\displaystyle {\begin{array}{lll}F_{0}(x,y)&=x+y\F_{n+1}(x,0)&=x&{\text{if }}n\geq 0\F_{n+1}(x,y+1)&=F_{n}(F_{n+1}(x,y),F_{n+1}(x,y)+y+1)&{\text{if }}n\geq 0\\end{array}}}

Write a function which returns the value of F(x, y).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sudan function step by step in the C++ programming language

The provided C++ code defines a recursive function F and demonstrates its usage in the main function. Here's how the code works:

  • Function F(int n, int x, int y):

    • This is a recursive function that takes three integer arguments: n, x, and y.
    • It uses if-else statements to handle different cases:
      • If n is equal to 0, it returns the sum of x and y.
      • If y is equal to 0, it returns x.
      • Otherwise, it makes two recursive calls to itself, one with y decremented by 1 and the other with both x and y decremented by 1. It then returns the result of the second recursive call plus y.
  • main Function:

    • In the main function, the program calls F(1, 3, 3) and prints the result.

Example Execution:

When you run this program, it will calculate and print the value of F(1, 3, 3).

  • The recursive calls will proceed as follows:
    • F(1, 3, 3) -> F(0, F(1, 3, 2), F(1, 3, 2) + 2) -> F(0, F(1, 3, 1), F(1, 3, 1) + 1) -> ...
  • Eventually, it will reach the base case where n or y becomes 0 and will start returning values:
    • F(0, 6, 6) -> 6
    • F(1, 6, 5) -> 11
    • F(1, 11, 4) -> 15
  • The final result will be: F(1, 3, 3) -> 15
  • Therefore, it prints: F(1,3,3) = 15

Source code in the cpp programming language

//Aamrun , 11th July, 2022

#include <iostream>
using namespace std;

int F(int n,int x,int y) {
  if (n == 0) {
    return x + y;
  }
 
  else if (y == 0) {
    return x;
  }
 
  return F(n - 1, F(n, x, y - 1), F(n, x, y - 1) + y);
}

int main() {
  cout << "F(1,3,3) = "<<F(1,3,3)<<endl;
  return 0;
}


  

You may also check:How to resolve the algorithm Doubly-linked list/Definition step by step in the Oforth programming language
You may also check:How to resolve the algorithm Take notes on the command line step by step in the HicEst programming language
You may also check:How to resolve the algorithm Colour pinstripe/Display step by step in the Ada programming language
You may also check:How to resolve the algorithm Anagrams step by step in the BQN programming language
You may also check:How to resolve the algorithm Terminal control/Hiding the cursor step by step in the Furor programming language