Skip to content

Commit a4b6558

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Project Euler #1: Multiples of 3 and 5 solved ✓
1 parent c3745a9 commit a4b6558

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package ae.hackerrank.projecteuler;
2+
3+
import java.text.MessageFormat;
4+
5+
/**
6+
* Multiples of 3 and 5.
7+
*
8+
* @link Problem definition [[docs/hackerrank/projecteuler/euler001.md]]
9+
*/
10+
public class Euler001 {
11+
12+
private Euler001() {}
13+
14+
static java.util.logging.Logger logger = ae.projecteuler.util.CustomLogger.getLogger();
15+
16+
/**
17+
* Greatest common divisor
18+
*/
19+
private static Long gcd(Integer a, Integer b) {
20+
if (a == 0) {
21+
return (long) b;
22+
}
23+
24+
return gcd(b % a, a);
25+
}
26+
27+
/**
28+
* Sum of Arithmetic Progression series
29+
*/
30+
private static Long sumAP(Long numberOfTerms, Long distance)
31+
{
32+
long n = numberOfTerms / distance;
33+
34+
return (long) ((n) * (1 + n) * distance / 2);
35+
}
36+
37+
/**
38+
* Multiples of 3 and 5.
39+
*/
40+
public static Long euler001(Integer a, Integer b, Integer n) {
41+
Long result = null;
42+
43+
n = n - 1;
44+
Long lcm = (a * b) / gcd(a, b);
45+
46+
String log = MessageFormat.format("Problem 000XX solved: {0}", result);
47+
48+
result = sumAP((long) n, (long) a) + sumAP((long) n, (long) b) - sumAP((long) n, lcm);
49+
50+
logger.info(log);
51+
52+
return result;
53+
}
54+
}
55+
56+
//CHECKSTYLE.ON: JavadocParagraph
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ae.hackerrank.projecteuler;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
7+
8+
9+
class Euler001Test {
10+
11+
@ParameterizedTest
12+
@CsvSource({
13+
"3, 5, 10, 23, Test Case 1",
14+
"3, 5, 100, 2318, Test Case 2",
15+
"3, 5, 1000, 233168, Test Case 3"
16+
})
17+
void euler001(
18+
int a,
19+
int b,
20+
int n,
21+
long answer,
22+
String testCase) {
23+
24+
Long solutionFound = Euler001.euler001(a, b, n);
25+
26+
String log = String.format("Problem 0023 {0} answer must be: {1}", testCase, answer);
27+
assertEquals(answer, solutionFound, log);
28+
}
29+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# [Multiples of 3 and 5](https://www.hackerrank.com/contests/projecteuler/challenges/euler001)
2+
3+
- Difficulty: #easy
4+
- Category: #ProjectEuler+
5+
6+
If we list all the natural numbers below $ 10 $ that are multiples of
7+
$ 3 $ or $ 5 $, we get $ 3, 5, 6 $ and $ 9 $.
8+
The sum of these multiples
9+
is $ 23 $.
10+
11+
Find the sum of all the multiples of $ 3 $ or $ 5 $ below $ N $.
12+
13+
## Input Format
14+
15+
First line contains $ T $ that denotes the number of test cases.
16+
This is followed by $ T $ lines, each containing an integer, $ N $.
17+
18+
## Constraints
19+
20+
- 1 $ \leq T \leq 10^5 $
21+
- 1 $ \leq N \leq 10^9 $
22+
23+
## Output Format
24+
25+
For each test case, print an integer that denotes the sum of all the multiples
26+
of $ 3 $ or $ 5 $ below $ N $.
27+
28+
## Sample Input 0
29+
30+
```text
31+
2
32+
10
33+
100
34+
```
35+
36+
## Sample Output 0
37+
38+
```text
39+
23
40+
2318
41+
```
42+
43+
## Explanation 0
44+
45+
For $ N = 10 $, if we list all the natural numbers below $ 10 $ that are
46+
multiples of $ 3 $ or $ 5 $, we get $ 3, 5, 6 $ and $ 9 $.
47+
The sum of these multiples is $ 23 $.
48+
49+
Similarly for $ N = 100 $, we get $ 2318 $.

0 commit comments

Comments
 (0)