Find all number pairs that sum to 121212 This problem is quite easy to do by brute force.  In fact, it'll be reasonably fast to brute force as well.  But where's the fun in that?

The Problem

Produce a list of all unique 5 digit number pairs that sum to 121212.  However, each decimal digit must appear exactly once in either number.  So a valid pair would be (98167, 23045).  But an invalid pair would be (23456, 97756) because 7, 5, 6 are repeated more than once, and 1, 8, 0 are not used at all.  There are exactly 192 unique pairs.

Rules


Efficiency: You can brute force this.  But that's trivial to do.  So instead, the real challenge here is to find a way to efficiently produce the list of numbers.
Source Code Requirements: It cannot store the number list (or any part of it).  The number sequence must be generated on the fly.


Have Fun!
C++

#include&lt;iostream&gt;
using namespace std;
#include&lt;algorithm&gt;

int main()
{
    long long N,F,S;
    char str[11]="1023456789";
    while (1) {
        sscanf(str,"%lld",&amp;N);
        F=N/100000;S=N%100000;
        if (F&gt;60000)
           break;
        if (F+S==121212)
           printf("%lld %lld\n",F,S);
        next_permutation(str,str+10);
    }
}


http://www.ideone.com/Lr84g