codeparrot/xlcost-text-to-code huggingface dataset:
Sum of Series ( n ^ 2 | CPP Program to finding the sum of the nth series ; function that calculate the sum of the nth series ; using formula of the nth term ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum_series ( int n ) { int nSquare = n * n ; return nSquare * ( nSquare - 1 ) / 4 ; } int main ( ) { int n = 2 ; cout << sum_series ( n ) << endl ; return 0 ; }
Check if a number is sandwiched between primes | CPP Program to check whether a number is sandwiched between two primes or not ; returns true if number n is prime ; 0 and 1 both are non - primes ; finding square root of n ; checking if n has any factor upto square root of n if yes its not prime ; Driver 's Code
#include <iostream> NEW_LINE #include <cmath> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n == 0 n == 1 ) return false ; int root = sqrt ( n ) ; for ( int i = 2 ; i <= root ; i ++ ) if ( n % i == 0 ) return false ; return true ; } bool isSandwitched ( int n ) { return ( isPrime ( n - 1 ) && isPrime ( n + 1 ) ) ; } int main ( ) { int n = 642 ; cout << n << " ▁ : ▁ " ; if ( isSandwitched ( n ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; n = 9 ; cout << n << " ▁ : ▁ " ; if ( isSandwitched ( n ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; return 0 ; }
Tomohiko Sakamoto 's Algorithm | A CPP program to implement the Tomohiko Sakamoto Algorithm ; function to implement tomohiko sakamoto algorithm ; array with leading number of days values ; if month is less than 3 reduce year by 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int day_of_the_week ( int y , int m , int d ) { int t [ ] = { 0 , 3 , 2 , 5 , 0 , 3 , 5 , 1 , 4 , 6 , 2 , 4 } ; if ( m < 3 ) y -= 1 ; return ( ( y + y / 4 - y / 100 + y / 400 + t [ m - 1 ] + d ) % 7 ) ; } int main ( void ) { int day = 13 , month = 7 , year = 2017 ; cout << ( day_of_the_week ( year , month , day ) ) ; return 0 ; }
Recursive program for prime number | CPP Program to find whether a Number is Prime or Not using Recursion ; Returns true if n is prime , else return false . i is current divisor to check . ; Base cases ; Check for next divisor ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n , int i = 2 ) { if ( n <= 2 ) return ( n == 2 ) ? true : false ; if ( n % i == 0 ) return false ; if ( i * i > n ) return true ; return isPrime ( n , i + 1 ) ; } int main ( ) { int n = 15 ; if ( isPrime ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Square Free Number | C ++ Program to print all prime factors ; Returns true if n is a square free number , else returns false . ; If 2 again divides n , then n is not a square free number . ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; Check if i is a prime factor ; If i again divides , then n is not square free ; Driver program to test above function
# include <bits/stdc++.h> NEW_LINE using namespace std ; bool isSquareFree ( int n ) { if ( n % 2 == 0 ) n = n / 2 ; if ( n % 2 == 0 ) return false ; for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { n = n / i ; if ( n % i == 0 ) return false ; } } return true ; } int main ( ) { int n = 10 ; if ( isSquareFree ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Area of a square from diagonal length | C ++ Program to find the area of square when its diagonal is given . ; Returns area of square from given diagonal ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double findArea ( double d ) { return ( d * d ) / 2.0 ; } int main ( ) { double d = 10 ; cout << ( findArea ( d ) ) ; return 0 ; }
Sum of series 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + . . . + ( 2 * n | Program to find sum of series 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + . . . + ( 2 * n - 1 ) ^ 2. ; Function to find sum of series . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum = sum + ( 2 * i - 1 ) * ( 2 * i - 1 ) ; return sum ; } int main ( ) { int n = 10 ; cout << sumOfSeries ( n ) ; return 0 ; }
Sum of series 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + . . . + ( 2 * n | Program to find sum of series 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + . . . + ( 2 * n - 1 ) ^ 2. ; Function that find sum of series . ; Formula to find sum of series . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfSeries ( int n ) { return ( n * ( 2 * n - 1 ) * ( 2 * n + 1 ) ) / 3 ; } int main ( ) { int n = 10 ; cout << sumOfSeries ( n ) ; return 0 ; }
Program to implement standard error of mean | C ++ Program to implement standard error of mean . ; Function to find sample mean . ; loop to calculate sum of array elements . ; Function to calculate sample standard deviation . ; Function to calculate sample error . ; Formula to find sample error . ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; float mean ( float arr [ ] , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + arr [ i ] ; return sum / n ; } float SSD ( float arr [ ] , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( arr [ i ] - mean ( arr , n ) ) * ( arr [ i ] - mean ( arr , n ) ) ; return sqrt ( sum / ( n - 1 ) ) ; } float sampleError ( float arr [ ] , int n ) { return SSD ( arr , n ) / sqrt ( n ) ; } int main ( ) { float arr [ ] = { 78.53 , 79.62 , 80.25 , 81.05 , 83.21 , 83.46 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << sampleError ( arr , n ) ; return 0 ; }
Minimum moves to reach target on a infinite line | Set 2 | CPP code to find minimum moves to reach target ; Function to find minimum steps to reach target ; Handling negatives by symmetry ; Keep moving while sum is smaller i . e calculating n ; case 1 : d is even ; d is odd ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int StepstoReachTarget ( int target ) { target = abs ( target ) ; int n = ceil ( ( -1.0 + sqrt ( 1 + 8.0 * target ) ) / 2 ) ; int sum = n * ( n + 1 ) / 2 ; if ( sum == target ) return n ; int d = sum - target ; if ( ( d & 1 ) == 0 ) return n ; else return n + ( ( n & 1 ) ? 2 : 1 ) ; } int main ( ) { int target = 5 ; cout << StepstoReachTarget ( target ) ; return 0 ; }
Sum of series 2 / 3 | C ++ program to find sum of given series ; Function to find sum of series up - to n terms ; initializing counter by 1 ; variable to calculate result ; while loop until nth term is not reached ; boolean type variable for checking validation ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double seriesSum ( int n ) { int i = 1 ; double res = 0.0 ; bool sign = true ; while ( n > 0 ) { n -- ; if ( sign ) { sign = ! sign ; res = res + ( double ) ++ i / ++ i ; } else { sign = ! sign ; res = res - ( double ) ++ i / ++ i ; } } return res ; } int main ( ) { int n = 5 ; cout << seriesSum ( n ) ; return 0 ; }
Number of Symmetric Relations on a Set | C ++ program to count total symmetric relations on a set of natural numbers . ; function find the square of n ; Base case ; Return 2 ^ ( n ( n + 1 ) / 2 ) ; Driver code
#include <bits/stdc++.h> NEW_LINE unsigned int countSymmetric ( unsigned int n ) { if ( n == 0 ) return 1 ; return 1 << ( ( n * ( n + 1 ) ) / 2 ) ; } int main ( ) { unsigned int n = 3 ; printf ( " % u " , countSymmetric ( n ) ) ; return 0 ; }
Program for centered nonagonal number | CPP Program to find nth centered nonagonal number . ; Function to find nth centered nonagonal number . ; Formula to find nth centered nonagonal number . ; Driver function .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int centeredNonagonal ( int n ) { return ( 3 * n - 2 ) * ( 3 * n - 1 ) / 2 ; } int main ( ) { int n = 10 ; cout << centeredNonagonal ( n ) ; return 0 ; }
Program for Mean Absolute Deviation | C ++ Program to find mean absolute deviation of given array . ; Function to find mean of the array elements . ; Calculate sum of all elements . ; Function to find mean absolute deviation of given elements . ; Calculate the sum of absolute deviation about mean . ; Return mean absolute deviation about mean . ; Driver function .
#include <bits/stdc++.h> NEW_LINE using namespace std ; float Mean ( float arr [ ] , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + arr [ i ] ; return sum / n ; } float meanAbsoluteDeviation ( float arr [ ] , int n ) { float absSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) absSum = absSum + abs ( arr [ i ] - Mean ( arr , n ) ) ; return absSum / n ; } int main ( ) { float arr [ ] = { 10 , 15 , 15 , 17 , 18 , 21 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << meanAbsoluteDeviation ( arr , n ) ; return 0 ; }
Find if it is possible to get a ratio from given ranges of costs and quantities | C ++ program to find if it is possible to get the ratio r ; Returns true if it is possible to get ratio r from given cost and quantity ranges . ; Calculating cost corresponding to value of i ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isRatioPossible ( int lowCost , int upCost , int lowQuant , int upQuant , int r ) { for ( int i = lowQuant ; i <= upQuant ; i ++ ) { int ans = i * r ; if ( lowCost <= ans && ans <= upCost ) return true ; } return false ; } int main ( ) { int lowCost = 14 , upCost = 30 , lowQuant = 5 , upQuant = 12 , r = 9 ; if ( isRatioPossible ( lowCost , upCost , lowQuant , upQuant , r ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find N integers with given difference between product and sum | CPP code to generate numbers with difference between product and sum is D ; Function to implement calculation ; Driver code
#include <iostream> NEW_LINE using namespace std ; void findNumbers ( int n , int d ) { for ( int i = 0 ; i < n - 2 ; i ++ ) cout << "1" << " ▁ " ; cout << "2" << " ▁ " ; cout << n + d << endl ; } int main ( ) { int N = 3 , D = 5 ; findNumbers ( N , D ) ; return 0 ; }
Sum of fourth powers of first n odd natural numbers | CPP Program to find the sum of fourth powers of first n odd natural numbers ; calculate the sum of fourth power of first n odd natural numbers ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int oddNumSum ( int n ) { return ( n * ( 2 * n + 1 ) * ( 24 * n * n * n - 12 * n * n - 14 * n + 7 ) ) / 15 ; } int main ( ) { int n = 4 ; cout << oddNumSum ( n ) << endl ; return 0 ; }
Trailing number of 0 s in product of two factorials | CPP program for count number of trailing zeros in N ! * M ! ; Returns number of zeros in factorial n ; dividing x by powers of 5 and update count ; Returns count of trailing zeros in M ! x N ! ; Driver program
#include <iostream> NEW_LINE using namespace std ; int trailingZero ( int x ) { int i = 5 , count = 0 ; while ( x > i ) { count = count + x / i ; i = i * 5 ; } return count ; } int countProductTrailing ( int M , int N ) { return trailingZero ( N ) + trailingZero ( M ) ; } int main ( ) { int N = 67 , M = 98 ; cout << countProductTrailing ( N , M ) ; return 0 ; }
Trimorphic Number | C ++ program to check if a number is Trimorphic ; Function to check Trimorphic number ; Store the cube ; Start Comparing digits ; Return false , if any digit of N doesn ' t ▁ match ▁ with ▁ ▁ its ▁ cube ' s digits from last ; Reduce N and cube ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isTrimorphic ( int N ) { int cube = N * N * N ; while ( N > 0 ) { if ( N % 10 != cube % 10 ) return false ; N /= 10 ; cube /= 10 ; } return true ; } int main ( ) { int N = 24 ; isTrimorphic ( N ) ? cout << " trimorphic " : cout << " not ▁ trimporphic " ; return 0 ; }
Trimorphic Number | CPP program to find nth Trimorphic number ; Functions to find nth Trimorphic number ; Comparing the digits ; Return false , if any digit of num doesn ' t ▁ match ▁ with ▁ ▁ its ▁ cube ' s digits from last ; Reduce num and cube ; Check in max int size ; check number is Trimorphic or not ; if counter is equal to the n then return nth number ; Driver code
#include <iostream> NEW_LINE using namespace std ; # define INT_MAX  2147483647 NEW_LINE bool checkTrimorphic ( int num ) { int cube = num * num * num ; while ( num > 0 ) { if ( num % 10 != cube % 10 ) return false ; num /= 10 ; cube /= 10 ; } return true ; } int nthTrimorphic ( int n ) { int count = 0 ; for ( int i = 0 ; i < INT_MAX ; i ++ ) { if ( checkTrimorphic ( i ) ) count ++ ; if ( count == n ) return i ; } } int main ( ) { int n = 9 ; cout << nthTrimorphic ( n ) ; return 0 ; }
Find minimum moves to reach target on an infinite line | CPP program to find minimum moves to reach target if we can move i steps in i - th move . ; Handling negatives by symmetry ; Keep moving while sum is smaller or difference is odd . ; Driver code
#include <iostream> NEW_LINE using namespace std ; int reachTarget ( int target ) { target = abs ( target ) ; int sum = 0 , step = 0 ; while ( sum < target || ( sum - target ) % 2 != 0 ) { step ++ ; sum += step ; } return step ; } int main ( ) { int target = 5 ; cout << reachTarget ( target ) ; return 0 ; }
Sum of fifth powers of the first n natural numbers | CPP Program to find the sum of fifth power of first n natural numbers ; calculate the sum of fifth power of first n natural numbers ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int fifthPowerSum ( int n ) { return ( ( 2 * n * n * n * n * n * n ) + ( 6 * n * n * n * n * n ) + ( 5 * n * n * n * n ) - ( n * n ) ) / 12 ; } int main ( ) { int n = 5 ; cout << fifthPowerSum ( n ) << endl ; return 0 ; }
Find unit digit of x raised to power y | Efficient C ++ program to find unit digit of x ^ y . ; Returns unit digit of x raised to power y ; Initialize result as 1 to handle case when y is 0. ; One by one multiply with x mod 10 to avoid overflow . ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int unitDigitXRaisedY ( int x , int y ) { int res = 1 ; for ( int i = 0 ; i < y ; i ++ ) res = ( res * x ) % 10 ; return res ; } int main ( ) { cout << unitDigitXRaisedY ( 4 , 2 ) ; return 0 ; }
Evaluation of Risk in Investments | C ++ code for above approach ; First Item in the pair is the value of observation ( xi ) . Second Item in the pair is the frequency of xi ( fi ) ; Vector stores the observation in pairs of format ( xi , fi ) , where xi = value of observation ; Function to calculate the summation of fi * xi ; Function to calculate summation fi ; Function to calculate the mean of the set of observations v ; Function to calculate the std deviation of set of observations v ; Get sum of frequencies ; Get the mean of the set of observations ; Driver Code
#include <iostream> NEW_LINE #include <vector> NEW_LINE #include <algorithm> NEW_LINE #include <cmath> NEW_LINE using namespace std ; typedef pair < float , float > Data ; typedef vector < Data > Vector ; float sigma_fx ( const Vector & v ) { float sum = 0 ; for ( auto i : v ) { sum += i . first * i . second ; } return sum ; } float sigma_f ( const Vector & v ) { float sum = 0.0 ; for ( auto i : v ) { sum += i . second ; } return sum ; } float calculate_mean ( const Vector & v ) { return sigma_fx ( v ) / sigma_f ( v ) ; } float calculate_std ( const Vector & v ) { float f = sigma_f ( v ) ; float mean = sigma_fx ( v ) / f ; float sum = 0 ; for ( auto i : v ) { sum += ( i . first - mean ) * ( i . first - mean ) * i . second ; } return sqrt ( sum / f ) ; } int main ( ) { Vector A = { { 0 , 0.1 } , { 100 , 0.1 } , { 200 , 0.2 } , { 333 , 0.3 } , { 400 , 0.3 } } ; Vector B = { { 100 , 0.1 } , { 200 , 0.5 } , { 700 , 0.4 } } ; float avg_A = calculate_mean ( A ) ; float avg_B = calculate_mean ( B ) ; float std_A = calculate_std ( A ) ; float std_B = calculate_std ( B ) ; cout << " For ▁ Investment ▁ A " << endl ; cout << " Average : ▁ " << avg_A << endl ; cout << " Standard ▁ Deviation : ▁ " << std_A << endl ; cout << " Normalised ▁ Std : ▁ " << std_A / avg_A << endl ; cout << " For ▁ Investment ▁ B " << endl ; cout << " Average : ▁ " << avg_B << endl ; cout << " Standard ▁ Deviation : ▁ " << std_B << endl ; cout << " Normalised ▁ Std : ▁ " << std_B / avg_B << endl ; ( std_B / avg_B ) < ( std_A / avg_A ) ? cout << " Investment ▁ B ▁ is ▁ less ▁ risky STRNEWLINE " : cout << " Investment ▁ A ▁ is ▁ less ▁ risky STRNEWLINE " ; return 0 ; }
Max occurring divisor in an interval | Efficient C ++ program to find maximum occurring factor in an interval ; function to find max occurring divisor interval [ x , y ] ; if there is only one number in the in the interval , return that number ; otherwise , 2 is the max occurring divisor ; Driver code
#include <iostream> NEW_LINE using namespace std ; int findDivisor ( int x , int y ) { if ( x == y ) return y ; return 2 ; } int main ( ) { int x = 3 , y = 16 ; cout << findDivisor ( x , y ) ; return 0 ; }
Average of Squares of Natural Numbers | C ++ program to calculate 1 ^ 2 + 2 ^ 2 + 3 ^ 2 + ... average of square number ; Function to calculate average of squares ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float AvgofSquareN ( int n ) { float sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += ( i * i ) ; return sum / n ; } int main ( ) { int n = 2 ; cout << AvgofSquareN ( n ) ; return 0 ; }
Find sum of even factors of a number | Formula based CPP program to find sum of all divisors of n . ; Returns sum of all factors of n . ; If n is odd , then there are no even factors . ; Traversing through all prime factors . ; While i divides n , print i and divide n ; here we remove the 2 ^ 0 that is 1. All other factors ; This condition is to handle the case when n is a prime number . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumofFactors ( int n ) { if ( n % 2 != 0 ) return 0 ; int res = 1 ; for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 , curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; if ( i == 2 && count == 1 ) curr_sum = 0 ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } int main ( ) { int n = 18 ; cout << sumofFactors ( n ) ; return 0 ; }
Find LCM of rational numbers | CPP program to find LCM of given array ; get lcm of two numbers ; Finds LCM of numerators ; calculate the lcm of all numerators ; return all numerator lcm ; Get GCD of all the denominators ; calculate the gcd of all the denominators ; return all denominator gcd ; find lcm of all the rational number ; return the LCM of all numerator / GCD of all denominator ; Driver code ; give rational number 2 / 7 , 3 / 14 , 5 / 3 make pair as a numerator and denominator
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LCM ( int a , int b ) { return ( a * b ) / ( __gcd ( a , b ) ) ; } int lcmOfNumerator ( vector < pair < int , int > > vect ) { int lcm = vect [ 0 ] . first ; for ( int i = 1 ; i < vect . size ( ) ; i ++ ) lcm = LCM ( vect [ i ] . first , lcm ) ; return lcm ; } int gcdOfDemoninators ( vector < pair < int , int > > vect ) { int gcd = vect [ 0 ] . second ; for ( int i = 1 ; i < vect . size ( ) ; i ++ ) gcd = __gcd ( vect [ i ] . second , gcd ) ; return gcd ; } void lcmOfRationals ( vector < pair < int , int > > vect ) { cout << lcmOfNumerator ( vect ) << " / " << gcdOfDemoninators ( vect ) ; } int main ( ) { vector < pair < int , int > > vect ; vect . push_back ( make_pair ( 2 , 7 ) ) ; vect . push_back ( make_pair ( 3 , 14 ) ) ; vect . push_back ( make_pair ( 5 , 3 ) ) ; lcmOfRationals ( vect ) ; return 0 ; }
Program to determine focal length of a spherical mirror | C ++ program to determine the focal length of a of a spherical mirror ; Determines focal length of a spherical concave mirror ; Determines focal length of a spherical convex mirror ; Driver function
#include <iostream> NEW_LINE using namespace std ; float focal_length_concave ( float R ) { return R / 2 ; } float focal_length_convex ( float R ) { return - ( R / 2 ) ; } int main ( ) { float R = 30 ; cout << " Focal ▁ length ▁ of ▁ spherical " << " concave ▁ mirror ▁ is ▁ : ▁ " << focal_length_concave ( R ) << " ▁ units STRNEWLINE " ; cout << " Focal ▁ length ▁ of ▁ spherical " << " convex ▁ mirror ▁ is ▁ : ▁ " << focal_length_convex ( R ) << " ▁ units " ; return 0 ; }
Find sum of odd factors of a number | Formula based CPP program to find sum of all divisors of n . ; Returns sum of all factors of n . ; Traversing through all prime factors . ; ignore even factors by removing all powers of 2 ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumofoddFactors ( int n ) { int res = 1 ; while ( n % 2 == 0 ) n = n / 2 ; for ( int i = 3 ; i <= sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } int main ( ) { int n = 30 ; cout << sumofoddFactors ( n ) ; return 0 ; }
Number of non | CPP program to find the numbers of non negative integral solutions ; return number of non negative integral solutions ; initialize total = 0 ; Base Case if n = 1 and val >= 0 then it should return 1 ; iterate the loop till equal the val ; total solution of equations and again call the recursive function Solutions ( variable , value ) ; return the total no possible solution ; driver code
#include <iostream> NEW_LINE using namespace std ; int countSolutions ( int n , int val ) { int total = 0 ; if ( n == 1 && val >= 0 ) return 1 ; for ( int i = 0 ; i <= val ; i ++ ) { total += countSolutions ( n - 1 , val - i ) ; } return total ; } int main ( ) { int n = 5 ; int val = 20 ; cout << countSolutions ( n , val ) ; }
Fibonomial coefficient and Fibonomial triangle | CPP Program to print Fibonomial Triangle of height n . ; Function to produce Fibonacci Series . ; 0 th and 1 st number of the series are 0 and 1 ; Add the previous 2 numbers in the series and store it ; Function to produce fibonomial coefficient ; Function to print Fibonomial Triangle . ; Finding the fibonacci series . ; to store triangle value . ; initialising the 0 th element of each row and diagonal element equal to 0. ; for each row . ; for each column . ; finding each element using recurrence relation . ; printing the Fibonomial Triangle . ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define N  6 NEW_LINE using namespace std ; void fib ( int f [ ] , int n ) { int i ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; for ( i = 2 ; i <= n ; i ++ ) f [ i ] = f [ i - 1 ] + f [ i - 2 ] ; } void fibcoef ( int fc [ ] [ N + 1 ] , int f [ ] , int n ) { for ( int i = 0 ; i <= n ; i ++ ) fc [ i ] [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { int k = j ; while ( k -- ) fc [ i ] [ j ] *= f [ k ] ; k = 1 ; while ( ( j + 1 ) != k ) fc [ i ] [ j ] /= f [ k ++ ] ; } } } void printFibonomialTriangle ( int n ) { int f [ N + 1 ] = { 0 } ; fib ( f , n ) ; int dp [ N + 1 ] [ N + 1 ] = { 0 } ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = dp [ i ] [ i ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j < i ; j ++ ) dp [ i ] [ j ] = f [ i - j + 1 ] * dp [ i - 1 ] [ j - 1 ] + f [ j - 1 ] * dp [ i - 1 ] [ j ] ; } for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) cout << dp [ i ] [ j ] << " ▁ " ; cout << endl ; } } int main ( ) { int n = 6 ; printFibonomialTriangle ( n ) ; return 0 ; }
Sum of Arithmetic Geometric Sequence | CPP Program to find the sum of first n terms . ; Return the sum of first n term of AGP ; finding the each term of AGP and adding it to sum . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumofNterm ( int a , int d , int b , int r , int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += ( ( a + ( i - 1 ) * d ) * ( b * pow ( r , i - 1 ) ) ) ; return sum ; } int main ( ) { int a = 1 , d = 1 , b = 2 , r = 2 , n = 3 ; cout << sumofNterm ( a , d , b , r , n ) << endl ; return 0 ; }