Computer >> Computer tutorials >  >> Programming >> Java

Java Program to find Product of unique prime factors of a number


To find product of unique prime factors of a number, the Java code is as follows −

Example

public class Demo {
   public static long prime_factors(int num){
      long my_prod = 1;
      for (int i = 2; i <= num; i++){
         if (num % i == 0){
            boolean is_prime = true;
            for (int j = 2; j <= i / 2; j++){
               if (i % j == 0){
                  is_prime = false;
                  break;
               }
            }
            if (is_prime){
               my_prod = my_prod * i;
            }
         }
      }
      return my_prod;
   }
   public static void main(String[] args){
      int num = 68;
      System.out.println("The product of unique prime factors is ");
      System.out.print(prime_factors(num));
   }
}

Output

The product of unique prime factors is
34

A class named Demo contains a static function named ‘prime_factors’ that finds the prime factors of a number, find the unique numbers, and stores the product of these prime factors in a variable. In the main function, the value for the number is defined, and the function is called by passing the number as the parameter. Relevant message is displayed on the console.