Write a method in JAVA/C++ to solve the Syracuse Sequence using recursion. A Syracuse Sequence is a sequence that begins with a number n0 and each element ni of the sequence is ni-1/2 if is ni-1 even and 3*ni-1+1 otherwise. You can write the method in a .docx file (no source code required). Also write the base, and induction clause. Draw the Activation Record (AR) diagram for the first six elements of the series when it starts with 7 (the value of n0).
CODE IN C++:
#include <iostream>
using namespace std;
int syracuseSequence(int n){
cout << " " <<n <<" ";
if(n==1)
return 1;
if(n%2==0)
return syracuseSequence(n/2);
return syracuseSequence(3*n+1);
}
int main()
{
int n;
cout << "Enter a number:";
cin >> n ;
cout<<"Following is the corresponding
syracuseSequence:"<<endl;
syracuseSequence(n);
}
OUTPUT:
Get Answers For Free
Most questions answered within 1 hours.