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:
Java Program to Implement Sieve Of Atkin
Java List UnsupportedOperationException
Java Program to Implement Warshall Algorithm
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
HTTP Authentification and CGI/Servlet
Spring Boot - Building RESTful Web Services
Java Program to Implement Pairing Heap
Guide to the Synchronized Keyword in Java
Spring WebClient and OAuth2 Support
Serialize Only Fields that meet a Custom Criteria with Jackson
A Quick Guide to Spring Cloud Consul
Java Program to Implement Karatsuba Multiplication Algorithm
Converting Between a List and a Set in Java
Getting Started with GraphQL and Spring Boot
Copy a List to Another List in Java
A Guide to Java 9 Modularity
Java Program to Implement Cubic convergence 1/pi Algorithm
Call Methods at Runtime Using Java Reflection
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Java Program to Implement Range Tree
Hướng dẫn Java Design Pattern – Facade
Java Program to Implement TreeSet API
Java Program to Implement Rolling Hash
Most commonly used String methods in Java
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
Java Program to Implement Skew Heap
Java Program to Implement Jarvis Algorithm
Sending Emails with Java
Guide to Guava Table
Finding the Differences Between Two Lists in Java
Java Program to Find the Edge Connectivity of a Graph
Guide to CopyOnWriteArrayList