Weird Number

Weird Number in Java

Question : Write a program to accept a number and check whether it is Weird number or not.

Weird Number : A Weird number is a natural number that is Abundant but not Semi-Perfect.

In other words, the sum of the proper divisors (divisors including 1 but not itself) of the number is greater than the number, but no subset of those divisors sums to the number itself. 

First few Weird Numbers : 70, 836, 4030, 5830, 7192, 7912, 9272, 10430, 10570, 10792, 10990, 11410, 11690

Abundant Number : A number ‘n’ is said to be Abundant Number if sum of all the proper divisors of the number is greater than the number ‘n’.


Semi-Perfect Number : A Semi-Perfect number or Pseudo-Perfect number is a natural number n that is equal to the sum of all or some of its proper divisors. 



Program to check whether the number is Weird Number or not :-

Program :-

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


For Proper Understanding Watch the Video :-




Watch this video : Weird Number in Java



All the best :)
Keep Learning :)

Comments

Popular posts from this blog

Composite Number in JAVA

Frequency of each digit of a number in Java

Fibonacci Prime Series in JAVA