
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
#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
#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
- Related Articles
- How to find whether a provided number is safe integer or not in JavaScript?
- Check whether a number has consecutive 0’s in the given base or not using Python
- How to check whether a number is a prime number or not?
- Queries for number of distinct elements in a subarray in C++
- Program to check whether a number is Proth number or not in C++
- How to Check Whether a Number is Krishnamurthy Number or Not in Java?
- How To Check Whether a Number is Strontio Number or Not in Java?
- How To Check Whether a Number is Tcefrep Number or Not in Java?
- How To Check Whether a Number is Tech Number or Not in Java?
- Find whether a given number is a power of 4 or not in C++
- How To Check Whether a Number Is a Pronic Number or Not in Java?
- How To Check Whether a Number Is a Bouncy Number or Not in Java?
- How To Check Whether a Number is a Evil Number or Not in Java?
- How To Check Whether a Number Is a Fascinating Number or Not in Java?
- How To Check Whether a Number is a Harshad Number or Not in Java?
