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:
Prevent Brute Force Authentication Attempts with Spring Security
SOAP Web service: Authentication trong JAX-WS
Java InputStream to String
Hashtable trong java
Java Program to Solve any Linear Equations
Java Program to Find the Vertex Connectivity of a Graph
A Guide to @RepeatedTest in Junit 5
Spring MVC Content Negotiation
Guide to the Volatile Keyword in Java
Basic Authentication with the RestTemplate
Guide to BufferedReader
Spring AMQP in Reactive Applications
Guide to the Java ArrayList
Binary Numbers in Java
Java Program to Perform Insertion in a BST
Java Program to Perform the Shaker Sort
Java Program to Create a Random Graph Using Random Edge Generation
Removing all Nulls from a List in Java
Java Program to Use rand and srand Functions
Functional Interfaces in Java 8
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Spring Boot - Building RESTful Web Services
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Java Program to Perform Addition Operation Using Bitwise Operators
Java Program to Find Maximum Element in an Array using Binary Search
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Rate Limiting in Spring Cloud Netflix Zuul
Java Program to Solve TSP Using Minimum Spanning Trees
Java Program to Implement Knight’s Tour Problem
Jackson – Bidirectional Relationships
Java Program to Implement the Bin Packing Algorithm