codeparrot/xlcost-text-to-code huggingface dataset:
Sum of n digit numbers divisible by a given number | Efficient CPP program to find the sum divisible numbers . ; find the Sum of having n digit and divisible by the number ; compute the first and last term ; first number which is divisible by given number ; last number which is divisible by given number ; total divisible number ; return the total sum ; Driver code
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int totalSumDivisibleByNum ( int digit , int number ) { int firstnum = pow ( 10 , digit - 1 ) ; int lastnum = pow ( 10 , digit ) ; firstnum = ( firstnum - firstnum % number ) + number ; lastnum = ( lastnum - lastnum % number ) ; int count = ( ( lastnum - firstnum ) / number + 1 ) ; return ( ( lastnum + firstnum ) * count ) / 2 ; } int main ( ) { int n = 3 , number = 7 ; cout << totalSumDivisibleByNum ( n , number ) ; return 0 ; }
Program for N | CPP Program to find nth term of Arithmetic progression ; using formula to find the Nth term t ( n ) = a ( 1 ) + ( n - 1 ) * d ; Driver code ; starting number ; Common difference ; N th term to be find ; Display the output
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Nth_of_AP ( int a , int d , int N ) { return ( a + ( N - 1 ) * d ) ; } int main ( ) { int a = 2 ; int d = 1 ; int N = 5 ; cout << " The ▁ " << N << " th ▁ term ▁ of ▁ the ▁ series ▁ is ▁ : ▁ " << Nth_of_AP ( a , d , N ) ; return 0 ; }
Fibbinary Numbers ( No consecutive 1 s in binary ) | CPP program to check if a number is fibinnary number or not ; function to check if binary representation of an integer has consecutive 1 s ; stores the previous last bit initially as 0 ; if current last bit and previous last bit is 1 ; stores the last bit ; right shift the number ; Driver code to check above function
#include <iostream> NEW_LINE using namespace std ; bool checkFibinnary ( int n ) { int prev_last = 0 ; while ( n ) { if ( ( n & 1 ) && prev_last ) return false ; prev_last = n & 1 ; n >>= 1 ; } return true ; } int main ( ) { int n = 10 ; if ( checkFibinnary ( n ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Sum of the series 5 + 55 + 555 + . . up to n terms | C ++ program for sum of the series 5 + 55 + 555. . ... n ; function which return the the sum of series ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfSeries ( int n ) { return 0.6172 * ( pow ( 10 , n ) - 1 ) - 0.55 * n ; } int main ( ) { int n = 2 ; cout << sumOfSeries ( n ) ; return 0 ; }
Nonagonal number | CPP Program to find nth nonagonal number . ; Function to find nth nonagonal number . ; Formula to find nth nonagonal number . ; Driver function .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Nonagonal ( int n ) { return n * ( 7 * n - 5 ) / 2 ; } int main ( ) { int n = 10 ; cout << Nonagonal ( n ) ; return 0 ; }
Check if a large number is divisible by 20 | CPP program to check if a large number is divisible by 20. ; Get number with last two digits ; Check if the number formed by last two digits is divisible by 5 and 4. ; Driver Program
#include <iostream> NEW_LINE using namespace std ; bool divisibleBy20 ( string num ) { int lastTwoDigits = stoi ( num . substr ( num . length ( ) - 2 , num . length ( ) - 1 ) ) ; return ( ( lastTwoDigits % 5 == 0 ) && ( lastTwoDigits % 4 == 0 ) ) ; } int main ( ) { string num = "63284689320" ; if ( divisibleBy20 ( num ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Divisibility by 12 for a large number | C ++ Program to check if number is divisible by 12 ; if number greater then 3 ; find last digit ; no is odd ; find second last digit ; find sum of all digits ; if number is less then or equal to 100 ; Driver function
#include <iostream> NEW_LINE using namespace std ; bool isDvisibleBy12 ( string num ) { if ( num . length ( ) >= 3 ) { int d1 = ( int ) num [ num . length ( ) - 1 ] ; if ( d1 % 2 != 0 ) return ( 0 ) ; int d2 = ( int ) num [ num . length ( ) - 2 ] ; int sum = 0 ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) sum += num [ i ] ; return ( sum % 3 == 0 && ( d2 * 10 + d1 ) % 4 == 0 ) ; } else { int number = stoi ( num ) ; return ( number % 12 == 0 ) ; } } int main ( ) { string num = "12244824607284961224" ; if ( isDvisibleBy12 ( num ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Largest number that is not a perfect square | CPP program to find the largest non perfect square number among n numbers ; takes the sqrt of the number ; checks if it is a perfect square number ; function to find the largest non perfect square number ; stores the maximum of all non perfect square numbers ; traverse for all elements in the array ; store the maximum if not a perfect square ; driver code to check the above functions ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int n ) { int d = sqrt ( n ) ; if ( d * d == n ) return true ; return false ; } int largestNonPerfectSquareNumber ( int a [ ] , int n ) { int maxi = -1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! check ( a [ i ] ) ) maxi = max ( a [ i ] , maxi ) ; } return maxi ; } int main ( ) { int a [ ] = { 16 , 20 , 25 , 2 , 3 , 10 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << largestNonPerfectSquareNumber ( a , n ) ; return 0 ; }
Program to print Arithmetic Progression series | CPP Program to print an arithmetic progression series ; Printing AP by simply adding d to previous term . ; Driver code ; starting number ; Common difference ; N th term to be find
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printAP ( int a , int d , int n ) { int curr_term ; curr_term = a ; for ( int i = 1 ; i <= n ; i ++ ) { cout << curr_term << " ▁ " ; curr_term = curr_term + d ; } } int main ( ) { int a = 2 ; int d = 1 ; int n = 5 ; printAP ( a , d , n ) ; return 0 ; }
Program to print non square numbers | CPP program to print first n non square number ; Print curr_count numbers . curr_count is current gap between two square numbers . ; skip a square number . ; Count of next non - square numbers is next even number . ; Driver code
#include <assert.h> NEW_LINE #include <math.h> NEW_LINE #include <stdio.h> NEW_LINE void printNonSquare ( int n ) { int curr_count = 2 , num = 2 , count = 0 ; while ( count < n ) { for ( int i = 0 ; i < curr_count && count < n ; i ++ ) { printf ( " % d ▁ " , num ) ; count ++ ; num ++ ; } num ++ ; curr_count += 2 ; } } int main ( ) { int n = 10 ; printNonSquare ( n ) ; return 0 ; }
Count number of trailing zeros in product of array | CPP program for count total zero in product of array ; Returns count of zeros in product of array ; count number of 2 s in each element ; count number of 5 s in each element ; return the minimum ; Driven Program
#include <iostream> NEW_LINE using namespace std ; int countZeros ( int a [ ] , int n ) { int count2 = 0 , count5 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( a [ i ] % 2 == 0 ) { a [ i ] = a [ i ] / 2 ; count2 ++ ; } while ( a [ i ] % 5 == 0 ) { a [ i ] = a [ i ] / 5 ; count5 ++ ; } } return ( count2 < count5 ) ? count2 : count5 ; } int main ( ) { int a [ ] = { 10 , 100 , 20 , 30 , 50 , 90 , 12 , 80 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << countZeros ( a , n ) ; return 0 ; }
Sum of square of first n even numbers | Efficient C ++ method to find sum of square of first n even numbers . ; Driver code
#include <iostream> NEW_LINE using namespace std ; int squareSum ( int n ) { return 2 * n * ( n + 1 ) * ( 2 * n + 1 ) / 3 ; } int main ( ) { cout << squareSum ( 8 ) ; return 0 ; }
MÃ ¼ nchhausen Number | C ++ code for MA14nchhausen Number ; pwr [ i ] is going to store i raised to power i . ; Function to check out whether the number is MA14nchhausen Number or not ; Precompute i raised to power i for every i ; The input here is fixed i . e . it will check up to n ; check the integer for MA14nchhausen Number , if yes then print out the number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned pwr [ 10 ] ; bool isMunchhausen ( unsigned n ) { unsigned sum = 0 ; int temp = n ; while ( temp ) { sum += pwr [ ( temp % 10 ) ] ; temp /= 10 ; } return ( sum == n ) ; } void printMunchhausenNumbers ( int n ) { for ( int i = 0 ; i < 10 ; i ++ ) pwr [ i ] = ( unsigned ) pow ( ( float ) i , ( float ) i ) ; for ( unsigned i = 1 ; i <= n ; i ++ ) if ( isMunchhausen ( i ) ) cout << i << " STRNEWLINE " ; } int main ( ) { int n = 10000 ; printMunchhausenNumbers ( n ) ; return 0 ; }
K | CPP program for finding k - th digit in a ^ b ; To compute k - th digit in a ^ b ; computing a ^ b ; getting last digit ; increasing count by 1 ; if current number is required digit ; remove last digit ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int kthdigit ( int a , int b , int k ) { int p = pow ( a , b ) ; int count = 0 ; while ( p > 0 && count < k ) { int rem = p % 10 ; count ++ ; if ( count == k ) return rem ; p = p / 10 ; } return 0 ; } int main ( ) { int a = 5 , b = 2 ; int k = 1 ; cout << kthdigit ( a , b , k ) ; return 0 ; }
Recursive sum of digit in n ^ x , where n and x are very large | CPP Code for Sum of digit of n ^ x where n and x are very large ; function to get sum of digits of a number ; function to return sum ; Find sum of digits in n ; Find remainder of exponent ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long digSum ( long n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } long PowDigSum ( long n , long x ) { long sum = digSum ( n ) ; long rem = x % 6 ; if ( ( sum == 3 sum == 6 ) && x > 1 ) return 9 ; else if ( x == 1 ) return sum ; else if ( x == 0 ) return 1 ; else if ( rem == 0 ) return digSum ( ( long ) pow ( sum , 6 ) ) ; else return digSum ( ( long ) pow ( sum , rem ) ) ; } int main ( ) { int n = 33333 ; int x = 332654 ; cout << PowDigSum ( n , x ) ; return 0 ; }
Container with Most Water | C ++ code for Max Water Container ; Calculating the max area ; Driver code
#include <iostream> NEW_LINE using namespace std ; int maxArea ( int A [ ] , int len ) { int l = 0 ; int r = len - 1 ; int area = 0 ; while ( l < r ) { area = max ( area , min ( A [ l ] , A [ r ] ) * ( r - l ) ) ; if ( A [ l ] < A [ r ] ) l += 1 ; else r -= 1 ; } return area ; } int main ( ) { int a [ ] = { 1 , 5 , 4 , 3 } ; int b [ ] = { 3 , 1 , 2 , 4 , 5 } ; int len1 = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << maxArea ( a , len1 ) ; int len2 = sizeof ( b ) / sizeof ( b [ 0 ] ) ; cout << endl << maxArea ( b , len2 ) ; }
Program for Mobius Function | Program to print all prime factors ; Returns value of mobius ( ) ; Handling 2 separately ; If 2 ^ 2 also divides N ; Check for all other prime factors ; If i divides n ; If i ^ 2 also divides N ; Driver code
# include <bits/stdc++.h> NEW_LINE using namespace std ; int mobius ( int n ) { int p = 0 ; if ( n % 2 == 0 ) { n = n / 2 ; p ++ ; if ( n % 2 == 0 ) return 0 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { n = n / i ; p ++ ; if ( n % i == 0 ) return 0 ; } } return ( p % 2 == 0 ) ? -1 : 1 ; } int main ( ) { int N = 17 ; cout << " Mobius ▁ Functions ▁ M ( N ) ▁ at ▁ N ▁ = ▁ " << N << " ▁ is : ▁ " << mobius ( N ) << endl ; cout << " Mobius ▁ Functions ▁ M ( N ) ▁ at ▁ N ▁ = ▁ " << 25 << " ▁ is : ▁ " << mobius ( 25 ) << endl ; cout << " Mobius ▁ Functions ▁ M ( N ) ▁ at ▁ N ▁ = ▁ " << 6 << " ▁ is : ▁ " << mobius ( 6 ) << endl ; }
Sum of squares of binomial coefficients | CPP Program to find the sum of square of binomial coefficient . ; function to return product of number from start to end . ; Return the sum of square of binomial coefficient ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorial ( int start , int end ) { int res = 1 ; for ( int i = start ; i <= end ; i ++ ) res *= i ; return res ; } int sumofsquare ( int n ) { return factorial ( n + 1 , 2 * n ) / factorial ( 1 , n ) ; } int main ( ) { int n = 4 ; cout << sumofsquare ( n ) << endl ; return 0 ; }
Find nth Fibonacci number using Golden ratio | CPP program to find n - th Fibonacci number ; Approximate value of golden ratio ; Fibonacci numbers upto n = 5 ; Function to find nth Fibonacci number ; Fibonacci numbers for n < 6 ; Else start counting from 5 th term ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double PHI = 1.6180339 ; int f [ 6 ] = { 0 , 1 , 1 , 2 , 3 , 5 } ; int fib ( int n ) { if ( n < 6 ) return f [ n ] ; int t = 5 , fn = 5 ; while ( t < n ) { fn = round ( fn * PHI ) ; t ++ ; } return fn ; } int main ( ) { int n = 9 ; cout << n << " th ▁ Fibonacci ▁ Number ▁ = ▁ " << fib ( n ) << endl ; return 0 ; }
Euler Method for solving differential equation | CPP Program to find approximation of a ordinary differential equation using euler method . ; Consider a differential equation dy / dx = ( x + y + xy ) ; Function for Euler formula ; Iterating till the point at which we need approximation ; Printing approximation ; Driver program ; Initial Values ; Value of x at which we need approximation
#include <iostream> NEW_LINE using namespace std ; float func ( float x , float y ) { return ( x + y + x * y ) ; } void euler ( float x0 , float y , float h , float x ) { float temp = -0 ; while ( x0 < x ) { temp = y ; y = y + h * func ( x0 , y ) ; x0 = x0 + h ; } cout << " Approximate ▁ solution ▁ at ▁ x ▁ = ▁ " << x << " ▁ is ▁ " << y << endl ; } int main ( ) { float x0 = 0 ; float y0 = 1 ; float h = 0.025 ; float x = 0.1 ; euler ( x0 , y0 , h , x ) ; return 0 ; }
Find x and y satisfying ax + by = n | CPP program to find solution of ax + by = n ; function to find the solution ; traverse for all possible values ; check if it is satisfying the equation ; driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solution ( int a , int b , int n ) { for ( int i = 0 ; i * a <= n ; i ++ ) { if ( ( n - ( i * a ) ) % b == 0 ) { cout << " x ▁ = ▁ " << i << " , ▁ y ▁ = ▁ " << ( n - ( i * a ) ) / b ; return ; } } cout << " No ▁ solution " ; } int main ( ) { int a = 2 , b = 3 , n = 7 ; solution ( a , b , n ) ; return 0 ; }
Sum of Binomial coefficients | CPP Program to find sum of Binomial Coefficient . ; Returns value of Binomial Coefficient Sum which is 2 raised to power n . ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binomialCoeffSum ( int n ) { return ( 1 << n ) ; } int main ( ) { int n = 4 ; printf ( " % d " , binomialCoeffSum ( n ) ) ; return 0 ; }
Program to compute division upto n decimal places | CPP program to compute division upto n decimal places . ; Base cases ; Since n <= 0 , don 't compute after  the decimal ; Handling negative numbers ; Integral division ; Now one by print digits after dot using school division method . ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void precisionCompute ( int x , int y , int n ) { if ( y == 0 ) { cout << " Infinite " << endl ; return ; } if ( x == 0 ) { cout << 0 << endl ; return ; } if ( n <= 0 ) { cout << x / y << endl ; return ; } if ( ( ( x > 0 ) && ( y < 0 ) ) || ( ( x < 0 ) && ( y > 0 ) ) ) { cout << " - " ; x = x > 0 ? x : - x ; y = y > 0 ? y : - y ; } int d = x / y ; for ( int i = 0 ; i <= n ; i ++ ) { cout << d ; x = x - ( y * d ) ; if ( x == 0 ) break ; x = x * 10 ; d = x / y ; if ( i == 0 ) cout << " . " ; } } int main ( ) { int x = 22 , y = 7 , n = 15 ; precisionCompute ( x , y , n ) ; return 0 ; }
Program to determine the quadrant of the cartesian plane | CPP program to check quadrant ; Function to check quadrant ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void quadrant ( int x , int y ) { if ( x > 0 and y > 0 ) cout << " lies ▁ in ▁ First ▁ quadrant " ; else if ( x < 0 and y > 0 ) cout << " lies ▁ in ▁ Second ▁ quadrant " ; else if ( x < 0 and y < 0 ) cout << " lies ▁ in ▁ Third ▁ quadrant " ; else if ( x > 0 and y < 0 ) cout << " lies ▁ in ▁ Fourth ▁ quadrant " ; else if ( x == 0 and y > 0 ) cout << " lies ▁ at ▁ positive ▁ y ▁ axis " ; else if ( x == 0 and y < 0 ) cout << " lies ▁ at ▁ negative ▁ y ▁ axis " ; else if ( y == 0 and x < 0 ) cout << " lies ▁ at ▁ negative ▁ x ▁ axis " ; else if ( y == 0 and x > 0 ) cout << " lies ▁ at ▁ positive ▁ x ▁ axis " ; else cout << " lies ▁ at ▁ origin " ; } int main ( ) { int x = 1 , y = 1 ; quadrant ( x , y ) ; return 0 ; }
Check if a number is Full Prime | CPP program for checking of full prime ; function to check digits ; check all digits are prime or not ; check if digits are prime or not ; To check if n is prime or not ; check for all factors ; To check if n is Full Prime ; The order is important here for efficiency . ; Driver code to check the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkDigits ( int n ) { while ( n ) { int dig = n % 10 ; if ( dig != 2 && dig != 3 && dig != 5 && dig != 7 ) return false ; n /= 10 ; } return true ; } bool prime ( int n ) { if ( n == 1 ) return false ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } int isFullPrime ( int n ) { return ( checkDigits ( n ) && prime ( n ) ) ; } int main ( ) { int n = 53 ; if ( isFullPrime ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Narcissistic number | CPP program for checking of Narcissistic number ; Traversing through the string ; Since ascii value of numbers starts from 48 so we subtract it from sum ; Converting string to integer ; Comparing number and sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE #include <string.h> NEW_LINE using namespace std ; string getResult ( string st ) { int sum = 0 ; int length = st . length ( ) ; for ( int i = 0 ; i < length ; i ++ ) { sum = sum + pow ( st [ i ] - '0' , length ) ; } int number = stoi ( st ) ; if ( number == sum ) return " yes " ; else return " no " ; } int main ( ) { string st = "153" ; cout << getResult ( st ) ; return 0 ; }
SchrÃ ¶ derâ €“ Hipparchus number | A memoization based optimized CPP program to find n - th SchrAderaHipparchus number ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define MAX  500 NEW_LINE using namespace std ; int nthSHN ( int n , int dp [ ] ) { if ( n == 1 n == 2 ) return dp [ n ] = 1 ; if ( dp [ n ] != -1 ) return dp [ n ] ; return dp [ n ] = ( ( 6 * n - 9 ) * nthSHN ( n - 1 , dp ) - ( n - 3 ) * nthSHN ( n - 2 , dp ) ) / n ; } int main ( ) { int n = 6 ; int dp [ MAX ] ; memset ( dp , -1 , sizeof dp ) ; cout << nthSHN ( n , dp ) << endl ; return 0 ; }
Sum of first n even numbers | C ++ implementation to find sum of first n even numbers ; function to find sum of first n even numbers ; sum of first n even numbers ; next even number ; required sum ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int evenSum ( int n ) { int curr = 2 , sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { sum += curr ; curr += 2 ; } return sum ; } int main ( ) { int n = 20 ; cout << " Sum ▁ of ▁ first ▁ " << n << " ▁ Even ▁ numbers ▁ is : ▁ " << evenSum ( n ) ; return 0 ; }
Sum of first n even numbers | C ++ implementation to find sum of first n even numbers ; function to find sum of first n even numbers ; required sum ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int evenSum ( int n ) { return ( n * ( n + 1 ) ) ; } int main ( ) { int n = 20 ; cout << " Sum ▁ of ▁ first ▁ " << n << " ▁ Even ▁ numbers ▁ is : ▁ " << evenSum ( n ) ; return 0 ; }
Program to Convert Km / hr to miles / hr and vice versa | Cpp program for conversion of kmph to mph and vice versa ; Function to convert kmph to mph ; Function to convert mph to kmph ; Driver code to check the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; double kmphTOmph ( double kmph ) { return 0.6214 * kmph ; } double mphTOkmph ( double mph ) { return mph * 1.60934 ; } int main ( ) { double kmph = 150 ; double mph = 100 ; cout << " the ▁ speed ▁ in ▁ mph ▁ is ▁ " << kmphTOmph ( kmph ) << endl ; cout << " the ▁ speed ▁ in ▁ kmph ▁ is ▁ " << mphTOkmph ( mph ) ; return 0 ; }
Number of GP ( Geometric Progression ) subsequences of size 3 | C ++ program to count GP subsequences of size 3. ; Returns count of G . P . subsequences with length 3 and common ratio r ; hashing to maintain left and right array elements to the main count ; stores the answer ; traverse through the elements ; traverse through all elements and find out the number of elements as k1 * k2 ; keep the count of left and right elements left is a [ i ] / r and right a [ i ] * r ; if the current element is divisible by k , count elements in left hash . ; decrease the count in right hash ; number of right elements ; calculate the answer ; left count of a [ i ] ; returns answer ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long subsequences ( int a [ ] , int n , int r ) { unordered_map < int , int > left , right ; long long ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int i = 0 ; i < n ; i ++ ) { long long c1 = 0 , c2 = 0 ; if ( a [ i ] % r == 0 ) c1 = left [ a [ i ] / r ] ; right [ a [ i ] ] -- ; c2 = right [ a [ i ] * r ] ; ans += c1 * c2 ; left [ a [ i ] ] ++ ; } return ans ; } int main ( ) { int a [ ] = { 1 , 2 , 6 , 2 , 3 , 6 , 9 , 18 , 3 , 9 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int r = 3 ; cout << subsequences ( a , n , r ) ; return 0 ; }
Find element in array that divides all array elements | CPP program to find such number in the array that all array elements are divisible by it ; Returns gcd of two numbers . ; Function to return the desired number if exists ; Find GCD of array ; Check if GCD is present in array ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } int findNumber ( int arr [ ] , int n ) { int ans = arr [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) ans = gcd ( ans , arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == ans ) return ans ; return -1 ; } int main ( ) { int arr [ ] = { 2 , 2 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findNumber ( arr , n ) << endl ; return 0 ; }
Special prime numbers | CPP program to check whether there exist at least k or not in range [ 2. . n ] ; Generating all the prime numbers from 2 to n . ; If a prime number is Special prime number , then we increments the value of k . ; If at least k Special prime numbers are present , then we return 1. else we return 0 from outside of the outer loop . ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > primes ; void SieveofEratosthenes ( int n ) { bool visited [ n ] ; for ( int i = 2 ; i <= n + 1 ; i ++ ) if ( ! visited [ i ] ) { for ( int j = i * i ; j <= n + 1 ; j += i ) visited [ j ] = true ; primes . push_back ( i ) ; } } bool specialPrimeNumbers ( int n , int k ) { SieveofEratosthenes ( n ) ; int count = 0 ; for ( int i = 0 ; i < primes . size ( ) ; i ++ ) { for ( int j = 0 ; j < i - 1 ; j ++ ) { if ( primes [ j ] + primes [ j + 1 ] + 1 == primes [ i ] ) { count ++ ; break ; } } if ( count == k ) return true ; } return false ; } int main ( ) { int n = 27 , k = 2 ; if ( specialPrimeNumbers ( n , k ) ) cout << " YES " << endl ; else cout << " NO " << endl ; return 0 ; }
Prime factors of a big number | CPP program to print prime factors and their powers . ; function to calculate all the prime factors and count of every prime factor ; count the number of times 2 divides ; n >>= 1 ; equivalent to n = n / 2 ; ; if 2 divides it ; check for all the possible numbers that can divide it ; if n at the end is a prime number . ; driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void factorize ( long long n ) { int count = 0 ; while ( ! ( n % 2 ) ) { count ++ ; } if ( count ) cout << 2 << " ▁ " << count << endl ; for ( long long i = 3 ; i <= sqrt ( n ) ; i += 2 ) { count = 0 ; while ( n % i == 0 ) { count ++ ; n = n / i ; } if ( count ) cout << i << " ▁ " << count << endl ; } if ( n > 2 ) cout << n << " ▁ " << 1 << endl ; } int main ( ) { long long n = 1000000000000000000 ; factorize ( n ) ; return 0 ; }