Skip to content

Commit dc9b48a

Browse files
authored
Merge pull request #4 from arnavb/problem-008
Added Project Euler problem #008
2 parents 390ea34 + 0b1c47f commit dc9b48a

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

solutions/Problem008.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
*
3+
* Solution to Problem 8 (https://projecteuler.net/problem=8) of Project Euler
4+
* Copyright (C) 2017 Arnav Borborah
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
*/
20+
21+
#include <iostream>
22+
#include <string>
23+
24+
int main()
25+
{
26+
const std::string numberString = "73167176531330624919225119674426574742355349194934"
27+
"96983520312774506326239578318016984801869478851843"
28+
"85861560789112949495459501737958331952853208805511"
29+
"12540698747158523863050715693290963295227443043557"
30+
"66896648950445244523161731856403098711121722383113"
31+
"62229893423380308135336276614282806444486645238749"
32+
"30358907296290491560440772390713810515859307960866"
33+
"70172427121883998797908792274921901699720888093776"
34+
"65727333001053367881220235421809751254540594752243"
35+
"52584907711670556013604839586446706324415722155397"
36+
"53697817977846174064955149290862569321978468622482"
37+
"83972241375657056057490261407972968652414535100474"
38+
"82166370484403199890008895243450658541227588666881"
39+
"16427171479924442928230863465674813919123162824586"
40+
"17866458359124566529476545682848912883142607690042"
41+
"24219022671055626321111109370544217506941658960408"
42+
"07198403850962455444362981230987879927244284909188"
43+
"84580156166097919133875499200524063689912560717606"
44+
"05886116467109405077541002256983155200055935729725"
45+
"71636269561882670428252483600823257530420752963450";
46+
47+
long long int greatestProduct = 1;
48+
49+
for (std::size_t i = 0; i < numberString.length() - 13; ++i)
50+
{
51+
long long int currentProduct = 1;
52+
53+
for (std::size_t j = i; j < i + 13; ++j)
54+
{
55+
currentProduct *= numberString[j] - '0'; // Convert to integer
56+
}
57+
58+
if (currentProduct > greatestProduct)
59+
{
60+
greatestProduct = currentProduct;
61+
}
62+
}
63+
64+
std::cout << greatestProduct << "\n";
65+
}

tests/expected_solutions.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
005 232792560
66
006 25164150
77
007 104743
8+
008 23514624000
89
014 837799
910
022 871198282
1011
028 669171001

tests/test_all_solutions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_all_solutions(directory_name, compiler_name, expected_solutions):
3939
print("Running: " + compile_command)
4040

4141
if (subprocess.call(compile_command.split(), stdout = subprocess.DEVNULL, stderr = subprocess.STDOUT) != 0): # Compile the current file
42-
print(f"Result: {color_string(colors.FAIL, 'FAILURE')}: File was unable to be compiled!")
42+
print(f"\nResult: {color_string(colors.FAIL, 'FAILURE')}: File was unable to be compiled!")
4343
else:
4444
current_output_string = subprocess.check_output(['./a.out']).decode('utf-8') # Run the executable and capture the output
4545
if current_output_string.strip().isdigit():
@@ -55,7 +55,7 @@ def test_all_solutions(directory_name, compiler_name, expected_solutions):
5555
else:
5656
print(f"{color_string(colors.FAIL, 'FAILURE')}: Current output {str(current_output)} does not match expected output {str(solution)}!")
5757
else:
58-
print(f"Result: {color_string(colors.FAIL, 'FAILURE')}: Expected integer but found: ")
58+
print(f"\nResult: {color_string(colors.FAIL, 'FAILURE')}: Expected integer but found: ")
5959
print(current_output_string)
6060

6161
print(color_string(colors.BOLD, "==============="))

0 commit comments

Comments
 (0)