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:
A Guide to JPA with Spring
Overflow and Underflow in Java
Java Program to Implement Repeated Squaring Algorithm
Binary Numbers in Java
A Quick Guide to Spring Cloud Consul
Daemon Threads in Java
Java Program to Check if it is a Sparse Matrix
CyclicBarrier in Java
The Thread.join() Method in Java
Life Cycle of a Thread in Java
Guide to the Volatile Keyword in Java
Java Program to Find the Edge Connectivity of a Graph
List Interface trong Java
Java – Combine Multiple Collections
Notify User of Login From New Device or Location
Apache Commons Collections MapUtils
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Collect a Java Stream to an Immutable Collection
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Java Program to Implement Cubic convergence 1/pi Algorithm
Java Program to Implement ArrayBlockingQueue API
Java Program to Implement Ternary Heap
Java Timer
Java Program to Perform Partition of an Integer in All Possible Ways
Java Scanner hasNext() vs. hasNextLine()
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Java Program to Implement Regular Falsi Algorithm
Annotation trong Java 8
Java Program to Perform Naive String Matching
Spring MVC Content Negotiation
Java Program to Implement Nth Root Algorithm
Java Program to Find the Longest Path in a DAG