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 – Reader to InputStream
Converting a Stack Trace to a String in Java
Introduction to Spring Security Expressions
Spring Boot - Eureka Server
Java Program to Implement Binary Search Tree
Java Program to Implement Gaussian Elimination Algorithm
Java Program to Find Inverse of a Matrix
Read an Outlook MSG file
Java Program to Implement Binomial Tree
Apache Camel with Spring Boot
The Registration API becomes RESTful
Mapping Nested Values with Jackson
Java Program to Implement LinkedHashSet API
Java Program to find the peak element of an array using Binary Search approach
Java Program to Implement Unrolled Linked List
Java Program to Implement LinkedBlockingDeque API
Guide to the Volatile Keyword in Java
Spring Boot with Multiple SQL Import Files
Spring Boot - Tomcat Port Number
HttpClient 4 – Follow Redirects for POST
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Converting a Stack Trace to a String in Java
Java 14 Record Keyword
Why String is Immutable in Java?
Spring Data JPA @Query
Hướng dẫn Java Design Pattern – Transfer Object
Marker Interface trong Java
How To Serialize and Deserialize Enums with Jackson
Constructor Injection in Spring with Lombok
Vector trong Java
Spring REST with a Zuul Proxy
Java Program to Perform Polygon Containment Test