codeparrot/xlcost-text-to-code huggingface dataset:
Subsequences of size three in an array whose sum is divisible by m | Brute Force Implementation ( Time Complexity : O ( N ^ 3 ) ) C ++ program to find count of subsequences of size three divisible by M . ; Three nested loop to find all the sub sequences of length three in the given array A [ ] . ; checking if the sum of the chosen three number is divisible by m . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int coutSubSeq ( int A [ ] , int N , int M ) { int sum = 0 ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { for ( int k = j + 1 ; k < N ; k ++ ) { sum = A [ i ] + A [ j ] + A [ k ] ; if ( sum % M == 0 ) ans ++ ; } } } return ans ; } int main ( ) { int M = 3 ; int A [ ] = { 1 , 2 , 4 , 3 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << coutSubSeq ( A , N , M ) ; return 0 ; }
Subsequences of size three in an array whose sum is divisible by m | O ( M ^ 2 ) time complexity C ++ program to find count of subsequences of size three divisible by M . ; Storing frequencies of all remainders when divided by M . ; including i and j in the sum rem calculate the remainder required to make the sum divisible by M ; if the required number is less than j , we skip as we have already calculated for that value before . As j here starts with i and rem is less than j . ; if satisfies the first case . ; if satisfies the second case ; if satisfies the third case ; Driver code
#include <iostream> NEW_LINE using namespace std ; int countSubSeq ( int A [ ] , int N , int M ) { int ans = 0 ; int h [ M ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { A [ i ] = A [ i ] % M ; h [ A [ i ] ] ++ ; } for ( int i = 0 ; i < M ; i ++ ) { for ( int j = i ; j < M ; j ++ ) { int rem = ( M - ( i + j ) % M ) % M ; if ( rem < j ) continue ; if ( i == j && rem == j ) ans += h [ i ] * ( h [ i ] - 1 ) * ( h [ i ] - 2 ) / 6 ; else if ( i == j ) ans += h [ i ] * ( h [ i ] - 1 ) * h [ rem ] / 2 ; else if ( i == rem ) ans += h [ i ] * ( h [ i ] - 1 ) * h [ j ] / 2 ; else if ( rem == j ) ans += h [ j ] * ( h [ j ] - 1 ) * h [ i ] / 2 ; else ans = ans + h [ i ] * h [ j ] * h [ rem ] ; } } return ans ; } int main ( ) { int M = 3 ; int A [ ] = { 1 , 2 , 4 , 3 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << countSubSeq ( A , N , M ) ; return 0 ; }
Find n | CPP program to find nth term ; utility function ; since first element of the series is 7 , we initialise a variable with 7 ; Using iteration to find nth term ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findTerm ( int n ) { if ( n == 1 ) return n ; else { int term = 7 ; for ( int i = 2 ; i <= n ; i ++ ) term = term * 2 + ( i - 1 ) ; return term ; } } int main ( ) { int n = 5 ; cout << findTerm ( n ) ; return 0 ; }
Find n | CPP program to find the value at n - th place in the given sequence ; Returns n - th number in sequence 1 , 1 , 2 , 1 , 2 , 3 , 1 , 2 , 4 , ... ; One by one subtract counts elements in different blocks ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNumber ( int n ) { n -- ; int i = 1 ; while ( n >= 0 ) { n -= i ; ++ i ; } return ( n + i ) ; } int main ( ) { int n = 3 ; cout << findNumber ( n ) << endl ; return 0 ; }
Program to find correlation coefficient | Program to find correlation coefficient ; function that returns correlation coefficient . ; sum of elements of array X . ; sum of elements of array Y . ; sum of X [ i ] * Y [ i ] . ; sum of square of array elements . ; use formula for calculating correlation coefficient . ; Driver function ; Find the size of array . ; Function call to correlationCoefficient .
#include <bits/stdc++.h> NEW_LINE using namespace std ; float correlationCoefficient ( int X [ ] , int Y [ ] , int n ) { int sum_X = 0 , sum_Y = 0 , sum_XY = 0 ; int squareSum_X = 0 , squareSum_Y = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum_X = sum_X + X [ i ] ; sum_Y = sum_Y + Y [ i ] ; sum_XY = sum_XY + X [ i ] * Y [ i ] ; squareSum_X = squareSum_X + X [ i ] * X [ i ] ; squareSum_Y = squareSum_Y + Y [ i ] * Y [ i ] ; } float corr = ( float ) ( n * sum_XY - sum_X * sum_Y ) / sqrt ( ( n * squareSum_X - sum_X * sum_X ) * ( n * squareSum_Y - sum_Y * sum_Y ) ) ; return corr ; } int main ( ) { int X [ ] = { 15 , 18 , 21 , 24 , 27 } ; int Y [ ] = { 25 , 25 , 27 , 31 , 32 } ; int n = sizeof ( X ) / sizeof ( X [ 0 ] ) ; cout << correlationCoefficient ( X , Y , n ) ; return 0 ; }
Find the number of spectators standing in the stadium at time t | CPP program to find number of spectators standing at a time ; If the time is less than k then we can print directly t time . ; If the time is n then k spectators are standing . ; Otherwise we calculate the spectators standing . ; Driver code ; Stores the value of n , k and t t is time n & k is the number of specators
#include <bits/stdc++.h> NEW_LINE using namespace std ; void result ( long long n , long long k , long long t ) { if ( t <= k ) cout << t ; else if ( t <= n ) cout << k ; else { long long temp = t - n ; temp = k - temp ; cout << temp ; } } int main ( ) { long long n , k , t ; n = 10 ; k = 5 ; t = 12 ; result ( n , k , t ) ; return 0 ; }
Program for weighted mean of natural numbers . | Program to find weighted mean of natural numbers . ; Function to calculate weighted mean . ; Driver program to test the function . ; Take num array and corresponding weight array and initialize it . ; Calculate the size of array . ; Check the size of both array is equal or not .
#include <bits/stdc++.h> NEW_LINE using namespace std ; float weightedMean ( int X [ ] , int W [ ] , int n ) { int sum = 0 , numWeight = 0 ; for ( int i = 0 ; i < n ; i ++ ) { numWeight = numWeight + X [ i ] * W [ i ] ; sum = sum + W [ i ] ; } return ( float ) numWeight / sum ; } int main ( ) { int X [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int W [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int n = sizeof ( X ) / sizeof ( X [ 0 ] ) ; int m = sizeof ( W ) / sizeof ( W [ 0 ] ) ; if ( n == m ) cout << weightedMean ( X , W , n ) ; else cout << " - 1" ; return 0 ; }
Program to find GCD of floating point numbers | CPP code for finding the GCD of two floating numbers . ; Recursive function to return gcd of a and b ; base case ; Driver Function .
#include <bits/stdc++.h> NEW_LINE using namespace std ; double gcd ( double a , double b ) { if ( a < b ) return gcd ( b , a ) ; if ( fabs ( b ) < 0.001 ) return a ; else return ( gcd ( b , a - floor ( a / b ) * b ) ) ; } int main ( ) { double a = 1.20 , b = 22.5 ; cout << gcd ( a , b ) ; return 0 ; }
Program for harmonic mean of numbers | CPP program to find harmonic mean of numbers . ; Function that returns harmonic mean . ; Declare sum variables and initialize with zero . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float harmonicMean ( float arr [ ] , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( float ) 1 / arr [ i ] ; return ( float ) n / sum ; } int main ( ) { float arr [ ] = { 13.5 , 14.5 , 14.8 , 15.2 , 16.1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << harmonicMean ( arr , n ) ; return 0 ; }
Program for harmonic mean of numbers | C ++ program to find harmonic mean . ; Function that returns harmonic mean . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float harmonicMean ( int arr [ ] , int freq [ ] , int n ) { float sum = 0 , frequency_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ( float ) freq [ i ] / arr [ i ] ; frequency_sum = frequency_sum + freq [ i ] ; } return frequency_sum / sum ; } int main ( ) { int num [ ] = { 13 , 14 , 15 , 16 , 17 } ; int freq [ ] = { 2 , 5 , 13 , 7 , 3 } ; int n = sizeof ( num ) / sizeof ( num [ 0 ] ) ; cout << harmonicMean ( num , freq , n ) ; return 0 ; }
First collision point of two series | CPP program to calculate the colliding point of two series ; Iterating through n terms of the first series ; x is i - th term of first series ; d is first element of second series and c is common difference for second series . ; If no term of first series is found ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void point ( int a , int b , int c , int d , int n ) { int x , flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { x = b + i * a ; if ( ( x - d ) % c == 0 and x - d >= 0 ) { cout << x << endl ; flag = 1 ; break ; } } if ( flag == 0 ) { cout << " No ▁ collision ▁ point " << endl ; } } int main ( ) { int a = 20 ; int b = 2 ; int c = 9 ; int d = 19 ; int n = 20 ; point ( a , b , c , d , n ) ; return 0 ; }
Armstrong Numbers between two integers | CPP program to find Armstrong numbers in a range ; Prints Armstrong Numbers in given range ; number of digits calculation ; compute sum of nth power of its digits ; checks if number i is equal to the sum of nth power of its digits ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findArmstrong ( int low , int high ) { for ( int i = low + 1 ; i < high ; ++ i ) { int x = i ; int n = 0 ; while ( x != 0 ) { x /= 10 ; ++ n ; } int pow_sum = 0 ; x = i ; while ( x != 0 ) { int digit = x % 10 ; pow_sum += pow ( digit , n ) ; x /= 10 ; } if ( pow_sum == i ) cout << i << " ▁ " ; } } int main ( ) { int num1 = 100 ; int num2 = 400 ; findArmstrong ( num1 , num2 ) ; cout << ' ' ; return 0 ; }
Lucas Primality Test | C ++ Program for Lucas Primality Test ; function to generate prime factors of n ; if 2 is a factor ; if prime > 2 is factor ; this function produces power modulo some number . It can be optimized to using ; Base cases ; Generating and storing factors of n - 1 ; Array for random generator . This array is to ensure one number is generated only once ; shuffle random array to produce randomness ; Now one by one perform Lucas Primality Test on random numbers generated . ; this is to check if every factor of n - 1 satisfy the condition ; if a ^ ( ( n - 1 ) / q ) equal 1 ; if all condition satisfy ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void primeFactors ( int n , vector < int > & factors ) { if ( n % 2 == 0 ) factors . push_back ( 2 ) ; while ( n % 2 == 0 ) n = n / 2 ; for ( int i = 3 ; i <= sqrt ( n ) ; i += 2 ) { if ( n % i == 0 ) factors . push_back ( i ) ; while ( n % i == 0 ) n = n / i ; } if ( n > 2 ) factors . push_back ( n ) ; } int power ( int n , int r , int q ) { int total = n ; for ( int i = 1 ; i < r ; i ++ ) total = ( total * n ) % q ; return total ; } string lucasTest ( int n ) { if ( n == 1 ) return " neither ▁ prime ▁ nor ▁ composite " ; if ( n == 2 ) return " prime " ; if ( n % 2 == 0 ) return " composite1" ; vector < int > factors ; primeFactors ( n - 1 , factors ) ; int random [ n - 3 ] ; for ( int i = 0 ; i < n - 2 ; i ++ ) random [ i ] = i + 2 ; shuffle ( random , random + n - 3 , default_random_engine ( time ( 0 ) ) ) ; for ( int i = 0 ; i < n - 2 ; i ++ ) { int a = random [ i ] ; if ( power ( a , n - 1 , n ) != 1 ) return " composite " ; bool flag = true ; for ( int k = 0 ; k < factors . size ( ) ; k ++ ) { if ( power ( a , ( n - 1 ) / factors [ k ] , n ) == 1 ) { flag = false ; break ; } } if ( flag ) return " prime " ; } return " probably ▁ composite " ; } int main ( ) { cout << 7 << " ▁ is ▁ " << lucasTest ( 7 ) << endl ; cout << 9 << " ▁ is ▁ " << lucasTest ( 9 ) << endl ; cout << 37 << " ▁ is ▁ " << lucasTest ( 37 ) << endl ; return 0 ; }
Pair with maximum GCD from two arrays | CPP program to find maximum GCD pair from two arrays ; Find the maximum GCD pair with maximum sum ; array to keep a count of existing elements ; first [ i ] and second [ i ] are going to store maximum multiples of i in a [ ] and b [ ] respectively . ; traverse through the first array to mark the elements in cnt ; Find maximum multiple of every number in first array ; Find maximum multiple of every number in second array We re - initialise cnt [ ] and traverse through the second array to mark the elements in cnt ; if the multiple is present in the second array then store the max of number or the pre - existing element ; traverse for every elements and checks the maximum N that is present in both the arrays ; driver program to test the above function ; Maximum possible value of elements in both arrays .
#include <bits/stdc++.h> NEW_LINE using namespace std ; void gcdMax ( int a [ ] , int b [ ] , int n , int N ) { int cnt [ N ] = { 0 } ; int first [ N ] = { 0 } , second [ N ] = { 0 } ; for ( int i = 0 ; i < n ; ++ i ) cnt [ a [ i ] ] = 1 ; for ( int i = 1 ; i < N ; ++ i ) for ( int j = i ; j < N ; j += i ) if ( cnt [ j ] ) first [ i ] = max ( first [ i ] , j ) ; memset ( cnt , 0 , sizeof ( cnt ) ) ; for ( int i = 0 ; i < n ; ++ i ) cnt [ b [ i ] ] = true ; for ( int i = 1 ; i < N ; ++ i ) for ( int j = i ; j < N ; j += i ) if ( cnt [ j ] ) second [ i ] = max ( second [ i ] , j ) ; int i ; for ( i = N - 1 ; i >= 0 ; i -- ) if ( first [ i ] && second [ i ] ) break ; cout << " Maximum ▁ GCD ▁ pair ▁ with ▁ maximum ▁ " " sum ▁ is ▁ " << first [ i ] << " ▁ " << second [ i ] << endl ; } int main ( ) { int a [ ] = { 3 , 1 , 4 , 2 , 8 } ; int b [ ] = { 5 , 2 , 12 , 8 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int N = 20 ; gcdMax ( a , b , n , N ) ; return 0 ; }
Pierpont Prime | CPP program to print Pierpont prime numbers smaller than n . ; Finding all numbers having factor power of 2 and 3 Using sieve ; Storing number of the form 2 ^ i . 3 ^ k + 1. ; Finding prime number using sieve of Eratosthenes . Reusing same array as result of above computations in v . ; Printing n pierpont primes smaller than n ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool printPierpont ( int n ) { bool arr [ n + 1 ] ; memset ( arr , false , sizeof arr ) ; int two = 1 , three = 1 ; while ( two + 1 < n ) { arr [ two ] = true ; while ( two * three + 1 < n ) { arr [ three ] = true ; arr [ two * three ] = true ; three *= 3 ; } three = 1 ; two *= 2 ; } vector < int > v ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] ) v . push_back ( i + 1 ) ; memset ( arr , false , sizeof arr ) ; for ( int p = 2 ; p * p < n ; p ++ ) { if ( arr [ p ] == false ) for ( int i = p * 2 ; i < n ; i += p ) arr [ i ] = true ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) if ( ! arr [ v [ i ] ] ) cout << v [ i ] << " ▁ " ; } int main ( ) { int n = 200 ; printPierpont ( n ) ; return 0 ; }
Woodall Number | CPP program to check if a number is Woodball or not . ; If number is even , return false . ; If x is 1 , return true . ; While x is divisible by 2 ; Divide x by 2 ; Count the power ; If at any point power and x became equal , return true . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isWoodall ( int x ) { if ( x % 2 == 0 ) return false ; if ( x == 1 ) return true ; int p = 0 ; while ( x % 2 == 0 ) { x = x / 2 ; p ++ ; if ( p == x ) return true ; } return false ; } int main ( ) { int x = 383 ; ( isWoodall ( x ) ) ? ( cout << " Yes " << endl ) : ( cout << " No " << endl ) ; return 0 ; }
Print k numbers where all pairs are divisible by m | CPP program to find a list of k elements from an array such that difference between all of them is divisible by m . ; function to generate k numbers whose difference is divisible by m ; Using an adjacency list like representation to store numbers that lead to same remainder . ; stores the modulus when divided by m ; If we found k elements which have same remainder . ; If we could not find k elements ; driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void print_result ( int a [ ] , int n , int k , int m ) { vector < int > v [ m ] ; for ( int i = 0 ; i < n ; i ++ ) { int rem = a [ i ] % m ; v [ rem ] . push_back ( a [ i ] ) ; if ( v [ rem ] . size ( ) == k ) { for ( int j = 0 ; j < k ; j ++ ) cout << v [ rem ] [ j ] << " ▁ " ; return ; } } cout << " - 1" ; } int main ( ) { int a [ ] = { 1 , 8 , 4 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; print_result ( a , n , 2 , 3 ) ; return 0 ; }
Largest number less than N whose each digit is prime number | C ++ program to find the largest number smaller than N whose all digits are prime . ; Number is given as string . ; We stop traversing digits , once it become smaller than current number . For that purpose we use small variable . ; Array indicating if index i ( represents a digit ) is prime or not . ; Store largest ; If there is only one character , return the largest prime less than the number ; If number starts with 1 , return number consisting of 7 ; Traversing each digit from right to left Continue traversing till the number we are forming will become less . ; If digit is prime , copy it simply . ; If not prime , copy the largest prime less than current number ; If not prime , and there is no largest prime less than current prime ; Make current digit as 7 Go left of the digit and make it largest prime less than number . Continue do that until we found a digit which has some largest prime less than it ; If the given number is itself a prime . ; Make last digit as highest prime less than given digit . ; If there is no highest prime less than current digit . ; Once one digit become less than any digit of input replace 7 ( largest 1 digit prime ) till the end of digits of number ; If number include 0 in the beginning , ignore them . Case like 2200 ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; char * PrimeDigitNumber ( char N [ ] , int size ) { char * ans = ( char * ) malloc ( size * sizeof ( char ) ) ; int ns = 0 ; int small = 0 ; int i ; int p [ ] = { 0 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 0 , 0 } ; int prevprime [ ] = { 0 , 0 , 0 , 2 , 3 , 3 , 5 , 5 , 7 , 7 } ; if ( size == 1 ) { ans [ 0 ] = prevprime [ N [ 0 ] - '0' ] + '0' ; ans [ 1 ] = ' \0' ; return ans ; } if ( N [ 0 ] == '1' ) { for ( int i = 0 ; i < size - 1 ; i ++ ) ans [ i ] = '7' ; ans [ size - 1 ] = ' \0' ; return ans ; } for ( i = 0 ; i < size && small == 0 ; i ++ ) { if ( p [ N [ i ] - '0' ] == 1 ) { ans [ ns ++ ] = N [ i ] ; } else { if ( p [ N [ i ] - '0' ] == 0 && prevprime [ N [ i ] - '0' ] != 0 ) { ans [ ns ++ ] = prevprime [ N [ i ] - '0' ] + '0' ; small = 1 ; } else if ( p [ N [ i ] - '0' ] == 0 && prevprime [ N [ i ] - '0' ] == 0 ) { int j = i ; while ( j > 0 && p [ N [ j ] - '0' ] == 0 && prevprime [ N [ j ] - '0' ] == 0 ) { ans [ j ] = N [ j ] = '7' ; N [ j - 1 ] = prevprime [ N [ j - 1 ] - '0' ] + '0' ; ans [ j - 1 ] = N [ j - 1 ] ; small = 1 ; j -- ; } i = ns ; } } } if ( small == 0 ) { if ( prevprime [ N [ size - 1 ] - '0' ] + '0' != '0' ) ans [ size - 1 ] = prevprime [ N [ size - 1 ] - '0' ] + '0' ; else { int j = size - 1 ; while ( j > 0 && prevprime [ N [ j ] - '0' ] == 0 ) { ans [ j ] = N [ j ] = '7' ; N [ j - 1 ] = prevprime [ N [ j - 1 ] - '0' ] + '0' ; ans [ j - 1 ] = N [ j - 1 ] ; small = 1 ; j -- ; } } } for ( ; ns < size ; ns ++ ) ans [ ns ] = '7' ; ans [ ns ] = ' \0' ; int k = 0 ; while ( ans [ k ] == '0' ) k ++ ; return ans + k ; } int main ( ) { char N [ ] = "1000" ; int size = strlen ( N ) ; cout << PrimeDigitNumber ( N , size ) << endl ; return 0 ; }
Smallest x such that 1 * n , 2 * n , ... x * n have all digits from 1 to 9 | CPP program to find x such that 1 * n , 2 * n , 3 * n ... x * n have all digits from 1 to 9 at least once ; Returns smallest value x such that 1 * n , 2 * n , 3 * n ... x * n have all digits from 1 to 9 at least once ; taking temporary array and variable . ; iterate till we get all the 10 digits at least once ; checking all the digits ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestX ( int n ) { int temp [ 10 ] = { 0 } ; if ( n == 0 ) return -1 ; int count = 0 , x = 0 ; for ( x = 1 ; count < 10 ; x ++ ) { int y = x * n ; while ( y ) { if ( temp [ y % 10 ] == false ) { count ++ ; temp [ y % 10 ] = true ; } y /= 10 ; } } return x - 1 ; } int main ( ) { int n = 5 ; cout << smallestX ( n ) ; return 0 ; }
Find a number x such that sum of x and its digits is equal to given n . | CPP program to find x such that x + digSum ( x ) is equal to n . ; utility function for digit sum ; function for finding x ; iterate from 1 to n . For every no . check if its digit sum with it is equal to n . ; if no such i found return - 1 ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int digSum ( int n ) { int sum = 0 , rem = 0 ; while ( n ) { rem = n % 10 ; sum += rem ; n /= 10 ; } return sum ; } int findX ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) if ( i + digSum ( i ) == n ) return i ; return -1 ; } int main ( ) { int n = 43 ; cout << " x ▁ = ▁ " << findX ( n ) ; return 0 ; }
9 's complement of a decimal number | C ++ program to find 9 's complement of a number. ; Driver code
#include <iostream> NEW_LINE using namespace std ; void complement ( string number ) { for ( int i = 0 ; i < number . length ( ) ; i ++ ) if ( number [ i ] != ' . ' ) number [ i ] = '9' - number [ i ] + '0' ; cout << "9 ' s ▁ complement ▁ is ▁ : ▁ " << number ; } int main ( ) { string number = "345.45" ; complement ( number ) ; return 0 ; }
Ways to express a number as product of two different factors | CPP program to find number of ways in which number expressed as product of two different factors ; To count number of ways in which number expressed as product of two different numbers ; To store count of such pairs ; Counting number of pairs upto sqrt ( n ) - 1 ; To return count of pairs ; Driver program to test countWays ( )
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countWays ( int n ) { int count = 0 ; for ( int i = 1 ; i * i < n ; i ++ ) if ( n % i == 0 ) count ++ ; return count ; } int main ( ) { int n = 12 ; cout << countWays ( n ) << endl ; return 0 ; }
Count divisors of n that have at | C ++ program to count divisors of n that have at least one digit common with n ; function to return true if any digit of m is present in hash [ ] . ; check till last digit ; if number is also present in original number then return true ; if no number matches then return 1 ; Count the no of divisors that have at least 1 digits same ; Store digits present in n in a hash [ ] ; marks that the number is present ; last digit removed ; loop to traverse from 1 to sqrt ( n ) to count divisors ; if i is the factor ; call the function to check if any digits match or not ; if n / i != i then a different number , then check it also ; return the answer ; driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDigitPresent ( int m , bool hash [ ] ) { while ( m ) { if ( hash [ m % 10 ] ) return true ; m = m / 10 ; } return false ; } int countDivisibles ( int n ) { bool hash [ 10 ] = { 0 } ; int m = n ; while ( m ) { hash [ m % 10 ] = true ; m = m / 10 ; } int ans = 0 ; for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( isDigitPresent ( i , hash ) ) ans ++ ; if ( n / i != i ) { if ( isDigitPresent ( n / i , hash ) ) ans ++ ; } } } return ans ; } int main ( ) { int n = 15 ; cout << countDivisibles ( n ) ; return 0 ; }