C++ Implementation of Problem 4
View source code here on GitHub!
Includes
Solution
-
uint64_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.
1/*
2Project Euler Problem 4
3
4This was pretty easy to do, given the C++ stdlib
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 <algorithm>
17#include <cstdio>
18#include <iostream>
19#include "include/macros.hpp"
20
21uint32_t EMSCRIPTEN_KEEPALIVE p0004() {
22 uint32_t answer = 0, i, j, prod;
23 for (i = 100; i < 1000; i++)
24 for (j = 100; j < 1000; j++) {
25 prod = i * j;
26 char buf[8] = {};
27 // I know snprintf exists, but it isn't defined in C++98,
28 // and this isn't taking in user input
29 sprintf(buf, "%u", prod);
30 std::string forward(buf);
31 std::string reverse = forward;
32 std::reverse(reverse.begin(), reverse.end());
33 if (forward == reverse)
34 answer = std::max(answer, prod);
35 }
36 return answer;
37}
38
39PROGRAM_TAIL(p0004)
40#endif