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:
Converting String to Stream of chars
Spring Cloud Series – The Gateway Pattern
Java Program to Perform Addition Operation Using Bitwise Operators
Java Program to Represent Graph Using Incidence Matrix
Prevent Cross-Site Scripting (XSS) in a Spring Application
Spring Boot - Code Structure
Spring Boot Gradle Plugin
Zipping Collections in Java
Java – Create a File
Call Methods at Runtime Using Java Reflection
Java Program to Implement Bit Array
Java Program to Implement Kosaraju Algorithm
Hướng dẫn sử dụng String Format trong Java
Returning Custom Status Codes from Spring Controllers
Spring Boot - Hystrix
Get the workstation name or IP
Setting Up Swagger 2 with a Spring REST API
Serialization và Deserialization trong java
Assertions in JUnit 4 and JUnit 5
Converting Between a List and a Set in Java
Hướng dẫn Java Design Pattern – Flyweight
Spring Boot - Creating Docker Image
Java Program to Implement Trie
Command-Line Arguments in Java
Overview of the java.util.concurrent
Removing all Nulls from a List in Java
Function trong Java 8
Java Program to Implement PriorityBlockingQueue API
Java Program to Find kth Largest Element in a Sequence
Java 8 Collectors toMap
Java Program to Implement ArrayList API
Java Program to implement Associate Array