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 - Tracing Micro Service Logs
Hướng dẫn Java Design Pattern – Adapter
Java Program to Generate Random Numbers Using Middle Square Method
So sánh HashMap và Hashtable trong Java
Working with Tree Model Nodes in Jackson
Disable Spring Data Auto Configuration
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Java Program to Implement Treap
Creating a Custom Starter with Spring Boot
How to Break from Java Stream forEach
Simultaneous Spring WebClient Calls
Debugging Reactive Streams in Java
Intro to Spring Boot Starters
Java Program to Implement SynchronosQueue API
Java Program to Evaluate an Expression using Stacks
Giới thiệu về Stream API trong Java 8
Spring REST API with Protocol Buffers
The Difference Between map() and flatMap()
Easy Ways to Write a Java InputStream to an OutputStream
Java Program to Generate All Possible Combinations of a Given List of Numbers
Sử dụng CyclicBarrier trong Java
Java Program to Implement Brent Cycle Algorithm
Java – Try with Resources
Tìm hiểu về Web Service
Java Program to Implement Efficient O(log n) Fibonacci generator
Spring Boot - Thymeleaf
Hướng dẫn tạo và sử dụng ThreadPool trong Java
How to Store Duplicate Keys in a Map in Java?
Hướng dẫn sử dụng luồng vào ra ký tự trong Java
ETL with Spring Cloud Data Flow
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Java Program to Implement Counting Sort