C Implementation of Problem 4
View source code here on GitHub!
Includes
"macros.h" (implicitly, via digits.h)
"iterator.h" (implicitly, via digits.h)
"math.h" (implicitly, via digits.h & if compiled on PCC)
<stdbool.h>
(implicitly, via digits.h)<stdlib.h>
(implicitly, via digits.h & if not compiled on PCC)<math.h>
(implicitly, via digits.h & if not compiled on PCC)
Solution
-
uint32_t p0004()
-
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 4
3
4This was pretty easy to do, given the digit infrastructure I already had built up
5
6Problem:
7
8A palindromic number reads the same both ways. The largest palindrome made from
9the product of two 2-digit numbers is 9009 = 91 × 99.
10
11Find the largest palindrome made from the product of two 3-digit numbers.
12*/
13#ifndef EULER_P0004
14#define EULER_P0004
15#include <stdint.h>
16#include <inttypes.h>
17#include <stdio.h>
18#include "include/macros.h"
19#include "include/digits.h"
20
21uint32_t EMSCRIPTEN_KEEPALIVE p0004() {
22 uint32_t answer = 0, i, j, a, z, prod;
23 bool broken;
24 digit_counter dc;
25 for (i = 100; i < 1000; i++)
26 for (j = 100; j < 1000; j++) {
27 prod = i * j;
28 dc = digits(prod);
29 broken = false;
30 for (a = 0, z = dc.idx; a < z; a++, z--)
31 if (dc.digits[a] != dc.digits[z]) {
32 broken = true;
33 break;
34 }
35 if (!broken)
36 answer = max(answer, prod);
37 free_digit_counter(dc);
38 }
39 return answer;
40}
41
42PROGRAM_TAIL("%" PRIu32, p0004)
43#endif