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:
OAuth 2.0 Resource Server With Spring Security 5
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Convert String to int or Integer in Java
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Spring @RequestParam Annotation
Java Program to Implement Sieve Of Sundaram
A Guide to WatchService in Java NIO2
A Guide to TreeSet in Java
Hướng dẫn Java Design Pattern – Template Method
JUnit5 @RunWith
Java Program to Compute the Area of a Triangle Using Determinants
Java Program to Implement Bellman-Ford Algorithm
Converting Strings to Enums in Java
Java Program to Generate Date Between Given Range
Java Program to Implement SimpeBindings API
Spring Webflux with Kotlin
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Implementing a Runnable vs Extending a Thread
Java InputStream to String
The Spring @Controller and @RestController Annotations
Create Java Applet to Simulate Any Sorting Technique
Java Program to Check whether Directed Graph is Connected using BFS
Java Program to Implement Word Wrap Problem
Testing in Spring Boot
A Guide to Spring Cloud Netflix – Hystrix
Java Program to Describe the Representation of Graph using Adjacency Matrix
Hướng dẫn Java Design Pattern – Service Locator
Kết hợp Java Reflection và Java Annotations
Handle EML file with JavaMail
Predicate trong Java 8
Spring Security 5 – OAuth2 Login
REST Web service: Basic Authentication trong Jersey 2.x