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:
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
Supplier trong Java 8
Giới thiệu Google Guice – Binding
Guide to Escaping Characters in Java RegExps
LinkedHashSet trong Java hoạt động như thế nào?
Java Program to Construct an Expression Tree for an Infix Expression
Java Program to Perform Complex Number Multiplication
Mệnh đề if-else trong java
Java Program to Perform the Shaker Sort
Easy Ways to Write a Java InputStream to an OutputStream
Java Program to Implement ArrayDeque API
How to Find an Element in a List with Java
Java Switch Statement
Java – Reader to String
Compact Strings in Java 9
Spring Security OAuth2 – Simple Token Revocation
Apache Commons Collections Bag
Introduction to Spring Data JPA
The HttpMediaTypeNotAcceptableException in Spring MVC
Guava – Join and Split Collections
Spring Boot - Introduction
Hướng dẫn Java Design Pattern – Mediator
Spring MVC Tutorial
Introduction to Spring Security Expressions
Spring Boot - Thymeleaf
Java Program to Implement Aho-Corasick Algorithm for String Matching
Java Program to Implement Lloyd’s Algorithm
Running Spring Boot Applications With Minikube
Java Program to Implement ArrayBlockingQueue API
XML Serialization and Deserialization with Jackson
Java Program to Generate Date Between Given Range
Hướng dẫn Java Design Pattern – Flyweight