codeparrot/xlcost-text-to-code huggingface dataset:
How to check if two given line segments intersect ? | A C ++ program to check if two given line segments intersect ; Given three colinear points p , q , r , the function checks if point q lies on line segment ' pr ' ; To find orientation of ordered triplet ( p , q , r ) . The function returns following values 0 -- > p , q and r are colinear 1 -- > Clockwise 2 -- > Counterclockwise ; www . geeksforgeeks . org / orientation - 3 - ordered - points / See https : for details of below formula . ; colinear ; clock or counterclock wise ; The main function that returns true if line segment ' p1q1' and ' p2q2' intersect . ; Find the four orientations needed for general and special cases ; General case ; Special Cases p1 , q1 and p2 are colinear and p2 lies on segment p1q1 ; p1 , q1 and q2 are colinear and q2 lies on segment p1q1 ; p2 , q2 and p1 are colinear and p1 lies on segment p2q2 ; p2 , q2 and q1 are colinear and q1 lies on segment p2q2 ; Doesn 't fall in any of the above cases ; Driver program to test above functions
#include <iostream> NEW_LINE using namespace std ; struct Point { int x ; int y ; } ; bool onSegment ( Point p , Point q , Point r ) { if ( q . x <= max ( p . x , r . x ) && q . x >= min ( p . x , r . x ) && q . y <= max ( p . y , r . y ) && q . y >= min ( p . y , r . y ) ) return true ; return false ; } int orientation ( Point p , Point q , Point r ) { int val = ( q . y - p . y ) * ( r . x - q . x ) - ( q . x - p . x ) * ( r . y - q . y ) ; if ( val == 0 ) return 0 ; return ( val > 0 ) ? 1 : 2 ; } bool doIntersect ( Point p1 , Point q1 , Point p2 , Point q2 ) { int o1 = orientation ( p1 , q1 , p2 ) ; int o2 = orientation ( p1 , q1 , q2 ) ; int o3 = orientation ( p2 , q2 , p1 ) ; int o4 = orientation ( p2 , q2 , q1 ) ; if ( o1 != o2 && o3 != o4 ) return true ; if ( o1 == 0 && onSegment ( p1 , p2 , q1 ) ) return true ; if ( o2 == 0 && onSegment ( p1 , q2 , q1 ) ) return true ; if ( o3 == 0 && onSegment ( p2 , p1 , q2 ) ) return true ; if ( o4 == 0 && onSegment ( p2 , q1 , q2 ) ) return true ; return false ; } int main ( ) { struct Point p1 = { 1 , 1 } , q1 = { 10 , 1 } ; struct Point p2 = { 1 , 2 } , q2 = { 10 , 2 } ; doIntersect ( p1 , q1 , p2 , q2 ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; p1 = { 10 , 0 } , q1 = { 0 , 10 } ; p2 = { 0 , 0 } , q2 = { 10 , 10 } ; doIntersect ( p1 , q1 , p2 , q2 ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; p1 = { -5 , -5 } , q1 = { 0 , 0 } ; p2 = { 1 , 1 } , q2 = { 10 , 10 } ; doIntersect ( p1 , q1 , p2 , q2 ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; return 0 ; }
Check whether a given point lies inside a triangle or not |  ; A utility function to calculate area of triangle formed by ( x1 , y1 ) , ( x2 , y2 ) and ( x3 , y3 ) ; A function to check whether point P ( x , y ) lies inside the triangle formed by A ( x1 , y1 ) , B ( x2 , y2 ) and C ( x3 , y3 ) ; Calculate area of triangle ABC ; Calculate area of triangle PBC ; Calculate area of triangle PAC ; Calculate area of triangle PAB ; Check if sum of A1 , A2 and A3 is same as A ; Driver program to test above function ; Let us check whether the point P ( 10 , 15 ) lies inside the triangle formed by A ( 0 , 0 ) , B ( 20 , 0 ) and C ( 10 , 30 )
#include <bits/stdc++.h> NEW_LINE using namespace std ; float area ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { return abs ( ( x1 * ( y2 - y3 ) + x2 * ( y3 - y1 ) + x3 * ( y1 - y2 ) ) / 2.0 ) ; } bool isInside ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 , int x , int y ) { float A = area ( x1 , y1 , x2 , y2 , x3 , y3 ) ; float A1 = area ( x , y , x2 , y2 , x3 , y3 ) ; float A2 = area ( x1 , y1 , x , y , x3 , y3 ) ; float A3 = area ( x1 , y1 , x2 , y2 , x , y ) ; return ( A == A1 + A2 + A3 ) ; } int main ( ) { if ( isInside ( 0 , 0 , 20 , 0 , 10 , 30 , 10 , 15 ) ) printf ( " Inside " ) ; else printf ( " Not ▁ Inside " ) ; return 0 ; }
Lucky Numbers | C ++ program for Lucky Numbers ; Returns 1 if n is a lucky no . otherwise returns 0 ; variable next_position is just for readability of the program we can remove it and use n only ; calculate next position of input no ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define bool  int NEW_LINE bool isLucky ( int n ) { static int counter = 2 ; int next_position = n ; if ( counter > n ) return 1 ; if ( n % counter == 0 ) return 0 ; next_position -= next_position / counter ; counter ++ ; return isLucky ( next_position ) ; } int main ( ) { int x = 5 ; if ( isLucky ( x ) ) cout << x << " ▁ is ▁ a ▁ lucky ▁ no . " ; else cout << x << " ▁ is ▁ not ▁ a ▁ lucky ▁ no . " ; }
Write you own Power without using multiplication ( * ) and division ( / ) operators | C ++ code for power function ; Works only if a >= 0 and b >= 0 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int pow ( int a , int b ) { if ( b == 0 ) return 1 ; int answer = a ; int increment = a ; int i , j ; for ( i = 1 ; i < b ; i ++ ) { for ( j = 1 ; j < a ; j ++ ) { answer += increment ; } increment = answer ; } return answer ; } int main ( ) { cout << pow ( 5 , 3 ) ; return 0 ; }
Write you own Power without using multiplication ( * ) and division ( / ) operators |  ; A recursive function to get x * y ; A recursive function to get a ^ b Works only if a >= 0 and b >= 0 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int multiply ( int x , int y ) { if ( y ) return ( x + multiply ( x , y - 1 ) ) ; else return 0 ; } int pow ( int a , int b ) { if ( b ) return multiply ( a , pow ( a , b - 1 ) ) ; else return 1 ; } int main ( ) { cout << pow ( 5 , 3 ) ; getchar ( ) ; return 0 ; }
Count numbers that don 't contain 3 |  ; returns count of numbers which are in range from 1 to n and don 't contain 3 as a digit  ; Base cases ( Assuming n is not negative ) ; Calculate 10 ^ ( d - 1 ) ( 10 raise to the power d - 1 ) where d is number of digits in n . po will be 100 for n = 578 ; find the most significant digit ( msd is 5 for 578 ) ; For 578 , total will be 4 * count ( 10 ^ 2 - 1 ) + 4 + count ( 78 ) ; For 35 , total will be equal to count ( 29 ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( int n ) { if ( n < 3 ) return n ; if ( n >= 3 && n < 10 ) return n - 1 ; int po = 1 ; while ( n / po > 9 ) po = po * 10 ; int msd = n / po ; if ( msd != 3 ) return count ( msd ) * count ( po - 1 ) + count ( msd ) + count ( n % po ) ; else return count ( msd * po - 1 ) ; } int main ( ) { cout << count ( 578 ) << " ▁ " ; return 0 ; }
Number which has the maximum number of distinct prime factors in the range M to N | C ++ program to print the Number which has the maximum number of distinct prime factors of numbers in range m to n ; Function to return the maximum number ; array to store the number of distinct primes ; true if index ' i ' is a prime ; initializing the number of factors to 0 and ; Used in Sieve ; condition works only when ' i ' is prime , hence for factors of all prime number , the prime status is changed to false ; Number is prime ; number of factor of a prime number is 1 ; incrementing factorCount all the factors of i ; and changing prime status to false ; Initialize the max and num ; Gets the maximum number ; Gets the maximum number ; Driver code ; Calling function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumNumberDistinctPrimeRange ( int m , int n ) { long long factorCount [ n + 1 ] ; bool prime [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { factorCount [ i ] = 0 ; prime [ i ] = true ; } for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] == true ) { factorCount [ i ] = 1 ; for ( int j = i * 2 ; j <= n ; j += i ) { factorCount [ j ] ++ ; prime [ j ] = false ; } } } int max = factorCount [ m ] ; int num = m ; for ( int i = m ; i <= n ; i ++ ) { if ( factorCount [ i ] > max ) { max = factorCount [ i ] ; num = i ; } } return num ; } int main ( ) { int m = 4 , n = 6 ; cout << maximumNumberDistinctPrimeRange ( m , n ) ; return 0 ; }
Lexicographic rank of a string | C ++ program to find lexicographic rank of a string ; A utility function to find factorial of n ; A utility function to count smaller characters on right of arr [ low ] ; A function to find rank of a string in all permutations of characters ; count number of chars smaller than str [ i ] from str [ i + 1 ] to str [ len - 1 ] ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE #include <string.h> NEW_LINE using namespace std ; int fact ( int n ) { return ( n <= 1 ) ? 1 : n * fact ( n - 1 ) ; } int findSmallerInRight ( char * str , int low , int high ) { int countRight = 0 , i ; for ( i = low + 1 ; i <= high ; ++ i ) if ( str [ i ] < str [ low ] ) ++ countRight ; return countRight ; } int findRank ( char * str ) { int len = strlen ( str ) ; int mul = fact ( len ) ; int rank = 1 ; int countRight ; int i ; for ( i = 0 ; i < len ; ++ i ) { mul /= len - i ; countRight = findSmallerInRight ( str , i , len - 1 ) ; rank += countRight * mul ; } return rank ; } int main ( ) { char str [ ] = " string " ; cout << findRank ( str ) ; return 0 ; }
Lexicographic rank of a string | A O ( n ) solution for finding rank of string ; all elements of count [ ] are initialized with 0 ; A utility function to find factorial of n ; Construct a count array where value at every index contains count of smaller characters in whole string ; Removes a character ch from count [ ] array constructed by populateAndIncreaseCount ( ) ; A function to find rank of a string in all permutations of characters ; Populate the count array such that count [ i ] contains count of characters which are present in str and are smaller than i ; count number of chars smaller than str [ i ] fron str [ i + 1 ] to str [ len - 1 ] ; Reduce count of characters greater than str [ i ] ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX_CHAR  256 NEW_LINE int count [ MAX_CHAR ] = { 0 } ; int fact ( int n ) { return ( n <= 1 ) ? 1 : n * fact ( n - 1 ) ; } void populateAndIncreaseCount ( int * count , char * str ) { int i ; for ( i = 0 ; str [ i ] ; ++ i ) ++ count [ str [ i ] ] ; for ( i = 1 ; i < MAX_CHAR ; ++ i ) count [ i ] += count [ i - 1 ] ; } void updatecount ( int * count , char ch ) { int i ; for ( i = ch ; i < MAX_CHAR ; ++ i ) -- count [ i ] ; } int findRank ( char * str ) { int len = strlen ( str ) ; int mul = fact ( len ) ; int rank = 1 , i ; populateAndIncreaseCount ( count , str ) ; for ( i = 0 ; i < len ; ++ i ) { mul /= len - i ; rank += count [ str [ i ] - 1 ] * mul ; updatecount ( count , str [ i ] ) ; } return rank ; } int main ( ) { char str [ ] = " string " ; cout << findRank ( str ) ; return 0 ; }
Print all permutations in sorted ( lexicographic ) order | C ++ Program to print all permutations of a string in sorted order . ; Following function is needed for library function qsort ( ) . Refer http : www . cplusplus . com / reference / clibrary / cstdlib / qsort / ; A utility function two swap two characters a and b ; This function finds the index of the smallest character which is greater than ' first ' and is present in str [ l . . h ] ; initialize index of ceiling element ; Now iterate through rest of the elements and find the smallest character greater than ' first ' ; Print all permutations of str in sorted order ; Get size of string ; Sort the string in increasing order ; Print permutations one by one ; print this permutation ; Find the rightmost character which is smaller than its next character . Let us call it ' first ▁ char ' ; If there is no such character , all are sorted in decreasing order , means we just printed the last permutation and we are done . ; Find the ceil of ' first ▁ char ' in right of first character . Ceil of a character is the smallest character greater than it ; Swap first and second characters ; Sort the string on right of ' first ▁ char ' ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int compare ( const void * a , const void * b ) { return ( * ( char * ) a - * ( char * ) b ) ; } void swap ( char * a , char * b ) { char t = * a ; * a = * b ; * b = t ; } int findCeil ( char str [ ] , char first , int l , int h ) { int ceilIndex = l ; for ( int i = l + 1 ; i <= h ; i ++ ) if ( str [ i ] > first && str [ i ] < str [ ceilIndex ] ) ceilIndex = i ; return ceilIndex ; } void sortedPermutations ( char str [ ] ) { int size = strlen ( str ) ; qsort ( str , size , sizeof ( str [ 0 ] ) , compare ) ; bool isFinished = false ; while ( ! isFinished ) { cout << str << endl ; int i ; for ( i = size - 2 ; i >= 0 ; -- i ) if ( str [ i ] < str [ i + 1 ] ) break ; if ( i == -1 ) isFinished = true ; else { int ceilIndex = findCeil ( str , str [ i ] , i + 1 , size - 1 ) ; swap ( & str [ i ] , & str [ ceilIndex ] ) ; qsort ( str + i + 1 , size - i - 1 , sizeof ( str [ 0 ] ) , compare ) ; } } } int main ( ) { char str [ ] = " ABCD " ; sortedPermutations ( str ) ; return 0 ; }
Print all permutations in sorted ( lexicographic ) order |  ; An optimized version that uses reverse instead of sort for finding the next permutation A utility function to reverse a string str [ l . . h ] ;  ;  ; Print all permutations of str in sorted order ; Get size of string ; Sort the string in increasing order ; Print permutations one by one ; print this permutation ; Find the rightmost character which is smaller than its next character . Let us call it ' first ▁ char ' ; If there is no such character , all are sorted in decreasing order , means we just printed the last permutation and we are done . ; Find the ceil of ' first ▁ char ' in right of first character . Ceil of a character is the smallest character greater than it ; Swap first and second characters ; reverse the string on right of ' first ▁ char '
#include <bits/stdc++.h> NEW_LINE using namespace std ; void reverse ( char str [ ] , int l , int h ) { while ( l < h ) { swap ( & str [ l ] , & str [ h ] ) ; l ++ ; h -- ; } } void swap ( char * a , char * b ) { char t = * a ; * a = * b ; * b = t ; } int compare ( const void * a , const void * b ) { return ( * ( char * ) a - * ( char * ) b ) ; } int findCeil ( char str [ ] , char first , int l , int h ) { int ceilIndex = l ; for ( int i = l + 1 ; i <= h ; i ++ ) if ( str [ i ] > first && str [ i ] < str [ ceilIndex ] ) ceilIndex = i ; return ceilIndex ; } void sortedPermutations ( char str [ ] ) { int size = strlen ( str ) ; qsort ( str , size , sizeof ( str [ 0 ] ) , compare ) ; bool isFinished = false ; while ( ! isFinished ) { cout << str << endl ; int i ; for ( i = size - 2 ; i >= 0 ; -- i ) if ( str [ i ] < str [ i + 1 ] ) break ; if ( i == -1 ) isFinished = true ; else { int ceilIndex = findCeil ( str , str [ i ] , i + 1 , size - 1 ) ; swap ( & str [ i ] , & str [ ceilIndex ] ) ; reverse ( str , i + 1 , size - 1 ) ; } } }
Efficient program to calculate e ^ x | C ++ Efficient program to calculate e raise to the power x ; Returns approximate value of e ^ x using sum of first n terms of Taylor Series ; initialize sum of series ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float exponential ( int n , float x ) { float sum = 1.0f ; for ( int i = n - 1 ; i > 0 ; -- i ) sum = 1 + x * sum / i ; return sum ; } int main ( ) { int n = 10 ; float x = 1.0f ; cout << " e ^ x ▁ = ▁ " << fixed << setprecision ( 5 ) << exponential ( n , x ) ; return 0 ; }
Measure one litre using two vessels and infinite water supply | Sample run of the Algo for V1 with capacity 3 and V2 with capacity 7 1. Fill V1 : V1 = 3 , V2 = 0 2. Transfer from V1 to V2 , and fill V1 : V1 = 3 , V2 = 3 2. Transfer from V1 to V2 , and fill V1 : V1 = 3 , V2 = 6 3. Transfer from V1 to V2 , and empty V2 : V1 = 2 , V2 = 0 4. Transfer from V1 to V2 , and fill V1 : V1 = 3 , V2 = 2 5. Transfer from V1 to V2 , and fill V1 : V1 = 3 , V2 = 5 6. Transfer from V1 to V2 , and empty V2 : V1 = 1 , V2 = 0 7. Stop as V1 now contains 1 litre . Note that V2 was made empty in steps 3 and 6 because it became full ; A utility function to get GCD of two numbers ; Class to represent a Vessel ; A vessel has capacity , and current amount of water in it ; Constructor : initializes capacity as given , and current as 0 ; The main function to fill one litre in this vessel . Capacity of V2 must be greater than this vessel and two capacities must be co - prime ; Fills vessel with given amount and returns the amount of water transferred to it . If the vessel becomes full , then the vessel is made empty . ; The main function to fill one litre in this vessel . Capacity of V2 must be greater than this vessel and two capacities must be coprime ; solution exists iff a and b are co - prime ; fill A ( smaller vessel ) ; Transfer water from V1 to V2 and reduce current of V1 by the amount equal to transferred water ; Finally , there will be 1 litre in vessel 1 ; Fills vessel with given amount and returns the amount of water transferred to it . If the vessel becomes full , then the vessel is made empty ; If the vessel can accommodate the given amount ; If the vessel cannot accommodate the given amount , then store the amount of water transferred ; Since the vessel becomes full , make the vessel empty so that it can be filled again ; Driver program to test above function ; a must be smaller than b ; Create two vessels of capacities a and b ; Get 1 litre in first vessel
#include <iostream> NEW_LINE using namespace std ; int gcd ( int a , int b ) { return b ? gcd ( b , a % b ) : a ; } class Vessel { int capacity , current ; public : Vessel ( int capacity ) { this -> capacity = capacity ; current = 0 ; } void makeOneLitre ( Vessel & V2 ) ; int transfer ( int amount ) ; } ; void Vessel :: makeOneLitre ( Vessel & V2 ) { if ( gcd ( capacity , V2 . capacity ) != 1 ) return ; while ( current != 1 ) { if ( current == 0 ) current = capacity ; cout << " Vessel ▁ 1 : ▁ " << current << " ▁ Vessel ▁ 2 : ▁ " << V2 . current << endl ; current = current - V2 . transfer ( current ) ; } cout << " Vessel ▁ 1 : ▁ " << current << " ▁ Vessel ▁ 2 : ▁ " << V2 . current << endl ; } int Vessel :: transfer ( int amount ) { if ( current + amount < capacity ) { current += amount ; return amount ; } int transferred = capacity - current ; current = 0 ; return transferred ; } int main ( ) { int a = 3 , b = 7 ; Vessel V1 ( a ) , V2 ( b ) ; V1 . makeOneLitre ( V2 ) ; return 0 ; }
Random number generator in arbitrary probability distribution fashion | C ++ program to generate random numbers according to given frequency distribution ; Utility function to find ceiling of r in arr [ l . . h ] ; Same as mid = ( l + h ) / 2 ; The main function that returns a random number from arr [ ] according to distribution array defined by freq [ ] . n is size of arrays . ; Create and fill prefix array ; prefix [ n - 1 ] is sum of all frequencies . Generate a random number with value from 1 to this sum ; Find index of ceiling of r in prefix arrat ; Driver code ; Use a different seed value for every run . ; Let us generate 10 random numbers accroding to given distribution
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCeil ( int arr [ ] , int r , int l , int h ) { int mid ; while ( l < h ) { mid = l + ( ( h - l ) >> 1 ) ; ( r > arr [ mid ] ) ? ( l = mid + 1 ) : ( h = mid ) ; } return ( arr [ l ] >= r ) ? l : -1 ; } int myRand ( int arr [ ] , int freq [ ] , int n ) { int prefix [ n ] , i ; prefix [ 0 ] = freq [ 0 ] ; for ( i = 1 ; i < n ; ++ i ) prefix [ i ] = prefix [ i - 1 ] + freq [ i ] ; int r = ( rand ( ) % prefix [ n - 1 ] ) + 1 ; int indexc = findCeil ( prefix , r , 0 , n - 1 ) ; return arr [ indexc ] ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int freq [ ] = { 10 , 5 , 20 , 100 } ; int i , n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; srand ( time ( NULL ) ) ; for ( i = 0 ; i < 5 ; i ++ ) cout << myRand ( arr , freq , n ) << endl ; return 0 ; }
How to check if a given number is Fibonacci number ? | C ++ program to check if x is a perfect square ; A utility function that returns true if x is perfect square ; Returns true if n is a Fibinacci Number , else false ; n is Fibinacci if one of 5 * n * n + 4 or 5 * n * n - 4 or both is a perferct square ; A utility function to test above functions
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; bool isPerfectSquare ( int x ) { int s = sqrt ( x ) ; return ( s * s == x ) ; } bool isFibonacci ( int n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } int main ( ) { for ( int i = 1 ; i <= 10 ; i ++ ) isFibonacci ( i ) ? cout << i << " ▁ is ▁ a ▁ Fibonacci ▁ Number ▁ STRNEWLINE " : cout << i << " ▁ is ▁ a ▁ not ▁ Fibonacci ▁ Number ▁ STRNEWLINE " ; return 0 ; }
Count trailing zeroes in factorial of a number | C ++ program to count trailing 0 s in n ! ; Function to return trailing 0 s in factorial of n ; Initialize result ; Keep dividing n by powers of 5 and update count ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int findTrailingZeros ( int n ) { int count = 0 ; for ( int i = 5 ; n / i >= 1 ; i *= 5 ) count += n / i ; return count ; } int main ( ) { int n = 100 ; cout << " Count ▁ of ▁ trailing ▁ 0s ▁ in ▁ " << 100 << " ! ▁ is ▁ " << findTrailingZeros ( n ) ; return 0 ; }
Program for nth Catalan Number |  ; A recursive function to find nth catalan number ; Base case ; catalan ( n ) is sum of catalan ( i ) * catalan ( n - i - 1 ) ; Driver code
#include <iostream> NEW_LINE using namespace std ; unsigned long int catalan ( unsigned int n ) { if ( n <= 1 ) return 1 ; unsigned long int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res += catalan ( i ) * catalan ( n - i - 1 ) ; return res ; } int main ( ) { for ( int i = 0 ; i < 10 ; i ++ ) cout << catalan ( i ) << " ▁ " ; return 0 ; }
Program for nth Catalan Number | A dynamic programming based function to find nth Catalan number ; Table to store results of subproblems ; Initialize first two values in table ; Fill entries in catalan [ ] using recursive formula ; Return last entry ; Driver code
#include <iostream> NEW_LINE using namespace std ; unsigned long int catalanDP ( unsigned int n ) { unsigned long int catalan [ n + 1 ] ; catalan [ 0 ] = catalan [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { catalan [ i ] = 0 ; for ( int j = 0 ; j < i ; j ++ ) catalan [ i ] += catalan [ j ] * catalan [ i - j - 1 ] ; } return catalan [ n ] ; } int main ( ) { for ( int i = 0 ; i < 10 ; i ++ ) cout << catalanDP ( i ) << " ▁ " ; return 0 ; }
Program for nth Catalan Number | C ++ program for nth Catalan Number ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Driver code
#include <iostream> NEW_LINE using namespace std ; unsigned long int binomialCoeff ( unsigned int n , unsigned int k ) { unsigned long int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } unsigned long int catalan ( unsigned int n ) { unsigned long int c = binomialCoeff ( 2 * n , n ) ; return c / ( n + 1 ) ; } int main ( ) { for ( int i = 0 ; i < 10 ; i ++ ) cout << catalan ( i ) << " ▁ " ; return 0 ; }
Program for nth Catalan Number |  ; Function to print the number ; For the first number C ( 0 ) ; Iterate till N ; Calculate the number and print it ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE #include <boost/multiprecision/cpp_int.hpp> NEW_LINE using boost :: multiprecision :: cpp_int ; using namespace std ; void catalan ( int n ) { cpp_int cat_ = 1 ; cout << cat_ << " ▁ " ; for ( cpp_int i = 1 ; i < n ; i ++ ) { cat_ *= ( 4 * i - 2 ) ; cat_ /= ( i + 1 ) ; cout << cat_ << " ▁ " ; } } int main ( ) { int n = 5 ; catalan ( n ) ; return 0 ; }