-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathUntouchable Numbers.java
More file actions
62 lines (53 loc) · 1.2 KB
/
Copy pathUntouchable Numbers.java
File metadata and controls
62 lines (53 loc) · 1.2 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
58
59
60
61
62
import java.util.*;
// Java program for the above approach
class Main{
// Function to calculate sum of
// all proper divisors of num
static int divSum(int num)
{
// Final result of summation
// of divisors
int result = 0;
// Find all divisors of num
for(int i = 2; i <= Math.sqrt(num); i++)
{
// If 'i' is divisor of 'num'
if (num % i == 0)
{
// If both divisors are same
// then add it only once else
// add both
if (i == (num / i))
result += i;
else
result += (i + num / i);
}
}
// Add 1 to the result as
// 1 is also a divisor
return (result + 1);
}
// Function to check if N is a
// Untouchable Number
static boolean isUntouchable(int n)
{
for(int i = 1; i <= 2 * n; i++)
{
if (divSum(i) == n)
return false;
}
return true;
}
// Driver code
public static void main(String[] args)
{
// Given Number N
int n;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
// Function Call
for (int i =0 ; i<=n; i++)
if (isUntouchable(i))
System.out.print(i+" ");
}
}