Number of Co-prime pairs obtained from the sum of digits of elements in the given range

Given two numbers A and B where 1 <= A <= B. The task is to count the number of pairs whose elements are co-prime where pairs are formed from the sum of the digits of the elements in the given range.

Note: Two pairs are counted as distinct if at least one of the number in the pair is different. It may be assumed that the maximum digit sum can be 162.

Examples:

Input: 12 15
Output: 4
12 = 1+2 = 3
13 = 1+3 = 4
14 = 1+4 = 5
15 = 1+5 = 6
Thus pairs who are co-prime to each other are 
(3, 4), (3, 5), (4, 5), (5, 6) 
i.e the answer is 4.


Input: 7 10
Output: 5


Method-1:

  • Consider each and every element from a to b.
  • Find the sum of the digits of every element and store it into a vector.
  • Consider each and every pair one by one and check if the gcd of the elements of that pair is 1.
  • If yes, count that pair as it is co-prime.
  • Print the count of pairs that are co-prime.

Below is the implementation of above approach:

C++

// C++ program to count the pairs 
// whose sum of digits is co-prime 
#include <bits/stdc++.h> 
using namespace std; 
  
// Function to find the elements 
// after doing the sum of digits
int makePairs(vector<int> &pairs, int a, int b) 
{
      
    // Traverse from a  to b
    for (int i = a; i <= b; i++)
    {
          
    // Find the sum of the digits of the elements
    // in the given range one by one
      int sumOfDigits = 0, k = i;
      while(k>0)
      {
          sumOfDigits += k%10;
          k /= 10;
      }
      if (sumOfDigits <= 162)
      pairs.push_back(sumOfDigits);
    }
}
  
// Function to count the co-prime pairs
int countCoPrime(int a, int b){
    vector<int> pairs;
      
    // Function to make the pairs 
    // by doing the sum of digits
    makePairs(pairs, a, b);
    int count = 0;
      
    // Count pairs that are co-primes
    for(int i = 0; i < pairs.size(); i++)
       for (int j = i+1; j < pairs.size(); j++)
          if (__gcd(pairs[i], pairs[j]) == 1)
                 count++;
               
   return count;
      
}
  
// Driver code 
int main() 
    int a = 12, b = 15; 
  
    // Function to count the pairs 
    cout << countCoPrime(a, b) ;
  
    return 0; 

Java

// Java program to count the pairs 
// whose sum of digits is co-prime 
import java.util.*;
  
class GFG
{
static int GCD(int a, int b) 
{
if (b == 0) return a;
return GCD(b, a % b);
}
// Function to find the elements 
// after doing the sum of digits
static void makePairs(Vector<Integer> pairs, 
                      int a, int b) 
{
      
    // Traverse from a to b
    for (int i = a; i <= b; i++)
    {
          
    // Find the sum of the digits 
    // of the elements in the given 
    // range one by one
    int sumOfDigits = 0, k = i;
    while(k > 0)
    {
        sumOfDigits += k % 10;
        k /= 10;
    }
    if (sumOfDigits <= 162)
        pairs.add(sumOfDigits);
    }
}
  
// Function to count 
// the co-prime pairs
static int countCoPrime(int a, int b)
{
    Vector<Integer> pairs = new Vector<Integer>();
      
    // Function to make the pairs 
    // by doing the sum of digits
    makePairs(pairs, a, b);
    int count = 0;
      
    // Count pairs that are co-primes
    for(int i = 0; i < pairs.size(); i++)
    for (int j = i+1; j < pairs.size(); j++)
        if (GCD(pairs.get(i), 
                pairs.get(j)) == 1)
                count++;
              
return count;
      
}
  
// Driver code 
public static void main(String args[])
    int a = 12, b = 15
  
    // Function to count the pairs 
    System.out.println(countCoPrime(a, b));
}
  
// This code is contributed by Arnab Kundu

Output:

4

Method-2:
As mentioned in the question, the maximum sum can be 162. So, find out the frequency of numbers having their digit sum from 1 to 162 in range A to B and store the frequency in the array. Later, find the answer using this frequency.
Since,

Number, Frequency
1, 0
2, 0
3, 1
4, 1
5, 1
6, 1
7, 0
8, 0
., .
., .
162, 0
Thus Number of gcd pairs = freq(3)*freq(4) + freq(3)*freq(5) + freq(4)*freq(5) + freq(5)* freq(6)
= 1+1+1+1
= 4

Thus pairs who are co-prime to each other are (3,4), (3,5), (4,5), (5,6) i.e the answer is 4.
Below is the required implementation:

// C++ program to count the pairs
// whose sum of digits is co-prime
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
  
// Recursive function to return the frequency
// of numbers having their sum of digits i
ll recursive(ll idx, ll sum, ll tight, string st,
             ll dp[20][2][166], ll num)
{
    if (idx == num)
        // Returns 1 or 0
        return sum == 0;
  
    // Returns value of the dp if already stored
    if (dp[idx][tight][sum] != -1)
        return dp[idx][tight][sum];
  
    bool newTight;
    ll ans = 0;
    ll d;
  
    // Loop from digit 0 to 9
    for (d = 0; d < 10; ++d) {
        newTight = false;
        if (tight && st[idx] - '0' < d)
            continue;
  
        // To change the tight to 1
        if (tight && st[idx] - '0' == d)
            newTight = true;
  
        // Calling the recursive function to find the frequency
        if (sum >= d)
            ans += recursive(idx + 1, sum - d,
                             newTight, st, dp, num);
    }
  
    return dp[idx][tight][sum] = ans;
}
  
// Function to find out frequency of numbers
// from 1 to N having their sum of digits
// from 1 to 162 and store them in array
vector<ll> formArray(ll N)
{
    ll dp[20][2][166];
    memset(dp, -1, sizeof dp);
  
    // Number to string conversion
    ostringstream x;
    x << N;
    string st = x.str();
    ll num = st.size();
  
    vector<ll> arr;
    for (int i = 1; i <= 162; ++i) {
  
        // Calling the recursive function
        // and pushing it into array
        arr.push_back(recursive(0, i, 1, st, dp, num));
    }
  
    return arr;
}
  
// Function to find the pairs
ll findPair(ll a, ll b)
{
    // Calling the  formArray function of a-1 numbers
    vector<ll> arr_smaller = formArray(a - 1);
  
    // Calling the  formArray function of b numbers
    vector<ll> arr_greater = formArray(b);
  
    // Subtracting the frequency of higher number array with lower
    // number array and thus finding the range of
    // numbers from a to b having sum from 1 to 162
    for (int i = 0; i < arr_greater.size(); ++i)
        arr_greater[i] -= arr_smaller[i];
  
    int ans = 0;
    for (int i = 1; i <= 162; ++i) {
        for (int j = i + 1; j <= 162; ++j) {
  
            // To find out total number of pairs
            // which are co-prime
            if (__gcd(i, j) == 1)
                ans = (ans + arr_greater[i - 1] * arr_greater[j - 1]);
        }
    }
  
    return ans;
}
  
// Driver code
int main()
{
    ll a = 12, b = 15;
  
    // Function to count the pairs
    cout << findPair(a, b);
  
    return 0;
}

Output:

4


My Personal Notes arrow_drop_up


Coder only XD Do send reference if contacting me

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : andrew1234