codeparrot/xlcost-text-to-code huggingface dataset:
Check if it is possible to perform the given Grid Division | C ++ implementation of the approach ; Function that returns true if it is possible to divide the grid satisfying the given conditions ; To store the sum of all the cells of the given parts ; If the sum is equal to the total number of cells in the given grid ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( int arr [ ] , int p , int n , int m ) { int sum = 0 ; for ( int i = 0 ; i < p ; i ++ ) sum += arr [ i ] ; if ( sum == ( n * m ) ) return true ; return false ; } int main ( ) { int n = 3 , m = 4 ; int arr [ ] = { 6 , 3 , 2 , 1 } ; int p = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( isPossible ( arr , p , n , m ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Minimum steps to make the product of the array equal to 1 | C ++ implementation of the approach ; Function to return the minimum steps required ; To store the count of 0 s , positive and negative numbers ; To store the ans ; If array element is equal to 0 ; If array element is a negative number ; Extra cost needed to make it - 1 ; If array element is a positive number ; Extra cost needed to make it 1 ; Now the array will have - 1 , 0 and 1 only ; As count of negative is even so we will change all 0 to 1 total cost here will be count of 0 s ; If there are zeroes present in the array ; Change one zero to - 1 and rest of them to 1 Total cost here will be count of '0' ; If there are no zeros in the array ; As no 0 s are available so we have to change one - 1 to 1 which will cost 2 to change - 1 to 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinStep ( int a [ ] , int n ) { int positive = 0 , negative = 0 , zero = 0 ; int step = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { zero ++ ; } else if ( a [ i ] < 0 ) { negative ++ ; step = step + ( -1 - a [ i ] ) ; } else { positive ++ ; step = step + ( a [ i ] - 1 ) ; } } if ( negative % 2 == 0 ) { step = step + zero ; } else { if ( zero > 0 ) { step = step + zero ; } else { step = step + 2 ; } } return step ; } int main ( ) { int a [ ] = { 0 , -2 , -1 , -3 , 4 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << MinStep ( a , n ) ; return 0 ; }
Find the first N integers such that the sum of their digits is equal to 10 | C ++ implementation of the approach ; Function to return the sum of digits of n ; Add the last digit to the sum ; Remove last digit ; Return the sum of digits ; Function to print the first n numbers whose sum of digits is 10 ; First number of the series is 19 ; If the sum of digits of the current number is equal to 10 ; Print the number ; Add 9 to the previous number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int n ) { int sum = 0 ; while ( n ) { sum = sum + n % 10 ; n = n / 10 ; } return sum ; } void firstN ( int n ) { int num = 19 , cnt = 1 ; while ( cnt != n ) { if ( sum ( num ) == 10 ) { cout << num << " ▁ " ; cnt ++ ; } num += 9 ; } } int main ( ) { int n = 10 ; firstN ( n ) ; return 0 ; }
Sum of last digit of all integers from 1 to N divisible by M | C ++ implementation of the approach ; Function to return the required sum ; Number of element between 1 to n divisible by m ; Array to store the last digit of elements in a cycle ; Storing and adding last digit of cycle ; Number of elements present in last cycle ; Sum of k / 10 cycle ; Adding value of digits of last cycle to the answer ; Driver Code ; input n and m
#include <iostream> NEW_LINE using namespace std ; #define long  long long NEW_LINE long sumOfLastDig ( long n , long m ) { long sum = 0 , k ; k = n / m ; long arr [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) { arr [ i ] = m * ( i + 1 ) % 10 ; sum += arr [ i ] ; } long rem = k % 10 ; long ans = ( k / 10 ) * sum ; for ( int i = 0 ; i < rem ; i ++ ) { ans += arr [ i ] ; } return ans ; } int main ( ) { long n = 100 , m = 3 ; cout << sumOfLastDig ( n , m ) ; return 0 ; }
Number of Subsequences with Even and Odd Sum | Set 2 | CPP program to find number of Subsequences with Even and Odd Sum ; Function to find number of Subsequences with Even and Odd Sum ; Counting number of odds ; Even count ; Total Subsequences is ( 2 ^ n - 1 ) For NumberOfEvenSubsequences subtract NumberOfOddSubsequences from total ; Driver code ; Calling the function
#include <bits/stdc++.h> NEW_LINE using namespace std ; pair < int , int > countSum ( int arr [ ] , int n ) { int NumberOfOdds = 0 , NumberOfEvens = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] & 1 ) NumberOfOdds ++ ; NumberOfEvens = n - NumberOfOdds ; int NumberOfOddSubsequences = ( 1 << NumberOfEvens ) * ( 1 << ( NumberOfOdds - 1 ) ) ; int NumberOfEvenSubsequences = ( 1 << n ) - 1 - NumberOfOddSubsequences ; return { NumberOfEvenSubsequences , NumberOfOddSubsequences } ; } int main ( ) { int arr [ ] = { 1 , 2 , 2 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; pair < int , int > ans = countSum ( arr , n ) ; cout << " EvenSum ▁ = ▁ " << ans . first ; cout << " ▁ OddSum ▁ = ▁ " << ans . second ; return 0 ; }
Program to find the next prime number | C ++ implementation of the approach ; Function that returns true if n is prime else returns false ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to return the smallest prime number greater than N ; Base case ; Loop continuously until isPrime returns true for a number greater than n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } int nextPrime ( int N ) { if ( N <= 1 ) return 2 ; int prime = N ; bool found = false ; while ( ! found ) { prime ++ ; if ( isPrime ( prime ) ) found = true ; } return prime ; } int main ( ) { int N = 3 ; cout << nextPrime ( N ) ; return 0 ; }
Check if a number is Flavius Number | C ++ implementation ; Return the number is Flavious Number or not ; index i starts from 2 because at 1 st iteration every 2 nd element was remove and keep going for k - th iteration ; removing the elements which are already removed at kth iteration ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool Survives ( int n ) { int i ; for ( int i = 2 ; ; i ++ ) { if ( i > n ) return true ; if ( n % i == 0 ) return false ; n -= n / i ; } } int main ( ) { int n = 17 ; if ( Survives ( n ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Nth XOR Fibonacci number | C ++ implementation of the approach ; Function to return the nth XOR Fibonacci number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nthXorFib ( int n , int a , int b ) { if ( n == 0 ) return a ; if ( n == 1 ) return b ; if ( n == 2 ) return ( a ^ b ) ; return nthXorFib ( n % 3 , a , b ) ; } int main ( ) { int a = 1 , b = 2 , n = 10 ; cout << nthXorFib ( n , a , b ) ; return 0 ; }
Sand Timer Flip Counting Problem | C ++ 14 implementation of the approach ; Recursive function to return the gcd of a and b ; Everything divides 0 ; Function to print the number of flips for both the sand timers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } void flip ( int a , int b ) { int lcm = ( a * b ) / gcd ( a , b ) ; a = lcm / a ; b = lcm / b ; cout << a - 1 << " ▁ " << b - 1 ; } int main ( ) { int a = 10 ; int b = 5 ; flip ( a , b ) ; }
Sum of N terms in the expansion of Arcsin ( x ) | C ++ implementation of the approach ; Function to find the arcsin ( x ) ; The power to which ' x ' is raised ; Numerator value ; Denominator value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find_Solution ( double x , int n ) { double sum = x , e = 2 , o = 1 , p = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { p += 2 ; sum += ( double ) ( o / e ) * ( double ) ( pow ( x , p ) / p ) ; o = o * ( o + 2 ) ; e = e * ( e + 2 ) ; } cout << setprecision ( 10 ) << sum ; } int main ( ) { double x = -0.5 ; if ( abs ( x ) >= 1 ) { cout << " Invalid ▁ Input STRNEWLINE " ; return 0 ; } int n = 8 ; find_Solution ( x , n ) ; return 0 ; }
Minimize the cost of buying the Objects | C ++ program of above approach ; Function that will calculate the price ; Calculate the number of items we can get for free ; Calculate the number of items we will have to pay the price for ; Calculate the price ; Driver code ; Calling function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int totalPay ( int totalItems , int priceOfOneItem , int N , int M ) { int freeItems = 0 , actual = 0 ; freeItems = totalItems / ( N + M ) ; actual = totalItems - freeItems ; int amount = actual * priceOfOneItem ; return amount ; } int main ( ) { int T = 12 , P = 8 ; int N = 2 , M = 1 ; cout << " Amount ▁ = ▁ " << totalPay ( T , P , N , M ) ; return 0 ; }
Count of all possible pairs of disjoint subsets of integers from 1 to N | C ++ implementation of the approach ; Modulo exponentiation function ; Function to calculate ( x ^ y ) % p in O ( log ( y ) ) ; Driver function ; Evaluating ( ( 3 ^ n - 2 ^ ( n + 1 ) + 1 ) / 2 ) % p ; From Fermatss little theorem a ^ - 1 ? a ^ ( m - 2 ) ( mod m )
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define p  1000000007 NEW_LINE long long power ( long long x , long long y ) { long long res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res % p ; } int main ( ) { long long n = 3 ; long long x = ( power ( 3 , n ) % p + 1 ) % p ; x = ( x - power ( 2 , n + 1 ) + p ) % p ; x = ( x * power ( 2 , p - 2 ) ) % p ; cout << x << " STRNEWLINE " ; }
Right most non | C ++ implementation of the approach ; Function to return the rightmost non - zero digit in the multiplication of the array elements ; To store the count of times 5 can divide the array elements ; Divide the array elements by 5 as much as possible ; increase count of 5 ; Divide the array elements by 2 as much as possible ; Decrease count of 5 , because a '2' and a '5' makes a number with last digit '0' ; If c5 is more than the multiplier should be taken as 5 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int rightmostNonZero ( int a [ ] , int n ) { int c5 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( a [ i ] > 0 && a [ i ] % 5 == 0 ) { a [ i ] /= 5 ; c5 ++ ; } } for ( int i = 0 ; i < n ; i ++ ) { while ( c5 && a [ i ] > 0 && ! ( a [ i ] & 1 ) ) { a [ i ] >>= 1 ; c5 -- ; } } long long ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) { ans = ( ans * a [ i ] % 10 ) % 10 ; } if ( c5 ) ans = ( ans * 5 ) % 10 ; if ( ans ) return ans ; return -1 ; } int main ( ) { int a [ ] = { 7 , 42 , 11 , 64 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << rightmostNonZero ( a , n ) ; return 0 ; }
Find the remainder when First digit of a number is divided by its Last digit | C ++ program to find the remainder when the First digit of a number is divided by its Last digit ; Function to find the remainder ; Get the last digit ; Get the first digit ; Compute the remainder ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findRemainder ( int n ) { int l = n % 10 ; while ( n >= 10 ) n /= 10 ; int f = n ; int remainder = f % l ; cout << remainder << endl ; } int main ( ) { int n = 5223 ; findRemainder ( n ) ; return 0 ; }
Percentage increase in the volume of cuboid if length , breadth and height are increased by fixed percentages | C ++ implementation of the approach ; Function to return the percentage increase in the volume of the cuboid ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double increaseInVol ( double l , double b , double h ) { double percentInc = ( 1 + ( l / 100 ) ) * ( 1 + ( b / 100 ) ) * ( 1 + ( h / 100 ) ) ; percentInc -= 1 ; percentInc *= 100 ; return percentInc ; } int main ( ) { double l = 50 , b = 20 , h = 10 ; cout << increaseInVol ( l , b , h ) << " % " ; return 0 ; }
Count the number of occurrences of a particular digit in a number | C ++ program to count the number of occurrences of a particular digit in a number ; Function to count the occurrences of the digit D in N ; Loop to find the digits of N ; check if the digit is D ; return the count of the occurrences of D in N ; Driver code
#include <iostream> NEW_LINE using namespace std ; long long int countOccurrances ( long long int n , int d ) { long long int count = 0 ; while ( n > 0 ) { count = ( n % 10 == d ) ? count + 1 : count ; n = n / 10 ; } return count ; } int main ( ) { int d = 2 ; long long int n = 214215421 ; cout << countOccurrances ( n , d ) << endl ; return 0 ; }
Find number of factors of N when location of its two factors whose product is N is given | C ++ program to implement the above problem ; Function to find the number of factors ; print the number of factors ; Driver code ; initialize the factors position
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findFactors ( int a , int b ) { int c ; c = a + b - 1 ; cout << c ; } int main ( ) { int a , b ; a = 13 ; b = 36 ; findFactors ( a , b ) ; return 0 ; }
21 Matchsticks Problem | C ++ implementation of the approach ; Function to return the optimal strategy ; Removing matchsticks in blocks of five ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void TwentyoneMatchstick ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i += 1 ) { cout << 5 - arr [ i ] << " ▁ " ; } cout << endl ; } int main ( ) { int arr [ ] = { 3 , 4 , 2 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; TwentyoneMatchstick ( arr , N ) ; return 0 ; }
Find the smallest positive number which can not be represented by given digits | CPP program to find the smallest positive number which can not be represented by given digits ; Function to find the smallest positive number which can not be represented by given digits ; Storing the count of 0 digit or store the value at 0 th index ; Calculates the min value in the array starting from 1 st index and also store it index . ; If its value at 0 th index is less than min value than either 10 , 100 , 1000 ... can 't be expressed ; If it value is greater than min value than iterate the loop upto first min value index and simply print it index value . ; Driver code ; Value of N is always 10 as we take digit from 0 to 9 ; Calling function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void expressDigit ( int arr [ ] , int n ) { int min = 9 , index = 0 , temp = 0 ; temp = arr [ 0 ] ; for ( int i = 1 ; i < 10 ; i ++ ) { if ( arr [ i ] < min ) { min = arr [ i ] ; index = i ; } } if ( temp < min ) { cout << 1 ; for ( int i = 1 ; i <= temp + 1 ; i ++ ) cout << 0 ; } else { for ( int i = 0 ; i < min ; i ++ ) cout << index ; cout << index ; } } int main ( ) { int arr [ ] = { 2 , 2 , 1 , 2 , 1 , 1 , 3 , 1 , 1 , 1 } ; int N = 10 ; expressDigit ( arr , N ) ; return 0 ; }
Find the average of k digits from the beginning and l digits from the end of the given number | implementation of the approach ; Function to return the count of digits in num ; Function to return the sum of first n digits of num ; Remove the unnecessary digits ; Function to return the sum of the last n digits of num ; If the average can 't be calculated without  using the same digit more than once ; Sum of the last l digits of n ; Sum of the first k digits of n ( totalDigits - k ) must be removed from the end of the number to get the remaining k digits from the beginning ; Return the average ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDigits ( int num ) { int cnt = 0 ; while ( num > 0 ) { cnt ++ ; num /= 10 ; } return cnt ; } int sumFromStart ( int num , int n , int rem ) { num /= ( ( int ) pow ( 10 , rem ) ) ; int sum = 0 ; while ( num > 0 ) { sum += ( num % 10 ) ; num /= 10 ; } return sum ; } int sumFromEnd ( int num , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ( num % 10 ) ; num /= 10 ; } return sum ; } float getAverage ( int n , int k , int l ) { int totalDigits = countDigits ( n ) ; if ( totalDigits < ( k + l ) ) return -1 ; int sum1 = sumFromEnd ( n , l ) ; int sum2 = sumFromStart ( n , k , totalDigits - k ) ; return ( ( float ) ( sum1 + sum2 ) / ( float ) ( k + l ) ) ; } int main ( ) { int n = 123456 , k = 2 , l = 3 ; cout << getAverage ( n , k , l ) ; return 0 ; }
Print N terms of Withoff Sequence | C ++ program to find Wythoff array ; Function to find the n , k term of Wythoff array ; tau = ( sqrt ( 5 ) + 1 ) / 2 ; Already_stored ; T ( n , - 1 ) = n - 1. ; T ( n , 0 ) = floor ( n * tau ) . ; T ( n , k ) = T ( n , k - 1 ) + T ( n , k - 2 ) for k >= 1. ; Store ; Return the ans ; Function to find first n terms of Wythoff array by traversing in anti - diagonal ; Map to store the Wythoff array ; Anti diagonal ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Wythoff ( map < int , map < int , int > > & mp , int n , int k ) { double tau = ( sqrt ( 5 ) + 1 ) / 2.0 , t_n_k ; if ( mp [ n ] [ k ] != 0 ) return mp [ n ] [ k ] ; if ( k == -1 ) { return n - 1 ; } else if ( k == 0 ) { t_n_k = floor ( n * tau ) ; } else { t_n_k = Wythoff ( mp , n , k - 1 ) + Wythoff ( mp , n , k - 2 ) ; } mp [ n ] [ k ] = t_n_k ; return ( int ) t_n_k ; } void Wythoff_Array ( int n ) { int i = 0 , j = 0 , count = 0 ; map < int , map < int , int > > mp ; while ( count < n ) { cout << Wythoff ( mp , i + 1 , j + 1 ) ; count ++ ; if ( count != n ) cout << " , ▁ " ; i ++ ; j -- ; if ( j < 0 ) { j = i ; i = 0 ; } } } int main ( ) { int n = 15 ; Wythoff_Array ( n ) ; return 0 ; }
Sum of two numbers if the original ratio and new ratio obtained by adding a given number to each number is given | C ++ implementation of the approach ; Function to return the sum of numbers which are in the ration a : b and after adding x to both the numbers the new ratio becomes c : d ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double sum ( double a , double b , double c , double d , double x ) { double ans = ( x * ( a + b ) * ( c - d ) ) / ( ( a * d ) - ( b * c ) ) ; return ans ; } int main ( ) { double a = 1 , b = 2 , c = 9 , d = 13 , x = 5 ; cout << sum ( a , b , c , d , x ) ; return 0 ; }
Triangle of numbers arising from Gilbreath 's conjecture | C ++ code for printing the Triangle of numbers arising from Gilbreath 's conjecture ; Check whether the number is prime or not ; Set the 0 th row of the matrix with c primes from 0 , 0 to 0 , c - 1 ; Find the n , k term of matrix of Gilbreath 's conjecture ; recursively find ; store the ans ; Print first n terms of Gilbreath sequence successive absolute differences of primes read by antidiagonals upwards . ; map to store the matrix and hash to check if the element is present or not ; set the primes of first row ; print the Gilbreath number ; increase the count ; anti diagonal upwards ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool is_Prime ( int n ) { if ( n < 2 ) return false ; for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) if ( n % i == 0 ) return false ; return true ; } void set_primes ( map < int , map < int , int > > & mp , map < int , map < int , int > > & hash , int c ) { int count = 0 ; for ( int i = 2 ; count < c ; i ++ ) { if ( is_Prime ( i ) ) { mp [ 0 ] [ count ++ ] = i ; hash [ 0 ] [ count - 1 ] = 1 ; } } } int Gilbreath ( map < int , map < int , int > > & mp , map < int , map < int , int > > & hash , int n , int k ) { if ( hash [ n ] [ k ] != 0 ) return mp [ n ] [ k ] ; int ans = abs ( Gilbreath ( mp , hash , n - 1 , k + 1 ) - Gilbreath ( mp , hash , n - 1 , k ) ) ; mp [ n ] [ k ] = ans ; return ans ; } void solve ( int n ) { int i = 0 , j = 0 , count = 0 ; map < int , map < int , int > > mp , hash ; set_primes ( mp , hash , 100 ) ; while ( count < n ) { cout << Gilbreath ( mp , hash , i , j ) << " , ▁ " ; count ++ ; i -- ; j ++ ; if ( i < 0 ) { i = j ; j = 0 ; } } } int main ( ) { int n = 15 ; solve ( n ) ; return 0 ; }
Roots of the quadratic equation when a + b + c = 0 without using Shridharacharya formula | C ++ implementation of the approach ; Function to print the roots of the quadratic equation when a + b + c = 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printRoots ( long a , long b , long c ) { cout << 1 << " , ▁ " << c / ( a * 1.0 ) ; } int main ( ) { long a = 2 ; long b = 3 ; long c = -5 ; printRoots ( a , b , c ) ; return 0 ; }
Longest alternative parity subsequence | C ++ program to find the length of the longest alternative parity subsequence ; Function to find the longest ; Marks the starting of odd number as sequence and alternatively changes ; Finding the longest odd / even sequence ; Find odd number ; Find even number ; Length of the longest even / odd sequence ; Find odd number ; Find even number ; Answer is maximum of both odd / even or even / odd subsequence ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int longestAlternativeSequence ( int a [ ] , int n ) { int maxi1 = 0 ; int f1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! f1 ) { if ( a [ i ] % 2 ) { f1 = 1 ; maxi1 ++ ; } } else { if ( a [ i ] % 2 == 0 ) { maxi1 ++ ; f1 = 0 ; } } } int maxi2 = 0 ; int f2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( f2 ) { if ( a [ i ] % 2 ) { f2 = 1 ; maxi2 ++ ; } } else { if ( a [ i ] % 2 == 0 ) { maxi2 ++ ; f2 = 0 ; } } } return max ( maxi1 , maxi2 ) ; } int main ( ) { int a [ ] = { 13 , 16 , 8 , 9 , 32 , 10 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << longestAlternativeSequence ( a , n ) ; }
Nambiar Number Generator | C ++ implementation of the approach ; Function to return the Nambiar number of the given number ; If there is no digit to choose ; Choose the first digit ; Chosen digit 's parity ; To store the sum of the consecutive digits starting from the chosen digit ; While there are digits to choose ; Update the sum ; If the parity differs ; Return the current sum concatenated with the Numbiar number for the rest of the string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string numbiarNumber ( string str , int i ) { if ( i > str . length ( ) ) return " " ; int firstDigit = str [ i ] - '0' ; int digitParity = firstDigit % 2 ; int sumDigits = 0 ; while ( i < str . length ( ) ) { sumDigits += ( str [ i ] - '0' ) ; int sumParity = sumDigits % 2 ; if ( digitParity != sumParity ) break ; i ++ ; } return ( to_string ( sumDigits ) + numbiarNumber ( str , i + 1 ) ) ; } int main ( ) { string str = "9880127431" ; cout << numbiarNumber ( str , 0 ) << endl ; return 0 ; }
Program to find the Depreciation of Value | CPP program to find depreciation of the value initial value , rate and time are given ; Function to return the depreciation of value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float Depreciation ( float v , float r , float t ) { float D = v * pow ( ( 1 - r / 100 ) , t ) ; return D ; } int main ( ) { float V1 = 200 , R = 10 , T = 2 ; cout << Depreciation ( V1 , R , T ) ; return 0 ; }
Program to find first N Fermat Numbers | CPP program to print fermat numbers ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; llu res = 1 ; Initialize result ; If y is odd , multiply x with the result ; n must be even now y = y >> 1 ; y = y / 2 x = x * x ; Change x to x ^ 2 ; Function to find nth fermat number ; 2 to the power i ; 2 to the power 2 ^ i ; Function to find first n Fermat numbers ; Calculate 2 ^ 2 ^ i ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE #include <boost/multiprecision/cpp_int.hpp> NEW_LINE using namespace boost :: multiprecision ; #define llu  int128_t NEW_LINE using namespace std ; llu power ( llu x , llu y ) { while ( y > 0 ) { if ( y & 1 ) res = res * x ; } return res ; } llu Fermat ( llu i ) { llu power2_i = power ( 2 , i ) ; llu power2_2_i = power ( 2 , power2_i ) ; return power2_2_i + 1 ; } void Fermat_Number ( llu n ) { for ( llu i = 0 ; i < n ; i ++ ) { cout << Fermat ( i ) ; if ( i != n - 1 ) cout << " , ▁ " ; } } int main ( ) { llu n = 7 ; Fermat_Number ( n ) ; return 0 ; }