codeparrot/xlcost-text-to-code huggingface dataset:
Palindromic Primes | C ++ Program to print all palindromic primes smaller than or equal to n . ; A function that returns true only if num contains one digit ; comparison operation is faster than division operation . So using following instead of " return ▁ num ▁ / ▁ 10 ▁ = = ▁ 0 ; " ; A recursive function to find out whether num is palindrome or not . Initially , dupNum contains address of a copy of num . ; Base case ( needed for recursion termination ) : This statement / mainly compares the first digit with the last digit ; This is the key line in this method . Note that all recursive / calls have a separate copy of num , but they all share same copy of * dupNum . We divide num while moving up the recursion tree ; The following statements are executed when we move up the recursion call tree ; At this point , if num % 10 contains i ' th ▁ ▁ digit ▁ from ▁ beginning , ▁ then ▁ ( * dupNum ) %10 ▁ ▁ contains ▁ i ' th digit from end ; The main function that uses recursive function isPalUtil ( ) to find out whether num is palindrome or not ; If num is negative , make it positive ; Create a separate copy of num , so that modifications made to address dupNum don 't  change the input number.  int *dupNum = new int(num); *dupNum = num ; Function to generate all primes and checking whether number is palindromic or not ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Print all palindromic prime numbers ; checking whether the given number is prime palindromic or not ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int oneDigit ( int num ) { return ( num >= 0 && num < 10 ) ; } bool isPalUtil ( int num , int * dupNum ) { if ( oneDigit ( num ) ) return ( num == ( * dupNum ) % 10 ) ; if ( ! isPalUtil ( num / 10 , dupNum ) ) return false ; * dupNum /= 10 ; return ( num % 10 == ( * dupNum ) % 10 ) ; } int isPal ( int num ) { if ( num < 0 ) num = - num ; return isPalUtil ( num , dupNum ) ; } void printPalPrimesLessThanN ( int n ) { bool prime [ n + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int p = 2 ; p <= n ; p ++ ) if ( prime [ p ] && isPal ( p ) ) cout << p << " ▁ " ; } int main ( ) { int n = 100 ; printf ( " Palindromic ▁ primes ▁ smaller ▁ than ▁ or ▁ " " equal ▁ to ▁ % d ▁ are ▁ : STRNEWLINE " , n ) ; printPalPrimesLessThanN ( n ) ; }
Almost Prime Numbers | Program to print first n numbers that are k - primes ; A function to count all prime factors of a given number ; Count the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , count i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; A function to print the first n numbers that are k - almost primes . ; Print this number if it is k - prime ; Increment count of k - primes printed so far ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPrimeFactors ( int n ) { int count = 0 ; while ( n % 2 == 0 ) { n = n / 2 ; count ++ ; } for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { n = n / i ; count ++ ; } } if ( n > 2 ) count ++ ; return ( count ) ; } void printKAlmostPrimes ( int k , int n ) { for ( int i = 1 , num = 2 ; i <= n ; num ++ ) { if ( countPrimeFactors ( num ) == k ) { printf ( " % d ▁ " , num ) ; i ++ ; } } return ; } int main ( ) { int n = 10 , k = 2 ; printf ( " First ▁ % d ▁ % d - almost ▁ prime ▁ numbers ▁ : ▁ STRNEWLINE " , n , k ) ; printKAlmostPrimes ( k , n ) ; return 0 ; }
Program to add two fractions | C ++ program to add 2 fractions ; Function to return gcd of a and b ; Function to convert the obtained fraction into it 's simplest form ; Finding gcd of both terms ; Converting both terms into simpler terms by dividing them by common factor ; Function to add two fractions ; Finding gcd of den1 and den2 ; Denominator of final fraction obtained finding LCM of den1 and den2 LCM * GCD = a * b ; Changing the fractions to have same denominator Numerator of the final fraction obtained ; Calling function to convert final fraction into it 's simplest form ; Driver program
#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 ) ; } void lowest ( int & den3 , int & num3 ) { int common_factor = gcd ( num3 , den3 ) ; den3 = den3 / common_factor ; num3 = num3 / common_factor ; } void addFraction ( int num1 , int den1 , int num2 , int den2 , int & num3 , int & den3 ) { den3 = gcd ( den1 , den2 ) ; den3 = ( den1 * den2 ) / den3 ; num3 = ( num1 ) * ( den3 / den1 ) + ( num2 ) * ( den3 / den2 ) ; lowest ( den3 , num3 ) ; } int main ( ) { int num1 = 1 , den1 = 500 , num2 = 2 , den2 = 1500 , den3 , num3 ; addFraction ( num1 , den1 , num2 , den2 , num3 , den3 ) ; printf ( " % d / % d ▁ + ▁ % d / % d ▁ is ▁ equal ▁ to ▁ % d / % d STRNEWLINE " , num1 , den1 , num2 , den2 , num3 , den3 ) ; return 0 ; }
The Lazy Caterer 's Problem | A C ++ program to find the solution to The Lazy Caterer 's Problem ; This function receives an integer n and returns the maximum number of pieces that can be made form pancake using n cuts ; Use the formula ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int findPieces ( int n ) { return ( n * ( n + 1 ) ) / 2 + 1 ; } int main ( ) { cout << findPieces ( 1 ) << endl ; cout << findPieces ( 2 ) << endl ; cout << findPieces ( 3 ) << endl ; cout << findPieces ( 50 ) << endl ; return 0 ; }
Count digits in a factorial | Set 2 | A optimised program to find the number of digits in a factorial ; Returns the number of digits present in n ! Since the result can be large long long is used as return type ; factorial of - ve number doesn 't exists ; base case ; Use Kamenetsky formula to calculate the number of digits ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long findDigits ( int n ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; double x = ( ( n * log10 ( n / M_E ) + log10 ( 2 * M_PI * n ) / 2.0 ) ) ; return floor ( x ) + 1 ; } int main ( ) { cout << findDigits ( 1 ) << endl ; cout << findDigits ( 50000000 ) << endl ; cout << findDigits ( 1000000000 ) << endl ; cout << findDigits ( 120 ) << endl ; return 0 ; }
Count digits in a factorial | Set 1 | A C ++ program to find the number of digits in a factorial ; This function receives an integer n , and returns the number of digits present in n ! ; factorial exists only for n >= 0 ; base case ; else iterate through n and calculate the value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findDigits ( int n ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; double digits = 0 ; for ( int i = 2 ; i <= n ; i ++ ) digits += log10 ( i ) ; return floor ( digits ) + 1 ; } int main ( ) { cout << findDigits ( 1 ) << endl ; cout << findDigits ( 5 ) << endl ; cout << findDigits ( 10 ) << endl ; cout << findDigits ( 120 ) << endl ; return 0 ; }
Find number of subarrays with even sum | C ++ program to count number of sub - arrays whose sum is even using brute force Time Complexity - O ( N ^ 2 ) Space Complexity - O ( 1 ) ; Find sum of all subarrays and increment result if sum is even ; Driver code
#include <iostream> NEW_LINE using namespace std ; int countEvenSum ( int arr [ ] , int n ) { int result = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { int sum = 0 ; for ( int j = i ; j <= n - 1 ; j ++ ) { sum = sum + arr [ j ] ; if ( sum % 2 == 0 ) result ++ ; } } return ( result ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 2 , 3 , 4 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " The ▁ Number ▁ of ▁ Subarrays ▁ with ▁ even " " ▁ sum ▁ is ▁ " << countEvenSum ( arr , n ) ; return ( 0 ) ; }
Find number of subarrays with even sum | C ++ program to count number of sub - arrays with even sum using an efficient algorithm Time Complexity - O ( N ) Space Complexity - O ( 1 ) ; A temporary array of size 2. temp [ 0 ] is going to store count of even subarrays and temp [ 1 ] count of odd . temp [ 0 ] is initialized as 1 because there a single even element is also counted as a subarray ; Initialize count . sum is sum of elements under modulo 2 and ending with arr [ i ] . ; i ' th ▁ iteration ▁ computes ▁ sum ▁ of ▁ arr [ 0 . . i ] ▁ ▁ under ▁ modulo ▁ 2 ▁ and ▁ increments ▁ even / odd ▁ count ▁ ▁ according ▁ to ▁ sum ' s value ; 2 is added to handle negative numbers ; Increment even / odd count ; Use handshake lemma to count even subarrays ( Note that an even cam be formed by two even or two odd ) ; Driver code
#include <iostream> NEW_LINE using namespace std ; int countEvenSum ( int arr [ ] , int n ) { int temp [ 2 ] = { 1 , 0 } ; int result = 0 , sum = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { sum = ( ( sum + arr [ i ] ) % 2 + 2 ) % 2 ; temp [ sum ] ++ ; } result = result + ( temp [ 0 ] * ( temp [ 0 ] - 1 ) / 2 ) ; result = result + ( temp [ 1 ] * ( temp [ 1 ] - 1 ) / 2 ) ; return ( result ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 2 , 3 , 4 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " The ▁ Number ▁ of ▁ Subarrays ▁ with ▁ even " " ▁ sum ▁ is ▁ " << countEvenSum ( arr , n ) ; return ( 0 ) ; }
Sum of Fibonacci Numbers | C ++ Program to find sum of Fibonacci numbers ; Computes value of first fibonacci numbers ; Initialize result ; Add remaining terms ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateSum ( int n ) { if ( n <= 0 ) return 0 ; int fibo [ n + 1 ] ; fibo [ 0 ] = 0 , fibo [ 1 ] = 1 ; int sum = fibo [ 0 ] + fibo [ 1 ] ; for ( int i = 2 ; i <= n ; i ++ ) { fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ; sum += fibo [ i ] ; } return sum ; } int main ( ) { int n = 4 ; cout << " Sum ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : ▁ " << calculateSum ( n ) << endl ; return 0 ; }
Sum of Fibonacci Numbers | C ++ Program to find sum of Fibonacci numbers in O ( Log n ) time . ; Create an array for memoization ; Returns n 'th Fibonacci number using table f[] ; Base cases ; If fib ( n ) is already computed ; Applying above formula [ Note value n & 1 is 1 if n is odd , else 0 ] . ; Computes value of first Fibonacci numbers ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1000 ; int f [ MAX ] = { 0 } ; int fib ( int n ) { if ( n == 0 ) return 0 ; if ( n == 1 n == 2 ) return ( f [ n ] = 1 ) ; if ( f [ n ] ) return f [ n ] ; int k = ( n & 1 ) ? ( n + 1 ) / 2 : n / 2 ; f [ n ] = ( n & 1 ) ? ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) : ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ; return f [ n ] ; } int calculateSum ( int n ) { return fib ( n + 2 ) - 1 ; } int main ( ) { int n = 4 ; cout << " Sum ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : ▁ " << calculateSum ( n ) << endl ; return 0 ; }
Find all combinations that add upto given number | C ++ program to find out all combinations of positive numbers that add upto given number ; arr - array to store the combination index - next location in array num - given number reducedNum - reduced number ; Base condition ; If combination is found , print it ; Find the previous number stored in arr [ ] It helps in maintaining increasing order ; note loop starts from previous number i . e . at array location index - 1 ; next element of array is k ; call recursively with reduced number ; Function to find out all combinations of positive numbers that add upto given number . It uses findCombinationsUtil ( ) ; array to store the combinations It can contain max n elements ; find all combinations ; Driver code
#include <iostream> NEW_LINE using namespace std ; void findCombinationsUtil ( int arr [ ] , int index , int num , int reducedNum ) { if ( reducedNum < 0 ) return ; if ( reducedNum == 0 ) { for ( int i = 0 ; i < index ; i ++ ) cout << arr [ i ] << " ▁ " ; cout << endl ; return ; } int prev = ( index == 0 ) ? 1 : arr [ index - 1 ] ; for ( int k = prev ; k <= num ; k ++ ) { arr [ index ] = k ; findCombinationsUtil ( arr , index + 1 , num , reducedNum - k ) ; } } void findCombinations ( int n ) { int arr [ n ] ; findCombinationsUtil ( arr , 0 , n , n ) ; } int main ( ) { int n = 5 ; findCombinations ( n ) ; return 0 ; }
Find Square Root under Modulo p | Set 2 ( Shanks Tonelli algorithm ) | C ++ program to implement Shanks Tonelli algorithm for finding Modular Square Roots ; utility function to find pow ( base , exponent ) % modulus ; utility function to find gcd ; Returns k such that b ^ k = 1 ( mod p ) ; Initializing k with first odd prime number ; function return p - 1 ( = x argument ) as x * 2 ^ e , where x will be odd sending e as reference because updation is needed in actual e ; Main function for finding the modular square root ; a and p should be coprime for finding the modular square root ; If below expression return ( p - 1 ) then modular square root is not possible ; expressing p - 1 , in terms of s * 2 ^ e , where s is odd number ; finding smallest q such that q ^ ( ( p - 1 ) / 2 ) ( mod p ) = p - 1 ; q - 1 is in place of ( - 1 % p ) ; Initializing variable x , b and g ; keep looping until b become 1 or m becomes 0 ; finding m such that b ^ ( 2 ^ m ) = 1 ; updating value of x , g and b according to algorithm ; driver program to test above function ; p should be prime
#include <bits/stdc++.h> NEW_LINE using namespace std ; int pow ( int base , int exponent , int modulus ) { int result = 1 ; base = base % modulus ; while ( exponent > 0 ) { if ( exponent % 2 == 1 ) result = ( result * base ) % modulus ; exponent = exponent >> 1 ; base = ( base * base ) % modulus ; } return result ; } int gcd ( int a , int b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } int order ( int p , int b ) { if ( gcd ( p , b ) != 1 ) { printf ( " p ▁ and ▁ b ▁ are ▁ not ▁ co - prime . STRNEWLINE " ) ; return -1 ; } int k = 3 ; while ( 1 ) { if ( pow ( b , k , p ) == 1 ) return k ; k ++ ; } } int convertx2e ( int x , int & e ) { e = 0 ; while ( x % 2 == 0 ) { x /= 2 ; e ++ ; } return x ; } int STonelli ( int n , int p ) { if ( gcd ( n , p ) != 1 ) { printf ( " a ▁ and ▁ p ▁ are ▁ not ▁ coprime STRNEWLINE " ) ; return -1 ; } if ( pow ( n , ( p - 1 ) / 2 , p ) == ( p - 1 ) ) { printf ( " no ▁ sqrt ▁ possible STRNEWLINE " ) ; return -1 ; } int s , e ; s = convertx2e ( p - 1 , e ) ; int q ; for ( q = 2 ; ; q ++ ) { if ( pow ( q , ( p - 1 ) / 2 , p ) == ( p - 1 ) ) break ; } int x = pow ( n , ( s + 1 ) / 2 , p ) ; int b = pow ( n , s , p ) ; int g = pow ( q , s , p ) ; int r = e ; while ( 1 ) { int m ; for ( m = 0 ; m < r ; m ++ ) { if ( order ( p , b ) == -1 ) return -1 ; if ( order ( p , b ) == pow ( 2 , m ) ) break ; } if ( m == 0 ) return x ; x = ( x * pow ( g , pow ( 2 , r - m - 1 ) , p ) ) % p ; g = pow ( g , pow ( 2 , r - m ) , p ) ; b = ( b * g ) % p ; if ( b == 1 ) return x ; r = m ; } } int main ( ) { int n = 2 ; int p = 113 ; int x = STonelli ( n , p ) ; if ( x == -1 ) printf ( " Modular ▁ square ▁ root ▁ is ▁ not ▁ exist STRNEWLINE " ) ; else printf ( " Modular ▁ square ▁ root ▁ of ▁ % d ▁ and ▁ % d ▁ is ▁ % d STRNEWLINE " , n , p , x ) ; }
Check if a number is a power of another number | CPP program to check given number number y is power of x ; logarithm function to calculate value ; compare to the result1 or result2 both are equal ; Driven program
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; bool isPower ( int x , int y ) { int res1 = log ( y ) / log ( x ) ; return ( res1 == res2 ) ; } int main ( ) { cout << isPower ( 27 , 729 ) << endl ; return 0 ; }
Program to find the Roots of Quadratic equation | C ++ program to find roots of a quadratic equation ; Prints roots of quadratic equation ax * 2 + bx + x ; If a is 0 , then equation is not quadratic , but linear ; else d < 0 ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findRoots ( int a , int b , int c ) { if ( a == 0 ) { cout << " Invalid " ; return ; } int d = b * b - 4 * a * c ; double sqrt_val = sqrt ( abs ( d ) ) ; if ( d > 0 ) { cout << " Roots ▁ are ▁ real ▁ and ▁ different ▁ STRNEWLINE " ; cout << ( double ) ( - b + sqrt_val ) / ( 2 * a ) << " STRNEWLINE " << ( double ) ( - b - sqrt_val ) / ( 2 * a ) ; } else if ( d == 0 ) { cout << " Roots ▁ are ▁ real ▁ and ▁ same ▁ STRNEWLINE " ; cout << - ( double ) b / ( 2 * a ) ; } { cout << " Roots ▁ are ▁ complex ▁ STRNEWLINE " ; cout << - ( double ) b / ( 2 * a ) << " ▁ + ▁ i " << sqrt_val << " STRNEWLINE " << - ( double ) b / ( 2 * a ) << " ▁ - ▁ i " << sqrt_val ; } } int main ( ) { int a = 1 , b = -7 , c = 12 ; findRoots ( a , b , c ) ; return 0 ; }
Check perfect square using addition / subtraction | C ++ program to check if n is perfect square or not ; This function returns true if n is perfect square , else false ; sum is sum of all odd numbers . i is used one by one hold odd numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfectSquare ( int n ) { for ( int sum = 0 , i = 1 ; sum < n ; i += 2 ) { sum += i ; if ( sum == n ) return true ; } return false ; } int main ( ) { isPerfectSquare ( 35 ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; isPerfectSquare ( 49 ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; return 0 ; }
Count ' d ' digit positive integers with 0 as a digit | C ++ program to find the count of positive integer of a given number of digits that contain atleast one zero ; Returns count of ' d ' digit integers have 0 as a digit ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCount ( int d ) { return 9 * ( pow ( 10 , d - 1 ) - pow ( 9 , d - 1 ) ) ; } int main ( ) { int d = 1 ; cout << findCount ( d ) << endl ; d = 2 ; cout << findCount ( d ) << endl ; d = 4 ; cout << findCount ( d ) << endl ; return 0 ; }
Dyck path | C ++ program to count number of Dyck Paths ; Returns count Dyck paths in n x n grid ; Compute value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int countDyckPaths ( unsigned int n ) { int res = 1 ; for ( int i = 0 ; i < n ; ++ i ) { res *= ( 2 * n - i ) ; res /= ( i + 1 ) ; } return res / ( n + 1 ) ; } int main ( ) { int n = 4 ; cout << " Number ▁ of ▁ Dyck ▁ Paths ▁ is ▁ " << countDyckPaths ( n ) ; return 0 ; }
Triangular Numbers | C ++ program to check if a number is a triangular number using simple approach . ; Returns true if ' num ' is triangular , else false ; Base case ; A Triangular number must be sum of first n natural numbers ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isTriangular ( int num ) { if ( num < 0 ) return false ; int sum = 0 ; for ( int n = 1 ; sum <= num ; n ++ ) { sum = sum + n ; if ( sum == num ) return true ; } return false ; } int main ( ) { int n = 55 ; if ( isTriangular ( n ) ) cout << " The ▁ number ▁ is ▁ a ▁ triangular ▁ number " ; else cout << " The ▁ number ▁ is ▁ NOT ▁ a ▁ triangular ▁ number " ; return 0 ; }
Triangular Numbers | C ++ program to check if a number is a triangular number using quadratic equation . ; Returns true if num is triangular ; Considering the equation n * ( n + 1 ) / 2 = num The equation is : a ( n ^ 2 ) + bn + c = 0 "; ; Find roots of equation ; checking if root1 is natural ; checking if root2 is natural ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isTriangular ( int num ) { if ( num < 0 ) return false ; int c = ( -2 * num ) ; int b = 1 , a = 1 ; int d = ( b * b ) - ( 4 * a * c ) ; if ( d < 0 ) return false ; float root1 = ( - b + sqrt ( d ) ) / ( 2 * a ) ; float root2 = ( - b - sqrt ( d ) ) / ( 2 * a ) ; if ( root1 > 0 && floor ( root1 ) == root1 ) return true ; if ( root2 > 0 && floor ( root2 ) == root2 ) return true ; return false ; } int main ( ) { int num = 55 ; if ( isTriangular ( num ) ) cout << " The ▁ number ▁ is ▁ a ▁ triangular ▁ number " ; else cout << " The ▁ number ▁ is ▁ NOT ▁ a ▁ triangular ▁ number " ; return 0 ; }
Frobenius coin problem | C ++ program to find the largest number that cannot be formed from given two coins ; Utility function to find gcd ; Function to print the desired output ; Solution doesn 't exist   if GCD is not 1 ; Else apply the formula ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { int c ; while ( a != 0 ) { c = a ; a = b % a ; b = c ; } return b ; } void forbenius ( int X , int Y ) { if ( gcd ( X , Y ) != 1 ) { cout << " NA STRNEWLINE " ; return ; } int A = ( X * Y ) - ( X + Y ) ; int N = ( X - 1 ) * ( Y - 1 ) / 2 ; cout << " Largest ▁ Amount ▁ = ▁ " << A << endl ; cout << " Total ▁ Count ▁ = ▁ " << N << endl ; } int main ( ) { int X = 2 , Y = 5 ; forbenius ( X , Y ) ; X = 5 , Y = 10 ; cout << endl ; forbenius ( X , Y ) ; return 0 ; }
Gray to Binary and Binary to Gray conversion | C ++ program for Binary To Gray and Gray to Binary conversion ; Helper function to xor two characters ; Helper function to flip the bit ; function to convert binary string to gray string ; MSB of gray code is same as binary code ; Compute remaining bits , next bit is computed by doing XOR of previous and current in Binary ; Concatenate XOR of previous bit with current bit ; function to convert gray code string to binary string ; MSB of binary code is same as gray code ; Compute remaining bits ; If current bit is 0 , concatenate previous bit ; Else , concatenate invert of previous bit ; Driver program to test above functions
#include <iostream> NEW_LINE using namespace std ; char xor_c ( char a , char b ) { return ( a == b ) ? '0' : '1' ; } char flip ( char c ) { return ( c == '0' ) ? '1' : '0' ; } string binarytoGray ( string binary ) { string gray = " " ; gray += binary [ 0 ] ; for ( int i = 1 ; i < binary . length ( ) ; i ++ ) { gray += xor_c ( binary [ i - 1 ] , binary [ i ] ) ; } return gray ; } string graytoBinary ( string gray ) { string binary = " " ; binary += gray [ 0 ] ; for ( int i = 1 ; i < gray . length ( ) ; i ++ ) { if ( gray [ i ] == '0' ) binary += binary [ i - 1 ] ; else binary += flip ( binary [ i - 1 ] ) ; } return binary ; } int main ( ) { string binary = "01001" ; cout << " Gray ▁ code ▁ of ▁ " << binary << " ▁ is ▁ " << binarytoGray ( binary ) << endl ; string gray = "01101" ; cout << " Binary ▁ code ▁ of ▁ " << gray << " ▁ is ▁ " << graytoBinary ( gray ) << endl ; return 0 ; }
Solving f ( n ) = ( 1 ) + ( 2 * 3 ) + ( 4 * 5 * 6 ) . . . n using Recursion | CPP Program to print the solution of the series f ( n ) = ( 1 ) + ( 2 * 3 ) + ( 4 * 5 * 6 ) . . . n using recursion ; Recursive function for finding sum of series calculated - number of terms till which sum of terms has been calculated current - number of terms for which sum has to becalculated N - Number of terms in the function to be calculated ; checking termination condition ; product of terms till current ; recursive call for adding terms next in the series ; Driver Code ; input number of terms in the series ; invoking the function to calculate the sum
#include <bits/stdc++.h> NEW_LINE using namespace std ; int seriesSum ( int calculated , int current , int N ) { int i , cur = 1 ; if ( current == N + 1 ) return 0 ; for ( i = calculated ; i < calculated + current ; i ++ ) cur *= i ; return cur + seriesSum ( i , current + 1 , N ) ; } int main ( ) { int N = 5 ; cout << seriesSum ( 1 , 1 , N ) << endl ; return 0 ; }
How to avoid overflow in modular multiplication ? | C ++ program for modular multiplication without any overflow ; To compute ( a * b ) % mod ; ll res = 0 ; Initialize result ; If b is odd , add ' a ' to result ; Multiply ' a ' with 2 ; Divide b by 2 ; Return result ; Driver program
#include <iostream> NEW_LINE using namespace std ; typedef long long int ll ; ll mulmod ( ll a , ll b , ll mod ) { a = a % mod ; while ( b > 0 ) { if ( b % 2 == 1 ) res = ( res + a ) % mod ; a = ( a * 2 ) % mod ; b /= 2 ; } return res % mod ; } int main ( ) { ll a = 9223372036854775807 , b = 9223372036854775807 ; cout << mulmod ( a , b , 100000000000 ) ; return 0 ; }