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:

Jackson – JsonMappingException (No serializer found for class)
Java Program to Implement HashTable API
Partition a List in Java
Introduction to PCollections
Từ khóa throw và throws trong Java
Guide to Java 8 groupingBy Collector
Jackson – Unmarshall to Collection/Array
Java Program to Perform Addition Operation Using Bitwise Operators
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Java Program to Implement Flood Fill Algorithm
Java Program to Implement Skew Heap
Introduction to Project Reactor Bus
Debugging Reactive Streams in Java
JUnit5 @RunWith
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
Set Interface trong Java
Java Program to Check Cycle in a Graph using Graph traversal
Exploring the Spring 5 WebFlux URL Matching
Java equals() and hashCode() Contracts
Spring’s RequestBody and ResponseBody Annotations
Java Program to Implement Pairing Heap
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Guide to ThreadLocalRandom in Java
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Java Program to Implement Fenwick Tree
Java Program to Check whether Directed Graph is Connected using BFS
Spring’s RequestBody and ResponseBody Annotations
DynamoDB in a Spring Boot Application Using Spring Data
Java – Convert File to InputStream
Life Cycle of a Thread in Java
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Java Program to Perform Cryptography Using Transposition Technique