Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C++: The Syracuse Sequence The Syracuse Sequence is a sequence of numbers that can be generated with an algorithm that takes an input number

In C++:

The Syracuse Sequence

The Syracuse Sequence is a sequence of numbers that can be generated with an algorithm that takes an input number n. It is named after Syracuse, NY, where it was originally studied. The function is part of a general class known as the '3n + 1' functions.

Here is the definition. The function starts with an input number, n, greater than or equal to 1.

If n is 1, the sequence stops.

If n is even, the next number is n / 2.

Else, n is odd and the next number is (3n + 1) / 2.

For example, if we start with n = 5, the sequence is:

5, 8, 4, 2, 1

Starting with n = 6, it will go a little longer:

6, 3, 5, 8, 4, 2, 1

One interesting aspect of this sequence: it appears that for any starting number, the sequence will always reach 1 (and stop), although this has not been proven. Some numbers (like 27) take many steps to reach 1.

Assignment

Write a recursive function syracuse() that accepts as input:

An integer for n

Another integer to specify the limit of the number of terms calculated. If the limit is reached, the sequence should stop.

Add any other parameters at your option. As with any recursive function, with a very deep sequence of function calls, the system will eventually run out of resources. The limit is useful to stop your program so that this does not happen.

Then, make your program output the following:

The input number 'n' and result of each call to syracuse(n)

Whether the sequence was aborted early because of the limit

The highest number reached

If the number of function calls to syracuse() reaches the limit, the program should stop printing any more numbers at that point.

Other requirements:

No global variables here! Use parameters to your syracuse() function.

Validate that the input number and limit are each greater than 1, looping with an error message and prompting until the input is valid (you can do this in main() before calling the function).

No need to loop for additional sequences. Your program can run once and exit.

Use a consistent C++ style in your program according to the guidelines.

Example Output (two separate runs)

Number: 5 Limit of terms: 10 Input Number syracuse(n) ------------------------------- 5 8 8 4 4 2 2 1 Highest number reached: 8 Number: 19 Limit of terms: 5 Input Number syracuse(n) ------------------------------- 19 29 29 44 44 22 22 11 11 17 Sequence was aborted early. Highest number reached: 44

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Assess three steps in the selection process.

Answered: 1 week ago

Question

Identify the steps in job analysis.

Answered: 1 week ago