forked from kanchanachathuranga/Hacktoberfest-2022-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHoaxNumber.java
103 lines (89 loc) · 2.58 KB
/
HoaxNumber.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Java code to check if a number is
// a hoax number or not.
import java.io.*;
import java.util.*;
public class HoaxNumber{
// Function to find distinct
// prime factors of given
// number n
static List<Integer> primeFactors(int n)
{
List<Integer> res = new ArrayList<Integer>();
if (n % 2 == 0)
{
while (n % 2 == 0)
n = n / 2;
res.add(2);
}
// n is odd at this point,
// since it is no longer
// divisible by 2. So we
// can test only for the
// odd numbers, whether they
// are factors of n
for (int i = 3; i <= Math.sqrt(n);
i = i + 2)
{
// Check if i is prime factor
if (n % i == 0)
{
while (n % i == 0)
n = n / i;
res.add(i);
}
}
// This condition is to
// handle the case when
// n is a prime number
// greater than 2
if (n > 2)
res.add(n);
return res;
}
// Function to calculate
// sum of digits of distinct
// prime factors of given
// number n and sum of
// digits of number n and
// compare the sums obtained
static boolean isHoax(int n)
{
// Distinct prime factors
// of n are being
// stored in vector pf
List<Integer> pf = primeFactors(n);
// If n is a prime number,
// it cannot be a hoax number
if (pf.get(0) == n)
return false;
// Finding sum of digits of distinct
// prime factors of the number n
int all_pf_sum = 0;
for (int i = 0; i < pf.size(); i++)
{
// Finding sum of digits in current
// prime factor pf[i].
int pf_sum;
for (pf_sum = 0; pf.get(i) > 0;
pf_sum += pf.get(i) % 10,
pf.set(i,pf.get(i) / 10));
all_pf_sum += pf_sum;
}
// Finding sum of digits of number n
int sum_n;
for (sum_n = 0; n > 0; sum_n += n % 10,
n /= 10)
;
// Comparing the two calculated sums
return sum_n == all_pf_sum;
}
// Driver Code
public static void main(String args[])
{
int n = 84;
if (isHoax(n))
System.out.print( "A Hoax Number\n");
else
System.out.print("Not a Hoax Number\n");
}
}