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:
HttpClient Basic Authentication
Guide to java.util.Formatter
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Hướng dẫn sử dụng String Format trong Java
Java Program to Describe the Representation of Graph using Adjacency Matrix
Spring Cloud – Securing Services
Unsatisfied Dependency in Spring
Encode a String to UTF-8 in Java
SOAP Web service: Authentication trong JAX-WS
Spring Webflux with Kotlin
Spring MVC Tutorial
HttpAsyncClient Tutorial
Spring Boot - Enabling HTTPS
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Spring Boot - Bootstrapping
Java Program to Search Number Using Divide and Conquer with the Aid of Fibonacci Numbers
How to Return 404 with Spring WebFlux
Spring Security – Reset Your Password
Java – Reader to InputStream
Java Program to Implement Randomized Binary Search Tree
Quick Guide to java.lang.System
Java InputStream to String
Hướng dẫn Java Design Pattern – Decorator
Java Program to Implement Aho-Corasick Algorithm for String Matching
Debug a JavaMail Program
List Interface trong Java
Java Program to Find Maximum Element in an Array using Binary Search
Jackson – Unmarshall to Collection/Array
Generating Random Numbers in a Range in Java
Split a String in Java
Java Program to Implement Dijkstra’s Algorithm using Priority Queue