Question

Please read the task first: http://codeabbey.com/index/task_view/neumanns-random-generator

I have to keep track of the number of iterations, but I get very strange results. In the example after the task we have the numbers 0001 and 4100 and they should come to loop after 2 and 4 iterations. But my results are 1, 4 or if I change the place of the counter 2 or 5 but never 2 and 4. Here is my code:

#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    int value;
    int counter;
    int result;
    int setvalue = 1; // use to exit the loop if setvalue == 0;
    cin >> n;
    vector<int> new_results(0); // use to store all the results from iterations
    vector<int> results_vec(0); // use to store the number of iterations for each number

    for (int i = 0; i < n ; i++)
    {

        cin >> value;
        while(setvalue == 1)
        {
            value = value*value;

            value = (value % 1000000) / 100;

            if(find(results_vec.begin(), results_vec.end(), value) == results_vec.end())
            {
                results_vec.push_back(value);
            }
            else
            {
                counter = results_vec.size();
                new_results.push_back(counter);
                setvalue = 0;
            }

        }
        results_vec.clear();


    }
    for (int i = 0; i < new_results.size() ; i++)
    {
        cout << new_results[i] << " ";
    }

}
Was it helpful?

Solution

Going in and out of a string the way you have is really very ugly and extremely expensive computationally.

Use

(value % 1000000) / 100;

instead to extract the middle four digits. This works by (1) taking the modulus to remove the leading two digits then (2) removing the last two with integer division.

As it's so much simpler, I suspect that will fix your bugs too.

OTHER TIPS

Here is the correct code, thank you for all your help.

#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    int value;
    int counter;
    int result;
    cin >> n;
    vector<int> new_results(0); // use to store all the results from iterations
    vector<int> results_vec(0); // use to store the number of iterations for each number

    for (int i = 0; i < n ; i++)
    {

        cin >> value;
        results_vec.push_back(value);
        while(true)
        {
            value = value*value;

            value = (value % 1000000) / 100;

            if(find(results_vec.begin(), results_vec.end(), value) == results_vec.end())
            {
                results_vec.push_back(value);
            }
            else
            {
                counter = results_vec.size();
                new_results.push_back(counter);
                break;
            }

        }
        results_vec.clear();


    }
    for (int i = 0; i < new_results.size() ; i++)
    {
        cout << new_results[i] << " ";
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top