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:
Migrating from JUnit 4 to JUnit 5
Allow user:password in URL
Spring AMQP in Reactive Applications
So sánh ArrayList và Vector trong Java
Java Program to Solve Knapsack Problem Using Dynamic Programming
Guide to Mustache with Spring Boot
Java IO vs NIO
Java Program to Implement Interval Tree
Spring REST API + OAuth2 + Angular
Java Program to Perform Sorting Using B-Tree
Java Program to Check the Connectivity of Graph Using BFS
Spring Boot - Google OAuth2 Sign-In
Java Program to Implement Depth-limited Search
Java Program to Perform Partition of an Integer in All Possible Ways
Custom JUnit 4 Test Runners
Giới thiệu Design Patterns
A Guide to Java HashMap
Java Program to Implement Disjoint Set Data Structure
Spring Security – Reset Your Password
Call Methods at Runtime Using Java Reflection
Guide to the Java Clock Class
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Java Program to implement Array Deque
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Quick Guide to Spring MVC with Velocity
More Jackson Annotations
Creating a Web Application with Spring 5
Properties with Spring and Spring Boot
Java Program to Implement vector
Spring Boot With H2 Database
Hướng dẫn Java Design Pattern – Command
Java Program to Implement Sieve Of Sundaram