Created
December 3, 2016 13:04
-
-
Save haampie/ad8b0ebb5545aab194c145524ed1c385 to your computer and use it in GitHub Desktop.
Problem 94
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <tuple> | |
using pell = std::pair<size_t, size_t>; | |
inline pell next(pell const &solution) | |
{ | |
return { | |
2 * solution.first + 3 * solution.second, | |
2 * solution.second + solution.first | |
}; | |
} | |
int main() | |
{ | |
size_t sum = 0; | |
size_t const max = 1000000000ul; | |
size_t perimeter = 0; | |
// Initial solution to Pells equation x^2 - 3y^2 = 1 | |
// producing a valid triangle | |
pell solution{7, 4}; | |
while (true) | |
{ | |
// Longer side | |
perimeter = 2 * (solution.first + 1); | |
if (perimeter > max) break; | |
sum += perimeter; | |
solution = next(solution); | |
// Smaller side | |
perimeter = 2 * (solution.first - 1); | |
if (perimeter > max) break; | |
sum += perimeter; | |
solution = next(solution); | |
} | |
std::cout << sum << '\n'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment