Skip to content

[Hacker Rank]: Project Euler #1: Multiples of 3 and 5 solved ✓ #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2023
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
@@ -0,0 +1,57 @@
package ae.hackerrank.projecteuler;

import java.text.MessageFormat;

/**
* Multiples of 3 and 5.
*
* @link Problem definition [[docs/hackerrank/projecteuler/euler001.md]]
*/
public class Euler001 {

private Euler001() {}

static java.util.logging.Logger logger = ae.projecteuler.util.CustomLogger.getLogger();

/**
* Greatest common divisor.
*/
private static Long gcd(Integer a, Integer b) {
if (a == 0) {
return (long) b;
}

return gcd(b % a, a);
}

/**
* Sum of Arithmetic Progression series.
*/
private static Long sumOfArithmeticProgression(Long numberOfTerms, Long distance) {
long n = numberOfTerms / distance;

return (long) ((n) * (1 + n) * distance / 2);
}

/**
* Multiples of 3 and 5.
*/
public static Long euler001(Integer a, Integer b, Integer n) {
Long result = null;

n = n - 1;
Long lcm = (a * b) / gcd(a, b);

String log = MessageFormat.format("Problem 000XX solved: {0}", result);

result = sumOfArithmeticProgression((long) n, (long) a)
+ sumOfArithmeticProgression((long) n, (long) b)
- sumOfArithmeticProgression((long) n, lcm);

logger.info(log);

return result;
}
}

//CHECKSTYLE.ON: JavadocParagraph
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ae.hackerrank.projecteuler;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;


class Euler001Test {

@ParameterizedTest
@CsvSource({
"3, 5, 10, 23, Test Case 1",
"3, 5, 100, 2318, Test Case 2",
"3, 5, 1000, 233168, Test Case 3"
})
void euler001(
int a,
int b,
int n,
long answer,
String testCase) {

Long solutionFound = Euler001.euler001(a, b, n);

String log = String.format("Problem 0023 {0} answer must be: {1}", testCase, answer);
assertEquals(answer, solutionFound, log);
}
}
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 $.