Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Problem You remember the Collatz sequence, don\'t ya? Here is a little remin

ID: 3602376 • Letter: T

Question

The Problem You remember the Collatz sequence, don't ya? Here is a little reminder in case you didn't. http://en.wikipedia.org/wiki/Collatz conjecture One of the problems is that, by doing the calculation over from the beginning for every number, you waste a lot of time redoing work that already has been done. Look at the first 6 sequences: 2 2,1 33,10, 5, 16, 8, 4, 2,1 4 4,2,1 55, 16, 8, 4, 2,'1 6 6, 3, 10, 5,16, 8,4, 2,1 Subsequences, whose value we already know, are recalculated. For example, the entire sequence of3 is already known, but we recalculate the entire sequence when starting from 6 Memoization Memoization (note the spelling, no "r") is a simple optimization technique used in algorithms where repeat calculations occur (http://en.wikipedia.org/wiki/Memoization) The idea is simple. We remember a sequence when we first calculate it. Subsequently, for any value we calculate we check first to see if we already know the remaining sequence. If so, we stop the calculation and just copy the known sequence in. For this lab, we will use a map to memorize Collatz sequences. Pay attention to this technique, it comes up in many guises and is very useful! Programming Task Make a new project directory called lab08. Copy lab08 functions.h to your directory. You need to write both lab08_functions.cpp and lab08_main.cpp. You will turn in lab08 functions. cpp to Mimir for testing Data structure, map of lona to vector The data structure you will use for the lab is a map that has: as a key, a long representing the first number in the Collatz sequence »

Explanation / Answer

Given below is the implementation of the given files in lab08_functions.cpp. Also lab0_main.cpp is given. This is only for testing the implementation. You only need to turn in lab08_functions.cpp . Please don't forget to rate the answer if it helped. Thank you.

lab08_functions.cpp


#include "lab08_functions.h"
#include <stdexcept>
using namespace std;
/*
input is a positive (1 or greater) integer
returns the next collatz number
if number is 0 or less, throws range_error;
*/
long collatz_next(long n)
{
if(n <= 0)
throw range_error("n should be +ve");
if(n == 1)
return 1;
else if(n % 2 == 0)
return n/2;
else
return 3 * n + 1;
}

/*
input is a Collatz pair (pair<long, vector<long > >)
output is a string of the format
number: sequence (comma separated) ending in 1
no trailing comma
*/
string Collatz_to_string(const Collatz &p)
{
string str = to_string(p.first) + ": " ;
vector<long> v = p.second;
str += to_string(v[0]);
for(int i = 1; i < v.size(); i++)
str += ", " + to_string(v[i]);
return str;
}

/*
input is a collatz map (map<long, vector<long> >)and a long
if the number exists as a key in the map
returns the Collatz_to_string of that pair
otw returns an empty string
*/
string sequence_in_map_to_string(map<long, vector<long> > &m, long number)

{
map<long, vector<long>>::iterator it = m.find(number);
if(it != m.end())
return Collatz_to_string(*it);
else
return "";
}

/*
input is a collatz map (map<long, vector<long> >)and a long
returns a vector<long>, the collatz sequence for that number
Operation. As you iterate through the collatz sequence
- uses collatz_next if the element in question is not in the map
- if the element *is* in the map, copies the sequence from the map
to the end of the current sequence and ends.
*/
vector<long> collatz_sequence(map<long, vector<long> > &m, long number)
{
long n = number;
vector<long> v;
if(n == 1)
{
v.push_back(1);
return v;
}
while(true)
{
if( m.find(n) == m.end()) //not existing
{
v.push_back(n);
n = collatz_next(n);
}
else //sequence existing , so copy over rest of the sequence
{
vector<long> v2 = m.find(n)->second;
for(int i = 0; i < v2.size(); i++)
v.push_back(v2[i]);
break;
}
}
return v;
}

/*
input is a collatz map (map<long, vector<long> >)and a low and high long
fills the map from low to high inclusive with each element's collatz sequence
using the function collatz_sequence
*/
void collatz_range(map<long, vector<long> > &m, long low, long high)
{
for(long n = low; n <= high; n++)
{
vector<long> v= collatz_sequence(m, n);
m[n] = v;
}
}

lab08_main.cpp

#include "lab08_functions.h"
#include <iostream>
using namespace std;
int main()
{
map<long, vector<long>> collatz;
collatz_range(collatz, 1, 6);
for(int n = 1; n <= 6; n++)
cout << Collatz_to_string(*collatz.find(n)) << endl;
}

output

1: 1
2: 2, 1
3: 3, 10, 5, 16, 8, 4, 2, 1
4: 4, 2, 1
5: 5, 16, 8, 4, 2, 1
6: 6, 3, 10, 5, 16, 8, 4, 2, 1
Program ended with exit code: 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote