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 - OAuth2 with JWT
Runnable vs. Callable in Java
Cơ chế Upcasting và Downcasting trong java
List Interface trong Java
Tạo chương trình Java đầu tiên sử dụng Eclipse
Chuyển đổi Array sang ArrayList và ngược lại
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Java Program to Implement Hash Tables Chaining with List Heads
Automatic Property Expansion with Spring Boot
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Java Program to Solve any Linear Equation in One Variable
Spring Boot - Creating Docker Image
Debug a HttpURLConnection problem
How to Convert List to Map in Java
Java Program to Perform Cryptography Using Transposition Technique
Spring Cloud – Tracing Services with Zipkin
Chương trình Java đầu tiên
Java Program for Topological Sorting in Graphs
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Map Serialization and Deserialization with Jackson
Java Program to Implement Sieve Of Atkin
Spring – Injecting Collections
Simultaneous Spring WebClient Calls
Introduction to Spring Data JDBC
ExecutorService – Waiting for Threads to Finish
Java Program to Perform String Matching Using String Library
Java List UnsupportedOperationException
Spring Web Annotations
Generic Constructors in Java
Copy a List to Another List in Java
Easy Ways to Write a Java InputStream to an OutputStream
Jackson vs Gson