forked from Ciphrox/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwierd_number.java
More file actions
95 lines (95 loc) · 2.11 KB
/
wierd_number.java
File metadata and controls
95 lines (95 loc) · 2.11 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.io.*;
import java.util.*;
class Weird
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int n;
System.out.println("Enter Number");
n= in.nextInt();
if(isAbundant(n)==true && isSemiperfect(n)==false)
{
System.out.println("It is a Weird Number : "+n);
}
else
{
System.out.println("It is not a Weird Number : "+n);
}
}// end of main method
public static boolean isAbundant(int x)
{
int sum=0,i;
for(i=1;i<x;i++)
{
if(x%i==0)
{
sum=sum+i;
}
}
if(sum>x)
{
return true;
}
else
{
return false;
}
}// end of isAbundant()
public static boolean isSemiperfect(int x)
{
int c=0,i,j,k,z,a=0,s=0,d=0,temp;
for(i=1;i<x;i++)
{
if(x%i==0)
{
c++;//calculating no. of factors
}
}
z=(int) Math.pow(2,c); //calculating no. of subsets required
int ar[]=new int[z];
for(i=1;i<x;i++)
{
if(x%i==0)
{
ar[a]=i; //storing factors in Array
a++;
}
}
for(i=0;i<c;i++) //Loops for calculating sum of factors
{
for(j=0;j<c;j++)
{
for(k=j;k<c;k++)
{
s=s+ar[k];
if(s==x)
{
d=1;
break;
}
}
if(d==1)
{
break;
}
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
s=0;
}
if(d==1)
{
break;
}
}
if(d==1)
{
return true;
}
else
{
return false;
}
}// end of isSemiperfect()
}//end of class