Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code in C++ please! 1. The Collatz Conjecture states that, given any positive natural number, you will always eventually reach 1 if you follow the

Code in C++ please!

1. The Collatz Conjecture states that, given any positive natural number, you will always eventually reach 1 if you follow the following formula: if the number is even, divide by 2, and if the number is odd, multiply by 3 and add 1. Continue the process indefinitely or until the number is equal to 1. This problem has never been proven, and someone did actually prove that a generalization of the problem is unprovable. That said, no ones found an exception either.

Write a function that takes as input parameters two integers, assuming the first is smaller than the second number. Continue modifying the function so that for every number between the first number and the second number (assume the first number is less than the second number), the number is printed out, and then it is tested with the Collatz Conjecture. If the loop results in a 1 the program should print out, Collatz Conjecture is still working. (Now, if it doesnt, the Collatz conjecture will loop infinitely and your program will hang . You should write down the numbers you sent in, because you just did something really amazing and found a number that doesnt work with the Collatz conjecture. Or you did something wrong. You decide which is more likely). This function should return nothing. Then follow the above formula until the number is equal to 1. Count how many times you loop, and write that out to the console window. Return the count of how many times the Collatz Conjecture had to loop

here's the code i had.... i just need it to be a function instead

int main (){

// Declaring variables

int max = -999, count = 0, k, startNum = 0;

// Getting the input entered by the user

cout << "Enter a starting value: ";

cin >> k;

cout << k << ":";

// This while loop continues to execute until the n value becomes 1

while (k != 1) {

// if n is even then this block of code will be executed

if (k % 2 == 0) {

count++;

k = k / 2;

} else {

count++;

// if n is odd then this block of code will be executed

k = (3 * k + 1);

}

if (k != 1) {

cout << k << "->";

}

}

cout << k << endl;

cout << "No of times loop executed: " << count << endl;

return 0;

}

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

3. Is it a topic that your audience will find worthwhile?

Answered: 1 week ago

Question

2. Does the topic meet the criteria specified in the assignment?

Answered: 1 week ago