Skip to content

Commit 356a4a4

Browse files
committed
Three Five Multiples
1 parent b55a16a commit 356a4a4

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

src/medium/ThreeFiveMultiples.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package medium;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* Have the function ThreeFiveMultiples(num) return the sum
8+
* of all the multiples of 3 and 5 that are below num.
9+
* For example: if num is 10, the multiples of 3 and 5
10+
* that are below 10 are 3, 5, 6, and 9,
11+
* and adding them up you get 23, so your program should return 23.
12+
* The integer being passed will be between 1 and 100.
13+
*/
14+
public class ThreeFiveMultiples {
15+
16+
private static final Set<Integer> freq = new HashSet<>();
17+
18+
/**
19+
* Count Multiples.
20+
*
21+
* @param num input number
22+
* @param mp a factor (multiplier)
23+
*/
24+
private static void countMultiples(int num, int mp) {
25+
for (int i = mp; i < num; i += mp) {
26+
if (i % mp == 0) {
27+
freq.add(i);
28+
}
29+
}
30+
}
31+
32+
/**
33+
* Three Five Multiples function.
34+
*
35+
* @param num input number
36+
* @return a result
37+
*/
38+
private static int threeFiveMultiples(int num) {
39+
int total = 0;
40+
countMultiples(num, 3);
41+
countMultiples(num, 5);
42+
for (int i : freq) {
43+
total += i;
44+
}
45+
return total;
46+
}
47+
48+
/**
49+
* Entry point.
50+
*
51+
* @param args command line arguments
52+
*/
53+
public static void main(String[] args) {
54+
var result1 = threeFiveMultiples(17);
55+
System.out.println(result1);
56+
var result2 = threeFiveMultiples(117);
57+
System.out.println(result2);
58+
}
59+
60+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy