codeparrot/xlcost-text-to-code huggingface dataset:
Check if a number is a power of another number | C # program to check given number y is power of x ; logarithm function to calculate value ; Note : this is double ; compare to the result1 or result2 both are equal ; Driver Code
using System ; class GFG { static bool isPower ( int x , int y ) { int res1 = ( int ) Math . Log ( y ) / ( int ) Math . Log ( x ) ; double res2 = Math . Log ( y ) / Math . Log ( x ) ; return ( res1 == res2 ) ; } static void Main ( ) { if ( isPower ( 27 , 729 ) ) Console . WriteLine ( "1" ) ; else Console . WriteLine ( "0" ) ; } }
Program to find the Roots of Quadratic equation | C # program to find roots of a quadratic equation ; Prints roots of quadratic equation ax * 2 + bx + x ; If a is 0 , then equation is not quadratic , but linear ; d < 0 ; Driver code ; Function call
using System ; class Quadratic { void findRoots ( int a , int b , int c ) { if ( a == 0 ) { Console . Write ( " Invalid " ) ; return ; } int d = b * b - 4 * a * c ; double sqrt_val = Math . Abs ( d ) ; if ( d > 0 ) { Console . Write ( " Roots ▁ are ▁ real ▁ and ▁ different ▁ STRNEWLINE " ) ; Console . Write ( ( double ) ( - b + sqrt_val ) / ( 2 * a ) + " STRNEWLINE " + ( double ) ( - b - sqrt_val ) / ( 2 * a ) ) ; } else { Console . Write ( " Roots ▁ are ▁ complex ▁ STRNEWLINE " ) ; Console . Write ( - ( double ) b / ( 2 * a ) + " ▁ + ▁ i " + sqrt_val + " STRNEWLINE " + - ( double ) b / ( 2 * a ) + " ▁ - ▁ i " + sqrt_val ) ; } } public static void Main ( ) { Quadratic obj = new Quadratic ( ) ; int a = 1 , b = - 7 , c = 12 ; obj . findRoots ( a , b , c ) ; } }
Check perfect square using addition / subtraction | C # program to check if n is perfect square or not ; This function returns true if n is perfect square , else false ; sum is sum of all odd numbers . i is used one by one hold odd numbers ; Driver Code
using System ; public class GFG { static bool isPerfectSquare ( int n ) { for ( int sum = 0 , i = 1 ; sum < n ; i += 2 ) { sum += i ; if ( sum == n ) return true ; } return false ; } public static void Main ( String [ ] args ) { if ( isPerfectSquare ( 35 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; if ( isPerfectSquare ( 49 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Count ' d ' digit positive integers with 0 as a digit | C # program to find the count of positive integer of a given number of digits that contain atleast one zero . ; Returns count of ' d ' digit integers have 0 as a digit ; Driver Code
using System ; class GFG { static int findCount ( int d ) { return 9 * ( ( int ) ( Math . Pow ( 10 , d - 1 ) ) - ( int ) ( Math . Pow ( 9 , d - 1 ) ) ) ; } public static void Main ( ) { int d = 1 ; Console . WriteLine ( findCount ( d ) ) ; d = 2 ; Console . WriteLine ( findCount ( d ) ) ; d = 4 ; Console . WriteLine ( findCount ( d ) ) ; } }
Dyck path | C # program to count number of Dyck Paths ; Returns count Dyck paths in n x n grid ; Compute value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Driver code
using System ; class GFG { static int countDyckPaths ( int n ) { int res = 1 ; for ( int i = 0 ; i < n ; ++ i ) { res *= ( 2 * n - i ) ; res /= ( i + 1 ) ; } return res / ( n + 1 ) ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( " Number ▁ of ▁ " + " Dyck ▁ Paths ▁ is ▁ " + countDyckPaths ( n ) ) ; } }
Triangular Numbers | C # program to check if a number is a triangular number using simple approach ; Returns true if ' num ' is triangular , else false ; Base case ; A Triangular number must be sum of first n natural numbers ; Driver code
using System ; class GFG { static bool isTriangular ( int num ) { if ( num < 0 ) return false ; int sum = 0 ; for ( int n = 1 ; sum <= num ; n ++ ) { sum = sum + n ; if ( sum == num ) return true ; } return false ; } public static void Main ( ) { int n = 55 ; if ( isTriangular ( n ) ) Console . WriteLine ( " The ▁ number ▁ " + " is ▁ a ▁ triangular ▁ number " ) ; else Console . WriteLine ( " The ▁ number " + " ▁ is ▁ NOT ▁ a ▁ triangular ▁ number " ) ; } }
Triangular Numbers | C # program to check if a number is a triangular number using quadratic equation . ; Returns true if num is triangular ; Considering the equation n * ( n + 1 ) / 2 = num The equation is : a ( n ^ 2 ) + bn + c = 0 "; ; Find roots of equation ; checking if root1 is natural ; checking if root2 is natural ; Driver code
using System ; class GFG { static bool isTriangular ( int num ) { if ( num < 0 ) return false ; int c = ( - 2 * num ) ; int b = 1 , a = 1 ; int d = ( b * b ) - ( 4 * a * c ) ; if ( d < 0 ) return false ; float root1 = ( - b + ( float ) Math . Sqrt ( d ) ) / ( 2 * a ) ; float root2 = ( - b - ( float ) Math . Sqrt ( d ) ) / ( 2 * a ) ; if ( root1 > 0 && Math . Floor ( root1 ) == root1 ) return true ; if ( root2 > 0 && Math . Floor ( root2 ) == root2 ) return true ; return false ; } public static void Main ( ) { int num = 55 ; if ( isTriangular ( num ) ) Console . WriteLine ( " The ▁ number ▁ is ▁ a ▁ " + " triangular ▁ number " ) ; else Console . WriteLine ( " The ▁ number ▁ is ▁ NOT ▁ " + " a ▁ triangular ▁ number " ) ; } }
Convert from any base to decimal and vice versa | C # program to convert a number from any base to decimal ; To return value of a char . For example , 2 is returned for '2' . 10 is returned for ' A ' , 11 for ' B ' ; Function to convert a number from given base ' b ' to decimal ; Initialize ; power of base Initialize result ; Decimal equivalent is str [ len - 1 ] * 1 + str [ len - 2 ] * base + str [ len - 3 ] * ( base ^ 2 ) + ... ; A digit in input number must be less than number 's base ; Driver code
using System ; class GFG { static int val ( char c ) { if ( c >= '0' && c <= '9' ) return ( int ) c - '0' ; else return ( int ) c - ' A ' + 10 ; } static int toDeci ( string str , int b_ase ) { int len = str . Length ; int power = 1 ; int num = 0 ; int i ; for ( i = len - 1 ; i >= 0 ; i -- ) { if ( val ( str [ i ] ) >= b_ase ) { Console . WriteLine ( " Invalid ▁ Number " ) ; return - 1 ; } num += val ( str [ i ] ) * power ; power = power * b_ase ; } return num ; } public static void Main ( ) { string str = "11A " ; int b_ase = 16 ; Console . WriteLine ( " Decimal ▁ equivalent ▁ of ▁ " + str + " ▁ in ▁ base ▁ " + b_ase + " ▁ is ▁ " + toDeci ( str , b_ase ) ) ; } }
Frobenius coin problem | C # program to find the largest number that cannot be formed from given two coins ; Utility function to find gcd ; Function to print the desired output ; Solution doesn 't exist   if GCD is not 1 ; Else apply the formula ; Driver Code
using System ; class GFG { static int gcd ( int a , int b ) { int c ; while ( a != 0 ) { c = a ; a = b % a ; b = c ; } return b ; } static void forbenius ( int X , int Y ) { if ( gcd ( X , Y ) != 1 ) { Console . WriteLine ( " NA " ) ; return ; } int A = ( X * Y ) - ( X + Y ) ; int N = ( X - 1 ) * ( Y - 1 ) / 2 ; Console . WriteLine ( " Largest ▁ Amount ▁ = ▁ " + A ) ; Console . WriteLine ( " Total ▁ Count ▁ = ▁ " + N ) ; } public static void Main ( ) { int X = 2 , Y = 5 ; forbenius ( X , Y ) ; X = 5 ; Y = 10 ; Console . WriteLine ( ) ; forbenius ( X , Y ) ; } }
Gray to Binary and Binary to Gray conversion | C # program for Binary To Gray and Gray to Binary conversion . ; Helper function to xor two characters ; Helper function to flip the bit ; function to convert binary string to gray string ; MSB of gray code is same as binary code ; Compute remaining bits , next bit is computed by doing XOR of previous and current in Binary ; Concatenate XOR of previous bit with current bit ; function to convert gray code string to binary string ; MSB of binary code is same as gray code ; Compute remaining bits ; If current bit is 0 , concatenate previous bit ; Else , concatenate invert of previous bit ; Driver program to test above functions
using System ; class GFG { static char xor_c ( char a , char b ) { return ( a == b ) ? '0' : '1' ; } static char flip ( char c ) { return ( c == '0' ) ? '1' : '0' ; } static String binarytoGray ( String binary ) { String gray = " " ; gray += binary [ 0 ] ; for ( int i = 1 ; i < binary . Length ; i ++ ) { gray += xor_c ( binary [ i - 1 ] , binary [ i ] ) ; } return gray ; } static String graytoBinary ( String gray ) { String binary = " " ; binary += gray [ 0 ] ; for ( int i = 1 ; i < gray . Length ; i ++ ) { if ( gray [ i ] == '0' ) binary += binary [ i - 1 ] ; else binary += flip ( binary [ i - 1 ] ) ; } return binary ; } public static void Main ( ) { String binary = "01001" ; Console . WriteLine ( " Gray ▁ code ▁ of ▁ " + binary + " ▁ is ▁ " + binarytoGray ( binary ) ) ; String gray = "01101" ; Console . Write ( " Binary ▁ code ▁ of ▁ " + gray + " ▁ is ▁ " + graytoBinary ( gray ) ) ; } }
Solving f ( n ) = ( 1 ) + ( 2 * 3 ) + ( 4 * 5 * 6 ) . . . n using Recursion | C # Program to print the solution of the series f ( n ) = ( 1 ) + ( 2 * 3 ) + ( 4 * 5 * 6 ) . . . n using recursion ; Recursive function for finding sum of series calculated - number of terms till which sum of terms has been calculated current - number of terms for which sum has to be calculated N - Number of terms in the function to be calculated ; checking termination condition ; product of terms till current ; recursive call for adding terms next in the series ; Driver Code ; input number of terms in the series ; invoking the method to calculate the sum
using System ; class GFG { static int seriesSum ( int calculated , int current , int N ) { int i , cur = 1 ; if ( current == N + 1 ) return 0 ; for ( i = calculated ; i < calculated + current ; i ++ ) cur *= i ; return cur + seriesSum ( i , current + 1 , N ) ; } public static void Main ( ) { int N = 5 ; Console . WriteLine ( seriesSum ( 1 , 1 , N ) ) ; } }
How to avoid overflow in modular multiplication ? | C # program for modular multiplication without any overflow ; To compute ( a * b ) % mod ; long res = 0 ; Initialize result ; If b is odd , add ' a ' to result ; Multiply ' a ' with 2 ; Divide b by 2 ; Return result ; Driver code
using System ; class GFG { static long mulmod ( long a , long b , long mod ) { a = a % mod ; while ( b > 0 ) { if ( b % 2 == 1 ) { res = ( res + a ) % mod ; } a = ( a * 2 ) % mod ; b /= 2 ; } return res % mod ; } public static void Main ( String [ ] args ) { long a = 9223372036854775807L , b = 9223372036854775807L ; Console . WriteLine ( mulmod ( a , b , 100000000000L ) ) ; } }
Count inversions in an array | Set 3 ( Using BIT ) | C # program to count inversions using Binary Indexed Tree ; Returns sum of arr [ 0. . index ] . This function assumes that the array is preprocessed and partial sums of array elements are stored in BITree [ ] . ; Initialize result ; Traverse ancestors of BITree [ index ] ; Add current element of BITree to sum ; Move index to parent node in getSum View ; Updates a node in Binary Index Tree ( BITree ) at given index in BITree . The given value ' val ' is added to BITree [ i ] and all of its ancestors in tree . ; Traverse all ancestors and add ' val ' ; Add ' val ' to current node of BI Tree ; Update index to that of parent in update View ; Converts an array to an array with values from 1 to n and relative order of smaller and greater elements remains same . For example , { 7 , - 90 , 100 , 1 } is converted to { 3 , 1 , 4 , 2 } ; Create a copy of arrp [ ] in temp and sort the temp array in increasing order ; Traverse all array elements ; lower_bound ( ) Returns pointer to the first element greater than or equal to arr [ i ] ; Returns inversion count arr [ 0. . n - 1 ] ; Initialize result ; Convert [ ] arr to an array with values from 1 to n and relative order of smaller and greater elements remains same . For example , { 7 , - 90 , 100 , 1 } is converted to { 3 , 1 , 4 , 2 } ; Create a BIT with size equal to maxElement + 1 ( Extra one is used so that elements can be directly be used as index ) ; Traverse all elements from right . ; Get count of elements smaller than arr [ i ] ; Add current element to BIT ; Driver code
using System ; class GFG { static int getSum ( int [ ] BITree , int index ) { int sum = 0 ; while ( index > 0 ) { sum += BITree [ index ] ; index -= index & ( - index ) ; } return sum ; } static void updateBIT ( int [ ] BITree , int n , int index , int val ) { while ( index <= n ) { BITree [ index ] += val ; index += index & ( - index ) ; } } static void convert ( int [ ] arr , int n ) { int [ ] temp = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) temp [ i ] = arr [ i ] ; Array . Sort ( temp ) ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = lower_bound ( temp , 0 , n , arr [ i ] ) + 1 ; } } static int lower_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } static int getInvCount ( int [ ] arr , int n ) { int invcount = 0 ; convert ( arr , n ) ; int [ ] BIT = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) BIT [ i ] = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { invcount += getSum ( BIT , arr [ i ] - 1 ) ; updateBIT ( BIT , n , arr [ i ] , 1 ) ; } return invcount ; } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 4 , 2 , 1 } ; int n = arr . Length ; Console . Write ( " Number ▁ of ▁ inversions ▁ are ▁ : ▁ " + getInvCount ( arr , n ) ) ; } }
Compute n ! under modulo p | Simple method to compute n ! % p ; Returns value of n ! % p ; Driver program
using System ; class GFG { static int modFact ( int n , int p ) { if ( n >= p ) return 0 ; int result = 1 ; for ( int i = 1 ; i <= n ; i ++ ) result = ( result * i ) % p ; return result ; } static void Main ( ) { int n = 25 , p = 29 ; Console . Write ( modFact ( n , p ) ) ; } }
Chinese Remainder Theorem | Set 2 ( Inverse Modulo based Implementation ) | A C # program to demonstrate working of Chinese remainder Theorem ; Returns modulo inverse of ' a ' with respect to ' m ' using extended Euclid Algorithm . Refer below post for details : https : www . geeksforgeeks . org / multiplicative - inverse - under - modulo - m / ; Apply extended Euclid Algorithm ; q is quotient ; m is remainder now , process same as euclid 's algo ; Make x1 positive ; k is size of num [ ] and rem [ ] . Returns the smallest number x such that : x % num [ 0 ] = rem [ 0 ] , x % num [ 1 ] = rem [ 1 ] , ... ... ... ... ... ... x % num [ k - 2 ] = rem [ k - 1 ] Assumption : Numbers in num [ ] are pairwise coprime ( gcd for every pair is 1 ) ; Compute product of all numbers ; Initialize result ; Apply above formula ; Driver Code
using System ; class GFG { static int inv ( int a , int m ) { int m0 = m , t , q ; int x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = a / m ; t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } static int findMinX ( int [ ] num , int [ ] rem , int k ) { int prod = 1 ; for ( int i = 0 ; i < k ; i ++ ) prod *= num [ i ] ; int result = 0 ; for ( int i = 0 ; i < k ; i ++ ) { int pp = prod / num [ i ] ; result += rem [ i ] * inv ( pp , num [ i ] ) * pp ; } return result % prod ; } static public void Main ( ) { int [ ] num = { 3 , 4 , 5 } ; int [ ] rem = { 2 , 3 , 1 } ; int k = num . Length ; Console . WriteLine ( " x ▁ is ▁ " + findMinX ( num , rem , k ) ) ; } }
Chinese Remainder Theorem | Set 1 ( Introduction ) | C # program to demonstrate working of Chinise remainder Theorem ; k is size of num [ ] and rem [ ] . Returns the smallest number x such that : x % num [ 0 ] = rem [ 0 ] , x % num [ 1 ] = rem [ 1 ] , ... ... ... ... ... ... x % num [ k - 2 ] = rem [ k - 1 ] Assumption : Numbers in num [ ] are pairwise coprime ( gcd for every pair is 1 ) ; Initialize result ; As per the Chinese remainder theorem , this loop will always break . ; Check if remainder of x % num [ j ] is rem [ j ] or not ( for all j from 0 to k - 1 ) ; If all remainders matched , we found x ; Else try next number ; Driver code
using System ; class GFG { static int findMinX ( int [ ] num , int [ ] rem , int k ) { int x = 1 ; while ( true ) { int j ; for ( j = 0 ; j < k ; j ++ ) if ( x % num [ j ] != rem [ j ] ) break ; if ( j == k ) return x ; x ++ ; } } public static void Main ( ) { int [ ] num = { 3 , 4 , 5 } ; int [ ] rem = { 2 , 3 , 1 } ; int k = num . Length ; Console . WriteLine ( " x ▁ is ▁ " + findMinX ( num , rem , k ) ) ; } }
Compute nCr % p | Set 2 ( Lucas Theorem ) | A Lucas Theorem based solution to compute nCr % p ; Returns nCr % p . In this Lucas Theorem based program , this function is only called for n < p and r < p . ; The array C is going to store last row of pascal triangle at the end . And last entry of last row is nCr ; One by constructs remaining rows of Pascal Triangle from top to bottom ; Fill entries of current row using previous row values ; nCj = ( n - 1 ) Cj + ( n - 1 ) C ( j - 1 ) ; ; Lucas Theorem based function that returns nCr % p . This function works like decimal to binary conversion recursive function . First we compute last digits of n and r in base p , then recur for remaining digits ; Base case ; Compute last digits of n and r in base p ; Compute result for last digits computed above , and for remaining digits . Multiply the two results and compute the result of multiplication in modulo p . return ( nCrModpLucas ( n / p , r / p , p ) * Last digits of n and r nCrModpDP ( ni , ri , p ) ) % p ; Remaining digits ; Driver Code
using System ; class GFG { static int nCrModpDP ( int n , int r , int p ) { int [ ] C = new int [ r + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . Min ( i , r ) ; j > 0 ; j -- ) C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p ; } return C [ r ] ; } static int nCrModpLucas ( int n , int r , int p ) { if ( r == 0 ) return 1 ; int ni = n % p ; int ri = r % p ; } public static void Main ( ) { int n = 1000 , r = 900 , p = 13 ; Console . Write ( " Value ▁ of ▁ nCr ▁ % ▁ p ▁ is ▁ " + nCrModpLucas ( n , r , p ) ) ; } }
Fibonacci Coding | C # program for Fibonacci Encoding of a positive integer n ; To limit on the largest Fibonacci number to be used ; Array to store fibonacci numbers . fib [ i ] is going to store ( i + 2 ) 'th Fibonacci number ; Stores values in fib and returns index of the largest fibonacci number smaller than n . ; Fib [ 0 ] stores 2 nd Fibonacci No . ; Fib [ 1 ] stores 3 rd Fibonacci No . ; Keep Generating remaining numbers while previously generated number is smaller ; Return index of the largest fibonacci number smaller than or equal to n . Note that the above loop stopped when fib [ i - 1 ] became larger . ; Returns pointer to the char string which corresponds to code for n ; Allocate memory for codeword ; Index of the largest Fibonacci f <= n ; Mark usage of Fibonacci f ( 1 bit ) ; Subtract f from n ; Move to Fibonacci just smaller than f ; Mark all Fibonacci > n as not used ( 0 bit ) , progress backwards ; Additional '1' bit ; Return pointer to codeword ; Driver code
using System ; class GFG { public static int N = 30 ; public static int [ ] fib = new int [ N ] ; public static int largestFiboLessOrEqual ( int n ) { fib [ 0 ] = 1 ; fib [ 1 ] = 2 ; int i ; for ( i = 2 ; fib [ i - 1 ] <= n ; i ++ ) { fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } return ( i - 2 ) ; } public static String fibonacciEncoding ( int n ) { int index = largestFiboLessOrEqual ( n ) ; char [ ] codeword = new char [ index + 3 ] ; int i = index ; while ( n > 0 ) { codeword [ i ] = '1' ; n = n - fib [ i ] ; i = i - 1 ; while ( i >= 0 && fib [ i ] > n ) { codeword [ i ] = '0' ; i = i - 1 ; } } codeword [ index + 1 ] = '1' ; codeword [ index + 2 ] = ' \0' ; string str = new string ( codeword ) ; return str ; } static public void Main ( ) { int n = 143 ; Console . WriteLine ( " Fibonacci ▁ code ▁ word ▁ for ▁ " + n + " ▁ is ▁ " + fibonacciEncoding ( n ) ) ; } }
Print all Good numbers in given range | C # program to print good numbers in a given range [ L , R ] ; Function to check whether n is a good number and doesn ' t ▁ contain ▁ digit ▁ ' d ' ; Get last digit and initialize sum from right side ; If last digit is d , return ; Traverse remaining digits ; Current digit ; If digit is d or digit is less than or equal to sum of digits on right side ; Update sum and n ; Print Good numbers in range [ L , R ] ; Traverse all numbers in given range ; If current numbers is good , print it ; Driver program ; Print good numbers in [ L , R ]
using System ; class GFG { static bool isValid ( int n , int d ) { int digit = n % 10 ; int sum = digit ; if ( digit == d ) return false ; n /= 10 ; while ( n > 0 ) { digit = n % 10 ; if ( digit == d digit <= sum ) return false ; else { sum += digit ; n /= 10 ; } } return true ; } static void printGoodNumber ( int L , int R , int d ) { for ( int i = L ; i <= R ; i ++ ) { if ( isValid ( i , d ) ) Console . Write ( i + " ▁ " ) ; } } public static void Main ( ) { int L = 410 , R = 520 , d = 3 ; printGoodNumber ( L , R , d ) ; } }
Count number of squares in a rectangle | C # program to count squares in a rectangle of size m x n ; Returns count of all squares in a rectangle of size m x n ; If n is smaller , swap m and n ; Now n is greater dimension , apply formula ; Driver Code
using System ; class GFG { static int countSquares ( int m , int n ) { if ( n < m ) { int temp = m ; m = n ; n = temp ; } return n * ( n + 1 ) * ( 3 * m - n + 1 ) / 6 ; } public static void Main ( String [ ] args ) { int m = 4 ; int n = 3 ; Console . Write ( " Count ▁ of ▁ squares ▁ is ▁ " + countSquares ( m , n ) ) ; } }
Zeckendorf 's Theorem (Non | C # program for Zeckendorf 's theorem. It finds the representation of n as sum of non-neighbouring Fibonacci Numbers. ; Corner cases ; Find the greatest Fibonacci Number smaller than n . ; Prints Fibonacci Representation of n using greedy algorithm ; Find the greates Fibonacci Number smallerthan or equal to n ; Print the found fibonacci number ; Reduce n ; Driver method
using System ; class GFG { public static int nearestSmallerEqFib ( int n ) { if ( n == 0 n == 1 ) return n ; int f1 = 0 , f2 = 1 , f3 = 1 ; while ( f3 <= n ) { f1 = f2 ; f2 = f3 ; f3 = f1 + f2 ; } return f2 ; } public static void printFibRepresntation ( int n ) { while ( n > 0 ) { int f = nearestSmallerEqFib ( n ) ; Console . Write ( f + " ▁ " ) ; n = n - f ; } } public static void Main ( ) { int n = 40 ; Console . WriteLine ( " Non - neighbouring ▁ Fibonacci ▁ " + " ▁ Representation ▁ of ▁ " + n + " ▁ is " ) ; printFibRepresntation ( n ) ; } }
Count number of ways to divide a number in 4 parts | A Dynamic Programming based solution to count number of ways to represent n as sum of four numbers ; " parts " is number of parts left , n is the value left " nextPart " is starting point from where we start trying for next part . ; Base cases ; If this subproblem is already solved ; Count number of ways for remaining number n - i remaining parts " parts - 1" , and for all part varying from ' nextPart ' to ' n ' ; Store computed answer in table and return result ; This function mainly initializes dp table and calls countWaysUtil ( ) ; Driver code
using System ; class GFG { static int [ , , ] dp = new int [ 5001 , 5001 , 5 ] ; static int countWaysUtil ( int n , int parts , int nextPart ) { if ( parts == 0 && n == 0 ) return 1 ; if ( n <= 0 parts <= 0 ) return 0 ; if ( dp [ n , nextPart , parts ] != - 1 ) return dp [ n , nextPart , parts ] ; for ( int i = nextPart ; i <= n ; i ++ ) ans += countWaysUtil ( n - i , parts - 1 , i ) ; return ( dp [ n , nextPart , parts ] = ans ) ; } static int countWays ( int n ) { for ( int i = 0 ; i < 5001 ; i ++ ) { for ( int j = 0 ; j < 5001 ; j ++ ) { for ( int l = 0 ; l < 5 ; l ++ ) dp [ i , j , l ] = - 1 ; } } return countWaysUtil ( n , 4 , 1 ) ; } public static void Main ( String [ ] args ) { int n = 8 ; Console . WriteLine ( countWays ( n ) ) ; } }