lmartin92
 

I have a particularly optimized loop that fills a std::vector with all the Pythagorean triples (non-primatives included). It can generate 10,000 in a second and 100,000 in under 40 seconds, and finally, 1 Million in 4000~. I thought it would be neat to post here. I'll port it to D one day when I'm extremely bored. Here's the loop (feel free to use it in any way you like in your program, BSD Liscense, no attribution required but appreciated). Please, if you find a faster way that generates the exact same amount of triples or more, please post it here, I've become obsessed with it's optimization.

generate(vector<Triple>& triplist, ullong limit) {
    cout<<"Please wait as triples are being generated."<<endl;
    register ullong a, b, c;
    register Triple trip;
    time_t timer = time(0);
   
    for(a = 1; a <= limit; ++a) {
        for(b = a + 1; b <= limit; ++b) {
            c = _root(_square(a) + _square(b));
           
            if(c != -1 && c <= limit) {
                trip.a = a; trip.b = b; trip.c = c;
               
                triplist.push_back(trip);
               
            } else if(c > limit)
                break;
        }
    }
   
    timer = time(0) - timer;
    cout<<"Generated "<<triplist.size()<<" in "<<timer<<" seconds."<<endl;
    cin.get();
    cin.get();
}