Friday, 17 November 2017

Java Program to Perform the Unique Factorization of a Given Number


Code:

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Unique_Prime_Factors 
{
    static Set primeFactors(long number) 
    {
        long copy = number, i;
Set primeFactor = new HashSet<>();
        for (i = 2; i <= copy; i++) 
        {
            if (copy % i == 0) 
            {
                primeFactor.add(i);
                copy /= i;
                i--;
            }
        }
        return primeFactor;
    }

    public static void main(String args[]) 
    {
        Scanner input = new Scanner(System.in);
        long n;
        System.out.println("Enter the number: ");
        n = input.nextLong();
        System.out.println("The Prime Factors of " + n + " is: "
+ primeFactors(n));
    }
}


Output:

Enter the number: 
35
The Prime Factors of 35 is: [5, 7]

Enter the number: 
1225
The Prime Factors of 1225 is: [5, 7]


More Java Programs:












100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...