-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuffinian.java
More file actions
46 lines (41 loc) · 1.05 KB
/
Duffinian.java
File metadata and controls
46 lines (41 loc) · 1.05 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
import java.util.*;
public class Duffinian //It is a composite number so that it(a) is relatively prime to the sum of divisors(b) of n
{
//sum of divisors of n
static int sum_div(int n)// n=35
{
int div =0;
for(int i =1; i<= Math.sqrt(n); i++)// i =1..5
{
if(n%i==0)// 35%5==0
{
div = div + i + (n/i); // div=36+5+7=48
}
}
return div; // div =48
}
static int gcd(int a, int b)// a=35, b=48
{
int gcd = 1;
for(int i = 1; i<= a && i<=b; i++) // i=36 -> f
{
if(a%i==0 && b%i==0)// 35%35==0 && 48%35==0
gcd=i; // gcd =1
}
return gcd; // gcd =1
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("enter number");
int a =sc.nextInt(); // n= 35
int b = sum_div(a); // s = 48
System.out.println("sum of divisors :"+b);
int gcd = gcd(a,b);
System.out.println("gcd = "+gcd);
if (gcd ==1) // ->t , if relatively prime
System.out.println("Duffinian Number");
else
System.out.println("Not Duffinian Number");
}
}