Java Program to Perform the Unique Factorization of a Given Number

This is the java program to find out all the prime factors of a given number. Any number can be represented as a product of its prime numbers. User have to input the number and output is the list of prime factors.

Here is the source code of the Java Program to perform the unique factorization of a given number. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

//This is sample program to find out all the prime factors of a given number
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:

$ javac Unique_Prime_Factors.java
$ java Unique_Prime_Factors
Enter the number: 
35
The Prime Factors of 35 is: [5, 7]
 
Enter the number: 
1225
The Prime Factors of 1225 is: [5, 7]

Related posts:

String Operations with Java Streams
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Java Program to Implement HashSet API
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Checked and Unchecked Exceptions in Java
Java Program to Implement the String Search Algorithm for Short Text Sizes
Quick Guide to Spring Bean Scopes
Giới thiệu Google Guice – Binding
Spring Security Login Page with React
Java Program to Implement Lloyd’s Algorithm
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Tránh lỗi NullPointerException trong Java như thế nào?
Finding Max/Min of a List or Collection
Một số tính năng mới về xử lý ngoại lệ trong Java 7
A Guide to Spring Cloud Netflix – Hystrix
The XOR Operator in Java
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Java Program to Create the Prufer Code for a Tree
Custom HTTP Header with the HttpClient
Spring Cloud – Adding Angular
Tính trừu tượng (Abstraction) trong Java
Java Program to Implement Insertion Sort
Using JWT with Spring Security OAuth (legacy stack)
Java Program to Implement Disjoint Sets
Java Program to Implement Counting Sort
Handling Errors in Spring WebFlux
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
Getting a File’s Mime Type in Java
Multi Dimensional ArrayList in Java
How to Change the Default Port in Spring Boot