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:
Spring Boot - Exception Handling
Java Program to Implement Gauss Jordan Elimination
Copy a List to Another List in Java
Spring Boot - Enabling Swagger2
Easy Ways to Write a Java InputStream to an OutputStream
Java Program to Implement Attribute API
Lớp Collectors trong Java 8
Netflix Archaius with Various Database Configurations
Marker Interface trong Java
Java Program to Implement a Binary Search Tree using Linked Lists
Count Occurrences of a Char in a String
String Joiner trong Java 8
Hướng dẫn Java Design Pattern – Observer
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Java Program to Implement Sorted Circularly Singly Linked List
Java Program to Represent Linear Equations in Matrix Form
Spring Boot Application as a Service
A Guide to EnumMap
The DAO with Spring and Hibernate
Get the workstation name or IP
Java Program to Use rand and srand Functions
Java Program to Implement Interpolation Search Algorithm
Dockerizing a Spring Boot Application
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
Java Program to Implement Heap
Hướng dẫn Java Design Pattern – Proxy
Java Program to Implement Segment Tree
Spring Boot: Customize Whitelabel Error Page
Java Program to Implement Quick sort
Upload and Display Excel Files with Spring MVC
Server-Sent Events in Spring
Java Program to Perform LU Decomposition of any Matrix