Skip to content

Commit 70b29e0

Browse files
author
Gonzalo Diaz
committed
[BUGFIX] [Hacker Rank]: Project Euler #1: Multiples of 3 and 5. Types changed to reach problem limits. Solved ✓
1 parent 8db751b commit 70b29e0

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/lib/exercises/include/exercises/hackerrank/projecteuler/euler001.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
namespace hackerrank::projecteuler {
44

5-
int euler001(int a, int b, int n);
5+
unsigned long euler001(int a, int b, int n);
66

77
}

src/lib/exercises/src/hackerrank/projecteuler/euler001.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@
88

99
namespace hackerrank::projecteuler {
1010

11+
unsigned long min(unsigned long a, unsigned long b) {
12+
if (a > b) {
13+
return b;
14+
}
15+
16+
return a;
17+
}
18+
1119
// Function to return gcd of a and b
12-
int gcd(int a, int b) {
20+
unsigned long gcd(unsigned long a, unsigned long b) {
1321
// Find Minimum of a and b
14-
int result = std::min(a, b);
22+
unsigned long result = min(a, b);
1523
while (result > 0) {
1624
if (a % result == 0 && b % result == 0) {
1725
break;
@@ -24,18 +32,18 @@ int gcd(int a, int b) {
2432
}
2533

2634
// Function to find sum of Arithmetic Progression series
27-
int sum_of_arithmetic_progression(int n, int d) {
35+
unsigned long sum_of_arithmetic_progression(unsigned long n, unsigned long d) {
2836
// Number of terms
2937
n = n / d;
3038
return n * (1 + n) * d / 2;
3139
}
3240

3341
// Function to find the sum of all multiples of a and b below n
34-
int euler001(int a, int b, int n) {
42+
unsigned long euler001(int a, int b, int n) {
3543
// Since, we need the sum of multiples less than N
3644
n = n - 1;
3745

38-
int lcm = (a * b) / gcd(a, b);
46+
unsigned long lcm = (a * b) / gcd(a, b);
3947

4048
return sum_of_arithmetic_progression(n, a) +
4149
sum_of_arithmetic_progression(n, b) -

0 commit comments

Comments
 (0)