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 <inttypes.h>
37#include <stdio.h>
38#include "include/macros.h"
39
40uint64_t EMSCRIPTEN_KEEPALIVE p0008() {
41    size_t i, j;
42    uint64_t answer = 0, tmp;
43    const char *plain_digits = ("73167176531330624919225119674426574742355349194934"
44                                "96983520312774506326239578318016984801869478851843"
45                                "85861560789112949495459501737958331952853208805511"
46                                "12540698747158523863050715693290963295227443043557"
47                                "66896648950445244523161731856403098711121722383113"
48                                "62229893423380308135336276614282806444486645238749"
49                                "30358907296290491560440772390713810515859307960866"
50                                "70172427121883998797908792274921901699720888093776"
51                                "65727333001053367881220235421809751254540594752243"
52                                "52584907711670556013604839586446706324415722155397"
53                                "53697817977846174064955149290862569321978468622482"
54                                "83972241375657056057490261407972968652414535100474"
55                                "82166370484403199890008895243450658541227588666881"
56                                "16427171479924442928230863465674813919123162824586"
57                                "17866458359124566529476545682848912883142607690042"
58                                "24219022671055626321111109370544217506941658960408"
59                                "07198403850962455444362981230987879927244284909188"
60                                "84580156166097919133875499200524063689912560717606"
61                                "05886116467109405077541002256983155200055935729725"
62                                "71636269561882670428252483600823257530420752963450");
63    char digits[1000];
64    for (i = 0; i < 1000; i++)
65        digits[i] = plain_digits[i] - '0';
66    for (i = 0; i < 1000 - 13; i++) {
67        tmp = digits[i];
68        for (j = i + 1; j < i + 13; j++)
69            tmp *= digits[j];
70        answer = max(answer, tmp);
71    }
72    return answer;
73}
74
75PROGRAM_TAIL("%" PRIu64, p0008)
76#endif