Powerball Lottery with C++

In this C ++ tutorial, we will learn how to count all possible Powerball guesses, as well as how to display all these numbers using the nested repeating structures technique;

The Powerball lottery

Most likely you already played at the Powerball right?
It works like this: You must choose 5 numbers from a universe of 69 numbers, from 1 to 69 and one number of 26.


In the end, they display the result in ascending order of values, ie from the smallest to the largest.

The 'lowest' guess is:
1 2 3 4 5  - 1

Already the 'biggest' guess is:
65 66 67 68 69 - 26

Here comes the secret:

The first dozen goes from 1 to 65
The second dozen goes from 2 to 66
The third dozen goes from 3 to 67
The fourth dozen goes from 4 to 68
The fifth dozen goes from 5 to 69
The last goes from 1 to 26

How many guesses are possible in Powerball

So, come on.
Let's use 6 variables for the numbers: ten1, ten2, ten3, ten4, ten5, and ten6.

The accumulator variable, to count how many iterations (hence, how many guesses are possible in the Powerball), is the sum.

Now just make FOR nested with FOR and count how many possibilities there are, always being careful about the interval each dozen can take.

Another important secret is that the variable ten1 starts from 1, and the following starts from the previous decade plus 1, since the tens are larger than the others, since we are assuming they are in ascending order. The last one, don't.

The code:
#include <iostream>
using namespace std;

int main()
{
    int dez1, dez2, dez3, dez4,
        dez5, dez6, sum=0;

    for(dez1=1; dez1<=65 ; dez1++)
        for(dez2=dez1+1; dez2<=66 ; dez2++)
            for(dez3=dez2+1; dez3<=67 ; dez3++)
                for(dez4=dez3+1; dez4<=68 ; dez4++)
                    for(dez5=dez4+1; dez5<=69 ; dez5++)
                        for(dez6=1; dez6<=26 ; dez6++)
                            sum++;
    cout << "Total: "<<sum<<endl;

    return 0;
}

The result
C++ tutorial

It took 0.821s here to run over 292 million iterations, and then on your machine?

Display all guesses from Powerball

Let's print all possibles results:
#include <iostream>
using namespace std;

int main()
{
    int dez1, dez2, dez3, dez4,
        dez5, dez6;

    for(dez1=1; dez1<=65 ; dez1++)
        for(dez2=dez1+1; dez2<=66 ; dez2++)
            for(dez3=dez2+1; dez3<=67 ; dez3++)
                for(dez4=dez3+1; dez4<=68 ; dez4++)
                    for(dez5=dez4+1; dez5<=69 ; dez5++)
                        for(dez6=1; dez6<=26 ; dez6++)
                            cout<<dez1<<"-"<<dez2<<"-"<<dez3<<"-"
                                <<dez4<<"-"<<dez5<<"--"<<dez6<<endl;

    return 0;
}




Note that now is waaaay longer, and this is because the cout function is slower, it takes time to display things on your screen, if the machine was just doing the calculations, as in the previous example, would be much faster. But here we have to show the results of the iterations, so it takes longer.

No comments:

Post a Comment