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 Hash Trie
Spring Boot - Hystrix
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Java 14 Record Keyword
Model, ModelMap, and ModelAndView in Spring MVC
Send email with SMTPS (eg. Google GMail)
Java Program to Implement Double Order Traversal of a Binary Tree
Spring Boot Security Auto-Configuration
Từ khóa throw và throws trong Java
Sending Emails with Java
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Serialize Only Fields that meet a Custom Criteria with Jackson
Toán tử instanceof trong java
Removing Elements from Java Collections
Serialization và Deserialization trong java
Java Program to Check Whether a Directed Graph Contains a Eulerian Path
Map to String Conversion in Java
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
TreeSet và sử dụng Comparable, Comparator trong java
Convert String to int or Integer in Java
Spring Boot - CORS Support
Auditing with JPA, Hibernate, and Spring Data JPA
Hướng dẫn Java Design Pattern – Interpreter
Quick Intro to Spring Cloud Configuration
REST Web service: Upload và Download file với Jersey 2.x
Java Program to Implement Splay Tree
New Features in Java 15
Spring Security with Maven
Mockito and JUnit 5 – Using ExtendWith
Java Program to Implement Binomial Tree
Prevent Brute Force Authentication Attempts with Spring Security
Service Registration with Eureka