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:
Model, ModelMap, and ModelAndView in Spring MVC
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Tìm hiểu cơ chế Lazy Evaluation của Stream trong Java 8
Feign – Tạo ứng dụng Java RESTful Client
Java Program to Implement Heap Sort Using Library Functions
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Java Program to Implement Repeated Squaring Algorithm
Quick Guide to Spring Controllers
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
An Intro to Spring Cloud Contract
The Modulo Operator in Java
Java Copy Constructor
Java Program to Implement HashSet API
Java Program to Implement String Matching Using Vectors
Integer Constant Pool trong Java
XML-Based Injection in Spring
Hướng dẫn sử dụng Java Reflection
Java Program to Implement Direct Addressing Tables
Giới thiệu thư viện Apache Commons Chain
Java Program to Implement Hash Tables
New Features in Java 10
Spring Data JPA @Query
HttpAsyncClient Tutorial
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
Template Engines for Spring
A Guide to Apache Commons Collections CollectionUtils
Java – Convert File to InputStream
Spring Boot - Logging
Java Program to Perform Partition of an Integer in All Possible Ways
A Custom Data Binder in Spring MVC
Java Program to Implement Binomial Tree
Hướng dẫn Java Design Pattern – Memento