Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace hackerrank::projecteuler {

int euler001(int a, int b, int n);
unsigned long euler001(int a, int b, int n);

}
18 changes: 13 additions & 5 deletions src/lib/exercises/src/hackerrank/projecteuler/euler001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@

namespace hackerrank::projecteuler {

unsigned long min(unsigned long a, unsigned long b) {
if (a > b) {
return b;
}

return a;
}

// Function to return gcd of a and b
int gcd(int a, int b) {
unsigned long gcd(unsigned long a, unsigned long b) {
// Find Minimum of a and b
int result = std::min(a, b);
unsigned long result = min(a, b);
while (result > 0) {
if (a % result == 0 && b % result == 0) {
break;
Expand All @@ -24,18 +32,18 @@ int gcd(int a, int b) {
}

// Function to find sum of Arithmetic Progression series
int sum_of_arithmetic_progression(int n, int d) {
unsigned long sum_of_arithmetic_progression(unsigned long n, unsigned long d) {
// Number of terms
n = n / d;
return n * (1 + n) * d / 2;
}

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

int lcm = (a * b) / gcd(a, b);
unsigned long lcm = (a * b) / gcd(a, b);

return sum_of_arithmetic_progression(n, a) +
sum_of_arithmetic_progression(n, b) -
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[
{ "a": 3, "b": 5, "n": 10, "expected": 23 },
{ "a": 5, "b": 3, "n": 10, "expected": 23 },
{ "a": 3, "b": 5, "n": 100, "expected": 2318 },
{ "a": 3, "b": 5, "n": 1000, "expected": 233168 }
]