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:
Introduction to the Java NIO Selector
Remove HTML tags from a file to extract only the TEXT
Java Program to Implement Binary Search Tree
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Spring Boot - Application Properties
Encode/Decode to/from Base64
Predicate trong Java 8
Java Program to Solve a Matching Problem for a Given Specific Case
Handle EML file with JavaMail
Java Program to Evaluate an Expression using Stacks
Java Program to Convert a Decimal Number to Binary Number using Stacks
Java Stream Filter with Lambda Expression
Serverless Functions with Spring Cloud Function
Spring Boot - CORS Support
Collection trong java
The HttpMediaTypeNotAcceptableException in Spring MVC
Java Program to Implement Queue using Linked List
Java Program to Represent Graph Using Incidence List
Static Content in Spring WebFlux
Java Program to Perform Right Rotation on a Binary Search Tree
Using a List of Values in a JdbcTemplate IN Clause
Spring MVC Tutorial
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Examine the internal DNS cache
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
A Guide to HashSet in Java
Debug a JavaMail Program
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Implement Stack using Linked List
Introduction to Spring Method Security
How to Store Duplicate Keys in a Map in Java?
How to Find an Element in a List with Java