codeparrot/xlcost-text-to-code huggingface dataset:
Compute the parity of a number using XOR and table look | CPP program to illustrate Compute the parity of a number using XOR ; Generating the look - up table while pre - processing ; LOOK_UP is the macro expansion to generate the table ; Function to find the parity ; Number is considered to be of 32 bits ; Dividing the number into 8 - bit chunks while performing X - OR ; Masking the number with 0xff ( 11111111 ) to produce valid 8 - bit result ; Driver code ; Result is 1 for odd parity , 0 for even parity ; Printing the desired result
#include <bits/stdc++.h> NEW_LINE #define P2 ( n )  n, n ^ 1, n ^ 1, n NEW_LINE #define P4 ( n )  P2(n), P2(n ^ 1), P2(n ^ 1), P2(n) NEW_LINE #define P6 ( n )  P4(n), P4(n ^ 1), P4(n ^ 1), P4(n) NEW_LINE #define LOOK_UP  P6(0), P6(1), P6(1), P6(0) NEW_LINE unsigned int table [ 256 ] = { LOOK_UP } ; int Parity ( int num ) { int max = 16 ; while ( max >= 8 ) { num = num ^ ( num >> max ) ; max = max / 2 ; } return table [ num & 0xff ] ; } int main ( ) { unsigned int num = 1742346774 ; bool result = Parity ( num ) ; result ? std :: cout << " Odd ▁ Parity " : std :: cout << " Even ▁ Parity " ; return 0 ; }
Count total number of digits from 1 to n | C ++ program to count total number of digits we have to write from 1 to n ; number_of_digits store total digits we have to write ; In the loop we are decreasing 0 , 9 , 99 ... from n till ( n - i + 1 ) is greater than 0 and sum them to number_of_digits to get the required sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int totalDigits ( int n ) { int number_of_digits = 0 ; for ( int i = 1 ; i <= n ; i *= 10 ) number_of_digits += ( n - i + 1 ) ; return number_of_digits ; } int main ( ) { int n = 13 ; cout << totalDigits ( n ) << endl ; return 0 ; }
Numbers with exactly 3 divisors | C ++ program to print all three - primes smaller than or equal to n using Sieve of Eratosthenes ; Generates all primes upto n and prints their squares ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; print squares of primes upto n . ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void numbersWith3Divisors ( int n ) { bool prime [ n + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; prime [ 0 ] = prime [ 1 ] = 0 ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } cout << " Numbers ▁ with ▁ 3 ▁ divisors ▁ : STRNEWLINE " ; for ( int i = 0 ; i * i <= n ; i ++ ) if ( prime [ i ] ) cout << i * i << " ▁ " ; } int main ( ) { int n = 96 ; numbersWith3Divisors ( n ) ; return 0 ; }
Program for decimal to hexadecimal conversion | C ++ program to convert a decimal number to hexadecimal number ; function to convert decimal to hexadecimal ; char array to store hexadecimal number ; counter for hexadecimal number array ; temporary variable to store remainder ; storing remainder in temp variable . ; check if temp < 10 ; printing hexadecimal number array in reverse order ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; void decToHexa ( int n ) { char hexaDeciNum [ 100 ] ; int i = 0 ; while ( n != 0 ) { int temp = 0 ; temp = n % 16 ; if ( temp < 10 ) { hexaDeciNum [ i ] = temp + 48 ; i ++ ; } else { hexaDeciNum [ i ] = temp + 55 ; i ++ ; } n = n / 16 ; } for ( int j = i - 1 ; j >= 0 ; j -- ) cout << hexaDeciNum [ j ] ; } int main ( ) { int n = 2545 ; decToHexa ( n ) ; return 0 ; }
Program for Decimal to Binary Conversion | C ++ program to convert a decimal number to binary number ; function to convert decimal to binary ; array to store binary number ; counter for binary array ; storing remainder in binary array ; printing binary array in reverse order ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; void decToBinary ( int n ) { int binaryNum [ 32 ] ; int i = 0 ; while ( n > 0 ) { binaryNum [ i ] = n % 2 ; n = n / 2 ; i ++ ; } for ( int j = i - 1 ; j >= 0 ; j -- ) cout << binaryNum [ j ] ; } int main ( ) { int n = 17 ; decToBinary ( n ) ; return 0 ; }
Break the number into three parts | C ++ program to count number of ways to break a number in three parts . ; Function to count number of ways to make the given number n ; Driver Function
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; ll count_of_ways ( ll n ) { ll count ; count = ( n + 1 ) * ( n + 2 ) / 2 ; return count ; } int main ( ) { ll n = 3 ; cout << count_of_ways ( n ) << endl ; return 0 ; }
Implement * , | CPP code to illustrate * , - , / using only ' + ' arithmetic operator ; Function to flip the sign using only " + " operator ( It is simple with ' * ' allowed . We need to do a = ( - 1 ) * a ; If sign is + ve turn it - ve and vice - versa ; Check if a and b are of different signs ; Function to subtract two numbers by negating b and adding them ; Negating b ; Function to multiply a by b by adding a to itself b times ; because algo is faster if b < a ; Adding a to itself b times ; Check if final sign must be - ve or + ve ; Function to divide a by b by counting how many times ' b ' can be subtracted from ' a ' before getting 0 ; Raise exception if b is 0 ; Negating b to subtract from a ; Subtracting divisor from dividend ; Check if a and b are of similar symbols or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int flipSign ( int a ) { int neg = 0 ; int tmp = a < 0 ? 1 : -1 ; while ( a != 0 ) { neg += tmp ; a += tmp ; } return neg ; } bool areDifferentSign ( int a , int b ) { return ( ( a < 0 && b > 0 ) || ( a > 0 && b < 0 ) ) ; } int sub ( int a , int b ) { return a + flipSign ( b ) ; } int mul ( int a , int b ) { if ( a < b ) return mul ( b , a ) ; int sum = 0 ; for ( int i = abs ( b ) ; i > 0 ; i -- ) sum += a ; if ( b < 0 ) sum = flipSign ( sum ) ; return sum ; } int division ( int a , int b ) { if ( b == 0 ) throw ( b ) ; int quotient = 0 , dividend ; int divisor = flipSign ( abs ( b ) ) ; for ( dividend = abs ( a ) ; dividend >= abs ( divisor ) ; dividend += divisor ) quotient ++ ; if ( areDifferentSign ( a , b ) ) quotient = flipSign ( quotient ) ; return quotient ; } int main ( ) { cout << " Subtraction ▁ is ▁ " << sub ( 4 , -2 ) << endl ; cout << " Product ▁ is ▁ " << mul ( -9 , 6 ) << endl ; try { cout << " Division ▁ is ▁ " << division ( 8 , 2 ) ; } catch ( int k ) { cout << " ▁ Exception ▁ : - ▁ Divide ▁ by ▁ 0" ; } return 0 ; }
Number of Groups of Sizes Two Or Three Divisible By 3 | Program to find groups of 2 or 3 whose sum is divisible by 3 ; Initialize groups to 0 ; Increment group with specified remainder ; Return groups using the formula ; Driver Function
#include <iostream> NEW_LINE using namespace std ; int numOfCombinations ( int arr [ ] , int N ) { int C [ 3 ] = { 0 , 0 , 0 } ; for ( int i = 0 ; i < N ; ++ i ) ++ C [ arr [ i ] % 3 ] ; return C [ 1 ] * C [ 2 ] + C [ 0 ] * ( C [ 0 ] - 1 ) / 2 + C [ 0 ] * ( C [ 0 ] - 1 ) * ( C [ 0 ] - 2 ) / 6 + C [ 1 ] * ( C [ 1 ] - 1 ) * ( C [ 1 ] - 2 ) / 6 + C [ 2 ] * ( C [ 2 ] - 1 ) * ( C [ 2 ] - 2 ) / 6 + C [ 0 ] * C [ 1 ] * C [ 2 ] ; } int main ( ) { int arr1 [ 6 ] = { 1 , 5 , 7 , 2 , 9 , 14 } ; cout << numOfCombinations ( arr1 , 6 ) << " STRNEWLINE " ; int arr2 [ 4 ] = { 3 , 6 , 9 , 12 } ; cout << numOfCombinations ( arr2 , 4 ) << " STRNEWLINE " ; return 0 ; }
Check if a number can be written as a sum of ' k ' prime numbers | C ++ implementation to check if N can be written as sum of k primes ; Checking if a number is prime or not ; check for numbers from 2 to sqrt ( x ) if it is divisible return false ; Returns true if N can be written as sum of K primes ; N < 2 K directly return false ; If K = 1 return value depends on primality of N ; if N is even directly return true ; ; If N is odd , then one prime must be 2. All other primes are odd and cannot have a pair sum as even . ; If K >= 3 return true ; ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isprime ( int x ) { for ( int i = 2 ; i * i <= x ; i ++ ) if ( x % i == 0 ) return false ; return true ; } bool isSumOfKprimes ( int N , int K ) { if ( N < 2 * K ) return false ; if ( K == 1 ) return isprime ( N ) ; if ( K == 2 ) { if ( N % 2 == 0 ) return true ; return isprime ( N - 2 ) ; } return true ; } int main ( ) { int n = 10 , k = 2 ; if ( isSumOfKprimes ( n , k ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Count total divisors of A or B in a given range | C ++ program to count total divisors of ' a ' or ' b ' in a given range ; Utility function to find LCM of two numbers ; Function to calculate all divisors in given range ; Find LCM of a and b ; Find common divisor by using LCM ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int FindLCM ( int a , int b ) { return ( a * b ) / __gcd ( a , b ) ; } int rangeDivisor ( int m , int n , int a , int b ) { int lcm = FindLCM ( a , b ) ; int a_divisor = n / a - ( m - 1 ) / a ; int b_divisor = n / b - ( m - 1 ) / b ; int common_divisor = n / lcm - ( m - 1 ) / lcm ; int ans = a_divisor + b_divisor - common_divisor ; return ans ; } int main ( ) { int m = 3 , n = 11 , a = 2 , b = 3 ; cout << rangeDivisor ( m , n , a , b ) << endl ; m = 11 , n = 1000000 , a = 6 , b = 35 ; cout << rangeDivisor ( m , n , a , b ) ; return 0 ; }
Numbers having Unique ( or Distinct ) digits | C ++ implementation to find unique digit numbers in a range ; Function to print unique digit numbers in range from l to r . ; Start traversing the numbers ; Find digits and maintain its hash ; if a digit occurs more than 1 time then break ; num will be 0 only when above loop doesn 't get break that means the  number is unique so print it. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printUnique ( int l , int r ) { for ( int i = l ; i <= r ; i ++ ) { int num = i ; bool visited [ 10 ] = { false } ; while ( num ) { if ( visited [ num % 10 ] ) break ; visited [ num % 10 ] = true ; num = num / 10 ; } if ( num == 0 ) cout << i << " ▁ " ; } } int main ( ) { int l = 1 , r = 20 ; printUnique ( l , r ) ; return 0 ; }
Fibonacci modulo p | C ++ program to find minimal 0 Fibonacci for a prime number p ; Returns position of first Fibonacci number whose modulo p is 0. ; add previous two remainders and then take its modulo p . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinZero ( int p ) { int first = 1 , second = 1 , number = 2 , next = 1 ; while ( next ) { next = ( first + second ) % p ; first = second ; second = next ; number ++ ; } return number ; } int main ( ) { int p = 7 ; cout << " Minimal ▁ zero ▁ is : ▁ " << findMinZero ( p ) << endl ; return 0 ; }
Perfect cubes in a range | A Simple Method to count cubes between a and b ; Traverse through all numbers in given range and one by one check if number is prime ; Check if current number ' i ' is perfect cube ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printCubes ( int a , int b ) { for ( int i = a ; i <= b ; i ++ ) { for ( int j = 1 ; j * j * j <= i ; j ++ ) { if ( j * j * j == i ) { cout << j * j * j << " ▁ " ; break ; } } } } int main ( ) { int a = 1 , b = 100 ; cout << " Perfect ▁ cubes ▁ in ▁ given ▁ range : STRNEWLINE ▁ " ; printCubes ( a , b ) ; return 0 ; }
Converting a Real Number ( between 0 and 1 ) to Binary String | C ++ program to Binary real number to String . ; Function to convert Binary real number to String ; Check if the number is Between 0 to 1 or Not ; Setting a limit on length : 32 characters . ; compare the number to .5 ; Now it become 0.25 ; Driver code ; Input value
#include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; string toBinary ( double n ) { if ( n >= 1 n <= 0 ) return " ERROR " ; string answer ; double frac = 0.5 ; answer . append ( " . " ) ; while ( n > 0 ) { if ( answer . length ( ) >= 32 ) return " ERROR " ; if ( n >= frac ) { answer . append ( "1" ) ; n = n - frac ; } else { answer . append ( "0" ) ; } frac /= 2 ; } return answer ; } int main ( ) { double n = 0.625 ; string result = toBinary ( n ) ; cout << " ( 0" << result << " ) ▁ in ▁ base ▁ 2" << endl ; double m = 0.72 ; result = toBinary ( m ) ; cout << " ( " << result << " ) " << endl ; }
Given a number n , find the first k digits of n ^ n | C ++ program to find the first k digits of n ^ n ; function that manually calculates n ^ n and then removes digits until k digits remain ; loop will terminate when there are only k digits left ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned long long firstkdigits ( int n , int k ) { unsigned long long product = 1 ; for ( int i = 0 ; i < n ; i ++ ) product *= n ; while ( ( int ) ( product / pow ( 10 , k ) ) != 0 ) product = product / 10 ; return product ; } int main ( ) { int n = 15 ; int k = 4 ; cout << firstkdigits ( n , k ) ; return 0 ; }
Check if a large number is divisible by 9 or not | C ++ program to find if a number is divisible by 9 or not ; Function to find that number divisible by 9 or not ; Compute sum of digits ; Check if sum of digits is divisible by 9. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int check ( string str ) { int n = str . length ( ) ; int digitSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) digitSum += ( str [ i ] - '0' ) ; return ( digitSum % 9 == 0 ) ; } int main ( ) { string str = "99333" ; check ( str ) ? cout << " Yes " : cout << " No ▁ " ; return 0 ; }
XOR of all subarray XORs | Set 1 | C ++ program to get total xor of all subarray xors ; Returns XOR of all subarray xors ; initialize result by 0 as ( a xor 0 = a ) ; select the starting element ; select the eNding element ; Do XOR of elements in current subarray ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getTotalXorOfSubarrayXors ( int arr [ ] , int N ) { int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = i ; j < N ; j ++ ) for ( int k = i ; k <= j ; k ++ ) res = res ^ arr [ k ] ; return res ; } int main ( ) { int arr [ ] = { 3 , 5 , 2 , 4 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << getTotalXorOfSubarrayXors ( arr , N ) ; return 0 ; }
XOR of all subarray XORs | Set 1 | C ++ program to get total xor of all subarray xors ; Returns XOR of all subarray xors ; initialize result by 0 as ( a XOR 0 = a ) ; loop over all elements once ; get the frequency of current element ; if frequency is odd , then include it in the result ; return the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getTotalXorOfSubarrayXors ( int arr [ ] , int N ) { int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int freq = ( i + 1 ) * ( N - i ) ; if ( freq % 2 == 1 ) res = res ^ arr [ i ] ; } return res ; } int main ( ) { int arr [ ] = { 3 , 5 , 2 , 4 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << getTotalXorOfSubarrayXors ( arr , N ) ; return 0 ; }
GCD of more than two ( or array ) numbers | C ++ program to find GCD of two or more numbers ; Function to return gcd of a and b ; Function to find gcd of array of numbers ; Driver code
#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 findGCD ( int arr [ ] , int n ) { int result = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { result = gcd ( arr [ i ] , result ) ; if ( result == 1 ) { return 1 ; } } return result ; } int main ( ) { int arr [ ] = { 2 , 4 , 6 , 8 , 16 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findGCD ( arr , n ) << endl ; return 0 ; }
GCD of more than two ( or array ) numbers |  ; recursive implementation ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int GcdOfArray ( vector < int > & arr , int idx ) { if ( idx == arr . size ( ) - 1 ) { return arr [ idx ] ; } int a = arr [ idx ] ; int b = GcdOfArray ( arr , idx + 1 ) ; return __gcd ( a , b ) ; } int main ( ) { vector < int > arr = { 1 , 2 , 3 } ; cout << GcdOfArray ( arr , 0 ) << " STRNEWLINE " ; arr = { 2 , 4 , 6 , 8 } ; cout << GcdOfArray ( arr , 0 ) << " STRNEWLINE " ; return 0 ; }
Superperfect Number | C ++ program to check whether number is superperfect or not ; Function to calculate sum of all divisors ; Final result of summation of divisors ; find all divisors which divides ' num ' ; if ' i ' is divisor of ' num ' ; if both divisors are same then add it only once else add both ; Returns true if n is Super Perfect else false . ; Find the sum of all divisors of number n ; Again find the sum of all divisors of n1 and check if sum is equal to n1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int divSum ( int num ) { int result = 0 ; for ( int i = 1 ; i * i <= num ; ++ i ) { if ( num % i == 0 ) { if ( i == ( num / i ) ) result += i ; else result += ( i + num / i ) ; } } return result ; } bool isSuperPerfect ( int n ) { int n1 = divSum ( n ) ; return ( 2 * n == divSum ( n1 ) ) ; } int main ( ) { int n = 16 ; cout << ( isSuperPerfect ( n ) ? " Yes STRNEWLINE " : " No STRNEWLINE " ) ; n = 6 ; cout << ( isSuperPerfect ( n ) ? " Yes STRNEWLINE " : " No STRNEWLINE " ) ; return 0 ; }
Refactorable number | C ++ program to check whether number is refactorable or not ; Function to count all divisors ; Initialize result ; If divisors are equal , count only one . ; Otherwise count both ; Driver Code
#include <bits/stdc++.h> NEW_LINE bool isRefactorableNumber ( int n ) { int divCount = 0 ; for ( int i = 1 ; i <= sqrt ( n ) ; ++ i ) { if ( n % i == 0 ) { if ( n / i == i ) ++ divCount ; else divCount += 2 ; } } return n % divCount == 0 ; } int main ( ) { int n = 8 ; if ( isRefactorableNumber ( n ) ) puts ( " yes " ) ; else puts ( " no " ) ; n = 14 ; if ( isRefactorableNumber ( n ) ) puts ( " yes " ) ; else puts ( " no " ) ; return 0 ; }
Count all perfect divisors of a number | Below is C ++ code to count total perfect Divisors ; Utility function to check perfect square number ; Returns count all perfect divisors of n ; Initialize result ; Consider every number that can be a divisor of n ; If i is a divisor ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfectSquare ( int n ) { int sq = ( int ) sqrt ( n ) ; return ( n == sq * sq ) ; } int countPerfectDivisors ( int n ) { int count = 0 ; for ( int i = 1 ; i * i <= n ; ++ i ) { if ( n % i == 0 ) { if ( isPerfectSquare ( i ) ) ++ count ; if ( n / i != i && isPerfectSquare ( n / i ) ) ++ count ; } } return count ; } int main ( ) { int n = 16 ; cout << " Total ▁ perfect ▁ divisors ▁ of ▁ " << n << " ▁ = ▁ " << countPerfectDivisors ( n ) << " STRNEWLINE " ; n = 12 ; cout << " Total ▁ perfect ▁ divisors ▁ of ▁ " << n << " ▁ = ▁ " << countPerfectDivisors ( n ) ; return 0 ; }
Nearest element with at | C ++ program to print nearest element with at least one common prime factor . ; Loop covers the every element of arr [ ] ; Loop that covers from 0 to i - 1 and i + 1 to n - 1 indexes simultaneously ; print position of closest element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void nearestGcd ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; ++ i ) { int closest = -1 ; for ( int j = i - 1 , k = i + 1 ; j > 0 k <= n ; -- j , ++ k ) { if ( j >= 0 && __gcd ( arr [ i ] , arr [ j ] ) > 1 ) { closest = j + 1 ; break ; } if ( k < n && __gcd ( arr [ i ] , arr [ k ] ) > 1 ) { closest = k + 1 ; break ; } } cout << closest << " ▁ " ; } } int main ( ) { int arr [ ] = { 2 , 9 , 4 , 3 , 13 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; nearestGcd ( arr , n ) ; return 0 ; }
Largest subsequence having GCD greater than 1 | Efficient C ++ program to find length of the largest subsequence with GCD greater than 1. ; prime [ ] for storing smallest prime divisor of element count [ ] for storing the number of times a particular divisor occurs in a subsequence ; Simple sieve to find smallest prime factors of numbers smaller than MAX ; Prime number will have same divisor ; Returns length of the largest subsequence with GCD more than 1. ; Fetch total unique prime divisor of element ; Increment count [ ] of Every unique divisor we get till now ; Find maximum frequency of divisor ; Driver code ; Pre - compute smallest divisor of all numbers
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100001 NEW_LINE int prime [ MAX ] , countdiv [ MAX ] ; void SieveOfEratosthenes ( ) { for ( int i = 2 ; i * i <= MAX ; ++ i ) { if ( ! prime [ i ] ) for ( int j = i * 2 ; j <= MAX ; j += i ) prime [ j ] = i ; } for ( int i = 1 ; i < MAX ; ++ i ) if ( ! prime [ i ] ) prime [ i ] = i ; } int largestGCDSubsequence ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int element = arr [ i ] ; while ( element > 1 ) { int div = prime [ element ] ; ++ countdiv [ div ] ; ans = max ( ans , countdiv [ div ] ) ; while ( element % div == 0 ) element /= div ; } } return ans ; } int main ( ) { SieveOfEratosthenes ( ) ; int arr [ ] = { 10 , 15 , 7 , 25 , 9 , 35 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << largestGCDSubsequence ( arr , size ) ; return 0 ; }
Count of Binary Digit numbers smaller than N | C ++ program to count all binary digit numbers smaller than N ; method returns count of binary digit numbers smaller than N ; queue to store all intermediate binary digit numbers ; binary digits start with 1 ; loop until we have element in queue ; push next binary digit numbers only if current popped element is N ; uncomment below line to print actual number in sorted order cout << t << " ▁ " ; ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOfBinaryNumberLessThanN ( int N ) { queue < int > q ; q . push ( 1 ) ; int cnt = 0 ; int t ; while ( ! q . empty ( ) ) { t = q . front ( ) ; q . pop ( ) ; if ( t <= N ) { cnt ++ ; q . push ( t * 10 ) ; q . push ( t * 10 + 1 ) ; } } return cnt ; } int main ( ) { int N = 200 ; cout << countOfBinaryNumberLessThanN ( N ) ; return 0 ; }
Sum of product of x and y such that floor ( n / x ) = y | C ++ program to find sum of product of x and y such that n / x = y ( Integer Division ) ; Return the sum of product x * y . ; Iterating x from 1 to n ; Finding y = n / x . ; Adding product of x and y to answer . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumofproduct ( int n ) { int ans = 0 ; for ( int x = 1 ; x <= n ; x ++ ) { int y = n / x ; ans += ( y * x ) ; } return ans ; } int main ( ) { int n = 10 ; cout << sumofproduct ( n ) << endl ; return 0 ; }
Program for Perrin numbers | Optimized C ++ program for n 'th perrin number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int per ( int n ) { int a = 3 , b = 0 , c = 2 , i ; int m ; if ( n == 0 ) return a ; if ( n == 1 ) return b ; if ( n == 2 ) return c ; while ( n > 2 ) { m = a + b ; a = b ; b = c ; c = m ; n -- ; } return m ; } int main ( ) { int n = 9 ; cout << per ( n ) ; return 0 ; }
Find the first natural number whose factorial is divisible by x | A cpp program for finding the Special Factorial Number ; function for calculating factorial ; function for check Special_Factorial_Number ; call fact function and the Modulo with k and check if condition is TRUE then return i ; driver function ; taking input
#include <bits/stdc++.h> NEW_LINE #include <boost/multiprecision/cpp_int.hpp> NEW_LINE using boost :: multiprecision :: cpp_int ; using namespace std ; cpp_int fact ( int n ) { cpp_int num = 1 ; for ( int i = 1 ; i <= n ; i ++ ) num = num * i ; return num ; } int Special_Factorial_Number ( int k ) { for ( int i = 1 ; i <= k ; i ++ ) { if ( ( fact ( i ) % k ) == 0 ) { return i ; } } } int main ( ) { int k = 16 ; cout << Special_Factorial_Number ( k ) ; }