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:
Guide to Spring 5 WebFlux
Properties with Spring and Spring Boot
Spring MVC + Thymeleaf 3.0: New Features
A Guide to Java 9 Modularity
Guide to the ConcurrentSkipListMap
Quick Guide on Loading Initial Data with Spring Boot
Java Program to Implement Patricia Trie
Lớp Arrarys trong Java (Arrays Utility Class)
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Map Interface trong java
Send email with JavaMail
A Guide to @RepeatedTest in Junit 5
CharSequence vs. String in Java
Java Program to Implement the Bin Packing Algorithm
Java Program to Describe the Representation of Graph using Adjacency List
Spring NoSuchBeanDefinitionException
Guide to Mustache with Spring Boot
Overview of the java.util.concurrent
Calling Stored Procedures from Spring Data JPA Repositories
Introduction to Project Reactor Bus
Using a Spring Cloud App Starter
Java Program to Compute Cross Product of Two Vectors
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Java Program to Solve the 0-1 Knapsack Problem
Java Program to Perform Insertion in a 2 Dimension K-D Tree
Simplify the DAO with Spring and Java Generics
Xử lý ngoại lệ trong Java (Exception Handling)
Java Program to do a Depth First Search/Traversal on a graph non-recursively
Java Program to Implement Shell Sort
Java Program to Implement Leftist Heap
Deploy a Spring Boot WAR into a Tomcat Server
Spring WebClient Filters