-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmithNumber.java
More file actions
81 lines (73 loc) · 1.21 KB
/
SmithNumber.java
File metadata and controls
81 lines (73 loc) · 1.21 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.Scanner;
class SmithNumber{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a Number");
if (isSmithNo(sc.nextInt())) {
System.out.println("It is Smith Number");
}
else {
System.out.println("It is not a Smith Number");
}
}
public static boolean isSmithNo(int no){
if (no<=1) {
return false;
}
return DigitSum(no)==primeFactorSum(no);
}
public static int primeFactorSum(int no){
int sum=0;
int i=2;
int n=no;
while(no>1) {
if (i==n) {
break;
}
else if(no%i==0){
if (isPrime(i)) {
if (countOfDigit(i)>1) {
i=DigitSum(i);
}
no/=i;
sum+=i;
}
i=2;
}
else
i++;
}
return sum;
}
public static int countOfDigit(int no){
int ct=0;
if(no>0){
while(no>0){
ct++;
no/=10;
}
}
return ct;
}
public static int DigitSum(int no) {
int sum=0;
int ct=countOfDigit(no);
while(no>0){
int last=no%10;
sum=sum+last;
no/=10;
}
if (no==0 && countOfDigit(sum)>1) {
sum=DigitSum(sum);
}
return sum;
}
public static boolean isPrime(int no){
for (int i=2;i<=no/2 ;i++ ) {
if(no%i==0){
return false;
}
}
return true;
}
}