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:
wait() and notify() Methods in Java
Introduction to Apache Commons Text
Java Program to Create a Random Graph Using Random Edge Generation
Ép kiểu trong Java (Type casting)
Java Program to Implement Hopcroft Algorithm
Introduction to the Java NIO2 File API
Java Program to Implement RenderingHints API
Java Program to Solve any Linear Equation in One Variable
Java Program to Implement Direct Addressing Tables
Concurrent Test Execution in Spring 5
A Custom Media Type for a Spring REST API
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Spring WebClient Filters
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Java Program to Implement Dijkstra’s Algorithm using Queue
Java Program to Implement Hamiltonian Cycle Algorithm
Most commonly used String methods in Java
Spring Boot - Application Properties
Java Optional as Return Type
Java equals() and hashCode() Contracts
Java Program to Implement AA Tree
Java Program to Implement ScapeGoat Tree
Java Program to Generate All Possible Combinations of a Given List of Numbers
Java Program to Implement Suffix Array
Cơ chế Upcasting và Downcasting trong java
Java Program to Implement Stack API
Java – Combine Multiple Collections
Introduction to PCollections
Java Program to Find All Pairs Shortest Path
DistinctBy in the Java Stream API
Calling Stored Procedures from Spring Data JPA Repositories
Converting Between an Array and a Set in Java