// floriankirsch.de/c++/tripel.html

#include <chrono>
#include <iostream>

void findTripel2_OneLoop(int64_t c)
{
    int64_t a = 1;
    int64_t b = c - 1;
    int64_t a2b2_c2 = a * a + b * b - c * c;
    while (a < b)
    {
        if (a2b2_c2 < 0)
        {
            a2b2_c2 += a + a;
            ++a;
        }
        else if (a2b2_c2 > 0)
        {
            a2b2_c2 -= b + b;
            --b;
        }
        else if (a2b2_c2 == 0)
        {
            std::cout << a << "² + " << b << "² = " << c << "²\n";
            a2b2_c2 += a + a;
            ++a;
        }
        ++a2b2_c2;
    }
}

int main()
{
	std::cout << "Pythagorean tripels found in 1 millisecond\n";
	auto start = std::chrono::steady_clock::now();
	for (int64_t c=1; ; ++c)
	{
	    findTripel2_OneLoop(c);
		auto duration = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start);
		if (duration.count() > 1000)
		    break;
	}
	return 0;
}