Queries to find whether a number has exactly four distinct factors or not in C++


In this problem, we are given a Q number of queries, each having a number N. Our task is to create a program to solve the Queries to find whether a number has exactly four distinct factors or not in C++.

Problem Description

To solve each query, we need to find whether the number N has exactly four distinct factors. If it has print YES, else No.

Let’s take an example to understand the problem,

Input: Q = 3, 4, 6, 15 

Output: NOYES YES

Explanation

For query 1: Factors of 4 are 1, 2, 4

For query 2: Factors of 6 are 1, 2, 3, 6

For query 3: Factors of 15 are 1, 3, 5, 15

Solution Approach

A simple solution to the problem is by finding all the factors of the number. This is done by finding all numbers from one to √N, and increasing the counter by 2. Then check if the counter is equal to 4 and print YES or NO based on its equality.

Example

 Live Demo

#include <iostream>
#include <math.h>
using namespace std;
   int solveQuery(int N){
   int factors = 0;
   for(int i = 1; i < sqrt(N); i++){
      if(N % i == 0){
         factors += 2;
      }
   }
   if(factors == 4){
      return 1;
   }
   return 0;
}
int main() {
   int Q = 3;
   int query[3] = {4, 6, 15};
   for(int i = 0; i < Q; i++){
      if(solveQuery(query[i]))
         cout<<"The number "<<query[i]<<" has exactly four distinct factors\n";
      else
         cout<<"The number "<<query[i]<<" does not have exactly four
      distinct factors\n";
   }
}

Output

The number 4 does not have exactly four distinct factors
The number 6 has exactly four distinct factors
The number 15 has exactly four distinct factors

An efficient approach is to use the concepts of number theory for four-factor numbers. So, if a number has four factors then,

  • If the number is a cube of a prime number. Then it will have four distinct factors. Example, if N = (p^3), the factors will be 1, p, (p^2), N.

  • If the number is a product of two distinct prime numbers. Then also it will have four distinct factors. For example, if N = p1*p2, the factors will be 1, p1, p2, N.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int N = 1000;
   bool hasFourFactors[1000];
   void fourDistinctFactors() {
      bool primeNo[N + 1];
      memset(primeNo, true, sizeof(primeNo));
      for (int i = 2; i <= sqrt(N); i++) {
         if (primeNo[i] == true) {
            for (int j = i * 2; j <= N; j += i)
               primeNo[j] = false;
         }
      }
      vector<int> primes;
      for (int i = 2; i <= N; i++)
         if (primeNo[i])
            primes.push_back(i);
            memset(hasFourFactors, false, sizeof(hasFourFactors));
   for (int i = 0; i < primes.size(); ++i) {
      int p1 = primes[i];
      if (1 *(pow(p1, 3)) <= N)
         hasFourFactors[p1*p1*p1] = true;
      for (int j = i + 1; j < primes.size(); ++j) {
         int p2 = primes[j];
         if (1 * p1*p2 > N)
            break;
         hasFourFactors[p1*p2] = true;
      }
   }
}
int main() {
   int Q = 3;
   int query[] = {3, 6, 15};
   fourDistinctFactors();
   for(int i = 0; i < Q; i++){
      if(hasFourFactors[query[i]])
         cout<<"The number "<<query[i]<<" has exactly four distinct
         factors\n";
      else
         cout<<"The number "<<query[i]<<" does not have exactly four distinct factors\n";
   }
   return 0;
}

Output

The number 3 does not have exactly four distinct factors
The number 6 has exactly four distinct factors
The number 15 has exactly four distinct factors

Updated on: 09-Oct-2020

266 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements