-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpro_elu_21.java
More file actions
57 lines (52 loc) · 1.54 KB
/
pro_elu_21.java
File metadata and controls
57 lines (52 loc) · 1.54 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.io.*;
import java.util.*;
public class AmicableNumbers {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner in = new Scanner(System.in);
int numberOfTestCases = in.nextInt();
HashMap<Integer, Long> sumUnderN = new HashMap<Integer, Long>();
long sum = 0;
/* for(int j=1;j<100000;j++)
{
System.out.println(j+" "+getSumOfProperDivisors(j));
}*/
for(int j=1;j<100000;j++)
{
int a = j;
long da = getSumOfProperDivisors(a);
long b = da;
long db = getSumOfProperDivisors(b);
if(a!=b && a==db)
{
sum+=a;
sumUnderN.put(j, sum);
// System.out.println(j+" "+sum);
}
else sumUnderN.put(j, sum);
}
for(int i=0;i<numberOfTestCases;i++)
{
int n = in.nextInt();
System.out.println(sumUnderN.get(n));
}
}
public static long getSumOfProperDivisors(long n)
{
if(n==0) return 0;
if(n==1) return 1;
int maxD = (int)Math.sqrt(n);
long sum=1;
for(int i=2;i<=maxD;i++)
{
if(n%i==0)
{
sum += i;
long d = n/i;
if(d!=i)
sum+=d;
}
}
return sum;
}
}