Question

In: Computer Science

Please follow the instructions and solve it by C++. Thank you! What to Submit Submit the...

Please follow the instructions and solve it by C++. Thank you!

What to Submit

Submit the following:

1) Your .cpp file of the solution.

2) For what n value (approximately) does your computer stop producing output? Why is this? Enter your answer in the 'Comments' field when you submit the file.  

So far in our study of recursion we identified a few common recursive number sequences, such as the Fibonacci numbers. Number sequences like this are not recursive algorithms themselves -- their definitions are ways of generating the "nth term", rather than ways of breaking down a larger problem -- but they can serve as good examples of the practical limits of recursion.

Some sequences are notoriously hard to compute (with recursion, at least) because of the deep layers required, as we will see in this week's lab.

Golomb's sequence is known as "self describing" -- it seems to define itself. It is defined as the sequence of natural numbers such that n appears exactly G(n) times in the sequence. Here are the first few values of G(n) for some n:

n 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
G(n) 1 2 2 3 3 4 4 4 5 5 5 6 6 6 6

The sequence starts at 1, with G(1) = 1.

It's not too hard if you step through it one term at a time.

For n = 1, G(n) is 1. If you look at the sequence (the entire sequence G(n) all the way to infinity -- it seems to "know" about itself), 1 appears only 1 time.

For n = 2, G(n) is 2. Looking again at the G(n) sequence, the number 2 appears 2 times.

For n = 3, G(n) is 2. You can see that 3 appears 2 times.

For n = 4, G(n) is 3. ... and 4 appears 3 times.

And so on and so forth.  

Assignment

Write a C++ program that recursively computes each term of G(n) up to and including a given n. You can "hard code" the input value (the program does not need to accept any input) but you will need to set it sufficiently high so you can determine your answer for the second part below -- you may need to experiment with different values.

The formula is:

G(n) = 1 + G(n - G(G(n - 1)))

Remember to think about the base case.

Example Output

The output of your program does not need to exactly match this, but please make sure each term is output on a separate line along with the n for that term.

For n = 15:

1: 1
2: 2
3: 2
4: 3
5: 3
6: 4
7: 4
8: 4
9: 5
10: 5
11: 5
12: 6
13: 6
14: 6
15: 6

Solutions

Expert Solution

using a recursive solution, the formula gives result upto 89.
This is because as the recursion goes deeper, the compiler has to store an activation record for every recursion call and at a stage, the memory of the activation gets eaten up. This causes segmentation error and the program crashes.
However, this can be improved by using arrays instead of recursion. This is called as memoisation. What we do here is store a few values for the initial numbers and instead of function calls, we refer the number in the array. This solves the problem associated with recursion, but this also has a problem with the memory. An array cannot hold more than 10^7 elements. Therefore the highest possible number for which the value can be retrieved is 222222.

Code:

Using Recursion:

#include<iostream>
using namespace std;
unsigned long long solve (unsigned long long int n)
{
   if(n == 1) return 1;
   if(n == 2 || n == 3) return 2;
   return (1 + solve( n - solve( solve( n - 1 ) ) ) );
}
int main()
{
   cout<<"Enter n"<<endl;
   unsigned long long i, n;cin >> n;
   for(i = 1 ; i <= n ; i++)
   {
       cout << i << ": " << solve(i) << endl;
   }
   return 0;
}

Using Arrays:

#include<iostream>
using namespace std;
unsigned long long solve (unsigned long long int n)
{
   if(n == 1) return 1;
   if(n == 2 || n == 3) return 2;
   return (1 + solve( n - solve( solve( n - 1 ) ) ) );
}
int main()
{
   unsigned long long a[222223], i, n;a[1] = 1;a[2] = 2;a[3] = 2;
   cout<<"Enter n"<<endl;cin>>n;
   for(i = 1 ; i <= n ; i++)
   {
      
       a[i] = 1 + a[i - a[a[i - 1]]];
   }
   for(i = 4 ; i <= n ; i++)
   {
       cout << i << ": " << a[i] << endl;
   }
   return 0;
}

Output:

Using recursion:

Using Arrays:


Related Solutions

Please follow the instructions and solve it by c++ Close the project and create a new...
Please follow the instructions and solve it by c++ Close the project and create a new one called 'Lab0-Part3' with the same options as in the previous step. Write a C++ program that does the following: Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1 and 100 inclusive Then, creates two more 100-element arrays, one holding odd values and the other holding even values. Prints both of the new arrays to the console.
NOTE: Please provide instructions on how to solve it in excel add screenshots Thank you Suppose...
NOTE: Please provide instructions on how to solve it in excel add screenshots Thank you Suppose a random sample of 137 households in Chicago was taken as part of a study on annual household spending for food at home. The sample data is below. For the sample data, compute the mean and the median and construct a box and whisker plot. (Use Excel to Descriptive statistics and copy paste the screenshot here). Are the data skewed or symmetric? Approximately what...
C++ Follow Instructions Please By placing a break in the IIF statement you are using it...
C++ Follow Instructions Please By placing a break in the IIF statement you are using it to force an exit from the WHILE loop. The break statement is only allowed with the SELECT/CASE statement. Also, don't use one structure to force an exit from another. Use the logic from the WHILE loop to end by removing the IF with the break #include <iostream> using namespace std; bool IsMultiple(int,int); int main() { char choice; int n1,n2; bool result; int test =...
Please solve questions in C++ ASAP!! thank you (a) Write a function in C++ called readNumbers()...
Please solve questions in C++ ASAP!! thank you (a) Write a function in C++ called readNumbers() to read data into an array from a file. Function should have the following parameters: (1) a reference to an ifstream object (2) the number of rows in the file (3) a pointer to an array of integers The function returns the number of values read into the array. It stops reading if it encounters a negative number or if the number of rows...
Please solve A, B, and C. Please use excel. Please show work. Thank you. A. Use...
Please solve A, B, and C. Please use excel. Please show work. Thank you. A. Use the stocks of Apple, SAP, IBM, Oracle, and Amazon Download the historical data of weekly stock prices and S&P 500 index prices from year 2017-2019 on the website of yahoo finance and save it on an excel file. B. Use a different sheet to save the market adjusted prices of Apple, SAP, IBM, Oracle, and Amazon t and the index. For each stock, compute...
Instructions: 1. Solve the following problems in a MATLAB script. You will be allowed to submit...
Instructions: 1. Solve the following problems in a MATLAB script. You will be allowed to submit only one script, please work all the problems in the same script (Hint: careful with variables names) 2. Show your work and make comments in the same script (Use %). Please write your name in the first line of the scrip with % 3. It is OK to work with other students, but what you submit should be your own work – not something...
Instructions: 1. Solve the following problems in a MATLAB script. You will be allowed to submit...
Instructions: 1. Solve the following problems in a MATLAB script. You will be allowed to submit only one script, please work all the problems in the same script (Hint: careful with variables names) 2. Show your work and make comments in the same script (Use %). Please write your name in the first line of the scrip with % 3. It is OK to work with other students, but what you submit should be your own work – not something...
Instructions: 1. Please use only C as the language of programming. 2. Please submit the following:...
Instructions: 1. Please use only C as the language of programming. 2. Please submit the following: (1) the client and the server source files each (2) a brief Readme le that shows the usage of the program. 3. Please appropriately comment your program and name all the identifiers suitable, to enable enhanced readability of the code. Problem: Write an ftp client and an ftp server such that the client sends a request to ftp server for downloading a file. The...
using C thank you Must submit as MS Word file with a screenshot of the 3...
using C thank you Must submit as MS Word file with a screenshot of the 3 outputs. Run your program 3 times. the output must be in a screenshot not typed for the each time you run the program. thank you Modify the code below to implement the program that will sum up 1000 numbers using 5 threads. 1st thread will sum up numbers from 1-200 2nd thread will sum up numbers from 201 - 400 ... 5th thread will...
I need to update this program to follow the these requirements please and thank you :...
I need to update this program to follow the these requirements please and thank you : Do not use packages Every class but the main routine class must have a toString method . The toString method for Quadrilateral should display its fields: 4 Points. All instance variables should be declared explicitly private . Area incorrect : enter points no: 1 1 3 enter points no: 2 6 3 enter points no: 3 0 0 enter points no: 4 5 0...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT