C Implementation of Problem 9
View source code here on GitHub!
Includes
Solution
-
uint32_t p0009()
-
int main(int argc, char const *argv[])
Note
This function is only present in the Python test runner, or when compiling as a standalone program. It is not present when compiling for the Unity test runner.
1/*
2Project Euler Problem 9
3
4This was fairly short to code, but it took me a minute to figure out how to deal with the lack of multi-loop breaking
5
6Problem:
7
8A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
9a2 + b2 = c2
10
11For example, 32 + 42 = 9 + 16 = 25 = 52.
12
13There exists exactly one Pythagorean triplet for which a + b + c = 1000.
14Find the product abc.
15*/
16#ifndef EULER_P0009
17#define EULER_P0009
18#include <stdint.h>
19#include <inttypes.h>
20#include <stdio.h>
21#include "include/macros.h"
22
23uint32_t EMSCRIPTEN_KEEPALIVE p0009() {
24 uint32_t answer = 0;
25 for (uint32_t c = 3; !answer && c < 1000; c++)
26 for (uint32_t b = 2; b < c; b++) {
27 uint32_t a = 1000 - c - b;
28 if (a < b && a*a + b*b == c*c) {
29 answer = a * b * c;
30 break;
31 }
32 }
33 return answer;
34}
35
36PROGRAM_TAIL("%" PRIu32, p0009)
37#endif