Question

c++ The Collatz Conjecture is a conjecture in mathematics that concerns a sequence sometimes known as...

c++

The Collatz Conjecture is a conjecture in mathematics that concerns a sequence sometimes known as hailstone numbers. Given any positive integer n, the following term, n+1 is calculated as follows:

  • If n is even, then n+1 is defined as n/2.
  • If n is odd, then n+1 is defined as 3n + 1

The Collatz Conjecture states that, for any value of n, the sequence will always reach 1. Once the pattern reaches 1, it repeats indefinitely (3 * 1 + 1 = 4, 4/2 = 2, 2/2 = 1)

For your program, you will write a recursive function to calculate and display the sequence of hailstone numbers from any initial starting point, n, until it reaches 1. Additionally, after the sequence terminates at 1, you should print out the number of steps that were required to reach that point.

Notes:

  • The Collatz Conjecture is unproven, but works for every case ever tested. For the purpose of this lab, we will assume it works in all cases, so you can rely on the sequence always reaching 1.
  • Counting the number of recursions required can be handled in a number of different ways, including additional parameters to the function or a static local variable. The choice is yours.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
using namespace std;

int sequence(int n) {
    cout << n << endl;
    if (n == 1) {
        return 1;
    } else {
        if (n % 2 == 0) {
            n /= 2;
        } else {
            n = 3*n + 1;
        }
        return 1 + sequence(n);
    }
}

int main() {
    int n;
    cout << "Enter a value for n: ";
    cin >> n;
    cout << "Collatz sequence is" << endl;
    int count = sequence(n);
    cout << "The number of steps is: " << count << endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
c++ The Collatz Conjecture is a conjecture in mathematics that concerns a sequence sometimes known as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assignment: Using the Fork System Call The Collatz conjecture concerns what happens when we take any...

    Assignment: Using the Fork System Call The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorthm: n={n / 2 , if n is even 3 * n + 1 , if n is odd The conjecture states that when this algorithm is continually applied, all positive integers will eventually reach 1. For example, if n = 35, the sequence is:  35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2,...

  • The Collatz conjecture concerns what happens when we take any positive integer n and apply the...

    The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorithm: if n is even 3 xn+1, if n is odd The conjecture states that when this algorithm is continually applied, all positive integers will eventually reach 1. For example, if n- 35, the sequence 1S 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4,2,'1 Write a C program using the fork) system call that generates this sequence in the...

  • use python please #The Joyner Conjecture is a not-at-all famous mathematical #series inspired by the Collatz...

    use python please #The Joyner Conjecture is a not-at-all famous mathematical #series inspired by the Collatz Conjecture for use in this #class. # #The Joyner Conjecture proceeds as follows: # #Start with any number. If the number is divisible by 3, #divide it by 3. Otherwise, add 2 to the number. Eventually, #no matter what number you begin with, this series will run #into 1 or 2. If it runs into 1, it will repeat 1-3 forever. #If it runs...

  • collatz public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an...

    collatz public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an array containing the Collatz sequence beginning with start up to numIterations. The Collatz function is defined by: 3n + 1 if n is odd n/2 if n is even Given start = 7 and numIterations = 3, this method returns [7, 22, 11, 34] Parameters: start - starting integer numIterations - how long to compute the Collatz sequence for Returns: an array containing the...

  • Within the 1900 folder on your desktop, create a new folder namned Lab4HwLastnameFirstnane (5 pts) Problem...

    Within the 1900 folder on your desktop, create a new folder namned Lab4HwLastnameFirstnane (5 pts) Problem 1 Consider the sequence of integers defined as follows: • The first term is any positive integer. . For each term n in the sequence, the next tcrin is computed like this: If n is even the next term is n/2. If n is odd, the next term is 3n +1. • Stop once the sequence reaches 1. Here are a few examples of...

  • Please answere using python language codes. Write a program to print out Collatz sequence for a...

    Please answere using python language codes. Write a program to print out Collatz sequence for a user-supplied number. Prompt the user for a positive integer which will become the first number in the sequence. Next number in the sequence is derived as follows: If previous number is odd, the next number is 3 times the previous, plus 1. If previous number is even, the next number is half of the previous According to Collatz proposition, the sequence ultimately reaches 1...

  • IN PYTHON 1.Choose a positive integer 2. To get the next number in the sequence we...

    IN PYTHON 1.Choose a positive integer 2. To get the next number in the sequence we do the following: If the integer is odd, we multiply by 3 and add 1. If the integer is even, we divide by 2. It is hypothesized that the above sequence will always converge to the value of 1, regardless of any valid initial choice. This hypothesis is known as the Collatz Conjecture. For example, if we start at 5, the numbers generated by...

  • Write a code using loop and flag function in python jupitior notebook: Ask the user for...

    Write a code using loop and flag function in python jupitior notebook: Ask the user for the number of items they bought. Also ask the price of each item they bought and quantity purchased. Display the total amount they owe. Ask the user for a number n. Calculate 1+2+3+….+n. Ask the user for a number n. Calculate 1*2*3*…*n. Ask the user for number of rows (r) and columns (c). Print * for r rows and c columns. Ask the user...

  • In mathematics, the Fibonacci numbers are the series of number that exhibit the following pattern: 0,1,1,2,3,5,8,13,21,34,55,89,144,.......

    In mathematics, the Fibonacci numbers are the series of number that exhibit the following pattern: 0,1,1,2,3,5,8,13,21,34,55,89,144,.... In mathematical notation the sequence Fn of Fibonacci number is defined by the following recurrence relation: Fn=Fn-1+Fn-2 With the initial values of F0=0 and F1=1. Thus, the next number in the series is the sum of the previous two numbers. Write a program that asks the user for a positive integer N and generate the Nth Fibonacci number. Your main function should handle user...

  • Write a recursive function called freq_of(letter, text) that finds the number of occurrences of a specified...

    Write a recursive function called freq_of(letter, text) that finds the number of occurrences of a specified letter in a string. This function has to be recursive; you may not use loops!   CodeRunner has been set to search for keywords in your answer that might indicate the use of loops, so please avoid them. For example: Test Result text = 'welcome' letter = 'e' result = freq_of(letter, text) print(f'{text} : {result} {letter}') welcome : 2 e and A list can be...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT