Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
C++ Program to check if given numbers are coprime or not
Suppose, we have n integers in an array nums. We have to find out if the numbers in the array are pairwise coprime, setwise coprime, or not coprime.
Two numbers nums[i] and nums[j] are said to be pairwise coprime if gcd(nums[i], nums[j]) = 1. This should hold for every number pair in the array and i
The numbers are said to be setwise coprime if gcd(nums[i]) = 1.
If they are neither, we say that they are not coprime.
So, if the input is like n = 4, nums = {7, 11, 13, 17}, then the output will be the numbers are pairwise coprime.
If we check every number pair in the array, the gcd of them will always be 1.
To solve this, we will follow these steps −
Define an array fac of size: 100 initialized with 0s. Define an array checkPrime of size: 100 initialized with 0s. gcdVal := 0 for initialize i := 0, when iExample
Let us see the following implementation to get better understanding −
#includeusing namespace std; void solve(int n, int nums[]){ int fac[100] = {0}; bool checkPrime[100] = {0}; int gcdVal = 0; for(int i = 0; i Input
4, {7, 11, 13, 17};Output
The numbers are pairwise coprime
