Skip to content

Commit

Permalink
Merge pull request #41 from sir-gon/feature/euler001
Browse files Browse the repository at this point in the history
[Hacker Rank]: Project Euler #1: Multiples of 3 and 5 solved ✓
  • Loading branch information
sir-gon authored Sep 18, 2024
2 parents 659bf79 + 25e315d commit a53bfbe
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
49 changes: 49 additions & 0 deletions docs/hackerrank/projecteuler/euler001.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# [Multiples of 3 and 5](https://www.hackerrank.com/contests/projecteuler/challenges/euler001)

- Difficulty: #easy
- Category: #ProjectEuler+

If we list all the natural numbers below $ 10 $ that are multiples of
$ 3 $ or $ 5 $, we get $ 3, 5, 6 $ and $ 9 $.
The sum of these multiples
is $ 23 $.

Find the sum of all the multiples of $ 3 $ or $ 5 $ below $ N $.

## Input Format

First line contains $ T $ that denotes the number of test cases.
This is followed by $ T $ lines, each containing an integer, $ N $.

## Constraints

- 1 $ \leq T \leq 10^5 $
- 1 $ \leq N \leq 10^9 $

## Output Format

For each test case, print an integer that denotes the sum of all the multiples
of $ 3 $ or $ 5 $ below $ N $.

## Sample Input 0

```text
2
10
100
```

## Sample Output 0

```text
23
2318
```

## Explanation 0

For $ N = 10 $, if we list all the natural numbers below $ 10 $ that are
multiples of $ 3 $ or $ 5 $, we get $ 3, 5, 6 $ and $ 9 $.
The sum of these multiples is $ 23 $.

Similarly for $ N = 100 $, we get $ 2318 $.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

namespace hackerrank::projecteuler {

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

}
45 changes: 45 additions & 0 deletions src/lib/exercises/src/hackerrank/projecteuler/euler001.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <exercises/hackerrank/projecteuler/euler001.hpp>

/**
* @link Problem definition [[docs/hackerrank/projecteuler/euler001.md]]
*/

#include <algorithm>

namespace hackerrank::projecteuler {

// Function to return gcd of a and b
int gcd(int a, int b) {
// Find Minimum of a and b
int result = std::min(a, b);
while (result > 0) {
if (a % result == 0 && b % result == 0) {
break;
}
result--;
}

// Return gcd of a and b
return result;
}

// Function to find sum of Arithmetic Progression series
int sum_of_arithmetic_progression(int n, int 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) {
// Since, we need the sum of multiples less than N
n = n - 1;

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

return sum_of_arithmetic_progression(n, a) +
sum_of_arithmetic_progression(n, b) -
sum_of_arithmetic_progression(n, lcm);
}

} // namespace hackerrank::projecteuler
29 changes: 29 additions & 0 deletions src/tests/unit/lib/hackerrank/projecteuler/euler001.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <catch2/catch_test_macros.hpp>

#include <exercises/hackerrank/projecteuler/euler001.hpp>
#include <iostream>
#include <vector>

#include <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;

TEST_CASE("euler001 JSON Test Cases",
"[hackerrank] [jsontestcase] [projecteuler]") {
std::filesystem::path cwd = std::filesystem::current_path();
std::string path =
cwd.string() +
"/unit/lib/hackerrank/projecteuler/euler001.testcases.json";

INFO("euler001 JSON test cases FILE: " << path);

std::ifstream f(path);
json data = json::parse(f);

for (auto testcase : data) {
int result = hackerrank::projecteuler::euler001(
testcase["a"], testcase["b"], testcase["n"]);
CHECK(result == testcase["expected"]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{ "a": 3, "b": 5, "n": 10, "expected": 23 },
{ "a": 3, "b": 5, "n": 100, "expected": 2318 },
{ "a": 3, "b": 5, "n": 1000, "expected": 233168 }
]

0 comments on commit a53bfbe

Please sign in to comment.