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:
Introduction to the Java ArrayDeque
Remove the First Element from a List
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Convert XML to JSON Using Jackson
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
ClassNotFoundException vs NoClassDefFoundError
Java 8 and Infinite Streams
Redirect to Different Pages after Login with Spring Security
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Java Program to Implement Skew Heap
A Guide to the ResourceBundle
Java Program to Implement vector
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Spring Boot - Quick Start
Java Program to Implement Gaussian Elimination Algorithm
Registration – Password Strength and Rules
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Java Program to Find the GCD and LCM of two Numbers
Spring Cloud Series – The Gateway Pattern
Converting Strings to Enums in Java
Guide to WeakHashMap in Java
Java Program to Find kth Largest Element in a Sequence
A Guide to Queries in Spring Data MongoDB
Java Program to Implement Hash Tables with Quadratic Probing
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
Lập trình mạng với java
Java Program to Implement Triply Linked List
Java Program to Implement Network Flow Problem
Introduction to Using Thymeleaf in Spring
Java Timer
How to Use if/else Logic in Java 8 Streams