user2971602
user2971602

Reputation: 15

Ideas for recursion

I recently wrote this simple C++ program and I was wondering if it would process faster if it was in a recursion. However, I did not manage to find a way to successfully write it in recursion so I am asking if you could help me with making it with recursion.

#include<iostream>
using namespace std;
int main()
{
    for (int i1 = 1; i1 <= 45; i1++)
    {
        for (int i2 = i1 + 2; i2 <= 46; i2++)
        {
            for (int i3 = i2 + 2; i3 <= 47; i3++)
            {
                for (int i4 = i3 + 2; i4 <= 48; i4++)
                {
                    cout<<i1<<" "<<i2<<" "<<i3<<" "<<i4<<endl;
                }
            }
        }
    }
    return 0;
}

Upvotes: 1

Views: 88

Answers (1)

Dan
Dan

Reputation: 13200

Recursion probably won't make it faster, but changing

cout<<i1<<" "<<i2<<" "<<i3<<" "<<i4<<endl;

to

cout<<i1<<" "<<i2<<" "<<i3<<" "<<i4<<"\n";

almost certainly will. This is because by default cout stores its output in a buffer, and only writes ("flushes") it occasionally. However, if you add endl it will write the buffer immediately, which is slower.

Upvotes: 2

Related Questions