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:
Hướng dẫn Java Design Pattern – Builder
Java Program to Implement Patricia Trie
Java 8 StringJoiner
A Guide to the Java ExecutorService
Java Program to Implement Heap
Integer Constant Pool trong Java
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Giới thiệu Aspect Oriented Programming (AOP)
Hướng dẫn Java Design Pattern – Flyweight
Hướng dẫn Java Design Pattern – Prototype
Registration – Password Strength and Rules
Java TreeMap vs HashMap
JUnit5 Programmatic Extension Registration with @RegisterExtension
Java Program to Create a Random Graph Using Random Edge Generation
Java 8 Stream findFirst() vs. findAny()
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Join and Split Arrays and Collections in Java
REST Web service: Upload và Download file với Jersey 2.x
Spring Boot Change Context Path
Java Program to Solve TSP Using Minimum Spanning Trees
Send email with SMTPS (eg. Google GMail)
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Java Stream Filter with Lambda Expression
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Sorting Query Results with Spring Data
Transaction Propagation and Isolation in Spring @Transactional
Properties with Spring and Spring Boot
Java Program to Implement Regular Falsi Algorithm
A Quick Guide to Using Keycloak with Spring Boot
Java Program to Implement Nth Root Algorithm
Spring Boot - Cloud Configuration Server
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence