Russian Peasant Multiplication Algorithm For university, we have just started to learn C++ as a language, and one of our challenges to do in our spare time is to write an algorithm for 'Peasant multiplication'. In the challenge, we have been told that we cannot use the multiplication operator *. 

My code for it is as follows.

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;cmath&gt;
#include &lt;string&gt;
#include &lt;limits&gt;
using namespace std;

vector&lt;int&gt; half(float numberToHalf) {
    vector&lt;int&gt; steps;
    while (numberToHalf != 1) {
        steps.push_back(numberToHalf);
        numberToHalf = floorf(numberToHalf / 2);
    }
    steps.push_back(1);
    return steps;
}

vector&lt;int&gt; repeatedDouble(int number, int limit) {
    vector&lt;int&gt; doubleList;
    for (int i = 0; i &lt; limit; i++)
    {       
        doubleList.push_back(number);
        number += number;
    } 
    return doubleList;
}

int adding(vector&lt;int&gt; halfList, vector&lt;int&gt; doubleList) {
    int total = 0;
    for (int i = 0; i &lt; halfList.size(); i++) {
        if (halfList[i] % 2 != 0) {
            total += doubleList[i];
        }
    }
    return total;
}

int verify(string timeRound) {
    bool roundAgain = true;
    int number;
    do {
        cout &lt;&lt; "Enter the " &lt;&lt; timeRound &lt;&lt; " number: " &lt;&lt; endl;
        cin &gt;&gt; number;
        if(cin.good() &amp;&amp; 0 &lt; number) { //if number entered is positive and an integer
            roundAgain = false;
        }else {
            //Make cin ready to take another input 
            cin.clear();
            cin.ignore(numeric_limits&lt;streamsize&gt;::max(),'\n'); //Snippet taken from https://stackoverflow.com/questions/16934183/integer-validation-for-input
            cout &lt;&lt; "A valid input is a POSITIVE, whole, number. No letters, words, or negatives" &lt;&lt; endl;
        }
    } while (roundAgain);
    return number;
}

int main() {
    int num1 = verify("First");
    int num2 = verify("Second");

    vector&lt;int&gt; halved = half(num1);
    vector&lt;int&gt; doubled = repeatedDouble(num2, halved.size());
    cout &lt;&lt; "The result is: " &lt;&lt; adding(halved, doubled);
}


Any help in optimising the code to make it look and run even better would be appreciated! Also, if any of you could give some tips for robustness or similar that would be great too! 

I am completely new to C++ so any tricks and tips from experienced coders would be amazing!
It looks to me that you're being too literal.  When trying to convert an analog process to code it's easy to fall into that trap.

Presently you're using 2 loops to make 2 lists.  This is unnecessary.  You can do everything including calculate the answer in one loop.

You're also not checking for which number is the lesser one and which is the greater one.  This is integral to the base algorithm, that you're trying to emulate.

A simplified version could look something like this:

int PeasantMultiply(int num1, int num2)
{
    auto pair = std::minmax(num1, num2);
    int min = pair.first;
    int max = pair.second;
    int total = 0;
    if (min != 0)
    {
        do
        {
            if (min % 2 == 1)
            {
                total += max;
            }
            min /= 2;
            max += max;
        } while (min &gt; 0);
    }
    return total;
}


After looking at this code again, I came upon an optimization.  Instead of using the modulus operator(%), I could accomplish the same thing with and extra int variable and use subtraction instead:

int PeasantMultiply(int num1, int num2)
{
    auto pair = std::minmax(num1, num2);
    int oldMin = pair.first;
    int max = pair.second;
    int total = 0;
    int newMin = 0;
    if (oldMin != 0)
    {
        do
        {
            newMin = oldMin / 2;
            if (oldMin - newMin != newMin)
            {
                total += max;
            }
            oldMin = newMin;
            max += max;
        } while (oldMin &gt; 0);
    }
    return total;
}


In my tests this saves about 10% in time.