-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day_19_Solution.java
42 lines (38 loc) · 1.12 KB
/
Day_19_Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Title : Day_19_Solution.java
* Author : Tridib Samanta
* Created : 13-01-2020
* Link : https://www.hackerrank.com/challenges/30-interfaces/problem
**/
import java.io.*;
import java.util.*;
interface AdvancedArithmetic{
int divisorSum(int n);
}
class Calculator implements AdvancedArithmetic {
public int divisorSum(int n) {
int sum = 0;
int sqrt = (int) Math.sqrt(n);
int stepSize = (n % 2 == 1) ? 2 : 1;
for (int i = 1; i <= sqrt; i += stepSize) {
if (n % i == 0) {
sum += i + n/i;
}
}
if (sqrt * sqrt == n) {
sum -= sqrt;
}
return sum;
}
}
class Day_19_Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
scan.close();
AdvancedArithmetic myCalculator = new Calculator();
int sum = myCalculator.divisorSum(n);
System.out.println("I implemented: " + myCalculator.getClass().getInterfaces()[0].getName() );
System.out.println(sum);
}
}