Jhon is doing research on special kind of numbers called Friendly Numbers

Jhon is doing research on special kind of numbers called Friendly Numbers. Friendly numbers are pair of numbers such that sum of all proper divisor of the first number(excluding number itself) exactly equal to the second number and sum of all proper divisor of the second number(excluding number itself) exactly equals to the first number.

 

Write a Java program that takes  two numbers as command line argument and display whether they are friendly numbers or not. 

Input

Command Line Argument: 2 numbers Output Format:

[Numbers are Friendly] or [Numbers are not Friendly] or [Missing required inputs]

 

If user does not supply 2 numbers as argument output "Missing required inputs" 

Example


Example 1: Command Line Input: 220 284

Output: Numbers are Friendly 


Example 2: Command Line Input: 5030 5570

Output: Numbers are not Friendly

Example 3: Command Line Input: 2620 2924

Output: Numbers are Friendly

solution

//program - 4
import java.io.*;
import java.util.Scanner;


public class Main
{
    // Function to calculate sum of all
    // proper divisors of a given number
    static int divSum(int n)
    {
        // Sum of divisors
        int result = 0;
    
        // find all divisors which divides 'num'
        for (int i = 2; i <= Math.sqrt(n); i++)
        {
            // if 'i' is divisor of 'n'
            if (n % i == 0)
            {
                // if both divisors are same
                // then add it once else add
                // both
                if (i == (n / i))
                    result += i;
                else
                    result += (i + n / i);
            }
        }
    
        // Add 1 and n to result as above loop
        // considers proper divisors greater
        // than 1.
        return (result + 1);
    }
    
    // Returns true if x and y are Amicable
    // else false.
    static boolean areAmicable(int xint y)
    {
        if (divSum(x) != y)
        return false;
    
        return (divSum(y) == x);
    }
    
    public static void main (String[] args)
    {
        Scanner inputnew Scanner(System.in);
        int x = input.nextInt();
        int y = input.nextInt();

        if (areAmicable(x, y))
        System.out.println"Numbers are Friendly");
        else
        System.out.println("Numbers are not Friendly");
    
    }
}


/*
Output :
220                                                                                                                           
284                                                                                                                           
Numbers are Friendly    
*/

No comments:

Get new posts by email:


APY Logo