C++ Implementation of Problem 8

View source code here on GitHub!

Includes

Solution

uint64_t p0008()
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 8
 3
 4This was easier to do in C++ than I would have thought
 5
 6Problem:
 7
 8The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.
 9
1073167176531330624919225119674426574742355349194934
1196983520312774506326239578318016984801869478851843
1285861560789112949495459501737958331952853208805511
1312540698747158523863050715693290963295227443043557
1466896648950445244523161731856403098711121722383113
1562229893423380308135336276614282806444486645238749
1630358907296290491560440772390713810515859307960866
1770172427121883998797908792274921901699720888093776
1865727333001053367881220235421809751254540594752243
1952584907711670556013604839586446706324415722155397
2053697817977846174064955149290862569321978468622482
2183972241375657056057490261407972968652414535100474
2282166370484403199890008895243450658541227588666881
2316427171479924442928230863465674813919123162824586
2417866458359124566529476545682848912883142607690042
2524219022671055626321111109370544217506941658960408
2607198403850962455444362981230987879927244284909188
2784580156166097919133875499200524063689912560717606
2805886116467109405077541002256983155200055935729725
2971636269561882670428252483600823257530420752963450
30
31Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
32*/
33#ifndef EULER_P0008
34#define EULER_P0008
35#include <stdint.h>
36#include <iostream>
37#include "include/macros.hpp"
38
39uint64_t EMSCRIPTEN_KEEPALIVE p0008() {
40    size_t i, j;
41    uint64_t answer = 0, tmp;
42    const char *plain_digits = ("73167176531330624919225119674426574742355349194934"
43                                "96983520312774506326239578318016984801869478851843"
44                                "85861560789112949495459501737958331952853208805511"
45                                "12540698747158523863050715693290963295227443043557"
46                                "66896648950445244523161731856403098711121722383113"
47                                "62229893423380308135336276614282806444486645238749"
48                                "30358907296290491560440772390713810515859307960866"
49                                "70172427121883998797908792274921901699720888093776"
50                                "65727333001053367881220235421809751254540594752243"
51                                "52584907711670556013604839586446706324415722155397"
52                                "53697817977846174064955149290862569321978468622482"
53                                "83972241375657056057490261407972968652414535100474"
54                                "82166370484403199890008895243450658541227588666881"
55                                "16427171479924442928230863465674813919123162824586"
56                                "17866458359124566529476545682848912883142607690042"
57                                "24219022671055626321111109370544217506941658960408"
58                                "07198403850962455444362981230987879927244284909188"
59                                "84580156166097919133875499200524063689912560717606"
60                                "05886116467109405077541002256983155200055935729725"
61                                "71636269561882670428252483600823257530420752963450");
62    char digits[1000];
63    for (i = 0; i < 1000; i++)
64        digits[i] = plain_digits[i] - 0x30;
65    for (i = 0; i < 1000 - 13; i++) {
66        tmp = digits[i];
67        for (j = i + 1; j < i + 13; j++)
68            tmp *= digits[j];
69        answer = std::max(answer, tmp);
70    }
71    return answer;
72}
73
74PROGRAM_TAIL(p0008)
75#endif