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:

Java Streams vs Vavr Streams
Java Program to implement Associate Array
Java Program to Show the Duality Transformation of Line and Point
Java Program to Perform Searching Based on Locality of Reference
Java Program to Implement LinkedTransferQueue API
Java Program to Implement Hash Tables Chaining with List Heads
Mapping Nested Values with Jackson
Java Stream Filter with Lambda Expression
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Java Program to Implement Meldable Heap
Sorting in Java
JPA/Hibernate Persistence Context
Java Program to Implement Fenwick Tree
Java Program to Generate a Sequence of N Characters for a Given Specific Case
How to Read HTTP Headers in Spring REST Controllers
RegEx for matching Date Pattern in Java
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Java Program to Implement Segment Tree
Java Program to Find the GCD and LCM of two Numbers
Getting a File’s Mime Type in Java
Spring WebClient vs. RestTemplate
HttpClient Connection Management
Giới thiệu thư viện Apache Commons Chain
Intro to Inversion of Control and Dependency Injection with Spring
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
Java Program to Solve the Fractional Knapsack Problem
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Implement Shoelace Algorithm
Getting Started with Forms in Spring MVC
Java Program to Perform Sorting Using B-Tree
Java Program to Generate a Graph for a Given Fixed Degree Sequence