Question

A mathematical conjecture states that if we start with any positive number we can get to the value 1 by repeating 2 poss...

A mathematical conjecture states that if we start with any positive number we can get to the value 1 by repeating 2 possible steps depending on the value of the current number. If the current value is even we divide the number by 2 and reapply the steps again (number / 2). If the number is odd we multiply the current number by 3 and add 1 to it and reply the previous steps again (number * 3 + 1). If we keep repeating the two possible alternative steps we will eventually get to the value 1 at which point we are done. Write a recursive function to test the conjecture. The function should return the number of times we had to recur through the function and the highest number that we generated by the use of the two possible steps during the process. Your solution will need to handle large numbers for processing. Sample run capture from the function Starting with 9 it took 19 steps; the highest number achieved 52. Starting with 27 it took 111 steps; the highest number achieved 9232. Starting with 837791 it took 162 steps; the highest number achieved 122288020. Starting with 837799 it took 524 steps; the highest number achieved 2974984576. Submit your commented solution as a text file along with the captured output from running the following set of values through your function. Format the output as in the sample captured runs above. Test Numbers 19 837,799 75,128,138,247 942,488,749,153,153

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ program

#include<iostream>
using namespace std;

long conjecture(long n,long &highest){
if(n>highest)highest=n;
   if(n==1)return 0;
   else if(n%2==0)return 1+conjecture(n/2,highest);
   else if(n%2==1)return 1+conjecture(n*3+1,highest);
}

int main(){
   long arr[]={9,19, 837,799 ,75,128,138,247, 942,488,749,153,153};
   long highest;
   int n = sizeof(arr)/sizeof(int);
   for(int i=0;i<n;i++){
       highest=-1;
       cout<<"n = "<<arr[i]<<" it took "<<conjecture(arr[i],highest)<<" steps\n";
       cout<<"Highest Number reached : "<<highest<<"\n\n";
   }

return 0;
}

//sample output

C:\Users\ IshuManish\Documents conjecture.exe -9 it took 19 steps ighest Number reached52 - 19 it took 20 steps ighest Number

Add a comment
Know the answer?
Add Answer to:
A mathematical conjecture states that if we start with any positive number we can get to the value 1 by repeating 2 poss...
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
  • 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...

  • Assignment Specifications We are going to use the Monte Carlo Method to determine the probability of...

    Assignment Specifications We are going to use the Monte Carlo Method to determine the probability of scoring outcomes of a single turn in a game called Pig. Your Task For this assignment you will simulate a given number of hold?at?N turns of a game called Pig, and report the estimated probabilities of the possible scoring outcomes. You are NOT implementing the game of Pig, only a single turn of this game. The value of N will be acquired via user...

  • Lab 7: Void and Value-Returning Functions Lab 7: Void and Value-returning functions             ...

    Lab 7: Void and Value-Returning Functions Lab 7: Void and Value-returning functions              Due date: 11/6/19 Problem: Write a C++ program that calculates the areas of a rectangle and of an ellipse.                    Area = base * height             Area = π * radius a * radius b Note: all images extracted from http://www.mathsisfun.com/area-calculation-tool.html ------------------------------------------------------------------------------------------------------------------------ Your task: implement in C++ the algorithm solution shown below. ------------------------------------------------------------------------------------------------------------------------ Part A (79...

  • programming language: C++ *Include Line Documenatations* Overview For this assignment, write a program that will simulate...

    programming language: C++ *Include Line Documenatations* Overview For this assignment, write a program that will simulate a game of Roulette. Roulette is a casino game of chance where a player may choose to place bets on either a single number, the colors red or black, or whether a number is even or odd. (Note: bets may also be placed on a range of numbers, but we will not cover that situation in this program.) A winning number and color is...

  • Self-check exercise: While-loops The value of (π^2)/8 can be approximated by the series Write a script...

    Self-check exercise: While-loops The value of (π^2)/8 can be approximated by the series Write a script that evaluates this expression, ignoring all terms that are strictly smaller than .000001. Your script should display the number of terms summed and the sum. Use a while-loop. Think about the initialization of your variables and the order of computation carefully! In the loop body you need to calculate the value of a term only once. We use the above series for approximating (π^2)/8...

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • 1 L, as a dynamical system (Notes from Assignment #2) We take our definition of dynamical system ...

    1 L, as a dynamical system (Notes from Assignment #2) We take our definition of dynamical system to be an "object" along with a specific set of modifications that can be performed (dynamically) upon this object. In this case, the object is a bi-infinite straight road with a lamp post at every street corner and a marked lamp (the position of the lamplighter). There are two possible types of modifications: the lamplighter can walk any distance in either direction from...

  • 18.1 Lab Lesson 11 (Part 1 of 1) Part of lab lesson 11 There in one...

    18.1 Lab Lesson 11 (Part 1 of 1) Part of lab lesson 11 There in one part to lab lesson 11. The entire lab will be worth 100 points. Lab lesson 11 part 1 is worth 100 points For part 1 you will have 80 points if you enter the program and successfully run the program tests. An additional 20 points will be based on the style and formatting of your C++ code. Style points The 20 points for coding...

  • Please write c++ (windows) in simple way and show all the steps with the required output

    please write c++ (windows) in simple way and show all the steps with the required output Write a C++ program that simulates the operation of a simple online banking system The program starts by displaying its main menu as shown in Figure1 splay Account nformatson verity Your credit Card elect vour choice Figure 1 Main menu of the program The menu is composed of the following choices 1. When choice 1 is selected (Display Account information), the program should display...

  • I've posted 3 classes after the instruction that were given at start You will implement and...

    I've posted 3 classes after the instruction that were given at start You will implement and test a PriorityQueue class, where the items of the priority queue are stored on a linked list. The material from Ch1 ~ 8 of the textbook can help you tremendously. You can get a lot of good information about implementing this assignment from chapter 8. There are couple notes about this assignment. 1. Using structure Node with a pointer point to Node structure to...

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
Active Questions
ADVERTISEMENT