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:
Spring REST API + OAuth2 + Angular
ArrayList trong java
REST Web service: Basic Authentication trong Jersey 2.x
Java Program to Optimize Wire Length in Electrical Circuit
Compare Two JSON Objects with Jackson
Introduction to the Java NIO2 File API
Java Program to Solve any Linear Equation in One Variable
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Java Program to Implement SynchronosQueue API
Java Program to Implement HashSet API
Spring @Primary Annotation
Serialization và Deserialization trong java
Spring Security 5 for Reactive Applications
Difference Between Wait and Sleep in Java
What is a POJO Class?
Spring Boot Integration Testing with Embedded MongoDB
Java Program to Implement Hash Tables Chaining with Binary Trees
Một số ký tự đặc biệt trong Java
Các nguyên lý thiết kế hướng đối tượng – SOLID
Programmatic Transaction Management in Spring
Java Web Services – JAX-WS – SOAP
Redirect to Different Pages after Login with Spring Security
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Class Loaders in Java
Quick Guide on Loading Initial Data with Spring Boot
An Intro to Spring Cloud Contract
Phương thức tham chiếu trong Java 8 – Method References
Converting a Stack Trace to a String in Java
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Model, ModelMap, and ModelAndView in Spring MVC
Java String Conversions