codeparrot/xlcost-text-to-code huggingface dataset:
Find the number after successive division | C ++ program to implement above approach ; Function to find the number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNum ( int div [ ] , int rem [ ] , int N ) { int num = rem [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) { num = num * div [ i ] + rem [ i ] ; } return num ; } int main ( ) { int div [ ] = { 8 , 3 } ; int rem [ ] = { 2 , 2 } ; int N = sizeof ( div ) / sizeof ( div [ 0 ] ) ; cout << findNum ( div , rem , N ) ; return 0 ; }
Ways to form n / 2 pairs such that difference of pairs is minimum | C ++ implementation of the above approach ; Using mod because the number of ways might be very large ; ways is serving the same purpose as discussed ; pairing up zero people requires one way . ; map count stores count of s . ; sort according to value ; Iterating backwards . ; Checking if current count is odd . ; if current count = 5 , multiply ans by ways [ 4 ] . ; left out person will be selected in current_count ways ; left out person will pair with previous person in previous_count ways ; if previous count is odd , * then multiply answer by ways [ prev_count - 1 ] . * since one has already been reserved , * remaining will be even . * reduce prev_count = 0 , since we don 't need it now. ; if prev count is even , one will be reserved , * therefore decrement by 1. * In the next iteration , prev_count will become odd * and it will be handled in the same way . ; if current count is even , * then simply multiply ways [ current_count ] * to answer . ; multiply answer by ways [ first__count ] since that is left out , after iterating the array . ; Driver code
#include <bits/stdc++.h> NEW_LINE #define mp  make_pair NEW_LINE #define pb  push_back NEW_LINE #define S  second NEW_LINE #define ll  long long NEW_LINE using namespace std ; const int mod = 1000000007 ; const int MAX = 100000 ; ll ways [ MAX + 1 ] ; void preCompute ( ) { ways [ 0 ] = 1LL ; ways [ 2 ] = 1LL ; for ( int i = 4 ; i <= MAX ; i += 2 ) { ways [ i ] = ( 1LL * ( i - 1 ) * ways [ i - 2 ] ) % mod ; } } void countWays ( int * arr , int n ) { map < int , int > count ; for ( int i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] ++ ; vector < pair < int , int > > count_vector ; map < int , int > :: iterator it ; for ( it = count . begin ( ) ; it != count . end ( ) ; it ++ ) { count_vector . pb ( mp ( it -> first , it -> second ) ) ; } sort ( count_vector . begin ( ) , count_vector . end ( ) ) ; ll ans = 1 ; for ( int i = count_vector . size ( ) - 1 ; i > 0 ; i -- ) { int current_count = count_vector [ i ] . S ; int prev_count = count_vector [ i - 1 ] . S ; if ( current_count & 1 ) { ans = ( ans * ways [ current_count - 1 ] ) % mod ; ans = ( ans * current_count ) % mod ; ans = ( ans * prev_count ) % mod ; if ( prev_count & 1 ) { ans = ( ans * ways [ prev_count - 1 ] ) % mod ; count_vector [ i - 1 ] . S = 0 ; } else { count_vector [ i - 1 ] . S -- ; } } else { ans = ( ans * ways [ current_count ] ) % mod ; } } ans = ( ans * ways [ count_vector [ 0 ] . S ] ) % mod ; cout << ans << " STRNEWLINE " ; } int main ( ) { preCompute ( ) ; int arr [ ] = { 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 4 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countWays ( arr , n ) ; return 0 ; }
Program to find the profit or loss when CP of N items is equal to SP of M items | C ++ implementation of above formula ; Function to calculate Profit or loss ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void profitLoss ( int N , int M ) { if ( N == M ) cout << " No ▁ Profit ▁ nor ▁ Loss " ; else { float result = 0.0 ; result = float ( abs ( N - M ) ) / M ; if ( N - M < 0 ) cout << " Loss ▁ = ▁ - " << result * 100 << " % " ; else cout << " Profit ▁ = ▁ " << result * 100 << " % " ; } } int main ( ) { int N = 8 , M = 9 ; profitLoss ( N , M ) ; return 0 ; }
Aronson 's Sequence | C ++ program to generate the first n terms of Aronson 's sequence ; Returns the given number in words ; Get number of digits in given number ; Base cases ; The following arrays contain one digit ( both cardinal and ordinal forms ) , two digit ( < 20 , ordinal forms ) numbers , and multiples ( ordinal forms ) and powers of 10. ; If single digit number ; here len can be 3 or 4 ; last two digits ; Handle all powers of 10 ; Handle two digit numbers < 20 ; Function to print the first n terms of Aronson 's sequence ; check if character is alphabet or not ; convert number to words in ordinal format and append ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string convert_to_words ( string num ) { int len = num . length ( ) ; if ( len == 0 len > 4 ) { return " " ; } string single_digits_temp [ ] = { " " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " } ; string single_digits [ ] = { " " , " first " , " second " , " third " , " fourth " , " fifth " , " sixth " , " seventh " , " eighth " , " ninth " } ; string two_digits [ ] = { " " , " tenth " , " eleventh " , " twelfth " , " thirteenth " , " fourteenth " , " fifteenth " , " sixteenth " , " seventeenth " , " eighteenth " , " nineteenth " } ; string tens_multiple [ ] = { " " , " tenth " , " twentieth " , " thirtieth " , " fortieth " , " fiftieth " , " sixtieth " , " seventieth " , " eightieth " , " ninetieth " } ; string tens_power [ ] = { " hundred " , " thousand " } ; string word = " " ; if ( len == 1 ) { word += single_digits [ num [ 0 ] - '0' ] ; return word ; } int i = 0 , ctr = 0 ; string s = " ▁ " ; while ( i < len ) { if ( len >= 3 ) { if ( num [ i ] != '0' ) { word += single_digits_temp [ num [ i ] - '0' ] + " ▁ " ; word += tens_power [ len - 3 ] + " ▁ " ; ctr ++ ; } len -- ; num . erase ( 0 , 1 ) ; } else { if ( ctr != 0 ) { s = " ▁ and ▁ " ; word . erase ( word . length ( ) - 1 ) ; } if ( num [ i + 1 ] == '0' ) if ( num [ i ] == '0' ) word = word + " th " ; else word += s + tens_multiple [ num [ i ] - '0' ] ; else if ( num [ i ] == '1' ) word += s + two_digits [ num [ i + 1 ] - '0' + 1 ] ; else { if ( num [ i ] != '0' ) word += s + tens_multiple [ num [ i ] - '0' ] . substr ( 0 , tens_multiple [ num [ i ] - '0' ] . length ( ) - 4 ) + " y ▁ " ; else word += s ; word += single_digits [ num [ i + 1 ] - '0' ] ; } i += 2 ; } if ( i == len ) { if ( word [ 0 ] == ' ▁ ' ) word . erase ( 0 , 1 ) ; } } return word ; } void Aronsons_sequence ( int n ) { string str = " T ▁ is ▁ the ▁ " ; int ind = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( isalpha ( str [ i ] ) ) { ind += 1 ; } if ( str [ i ] == ' t ' or str [ i ] == ' T ' ) { n -= 1 ; str += convert_to_words ( to_string ( ind ) ) + " , ▁ " ; cout << ind << " , ▁ " ; } if ( n == 0 ) break ; } } int main ( void ) { int n = 6 ; Aronsons_sequence ( n ) ; return 0 ; }
Find all good indices in the given Array | C ++ program to find all good indices in the given array ; Function to find all good indices in the given array ; hash to store frequency of each element ; Storing frequency of each element and calculating sum simultaneously ; check if array is good after removing i - th index element ; print good indices ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void niceIndices ( int A [ ] , int n ) { int sum = 0 ; map < int , int > m ; for ( int i = 0 ; i < n ; ++ i ) { m [ A [ i ] ] ++ ; sum += A [ i ] ; } for ( int i = 0 ; i < n ; ++ i ) { int k = sum - A [ i ] ; if ( k % 2 == 0 ) { k = k >> 1 ; if ( m . find ( k ) != m . end ( ) ) { if ( ( A [ i ] == k && m [ k ] > 1 ) || ( A [ i ] != k ) ) cout << ( i + 1 ) << " ▁ " ; } } } } int main ( ) { int A [ ] = { 8 , 3 , 5 , 2 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; niceIndices ( A , n ) ; return 0 ; }
Count pieces of circle after N cuts | C ++ program to find number of pieces of circle after N cuts ; Function to find number of pieces of circle after N cuts ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPieces ( int N ) { return 2 * N ; } int main ( ) { int N = 100 ; cout << countPieces ( N ) ; return 0 ; }
Sum of all the multiples of 3 and 7 below N | C ++ program to find the sum of all multiples of 3 and 7 below N ; Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of 3 and 7 below N ; Since , we need the sum of multiples less than N ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long sumAP ( long long n , long long d ) { n /= d ; return ( n ) * ( 1 + n ) * d / 2 ; } long long sumMultiples ( long long n ) { n -- ; return sumAP ( n , 3 ) + sumAP ( n , 7 ) - sumAP ( n , 21 ) ; } int main ( ) { long long n = 24 ; cout << sumMultiples ( n ) ; return 0 ; }
Check whether product of digits at even places is divisible by sum of digits at odd place of a number | C ++ implementation of the above approach ; below function checks whether product of digits at even places is divisible by sum of digits at odd places ; if size is even ; if size is odd ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool productSumDivisible ( int n , int size ) { int sum = 0 , product = 1 ; while ( n > 0 ) { if ( size % 2 == 0 ) { product *= n % 10 ; } else { sum += n % 10 ; } n = n / 10 ; size -- ; } if ( product % sum == 0 ) return true ; return false ; } int main ( ) { int n = 1234 ; int len = 4 ; if ( productSumDivisible ( n , len ) ) cout << " TRUE " ; else cout << " FALSE " ; return 0 ; }
GCD of a number raised to some power and another number | CPP program to find GCD of a ^ n and b . ; Returns GCD of a ^ n and b . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long int ll ; ll gcd ( ll a , ll b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } ll powGCD ( ll a , ll n , ll b ) { for ( int i = 0 ; i < n ; i ++ ) a = a * a ; return gcd ( a , b ) ; } int main ( ) { ll a = 10 , b = 5 , n = 2 ; cout << powGCD ( a , n , b ) ; return 0 ; }
Position after taking N steps to the right and left in an alternate manner | C ++ program to find the last coordinate where it ends his journey ; Function to return the last destination ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lastCoordinate ( int n , int a , int b ) { return ( ( n + 1 ) / 2 ) * a - ( n / 2 ) * b ; } int main ( ) { int n = 3 , a = 5 , b = 2 ; cout << lastCoordinate ( n , a , b ) ; return 0 ; }
Smallest number greater than or equal to N divisible by K | C ++ implementation of the above approach ; Function to find the smallest number greater than or equal to N that is divisible by k ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNum ( int N , int K ) { int rem = ( N + K ) % K ; if ( rem == 0 ) return N ; else return N + K - rem ; } int main ( ) { int N = 45 , K = 6 ; cout << " Smallest ▁ number ▁ greater ▁ than ▁ or ▁ equal ▁ to ▁ " << N << " that is divisible by " ▁ < < ▁ K ▁ < < ▁ " is " return 0 ; }
Sum and Product of digits in a number that divide the number | C ++ implementation of the above approach ; Print the sum and product of digits that divides the number . ; Fetching each digit of the number ; Checking if digit is greater than 0 and can divides n . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countDigit ( int n ) { int temp = n , sum = 0 , product = 1 ; while ( temp != 0 ) { int d = temp % 10 ; temp /= 10 ; if ( d > 0 && n % d == 0 ) { sum += d ; product *= d ; } } cout << " Sum ▁ = ▁ " << sum ; cout << " Product = " } int main ( ) { int n = 1012 ; countDigit ( n ) ; return 0 ; }
Largest number smaller than or equal to N divisible by K | C ++ implementation of the above approach ; Function to find the largest number smaller than or equal to N that is divisible by k ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNum ( int N , int K ) { int rem = N % K ; if ( rem == 0 ) return N ; else return N - rem ; } int main ( ) { int N = 45 , K = 6 ; cout << " Largest ▁ number ▁ smaller ▁ than ▁ or ▁ equal ▁ to ▁ " << N << " that is divisible by " < < ▁ K ▁ < < ▁ " is " return 0 ; }
Check if any permutation of a number is divisible by 3 and is Palindromic | C ++ program to check if any permutation of a number is divisible by 3 and is Palindromic ; Function to check if any permutation of a number is divisible by 3 and is Palindromic ; Hash array to store frequency of digits of n ; traverse the digits of integer and store their frequency ; Calculate the sum of digits simultaneously ; Check if number is not divisible by 3 ; If more than one digits have odd frequency , palindromic permutation not possible ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDivisiblePalindrome ( int n ) { int hash [ 10 ] = { 0 } ; int digitSum = 0 ; while ( n ) { digitSum += n % 10 ; hash [ n % 10 ] ++ ; n /= 10 ; } if ( digitSum % 3 != 0 ) return false ; int oddCount = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) { if ( hash [ i ] % 2 != 0 ) oddCount ++ ; } if ( oddCount > 1 ) return false ; else return true ; } int main ( ) { int n = 34734 ; isDivisiblePalindrome ( n ) ? cout << " True " : cout << " False " ; return 0 ; }
Minimum time to return array to its original state after given modifications | C ++ implementation of above approach ; Function to return lcm of two numbers ; Make a graph ; Add edge ; Count reachable nodes from every node . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lcm ( int a , int b ) { return ( a * b ) / ( __gcd ( a , b ) ) ; } int dfs ( int src , vector < int > adj [ ] , vector < bool > & visited ) { visited [ src ] = true ; int count = 1 ; for ( int i = 0 ; i < adj [ src ] . size ( ) ; i ++ ) if ( ! visited [ adj [ src ] [ i ] ] ) count += dfs ( adj [ src ] [ i ] , adj , visited ) ; return count ; } int findMinTime ( int arr [ ] , int P [ ] , int n ) { vector < int > adj [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { adj [ arr [ i ] ] . push_back ( P [ i ] ) ; } vector < bool > visited ( n + 1 ) ; int ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! visited [ i ] ) { ans = lcm ( ans , dfs ( i , adj , visited ) ) ; } } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 } ; int P [ ] = { 3 , 2 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMinTime ( arr , P , n ) ; return 0 ; }
Check whether product of digits at even places of a number is divisible by K | C ++ implementation of the above approach ; below function checks whether product of digits at even places is divisible by K ; if position is even ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool productDivisible ( int n , int k ) { int product = 1 , position = 1 ; while ( n > 0 ) { if ( position % 2 == 0 ) product *= n % 10 ; n = n / 10 ; position ++ ; } if ( product % k == 0 ) return true ; return false ; } int main ( ) { int n = 321922 ; int k = 3 ; if ( productDivisible ( n , k ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Permutations of n things taken r at a time with k things together | CPP program to find the number of permutations of n different things taken r at a time with k things grouped together ; Function to find factorial of a number ; Function to calculate p ( n , r ) ; Function to find the number of permutations of n different things taken r at a time with k things grouped together ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorial ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) fact = fact * i ; return fact ; } int npr ( int n , int r ) { int pnr = factorial ( n ) / factorial ( n - r ) ; return pnr ; } int countPermutations ( int n , int r , int k ) { return factorial ( k ) * ( r - k + 1 ) * npr ( n - k , r - k ) ; } int main ( ) { int n = 8 ; int r = 5 ; int k = 2 ; cout << countPermutations ( n , r , k ) ; return 0 ; }
Greatest Integer Function | CPP program to illustrate greatest integer Function ; Function to calculate the GIF value of a number ; GIF is the floor of a number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int GIF ( float n ) { return floor ( n ) ; } int main ( ) { int n = 2.3 ; cout << GIF ( n ) ; return 0 ; }
Number of triangles formed from a set of points on three lines | CPP program to find the possible number of triangles that can be formed from set of points on three lines ; Returns factorial of a number ; calculate c ( n , r ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorial ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) fact = fact * i ; return fact ; } int ncr ( int n , int r ) { return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) ) ; } int main ( ) { int m = 3 , n = 4 , k = 5 ; int totalTriangles = ncr ( m + n + k , 3 ) - ncr ( m , 3 ) - ncr ( n , 3 ) - ncr ( k , 3 ) ; cout << totalTriangles << endl ; }
Check whether sum of digits at odd places of a number is divisible by K | C ++ implementation of the approach ; function that checks the divisibility of the sum of the digits at odd places of the given number ; if position is odd ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool SumDivisible ( int n , int k ) { int sum = 0 , position = 1 ; while ( n > 0 ) { if ( position % 2 == 1 ) sum += n % 10 ; n = n / 10 ; position ++ ; } if ( sum % k == 0 ) return true ; return false ; } int main ( ) { int n = 592452 ; int k = 3 ; if ( SumDivisible ( n , k ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Check whether sum of digits at odd places of a number is divisible by K | C ++ implementation of the above approach ; Converting integer to string ; Traversing the string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool sumDivisible ( int n , int k ) { int sum = 0 ; string num = to_string ( n ) ; int i ; for ( i = 0 ; i < num . size ( ) ; i ++ ) { if ( i % 2 != 0 ) { sum = sum + ( num [ i ] - '0' ) ; } } if ( sum % k == 0 ) { return true ; } else { return false ; } } int main ( ) { int n = 592452 ; int k = 3 ; if ( sumDivisible ( n , k ) ) { cout << ( " YES " ) ; } else { cout << ( " NO " ) ; } return 0 ; }
Check if a triangle of positive area is possible with the given angles | C ++ program to check if a triangle of positive area is possible with the given angles ; Checking if the sum of three angles is 180 and none of the angles is zero ; Checking if sum of any two angles is greater than equal to the third one ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string isTriangleExists ( int a , int b , int c ) { if ( a != 0 && b != 0 && c != 0 && ( a + b + c ) == 180 ) if ( ( a + b ) >= c || ( b + c ) >= a || ( a + c ) >= b ) return " YES " ; else return " NO " ; else return " NO " ; } int main ( ) { int a = 50 , b = 60 , c = 70 ; cout << isTriangleExists ( a , b , c ) << endl ; return 0 ; }
Find maximum value of x such that n ! % ( k ^ x ) = 0 | C ++ program to maximize the value of x such that n ! % ( k ^ x ) = 0 ; Function to maximize the value of x such that n ! % ( k ^ x ) = 0 ; Find square root of k and add 1 to it ; Run the loop from 2 to m and k should be greater than 1 ; optimize the value of k ; Minimum store ; Driver Code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; class GfG { public : int findX ( int n , int k ) { int r = n , v , u ; int m = sqrt ( k ) + 1 ; for ( int i = 2 ; i <= m && k > 1 ; i ++ ) { if ( i == m ) { i = k ; } for ( u = v = 0 ; k % i == 0 ; v ++ ) { k /= i ; } if ( v > 0 ) { int t = n ; while ( t > 0 ) { t /= i ; u += t ; } r = min ( r , u / v ) ; } } return r ; } } ; int main ( ) { GfG g ; int n = 5 ; int k = 2 ; cout << g . findX ( n , k ) ; }
Ways of selecting men and women from a group to make a team | C ++ implementation of the approach ; Returns factorial of the number ; Function to calculate ncr ; Function to calculate the total possible ways ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) fact *= i ; return fact ; } int ncr ( int n , int r ) { int ncr = fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; return ncr ; } int ways ( int m , int w , int n , int k ) { int ans = 0 ; while ( m >= k ) { ans += ncr ( m , k ) * ncr ( w , n - k ) ; k += 1 ; } return ans ; } int main ( ) { int m , w , n , k ; m = 7 ; w = 6 ; n = 5 ; k = 3 ; cout << ways ( m , w , n , k ) ; }
Product of every K ’ th prime number in an array | C ++ implementation of the approach ; Create a boolean array " prime [ 0 . . n ] " and initialize all the entries as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; 0 and 1 are not prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; compute the answer ; count of primes ; product of the primes ; traverse the array ; if the number is a prime ; increase the count ; if it is the K 'th prime ; Driver code ; create the sieve
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000000 NEW_LINE bool prime [ MAX + 1 ] ; void SieveOfEratosthenes ( ) { memset ( prime , true , sizeof ( prime ) ) ; prime [ 1 ] = false ; prime [ 0 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } } void productOfKthPrimes ( int arr [ ] , int n , int k ) { int c = 0 ; long long int product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { c ++ ; if ( c % k == 0 ) { product *= arr [ i ] ; c = 0 ; } } } cout << product << endl ; } int main ( ) { SieveOfEratosthenes ( ) ; int n = 5 , k = 2 ; int arr [ n ] = { 2 , 3 , 5 , 7 , 11 } ; productOfKthPrimes ( arr , n , k ) ; return 0 ; }
Sum of greatest odd divisor of numbers in given range | C ++ program to find sum of greatest odd divisor of numbers in given range ; Function to return sum of first n odd numbers ; Recursive function to return sum of greatest odd divisor of numbers in range [ 1 , n ] ; if ( n % 2 == 1 ) { Odd n ; else { Even n ; Function to return sum of greatest odd divisor of numbers in range [ a , b ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int square ( int n ) { return n * n ; } int sum ( int n ) { if ( n == 0 ) return 0 ; return square ( ( n + 1 ) / 2 ) + sum ( n / 2 ) ; } return square ( n / 2 ) + sum ( n / 2 ) ; } } int oddDivSum ( int a , int b ) { return sum ( b ) - sum ( a - 1 ) ; } int main ( ) { int a = 3 , b = 9 ; cout << oddDivSum ( a , b ) << endl ; return 0 ; }
Minimum numbers needed to express every integer below N as a sum | CPP program to find count of integers needed to express all numbers from 1 to N . ; function to count length of binary expression of n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countBits ( int n ) { int count = 0 ; while ( n ) { count ++ ; n >>= 1 ; } return count ; } int main ( ) { int n = 32 ; cout << " Minimum ▁ value ▁ of ▁ K ▁ is ▁ = ▁ " << countBits ( n ) << endl ; return 0 ; }