codeparrot/xlcost-text-to-code huggingface dataset:
Number of balanced bracket expressions that can be formed from a string | C # program to find number of balanced bracket expressions possible ; Max string length ; Function to check whether index start and end can form a bracket pair or not ; Check for brackets ( ) ; Check for brackets [ ] ; Check for brackets { } ; Function to find number of proper bracket expressions ; If starting index is greater than ending index ; If dp [ start , end ] has already been computed ; Search for the bracket in from next index ; If bracket pair is formed , add number of combination ; If ? comes then all three bracket expressions are possible ; Return answer ; If n is odd , string cannot be balanced ; Driving function
using System ; class GFG { static int MAX = 300 ; static int checkFunc ( int i , int j , string st ) { if ( st [ i ] == ' ( ' && st [ j ] == ' ) ' ) return 1 ; if ( st [ i ] == ' ( ' && st [ j ] == ' ? ' ) return 1 ; if ( st [ i ] == ' ? ' && st [ j ] == ' ) ' ) return 1 ; if ( st [ i ] == ' [ ' && st [ j ] == ' ] ' ) return 1 ; if ( st [ i ] == ' [ ' && st [ j ] == ' ? ' ) return 1 ; if ( st [ i ] == ' ? ' && st [ j ] == ' ] ' ) return 1 ; if ( st [ i ] == ' { ' && st [ j ] == ' } ' ) return 1 ; if ( st [ i ] == ' { ' && st [ j ] == ' ? ' ) return 1 ; if ( st [ i ] == ' ? ' && st [ j ] == ' } ' ) return 1 ; return 0 ; } static int countRec ( int start , int end , int [ , ] dp , string st ) { int sum = 0 ; if ( start > end ) return 1 ; if ( dp [ start , end ] != - 1 ) return dp [ start , end ] ; int i ; for ( i = start + 1 ; i <= end ; i += 2 ) { if ( checkFunc ( start , i , st ) == 1 ) { sum = sum + countRec ( start + 1 , i - 1 , dp , st ) * countRec ( i + 1 , end , dp , st ) ; } else if ( st [ start ] == ' ? ' && st [ i ] == ' ? ' ) { sum = sum + countRec ( start + 1 , i - 1 , dp , st ) * countRec ( i + 1 , end , dp , st ) * 3 ; } } return dp [ start , end ] = sum ; } static int countWays ( string st ) { int n = st . Length ; if ( n % 2 == 1 ) return 0 ; int [ , ] dp = new int [ MAX , MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) dp [ i , j ] = - 1 ; return countRec ( 0 , n - 1 , dp , st ) ; } public static void Main ( ) { string st = " ( ? ( [ ? ) ] ? } ? " ; Console . WriteLine ( countWays ( st ) ) ; } }
Count pairs ( A , B ) such that A has X and B has Y number of set bits and A + B = C | C # implementation of the above approach ; Initial DP array ; Recursive function to generate all combinations of bits ; if the state has already been visited ; find if C has no more set bits on left ; if no set bits are left for C and there are no set bits for A and B and the carry is 0 , then this combination is possible ; if no set bits are left for C and requirement of set bits for A and B have exceeded ; Find if the bit is 1 or 0 at third index to the left ; carry = 1 and bit set = 1 ; since carry is 1 , and we need 1 at C 's bit position, we can use 0 and 0  or 1 and 1 at A and B bit position ; carry = 0 and bit set = 1 ; since carry is 0 , and we need 1 at C 's bit position, we can use 1 and 0  or 0 and 1 at A and B bit position ; carry = 1 and bit set = 0 ; since carry is 1 , and we need 0 at C 's bit position, we can use 1 and 0  or 0 and 1 at A and B bit position ; carry = 0 and bit set = 0 ; since carry is 0 , and we need 0 at C 's bit position, we can use 0 and 0  or 1 and 1 at A and B bit position ; Function to count ways ; function call that returns the answer ; Driver Code
using System ; class GFG { static int [ , , , ] dp = new int [ 64 , 64 , 64 , 2 ] ; static int func ( int third , int seta , int setb , int carry , int number ) { if ( third > - 1 && seta > - 1 && setb > - 1 && carry > - 1 ) if ( dp [ third , seta , setb , carry ] != - 1 ) return dp [ third , seta , setb , carry ] ; int shift = ( number >> third ) ; if ( shift == 0 && seta == 0 && setb == 0 && carry == 0 ) return 1 ; if ( shift == 0 seta < 0 setb < 0 ) return 0 ; int mask = shift & 1 ; dp [ third , seta , setb , carry ] = 0 ; if ( ( mask == 1 ) && carry == 1 ) { dp [ third , seta , setb , carry ] += func ( third + 1 , seta , setb , 0 , number ) + func ( third + 1 , seta - 1 , setb - 1 , 1 , number ) ; } else if ( mask == 1 && carry == 0 ) { dp [ third , seta , setb , carry ] += func ( third + 1 , seta - 1 , setb , 0 , number ) + func ( third + 1 , seta , setb - 1 , 0 , number ) ; } else if ( mask == 0 && carry == 1 ) { dp [ third , seta , setb , carry ] += func ( third + 1 , seta - 1 , setb , 1 , number ) + func ( third + 1 , seta , setb - 1 , 1 , number ) ; } else if ( mask == 0 && carry == 0 ) { dp [ third , seta , setb , carry ] += func ( third + 1 , seta , setb , 0 , number ) + func ( third + 1 , seta - 1 , setb - 1 , 1 , number ) ; } return dp [ third , seta , setb , carry ] ; } static int possibleSwaps ( int a , int b , int c ) { for ( int q = 0 ; q < 64 ; q ++ ) { for ( int r = 0 ; r < 64 ; r ++ ) { for ( int p = 0 ; p < 64 ; p ++ ) { for ( int d = 0 ; d < 2 ; d ++ ) { dp [ q , r , p , d ] = - 1 ; } } } } int ans = func ( 0 , a , b , 0 , c ) ; return ans ; } public static void Main ( String [ ] args ) { int x = 2 , y = 2 , c = 20 ; Console . WriteLine ( possibleSwaps ( x , y , c ) ) ; } }
Sum of Fibonacci numbers at even indexes upto N terms | C # Program to find sum of even - indiced Fibonacci numbers ; Computes value of first fibonacci numbers and stores the even - indexed sum ; Initialize result ; Add remaining terms ; For even indices ; Return the alternting sum ; Driver Code ; Get n ; Find the even - indiced sum
using System ; class GFG { static int calculateEvenSum ( int n ) { if ( n <= 0 ) return 0 ; int [ ] fibo = new int [ 2 * n + 1 ] ; fibo [ 0 ] = 0 ; fibo [ 1 ] = 1 ; int sum = 0 ; for ( int i = 2 ; i <= 2 * n ; i ++ ) { fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ; if ( i % 2 == 0 ) sum += fibo [ i ] ; } return sum ; } static public void Main ( ) { int n = 8 ; Console . WriteLine ( " Even ▁ indexed ▁ Fibonacci ▁ Sum ▁ upto ▁ " + n + " ▁ terms : ▁ " + calculateEvenSum ( n ) ) ; } }
Sum of Fibonacci numbers at even indexes upto N terms | C # Program to find even indexed Fibonacci Sum in O ( Log n ) time . ; Create an array for memoization ; Returns n 'th Fibonacci number  using table f[] ; Base cases ; If fib ( n ) is already computed ; Applying above formula [ Note value n & 1 is 1 if n is odd , else 0 ] . ; Computes value of even - indexed Fibonacci Sum ; Driver code ; Get n ; Find the alternating sum
using System ; class GFG { static int MAX = 1000 ; static int [ ] f = new int [ MAX ] ; static int fib ( int n ) { if ( n == 0 ) { return 0 ; } if ( n == 1 n == 2 ) { return ( f [ n ] = 1 ) ; } if ( f [ n ] == 1 ) { return f [ n ] ; } int k = ( n % 2 == 1 ) ? ( n + 1 ) / 2 : n / 2 ; f [ n ] = ( n % 2 == 1 ) ? ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) : ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ; return f [ n ] ; } static int calculateEvenSum ( int n ) { return ( fib ( 2 * n + 1 ) - 1 ) ; } public static void Main ( ) { int n = 8 ; Console . WriteLine ( " Even ▁ indexed ▁ Fibonacci ▁ Sum ▁ upto ▁ " + n + " ▁ terms : ▁ " + calculateEvenSum ( n ) ) ; } }
Gould 's Sequence | C # program to generate Gould 's Sequence ; 32768 = 2 ^ 15 ; Array to store Sequence up to 2 ^ 16 = 65536 ; Utility function to pre - compute odd numbers in ith row of Pascals 's triangle ; First term of the Sequence is 1 ; Initialize i to 1 ; Initialize p to 1 ( i . e 2 ^ i ) in each iteration i will be pth power of 2 ; loop to generate gould 's Sequence ; i is pth power of 2 traverse the array from j = 0 to i i . e ( 2 ^ p ) ; double the value of arr [ j ] and store to arr [ i + j ] ; update i to next power of 2 ; increment p ; Function to print gould 's Sequence ; loop to generate gould 's Sequence up to n ; Driver code ; Get n ; Function call
using System ; class GFG { static int MAX = 32768 ; static int [ ] arr = new int [ 2 * MAX ] ; static void gouldSequence ( ) { arr [ 0 ] = 1 ; int i = 1 ; int p = 1 ; while ( i <= MAX ) { int j = 0 ; while ( j < i ) { arr [ i + j ] = 2 * arr [ j ] ; j ++ ; } i = ( 1 << p ) ; p ++ ; } } static void printSequence ( int n ) { for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( ) { gouldSequence ( ) ; int n = 16 ; printSequence ( n ) ; } }
Count the number of ways to traverse a Matrix | C # Program for above approach ; Find factorial ; Find number of ways to reach mat [ m - 1 ] [ n - 1 ] from mat [ 0 ] [ 0 ] in a matrix mat [ ] [ ] ] ; Driver code ; Function Call
using System ; class GFG { static int factorial ( int n ) { int res = 1 , i ; for ( i = 2 ; i <= n ; i ++ ) res *= i ; return res ; } static int countWays ( int m , int n ) { m = m - 1 ; n = n - 1 ; return factorial ( m + n ) / ( factorial ( m ) * factorial ( n ) ) ; } static void Main ( ) { int m = 5 ; int n = 5 ; int result = countWays ( m , n ) ; Console . WriteLine ( result ) ; } }
Matrix Chain Multiplication ( A O ( N ^ 2 ) Solution ) | C # program to implement optimized matrix chain multiplication problem . ; Matrix Mi has dimension p [ i - 1 ] x p [ i ] for i = 1. . n ; For simplicity of the program , one extra row and one extra column are allocated in dp [ ] [ ] . 0 th row and 0 th column of dp [ ] [ ] are not used ; cost is zero when multiplying one matrix . ; Simply following above recursive formula . ; Driver code
using System ; class GFG { static int MatrixChainOrder ( int [ ] p , int n ) { int [ , ] dp = new int [ n , n ] ; for ( int i = 1 ; i < n ; i ++ ) dp [ i , i ] = 0 ; for ( int L = 1 ; L < n - 1 ; L ++ ) for ( int i = 1 ; i < n - L ; i ++ ) dp [ i , i + L ] = Math . Min ( dp [ i + 1 , i + L ] + p [ i - 1 ] * p [ i ] * p [ i + L ] , dp [ i , i + L - 1 ] + p [ i - 1 ] * p [ i + L - 1 ] * p [ i + L ] ) ; return dp [ 1 , n - 1 ] ; } public static void Main ( ) { int [ ] arr = { 10 , 20 , 30 , 40 , 30 } ; int size = arr . Length ; Console . WriteLine ( " Minimum ▁ number ▁ of ▁ multiplications ▁ is ▁ " + MatrixChainOrder ( arr , size ) ) ; } }
Count common subsequence in two strings | C # program to count common subsequence in two strings ; return the number of common subsequence in two strings ; for each character of S ; for each character in T ; if character are same in both the string ; Driver code
using System ; class GFG { static int CommomSubsequencesCount ( string s , string t ) { int n1 = s . Length ; int n2 = t . Length ; int [ , ] dp = new int [ n1 + 1 , n2 + 1 ] ; for ( int i = 0 ; i <= n1 ; i ++ ) { for ( int j = 0 ; j <= n2 ; j ++ ) { dp [ i , j ] = 0 ; } } for ( int i = 1 ; i <= n1 ; i ++ ) { for ( int j = 1 ; j <= n2 ; j ++ ) { if ( s [ i - 1 ] == t [ j - 1 ] ) dp [ i , j ] = 1 + dp [ i , j - 1 ] + dp [ i - 1 , j ] ; else dp [ i , j ] = dp [ i , j - 1 ] + dp [ i - 1 , j ] - dp [ i - 1 , j - 1 ] ; } } return dp [ n1 , n2 ] ; } public static void Main ( ) { string s = " ajblqcpdz " ; string t = " aefcnbtdi " ; Console . Write ( CommomSubsequencesCount ( s , t ) ) ; } }
Minimum number of palindromes required to express N as a sum | Set 2 | C # program to find the minimum number of palindromes required to express N as a sum ; A utility for creating palindrome ; checks if number of digits is odd or even if odd then neglect the last digit of input in finding reverse as in case of odd number of digits middle element occur once ; Creates palindrome by just appending reverse of number to itself ; Function to generate palindromes ; Run two times for odd and even length palindromes ; Creates palindrome numbers with first half as i . Value of j decides whether we need an odd length or even length palindrome . ; Swap values of left and right ; Function to find the minimum number of palindromes required to express N as a sum ; Checking if the number is a palindrome ; Checking if the number is a sum of two palindromes Getting the list of all palindromes upto N ; Sorting the list of palindromes ; The answer is three if the control reaches till this point ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int createPalindrome ( int input , int isOdd ) { int n = input ; int palin = input ; if ( isOdd % 2 == 1 ) { n /= 10 ; } while ( n > 0 ) { palin = palin * 10 + ( n % 10 ) ; n /= 10 ; } return palin ; } static List < int > generatePalindromes ( int N ) { List < int > palindromes = new List < int > ( ) ; int number ; for ( int j = 0 ; j < 2 ; j ++ ) { int i = 1 ; while ( ( number = createPalindrome ( i ++ , j ) ) <= N ) { palindromes . Add ( number ) ; } } return palindromes ; } static String reverse ( String input ) { char [ ] temparray = input . ToCharArray ( ) ; int left , right = 0 ; right = temparray . Length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . Join ( " " , temparray ) ; } static int minimumNoOfPalindromes ( int N ) { String a = String . Join ( " " , N ) ; String b = String . Join ( " " , N ) ; b = reverse ( b ) ; if ( a . Equals ( b ) ) { return 1 ; } List < int > palindromes = generatePalindromes ( N ) ; palindromes . Sort ( ) ; int l = 0 , r = palindromes . Count - 1 ; while ( l < r ) { if ( palindromes [ l ] + palindromes [ r ] == N ) { return 2 ; } else if ( palindromes [ l ] + palindromes [ r ] < N ) { ++ l ; } else { -- r ; } } return 3 ; } public static void Main ( String [ ] args ) { int N = 65 ; Console . WriteLine ( minimumNoOfPalindromes ( N ) ) ; } }
Minimum Cost to make two Numeric Strings Identical | C # program to find minimum cost to make two numeric strings identical ; Function to find weight of LCS ; if this state is already calculated then return ; adding required weight for particular match ; recurse for left and right child and store the max ; Function to calculate cost of string ; Driver code ; Minimum cost needed to make two strings identical
using System ; public class GFG { static int lcs ( int [ , ] dp , String a , String b , int m , int n ) { for ( int i = 0 ; i < 100 ; i ++ ) for ( int j = 0 ; j < 100 ; j ++ ) dp [ i , j ] = - 1 ; if ( m < 0 n < 0 ) { return 0 ; } if ( dp [ m , n ] != - 1 ) return dp [ m , n ] ; int ans = 0 ; if ( a [ m ] == b [ n ] ) { ans = ( a [ m ] - 48 ) + lcs ( dp , a , b , m - 1 , n - 1 ) ; } else ans = Math . Max ( lcs ( dp , a , b , m - 1 , n ) , lcs ( dp , a , b , m , n - 1 ) ) ; dp [ m , n ] = ans ; return ans ; } static int costOfString ( String str ) { int cost = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) cost += ( str [ i ] - 48 ) ; return cost ; } public static void Main ( ) { String a , b ; a = "9142" ; b = "1429" ; int [ , ] dp = new int [ 101 , 101 ] ; Console . Write ( ( costOfString ( a ) + costOfString ( b ) - 2 * lcs ( dp , a , b , a . Length - 1 , b . Length - 1 ) ) ) ; } }
Longest subarray having maximum sum | C # implementation to find the length of the longest subarray having maximum sum ; function to find the maximum sum that exists in a subarray ; function to find the length of longest subarray having sum k ; unordered_map ' um ' implemented as hash table ; traverse the given array ; accumulate sum ; when subarray starts from index '0' ; make an entry for ' sum ' if it is not present in ' um ' ; check if ' sum - k ' is present in ' um ' or not ; update maxLength ; required maximum length ; function to find the length of the longest subarray having maximum sum ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { static int maxSubArraySum ( int [ ] arr , int size ) { int max_so_far = arr [ 0 ] ; int curr_max = arr [ 0 ] ; for ( int i = 1 ; i < size ; i ++ ) { curr_max = Math . Max ( arr [ i ] , curr_max + arr [ i ] ) ; max_so_far = Math . Max ( max_so_far , curr_max ) ; } return max_so_far ; } static int lenOfLongSubarrWithGivenSum ( int [ ] arr , int n , int k ) { Dictionary < int , int > um = new Dictionary < int , int > ( ) ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum == k ) maxLen = i + 1 ; if ( um . ContainsKey ( sum ) ) um . Add ( sum , i ) ; if ( um . ContainsKey ( sum - k ) ) { if ( maxLen < ( i - um [ sum - k ] ) ) maxLen = i - um [ sum - k ] ; } } return maxLen ; } static int lenLongSubarrWithMaxSum ( int [ ] arr , int n ) { int maxSum = maxSubArraySum ( arr , n ) ; return lenOfLongSubarrWithGivenSum ( arr , n , maxSum ) ; } public static void Main ( ) { int [ ] arr = { 5 , - 2 , - 1 , 3 , - 4 } ; int n = arr . Length ; Console . WriteLine ( " Length ▁ of ▁ longest ▁ subarray ▁ " + " having ▁ maximum ▁ sum ▁ = ▁ " + lenLongSubarrWithMaxSum ( arr , n ) ) ; } }
Sum of elements of all partitions of number such that no element is less than K | C # implementation of above approach ; Function that returns total number of valid partitions of integer N ; Global declaration of 2D dp array which will be later used for memoization ; initializing 2D dp array with - 1 we will use this 2D array for memoization ; if this subproblem is already previously calculated , then directly return that answer ; if N < K , then no valid partition is possible ; if N is between K to 2 * K then there is only one partition and that is the number N itself ; Initialize answer with 1 as the number N itself is always a valid partition ; for loop to iterate over K to N and find number of possible valid partitions recursively . ; memoization is done by storing this calculated answer ; returning number of valid partitions ; Driver code ; Printing total number of valid partitions
using System ; class GFG { public static long countPartitions ( int n , int k ) { long [ , ] dp = new long [ 201 , 201 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < n + 1 ; j ++ ) { dp [ i , j ] = - 1 ; } } if ( dp [ n , k ] >= 0 ) return dp [ n , k ] ; if ( n < k ) return 0 ; if ( n < 2 * k ) return 1 ; long answer = 1 ; for ( int i = k ; i < n ; i ++ ) answer = answer + countPartitions ( n - i , i ) ; dp [ n , k ] = answer ; return answer ; } static void Main ( ) { int n = 10 , k = 3 ; Console . Write ( " Total ▁ Aggregate ▁ sum ▁ of ▁ all ▁ Valid ▁ Partitions : ▁ " + countPartitions ( n , k ) * n ) ; } }
Number of Co | C # program to count the pairs whose sum of digits is co - prime ; Function to find the elements after doing the sum of digits ; Traverse from a to b ; Find the sum of the digits of the elements in the given range one by one ; Function to count the co - prime pairs ; Function to make the pairs by doing the sum of digits ; Count pairs that are co - primes ; Driver code ; Function to count the pairs
using System ; using System . Collections . Generic ; class GFG { public static int GCD ( int a , int b ) { if ( b == 0 ) { return a ; } return GCD ( b , a % b ) ; } public static void makePairs ( List < int > pairs , int a , int b ) { for ( int i = a ; i <= b ; i ++ ) { int sumOfDigits = 0 , k = i ; while ( k > 0 ) { sumOfDigits += k % 10 ; k /= 10 ; } if ( sumOfDigits <= 162 ) { pairs . Add ( sumOfDigits ) ; } } } public static int countCoPrime ( int a , int b ) { List < int > pairs = new List < int > ( ) ; makePairs ( pairs , a , b ) ; int count = 0 ; for ( int i = 0 ; i < pairs . Count ; i ++ ) { for ( int j = i + 1 ; j < pairs . Count ; j ++ ) { if ( GCD ( pairs [ i ] , pairs [ j ] ) == 1 ) { count ++ ; } } } return count ; } public static void Main ( string [ ] args ) { int a = 12 , b = 15 ; Console . WriteLine ( countCoPrime ( a , b ) ) ; } }
Number of Co | C # program to count the pairs whose sum of digits is co - prime ; Recursive function to return the frequency of numbers having their sum of digits i ; Returns 1 or 0 ; Returns value of the dp if already stored ; Loop from digit 0 to 9 ; To change the tight to 1 ; Calling the recursive function to find the frequency ; Function to find out frequency of numbers from 1 to N having their sum of digits from 1 to 162 and store them in array ; Number to string conversion ; Calling the recursive function and pushing it into array ; Function to find the pairs ; Calling the formArray function of a - 1 numbers ; Calling the formArray function of b numbers ; Subtracting the frequency of higher number array with lower number array and thus finding the range of numbers from a to b having sum from 1 to 162 ; To find out total number of pairs which are co - prime ; Driver code ; Function to count the pairs
using System ; using System . Collections . Generic ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static long recursive ( long idx , long sum , long tight , string st , long [ , , ] dp , long num ) { if ( idx == num ) { return sum == 0 ? 1 : 0 ; } if ( dp [ ( int ) idx , ( int ) tight , ( int ) sum ] != - 1 ) return dp [ ( int ) idx , ( int ) tight , ( int ) sum ] ; long newTight ; long ans = 0 ; long d ; for ( d = 0 ; d < 10 ; ++ d ) { newTight = 0 ; if ( tight == 1 && st [ ( ( int ) idx ) ] - '0' < d ) continue ; if ( tight == 1 && st [ ( ( int ) idx ) ] - '0' == d ) newTight = 1 ; if ( sum >= d ) ans += recursive ( idx + 1 , sum - d , ( int ) newTight , st , dp , num ) ; } return dp [ ( int ) idx , ( int ) tight , ( int ) sum ] = ans ; } static List < long > formArray ( long N ) { long [ , , ] dp = new long [ 20 , 2 , 166 ] ; for ( int i = 0 ; i < 20 ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { for ( int k = 0 ; k < 166 ; k ++ ) { dp [ i , j , k ] = - 1 ; } } } string st = N . ToString ( ) ; long num = st . Length ; List < long > arr = new List < long > ( ) ; for ( int i = 1 ; i <= 162 ; ++ i ) { arr . Add ( recursive ( 0 , i , 1 , st , dp , num ) ) ; } return arr ; } static long findPair ( long a , long b ) { List < long > arr_smaller = formArray ( a - 1 ) ; List < long > arr_greater = formArray ( b ) ; for ( int i = 0 ; i < arr_greater . Count ; ++ i ) { arr_greater [ i ] = arr_greater [ i ] - arr_smaller [ i ] ; } long ans = 0 ; for ( int i = 1 ; i <= 162 ; ++ i ) { for ( int j = i + 1 ; j <= 162 ; ++ j ) { if ( gcd ( i , j ) == 1 ) { ans = ( ans + arr_greater [ i - 1 ] * arr_greater [ j - 1 ] ) ; } } } return ans ; } static public void Main ( ) { long a = 12 , b = 15 ; Console . WriteLine ( findPair ( a , b ) ) ; } }
Semiperfect Number | C # program to check if the number is semi - perfect or not ; Code to find all the factors of the number excluding the number itself ; vector to store the factors ; note that this loop runs till Math . Sqrt ( n ) ; if the value of i is a factor ; condition to check the divisor is not the number itself ; return the vector ; Function to check if the number is semi - perfect or not ; find the divisors ; sorting the vector ; subset to check if no is semiperfect ; initialising 1 st column to true ; initializing 1 st row except zero position to 0 ; loop to find whether the number is semiperfect ; calculation to check if the number can be made by summation of divisors ; If not possible to make the number by any combination of divisors ; Driver code
using System ; using System . Collections . Generic ; class GFG { static List < int > factors ( int n ) { List < int > v = new List < int > ( ) ; v . Add ( 1 ) ; for ( int i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v . Add ( i ) ; if ( n / i != i ) { v . Add ( n / i ) ; } } } return v ; } static bool check ( int n ) { List < int > v = new List < int > ( ) ; v = factors ( n ) ; v . Sort ( ) ; int r = v . Count ; bool [ , ] subset = new bool [ r + 1 , n + 1 ] ; for ( int i = 0 ; i <= r ; i ++ ) subset [ i , 0 ] = true ; for ( int i = 1 ; i <= n ; i ++ ) subset [ 0 , i ] = false ; for ( int i = 1 ; i <= r ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( j < v [ i - 1 ] ) subset [ i , j ] = subset [ i - 1 , j ] ; else { subset [ i , j ] = subset [ i - 1 , j ] || subset [ i - 1 , ( j - v [ i - 1 ] ) ] ; } } } if ( ( subset [ r , n ] ) == false ) return false ; else return true ; } public static void Main ( String [ ] args ) { int n = 40 ; if ( check ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }