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:
REST Web service: Upload và Download file với Jersey 2.x
Introduction to Project Reactor Bus
Working with Kotlin and JPA
Java Program to Implement Bresenham Line Algorithm
Java Program to Implement Cartesian Tree
How to Use if/else Logic in Java 8 Streams
“Stream has already been operated upon or closed” Exception in Java
Spring Security Custom AuthenticationFailureHandler
Finding Max/Min of a List or Collection
JUnit 5 for Kotlin Developers
Spring Boot - Service Components
Java Program to Describe the Representation of Graph using Incidence List
Java Program to Implement Vector API
Java Program to Implement Hash Tables with Quadratic Probing
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Java program to Implement Tree Set
Mix plain text and HTML content in a mail
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Spring Boot - Eureka Server
Java Web Services – JAX-WS – SOAP
Spring Boot - Creating Docker Image
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Spring Boot - Application Properties
Introduction to Spring Method Security
Java Program to Implement Booth Algorithm
Lớp Properties trong java
Spring REST API + OAuth2 + Angular
Spring Boot - Rest Template
Java Program to Perform LU Decomposition of any Matrix
Spring Boot - Internationalization
Spring MVC Setup with Kotlin
Hướng dẫn sử dụng luồng vào ra ký tự trong Java