codeparrot/xlcost-text-to-code huggingface dataset:
Count sub | C # implementation for above approach ; Segment tree implemented as an array ; Function to build the segment tree ; Function to query product of sub - array [ l . . r ] in O ( log n ) time ; Function to count sub - arrays whose product is divisible by K ; Query segment tree to find product % k of the sub - array [ i . . j ] ; Driver code ; Build the segment tree
using System ; class GFG { static int MAX = 100002 ; static long [ ] tree = new long [ 4 * MAX ] ; static void build ( int node , int start , int end , int [ ] arr , int k ) { if ( start == end ) { tree [ node ] = ( 1L * arr [ start ] ) % k ; return ; } int mid = ( start + end ) >> 1 ; build ( 2 * node , start , mid , arr , k ) ; build ( 2 * node + 1 , mid + 1 , end , arr , k ) ; tree [ node ] = ( tree [ 2 * node ] * tree [ 2 * node + 1 ] ) % k ; } static long query ( int node , int start , int end , int l , int r , int k ) { if ( start > end start > r end < l ) { return 1 ; } if ( start >= l && end <= r ) { return tree [ node ] % k ; } int mid = ( start + end ) >> 1 ; long q1 = query ( 2 * node , start , mid , l , r , k ) ; long q2 = query ( 2 * node + 1 , mid + 1 , end , l , r , k ) ; return ( q1 * q2 ) % k ; } static long countSubarrays ( int [ ] arr , int n , int k ) { long count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { long product_mod_k = query ( 1 , 0 , n - 1 , i , j , k ) ; if ( product_mod_k == 0 ) { count ++ ; } } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 2 , 8 } ; int n = arr . Length ; int k = 4 ; build ( 1 , 0 , n - 1 , arr , k ) ; Console . WriteLine ( countSubarrays ( arr , n , k ) ) ; } }
Find a pair from the given array with maximum nCr value | C # implementation of the approach ; Function to print the pair that gives maximum nCr ; This gives the value of N in nCr ; Case 1 : When N is odd ; Case 2 : When N is even ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void printMaxValPair ( List < long > v , int n ) { v . Sort ( ) ; long N = v [ ( int ) n - 1 ] ; if ( N % 2 == 1 ) { long first_maxima = N / 2 ; long second_maxima = first_maxima + 1 ; long ans1 = ( long ) 3e18 , ans2 = ( long ) 3e18 ; long from_left = - 1 , from_right = - 1 ; long from = - 1 ; for ( long i = 0 ; i < n ; ++ i ) { if ( v [ ( int ) i ] > first_maxima ) { from = i ; break ; } else { long diff = first_maxima - v [ ( int ) i ] ; if ( diff < ans1 ) { ans1 = diff ; from_left = v [ ( int ) i ] ; } } } from_right = v [ ( int ) from ] ; long diff1 = first_maxima - from_left ; long diff2 = from_right - second_maxima ; if ( diff1 < diff2 ) Console . WriteLine ( N + " ▁ " + from_left ) ; else Console . WriteLine ( N + " ▁ " + from_right ) ; } else { long maxima = N / 2 ; long ans1 = ( long ) 3e18 ; long R = - 1 ; for ( long i = 0 ; i < n - 1 ; ++ i ) { long diff = Math . Abs ( v [ ( int ) i ] - maxima ) ; if ( diff < ans1 ) { ans1 = diff ; R = v [ ( int ) i ] ; } } Console . WriteLine ( N + " ▁ " + R ) ; } } public static void Main ( String [ ] args ) { long [ ] arr = { 1 , 1 , 2 , 3 , 6 , 1 } ; List < long > v = new List < long > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) v . Add ( arr [ i ] ) ; int n = v . Count ; printMaxValPair ( v , n ) ; } }
Find the number of good permutations | C # implementation of the above approach . ; Function to return the count of good permutations ; For m = 0 , ans is 1 ; If k is greater than 1 ; If k is greater than 2 ; If k is greater than 3 ; Driver code
using System ; class GFG { static int Permutations ( int n , int k ) { int ans = 1 ; if ( k >= 2 ) ans += ( n ) * ( n - 1 ) / 2 ; if ( k >= 3 ) ans += ( n ) * ( n - 1 ) * ( n - 2 ) * 2 / 6 ; if ( k >= 4 ) ans += ( n ) * ( n - 1 ) * ( n - 2 ) * ( n - 3 ) * 9 / 24 ; return ans ; } public static void Main ( ) { int n = 5 , k = 2 ; Console . WriteLine ( Permutations ( n , k ) ) ; } }
Count integers in a range which are divisible by their euler totient value | C # implementation of the above approach . ; Function to return a ^ n ; Function to return count of integers that satisfy n % phi ( n ) = 0 ; Driver Code
using System ; class GFG { static long power ( long a , long n ) { if ( n == 0 ) return 1 ; long p = power ( a , n / 2 ) ; p = p * p ; if ( n % 2 == 1 ) p = p * a ; return p ; } static int countIntegers ( long l , long r ) { long ans = 0 , i = 1 ; long v = power ( 2 , i ) ; while ( v <= r ) { while ( v <= r ) { if ( v >= l ) ans ++ ; v = v * 3 ; } i ++ ; v = power ( 2 , i ) ; } if ( l == 1 ) ans ++ ; return ( int ) ans ; } public static void Main ( ) { long l = 12 , r = 21 ; Console . WriteLine ( countIntegers ( l , r ) ) ; } }
Number of pairs from the first N natural numbers whose sum is divisible by K | C # implementation of the approach ; Function to find the number of pairs from the set of natural numbers up to N whose sum is divisible by K ; Declaring a Hash to store count ; Storing the count of integers with a specific remainder in Hash array ; Check if K is even ; Count of pairs when both integers are divisible by K ; Count of pairs when one remainder is R and other remainder is K - R ; Count of pairs when both the remainders are K / 2 ; Count of pairs when both integers are divisible by K ; Count of pairs when one remainder is R and other remainder is K - R ; Driver code ; Print the count of pairs
class GfG { static int findPairCount ( int N , int K ) { int count = 0 ; int [ ] rem = new int [ K ] ; rem [ 0 ] = N / K ; for ( int i = 1 ; i < K ; i ++ ) rem [ i ] = ( N - i ) / K + 1 ; if ( K % 2 == 0 ) { count += ( rem [ 0 ] * ( rem [ 0 ] - 1 ) ) / 2 ; for ( int i = 1 ; i < K / 2 ; i ++ ) count += rem [ i ] * rem [ K - i ] ; count += ( rem [ K / 2 ] * ( rem [ K / 2 ] - 1 ) ) / 2 ; } else { count += ( rem [ 0 ] * ( rem [ 0 ] - 1 ) ) / 2 ; for ( int i = 1 ; i <= K / 2 ; i ++ ) count += rem [ i ] * rem [ K - i ] ; } return count ; } static void Main ( ) { int N = 10 , K = 4 ; System . Console . WriteLine ( findPairCount ( N , K ) ) ; } }
Find the sum of all Truncatable primes below N | C # implementation of the above approach . ; To check if a number is prime or not ; Sieve of Eratosthenes function to find all prime numbers ; Function to return the sum of all truncatable primes below n ; To store the required sum ; Check every number below n ; Check from right to left ; If number is not prime at any stage ; Check from left to right ; If number is not prime at any stage ; If flag is still true ; Return the required sum ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int N = 1000005 ; static Boolean [ ] prime = new Boolean [ N ] ; static void sieve ( ) { Array . Fill ( prime , true ) ; prime [ 1 ] = false ; prime [ 0 ] = false ; for ( int i = 2 ; i < N ; i ++ ) { if ( prime [ i ] ) { for ( int j = i * 2 ; j < N ; j += i ) { prime [ j ] = false ; } } } } static int sumTruncatablePrimes ( int n ) { int sum = 0 ; for ( int i = 2 ; i < n ; i ++ ) { int num = i ; Boolean flag = true ; while ( num > 0 ) { if ( ! prime [ num ] ) { flag = false ; break ; } num /= 10 ; } num = i ; int power = 10 ; while ( num / power > 0 ) { if ( ! prime [ num % power ] ) { flag = false ; break ; } power *= 10 ; } if ( flag ) { sum += i ; } } return sum ; } public static void Main ( String [ ] args ) { int n = 25 ; sieve ( ) ; Console . WriteLine ( sumTruncatablePrimes ( n ) ) ; } }
Smallest and Largest N | C # implementation of the approach ; Function to print the largest and the smallest n - digit perfect squares ; Smallest n - digit perfect square ; Largest n - digit perfect square ; Driver code
using System ; public class GFG { static void nDigitPerfectSquares ( int n ) { int smallest = ( int ) Math . Pow ( Math . Ceiling ( Math . Sqrt ( Math . Pow ( 10 , n - 1 ) ) ) , 2 ) ; Console . Write ( smallest + " ▁ " ) ; int largest = ( int ) Math . Pow ( Math . Ceiling ( Math . Sqrt ( Math . Pow ( 10 , n ) ) ) - 1 , 2 ) ; Console . Write ( largest ) ; } public static void Main ( String [ ] args ) { int n = 4 ; nDigitPerfectSquares ( n ) ; } }
Maximum trace possible for any sub | C # program for the above approach ; Function to return the maximum trace possible for a sub - matrix of the given matrix ; Calculate the trace for each of the sub - matrix with top left corner at cell ( r , s ) ; Update the maximum trace ; Return the maximum trace ; Driver code
using System ; class GFG { static int N = 3 ; static int MaxTraceSub ( int [ ] [ ] mat ) { int max_trace = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { int r = i , s = j , trace = 0 ; while ( r < N && s < N ) { trace += mat [ r ] [ s ] ; r ++ ; s ++ ; max_trace = Math . Max ( trace , max_trace ) ; } } } return max_trace ; } public static void Main ( ) { int [ ] [ ] mat = new int [ ] [ ] { new int [ ] { 10 , 2 , 5 } , new int [ ] { 6 , 10 , 4 } , new int [ ] { 2 , 7 , - 10 } } ; Console . WriteLine ( MaxTraceSub ( mat ) ) ; } }
Check if matrix can be converted to another matrix by transposing square sub | C # implementation of the approach ; Function that returns true if matrix1 can be converted to matrix2 with the given operation ; Traverse all the diagonals starting at first column ; Traverse in diagonal ; Store the diagonal elements ; Move up ; Sort the elements ; Check if they are same ; Traverse all the diagonals starting at last row ; Traverse in the diagonal ; Store diagonal elements ; Sort all elements ; Check for same ; If every element matches ; Driver code
using System ; using System . Collections . Generic ; class GFG { static readonly int n = 3 ; static readonly int m = 3 ; static bool check ( int [ , ] a , int [ , ] b ) { for ( int i = 0 ; i < n ; i ++ ) { List < int > v1 = new List < int > ( ) ; List < int > v2 = new List < int > ( ) ; int r = i ; int col = 0 ; while ( r >= 0 && col < m ) { v1 . Add ( a [ r , col ] ) ; v2 . Add ( b [ r , col ] ) ; r -- ; col ++ ; } v1 . Sort ( ) ; v2 . Sort ( ) ; for ( int j = 0 ; j < v1 . Count ; j ++ ) { if ( v1 [ j ] != v2 [ j ] ) { return false ; } } } for ( int j = 1 ; j < m ; j ++ ) { List < int > v1 = new List < int > ( ) ; List < int > v2 = new List < int > ( ) ; int r = n - 1 ; int col = j ; while ( r >= 0 && col < m ) { v1 . Add ( a [ r , col ] ) ; v2 . Add ( b [ r , col ] ) ; r -- ; col ++ ; } v1 . Sort ( ) ; v2 . Sort ( ) ; for ( int i = 0 ; i < v1 . Count ; i ++ ) { if ( v1 [ i ] != v2 [ i ] ) { return false ; } } } return true ; } public static void Main ( ) { int [ , ] a = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int [ , ] b = { { 1 , 4 , 7 } , { 2 , 5 , 6 } , { 3 , 8 , 9 } } ; if ( check ( a , b ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Last digit of Product of two Large or Small numbers ( a * b ) | CSharp implementation of the above approach ; Function to print last digit of product a * b ; Driver code
using System ; class Solution { public static void lastDigit ( String a , String b ) { int lastDig = ( a [ a . Length - 1 ] - '0' ) * ( b [ b . Length - 1 ] - '0' ) ; Console . Write ( lastDig % 10 ) ; } public static void Main ( ) { String a = "1234567891233" , b = "1234567891233156" ; lastDigit ( a , b ) ; } }
Smallest and Largest Palindrome with N Digits | C # implementation of the approach ; Function to print the smallest and largest palindrome with N digits ; Driver Code
using System ; class GfG { static void printPalindrome ( int n ) { if ( n == 1 ) { Console . WriteLine ( " Smallest ▁ Palindrome : ▁ 0" ) ; Console . WriteLine ( " Largest ▁ Palindrome : ▁ 9" ) ; } else { Console . WriteLine ( " Smallest ▁ Palindrome : ▁ " + ( int ) ( Math . Pow ( 10 , n - 1 ) ) + 1 ) ; Console . WriteLine ( " Largest ▁ Palindrome : ▁ " + ( ( int ) ( Math . Pow ( 10 , n ) ) - 1 ) ) ; } } public static void Main ( String [ ] args ) { int n = 4 ; printPalindrome ( n ) ; } }
Addition of two numbers without propagating Carry | C # implementation of the above approach ; Function to print sum of 2 numbers without propagating carry ; Reverse a ; Reverse b ; Generate sum Since length of both a and b are same , take any one of them . ; Extract digits from a and b and add ; If sum is single digit ; If sum is not single digit reverse sum ; Extract digits from sum and append to result ; Driver code
using System ; class GFG { static int printSum ( int a , int b ) { int res = 0 ; int temp1 = 0 , temp2 = 0 ; while ( a != 0 ) { temp1 = temp1 * 10 + ( a % 10 ) ; a /= 10 ; } a = temp1 ; while ( b != 0 ) { temp2 = temp2 * 10 + ( b % 10 ) ; b /= 10 ; } b = temp2 ; while ( a != 0 ) { int sum = ( a % 10 + b % 10 ) ; if ( sum / 10 == 0 ) res = res * 10 + sum ; else { temp1 = 0 ; while ( sum != 0 ) { temp1 = temp1 * 10 + ( sum % 10 ) ; sum /= 10 ; } sum = temp1 ; while ( sum != 0 ) { res = res * 10 + ( sum % 10 ) ; sum /= 10 ; } } a /= 10 ; b /= 10 ; } return res ; } public static void Main ( ) { int a = 7752 , b = 8834 ; Console . Write ( printSum ( a , b ) ) ; } }
Number of digits before the decimal point in the division of two numbers | C # implementation of the approach ; Function to return the number of digits before the decimal in a / b ; Absolute value of a / b ; If result is 0 ; Count number of digits in the result ; Return the required count of digits ; Driver code
using System ; class GFG { static int countDigits ( int a , int b ) { int count = 0 ; int p = Math . Abs ( a / b ) ; if ( p == 0 ) return 1 ; while ( p > 0 ) { count ++ ; p = p / 10 ; } return count ; } public static void Main ( ) { int a = 100 ; int b = 10 ; Console . Write ( countDigits ( a , b ) ) ; } }
Number of digits before the decimal point in the division of two numbers | C # implementation of the approach ; Function to return the number of digits before the decimal in a / b ; Return the required count of digits ; Driver code
using System ; class GFG { public static int countDigits ( int a , int b ) { double digits = Math . Log10 ( Math . Abs ( a ) ) - Math . Log10 ( Math . Abs ( b ) ) + 1 ; return ( int ) Math . Floor ( digits ) ; } static void Main ( ) { int a = 100 ; int b = 10 ; Console . Write ( countDigits ( a , b ) ) ; } }
Smallest odd number with N digits | C # implementation of the approach ; Function to return smallest odd with n digits ; Driver code
using System ; class Solution { static int smallestOdd ( int n ) { if ( n == 1 ) return 0 ; return Math . pow ( 10 , n - 1 ) + 1 ; } public static void Main ( ) { int n = 4 ; Console . Write ( smallestOdd ( n ) ) ; } }
Largest Even and Odd N | C # implementation of the approach ; Function to print the largest n - digit even and odd numbers ; Driver code
using System ; class GFG { static void findNumbers ( int n ) { int odd = ( int ) Math . Pow ( 10 , n ) - 1 ; int even = odd - 1 ; Console . WriteLine ( " Even ▁ = ▁ " + even ) ; Console . Write ( " Odd ▁ = ▁ " + odd ) ; } public static void Main ( ) { int n = 4 ; findNumbers ( n ) ; } }
Longest sub | C # implementation of the approach ; Function to return the length of the longest sub - array whose product of elements is 0 ; Driver code
using System ; class GFG { static int longestSubArray ( int [ ] arr , int n ) { bool isZeroPresent = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { isZeroPresent = true ; break ; } } if ( isZeroPresent ) return n ; return 0 ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 0 , 1 , 2 , 0 } ; int n = arr . Length ; Console . Write ( longestSubArray ( arr , n ) ) ; } }
Smallest Even number with N digits | C # implementation of the approach ; Function to return smallest even number with n digits ; Driver code
using System ; class Solution { static int smallestEven ( int n ) { if ( n == 1 ) return 0 ; return Math . pow ( 10 , n - 1 ) ; } public static void Main ( ) { int n = 4 ; Console . Write ( smallestEven ( n ) ) ; } }
Maximize profit when divisibility by two numbers have associated profits | C # implementation of the approach ; Function to return the maximum profit ; min ( x , y ) * n / lcm ( a , b ) ; Driver code
using System ; class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int maxProfit ( int n , int a , int b , int x , int y ) { int res = x * ( n / a ) ; res += y * ( n / b ) ; res -= Math . Min ( x , y ) * ( n / ( ( a * b ) / __gcd ( a , b ) ) ) ; return res ; } static void Main ( ) { int n = 6 , a = 6 , b = 2 , x = 8 , y = 2 ; Console . WriteLine ( maxProfit ( n , a , b , x , y ) ) ; } }
Series summation if T ( n ) is given and n is very large | C # implementation of the approach ; Function to return the sum of the given series ; Driver code
using System ; class GFG { const int MOD = 1000000007 ; static int sumOfSeries ( int n ) { int ans = ( int ) Math . Pow ( n % MOD , 2 ) ; return ( ans % MOD ) ; } public static void Main ( ) { int n = 10 ; Console . Write ( sumOfSeries ( n ) ) ; } }
Kth odd number in an array | C # implementation of the approach ; Function to return the kth odd element from the array ; Traverse the array ; If current element is odd ; If kth odd element is found ; Total odd elements in the array are < k ; Driver code
using System ; class GFG { static int kthOdd ( int [ ] arr , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) k -- ; if ( k == 0 ) return arr [ i ] ; } return - 1 ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; int k = 2 ; Console . WriteLine ( kthOdd ( arr , n , k ) ) ; } }
Find last five digits of a given five digit number raised to power five | C # program to find last five digits of a five digit number raised to power five ; Function to find the last five digits of a five digit number raised to power five ; Driver code
using System ; class GFG { public static void lastFiveDigits ( int n ) { n = ( n / 10000 ) * 10000 + ( ( n / 100 ) % 10 ) * 1000 + ( n % 10 ) * 100 + ( ( n / 10 ) % 10 ) * 10 + ( n / 1000 ) % 10 ; int ans = 1 ; for ( int i = 0 ; i < 5 ; i ++ ) { ans *= n ; ans %= 100000 ; } Console . WriteLine ( ans ) ; } public static void Main ( string [ ] args ) { int n = 12345 ; lastFiveDigits ( n ) ; } }
Sum of ( maximum element | C # implementation of the above approach ; Function to return a ^ n % mod ; Compute sum of max ( A ) - min ( A ) for all subsets ; Sort the array . ; Maxs = 2 ^ i - 1 ; Mins = 2 ^ ( n - 1 - i ) - 1 ; Driver code
using System ; using System . Collections ; class GFG { static int mod = 1000000007 ; static long power ( long a , long n ) { if ( n == 0 ) { return 1 ; } long p = power ( a , n / 2 ) % mod ; p = ( p * p ) % mod ; if ( n == 1 ) { p = ( p * a ) % mod ; } return p ; } static long computeSum ( int [ ] arr , int n ) { Array . Sort ( arr ) ; long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { long maxs = ( power ( 2 , i ) - 1 + mod ) % mod ; maxs = ( maxs * arr [ i ] ) % mod ; long mins = ( power ( 2 , n - 1 - i ) - 1 + mod ) % mod ; mins = ( mins * arr [ i ] ) % mod ; long V = ( maxs - mins + mod ) % mod ; sum = ( sum + V ) % mod ; } return sum ; } public static void Main ( ) { int [ ] arr = { 4 , 3 , 1 } ; int n = arr . Length ; Console . WriteLine ( computeSum ( arr , n ) ) ; } }
Count of all N digit numbers such that num + Rev ( num ) = 10 ^ N | C # implementation of the approach ; Function to return the count of such numbers ; If n is odd ; Driver code
using System ; class GFG { static int countNumbers ( int n ) { if ( n % 2 == 1 ) return 0 ; return ( 9 * ( int ) Math . Pow ( 10 , n / 2 - 1 ) ) ; } public static void Main ( ) { int n = 2 ; Console . WriteLine ( countNumbers ( n ) ) ; } }
Count of numbers having only 1 set bit in the range [ 0 , n ] | C # implementation of the approach ; Function to return the required count ; To store the count of numbers ; Every power of 2 contains only 1 set bit ; Driver code
using System ; class GFG { static int count ( int n ) { int cnt = 0 ; int p = 1 ; while ( p <= n ) { cnt ++ ; p *= 2 ; } return cnt ; } public static void Main ( ) { int n = 7 ; Console . Write ( count ( n ) ) ; } }
Find the K | C # programme to find the K 'th minimum element from an array concatenated M times ; Function to find the K - th minimum element from an array concatenated M times ; Sort the elements in ascending order ; Return the K 'th Min element  present at ( (K-1) / M ) index ; Driver Code
using System ; class GFG { static int KthMinValAfterMconcatenate ( int [ ] A , int N , int M , int K ) { Array . Sort ( A ) ; return ( A [ ( ( K - 1 ) / M ) ] ) ; } static void Main ( ) { int [ ] A = { 3 , 1 , 2 } ; int M = 3 , K = 4 ; int N = A . Length ; Console . WriteLine ( KthMinValAfterMconcatenate ( A , N , M , K ) ) ; } }
Sum of all i such that ( 2 ^ i + 1 ) % 3 = 0 where i is in range [ 1 , n ] | C # implementation of the approach ; Function to return the required sum ; Total odd numbers from 1 to n ; Sum of first n odd numbers ; Driver code
using System ; public class GFG { public static int sum ( int n ) { n = ( n + 1 ) / 2 ; return ( n * n ) ; } public static void Main ( string [ ] args ) { int n = 3 ; Console . WriteLine ( sum ( n ) ) ; } }
Numbers that are not divisible by any number in the range [ 2 , 10 ] | C # implementation of the approach ; Function to return the count of numbers from 1 to N which are not divisible by any number in the range [ 2 , 10 ] ; Driver code
using System ; class GFG { static int countNumbers ( int n ) { return n - n / 2 - n / 3 - n / 5 - n / 7 + n / 6 + n / 10 + n / 14 + n / 15 + n / 21 + n / 35 - n / 30 - n / 42 - n / 70 - n / 105 + n / 210 ; } static void Main ( ) { int n = 20 ; Console . WriteLine ( countNumbers ( n ) ) ; } }
Maximum Primes whose sum is equal to given N | C # program for above approach ; Function to find max count of primes ; if n is even n / 2 is required answer if n is odd floor ( n / 2 ) = ( int ) ( n / 2 ) is required answer ; Driver Code
using System ; class GFG { static int maxPrimes ( int n ) { return n / 2 ; } public static void Main ( ) { int n = 17 ; Console . WriteLine ( maxPrimes ( n ) ) ; } }
Sum of the series ( 1 * 2 ) + ( 2 * 3 ) + ( 3 * 4 ) + ... ... upto n terms | C # implementation of the approach ; Function to return sum ; Driver code
using System ; class GFG { static int sum ( int n ) { return n * ( n + 1 ) * ( n + 2 ) / 3 ; } public static void Main ( String [ ] args ) { int n = 2 ; Console . WriteLine ( sum ( n ) ) ; } }
Smallest divisor D of N such that gcd ( D , M ) is greater than 1 | C # implementation of the above approach ; Function to find the minimum divisor ; Iterate for all factors of N ; Check for gcd > 1 ; Check for gcd > 1 ; If gcd is m itself ; Driver code
using System ; class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int findMinimum ( int n , int m ) { int mini = m ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { int sec = n / i ; if ( __gcd ( m , i ) > 1 ) { return i ; } else if ( __gcd ( sec , m ) > 1 ) { mini = Math . Min ( sec , mini ) ; } } } if ( mini == m ) return - 1 ; else return mini ; } static void Main ( ) { int n = 8 , m = 10 ; Console . WriteLine ( findMinimum ( n , m ) ) ; } }
Find Nth term of the series 1 , 5 , 32 , 288 ... | C # code to generate ' Nth ' terms of this sequence ; Function to generate a fixed \ number ; Finding nth term ; Driver Method
using System ; class GFG { public static int nthTerm ( int N ) { int nth = 0 , i ; for ( i = N ; i > 0 ; i -- ) { nth += ( int ) Math . Pow ( i , i ) ; } return nth ; } public static void Main ( ) { int N = 3 ; Console . WriteLine ( nthTerm ( N ) ) ; } }
Find kth smallest number in range [ 1 , n ] when all the odd numbers are deleted | C # implementation of the approach ; Function to return the kth smallest element from the range [ 1 , n ] after removing all the odd elements ; Driver code
using System ; class GFG { static int kthSmallest ( int n , int k ) { return ( 2 * k ) ; } public static void Main ( ) { int n = 8 , k = 4 ; Console . WriteLine ( kthSmallest ( n , k ) ) ; } }
Check if a number can be represented as sum of non zero powers of 2 | C # implementation of the approach ; Function that return true if n can be represented as the sum of powers of 2 without using 2 ^ 0 ; Driver code
using System ; class GFG { static bool isSumOfPowersOfTwo ( int n ) { if ( n % 2 == 1 ) return false ; else return true ; } public static void Main ( ) { int n = 10 ; if ( isSumOfPowersOfTwo ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }