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 Boot - Code Structure
Bootstrapping Hibernate 5 with Spring
Composition, Aggregation, and Association in Java
Autoboxing và Unboxing trong Java
A Guide to LinkedHashMap in Java
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Introduction to the Java ArrayDeque
Interface trong Java 8 – Default method và Static method
Java Program to Implement CopyOnWriteArraySet API
Java Program to Implement K Way Merge Algorithm
Redirect to Different Pages after Login with Spring Security
Java Program to Permute All Letters of an Input String
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Request a Delivery / Read Receipt in Javamail
Spring Cloud Bus
Java Program to Implement CountMinSketch
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Implement Bucket Sort
Java Program to Implement Adjacency Matrix
Java Program to Implement Circular Singly Linked List
Spring – Injecting Collections
A Guide to Concurrent Queues in Java
So sánh HashMap và Hashtable trong Java
Testing an OAuth Secured API with Spring MVC
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
How to Round a Number to N Decimal Places in Java
Java – Byte Array to Reader
Setting a Request Timeout for a Spring REST API
Using a List of Values in a JdbcTemplate IN Clause
Recommended Package Structure of a Spring Boot Project
HttpClient Basic Authentication