codeparrot/xlcost-text-to-code huggingface dataset:
Find the sum of the costs of all possible arrangements of the cells | C # implementation of the approach ; To store the factorials and factorial mod inverse of the numbers ; Function to return ( a ^ m1 ) % mod ; Function to find the factorials of all the numbers ; Function to find factorial mod inverse of all the numbers ; Function to return nCr ; Function to return the sum of the costs of all the possible arrangements of the cells ; For all possible X 's ; For all possible Y 's ; Driver code
using System ; class GFG { static int N = 20 ; static int mod = 1000000007 ; static int [ ] factorial = new int [ N ] ; static int [ ] modinverse = new int [ N ] ; static int power ( int a , int m1 ) { if ( m1 == 0 ) return 1 ; else if ( m1 == 1 ) return a ; else if ( m1 == 2 ) return ( a * a ) % mod ; else if ( ( m1 & 1 ) != 0 ) return ( a * power ( power ( a , m1 / 2 ) , 2 ) ) % mod ; else return power ( power ( a , m1 / 2 ) , 2 ) % mod ; } static void factorialfun ( ) { factorial [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) factorial [ i ] = ( factorial [ i - 1 ] * i ) % mod ; } static void modinversefun ( ) { modinverse [ N - 1 ] = power ( factorial [ N - 1 ] , mod - 2 ) % mod ; for ( int i = N - 2 ; i >= 0 ; i -- ) modinverse [ i ] = ( modinverse [ i + 1 ] * ( i + 1 ) ) % mod ; } static int binomial ( int n , int r ) { if ( r > n ) return 0 ; int a = ( factorial [ n ] * modinverse [ n - r ] ) % mod ; a = ( a * modinverse [ r ] ) % mod ; return a ; } static int arrange ( int n , int m , int k ) { factorialfun ( ) ; modinversefun ( ) ; int ans = 0 ; for ( int i = 1 ; i < n ; i ++ ) ans += ( i * ( n - i ) * m * m ) % mod ; ans = 8 ; for ( int i = 1 ; i < m ; i ++ ) ans += ( i * ( m - i ) * n * n ) % mod ; ans = ( ans * binomial ( n * m - 2 , k - 2 ) ) % mod + 8 ; return ans ; } public static void Main ( String [ ] args ) { int n = 2 , m = 2 , k = 2 ; Console . WriteLine ( arrange ( n , m , k ) ) ; } }
Find the Nth digit in the proper fraction of two numbers | C # implementation of the approach ; Function to print the Nth digit in the fraction ( p / q ) ; To store the resultant digit ; While N > 0 compute the Nth digit by dividing p and q and store the result into variable res and go to next digit ; Driver code
using System ; class GFG { static int findNthDigit ( int p , int q , int N ) { int res = 0 ; while ( N > 0 ) { N -- ; p *= 10 ; res = p / q ; p %= q ; } return res ; } public static void Main ( ) { int p = 1 , q = 2 , N = 1 ; Console . WriteLine ( findNthDigit ( p , q , N ) ) ; } }
Sum of the updated array after performing the given operation | C # implementation of the approach ; Utility function to return the sum of the array ; Function to return the sum of the modified array ; Subtract the subarray sum ; Sum of subarray arr [ i ... n - 1 ] ; Return the sum of the modified array ; Driver code
using System ; class GFG { static int sumArr ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } static int sumModArr ( int [ ] arr , int n ) { int subSum = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int curr = arr [ i ] ; arr [ i ] -= subSum ; subSum += curr ; } return sumArr ( arr , n ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 40 , 25 , 12 , 10 } ; int n = arr . Length ; Console . WriteLine ( sumModArr ( arr , n ) ) ; } }
Find closest integer with the same weight | C # implementation of the approach ; Function to return the number closest to x which has equal number of set bits as x ; Loop for each bit in x and compare with the next bit ; Driver code
using System ; class GFG { static int NumUnsignBits = 64 ; static long findNum ( long x ) { for ( int i = 0 ; i < NumUnsignBits - 1 ; i ++ ) { if ( ( ( x >> i ) & 1 ) != ( ( x >> ( i + 1 ) ) & 1 ) ) { x ^= ( 1 << i ) | ( 1 << ( i + 1 ) ) ; return x ; } } return long . MinValue ; } public static void Main ( String [ ] args ) { int n = 92 ; Console . WriteLine ( findNum ( n ) ) ; } }
Cake Distribution Problem | C # implementation of the approach ; Function to return the remaining count of cakes ; Sum for 1 cycle ; no . of full cycle and remainder ; Driver Code
using System ; class GFG { static int cntCakes ( int n , int m ) { int sum = ( n * ( n + 1 ) ) / 2 ; int quo = m / sum ; int rem = m % sum ; double ans = m - quo * sum ; double x = ( - 1 + Math . Pow ( ( 8 * rem ) + 1 , 0.5 ) ) / 2 ; ans = ans - x * ( x + 1 ) / 2 ; return ( int ) ans ; } static public void Main ( ) { int n = 3 ; int m = 8 ; int ans = cntCakes ( n , m ) ; Console . Write ( ans ) ; } }
Find the number of squares inside the given square grid | C # implementation of the approach ; Function to return the number of squares inside an n * n grid ; Driver code
using System ; class GFG { static int cntSquares ( int n ) { return n * ( n + 1 ) * ( 2 * n + 1 ) / 6 ; } public static void Main ( String [ ] args ) { Console . Write ( cntSquares ( 4 ) ) ; } }
Find all palindrome numbers of given digits | C # implementation of the approach ; Function to return the reverse of num ; Function that returns true if num is palindrome ; If the number is equal to the reverse of it then it is a palindrome ; Function to print all the d - digit palindrome numbers ; Smallest and the largest d - digit numbers ; Starting from the smallest d - digit number till the largest ; If the current number is palindrome ; Driver code
using System ; class GFG { static int reverse ( int num ) { int rev = 0 ; while ( num > 0 ) { rev = rev * 10 + num % 10 ; num = num / 10 ; } return rev ; } static bool isPalindrome ( int num ) { if ( num == reverse ( num ) ) return true ; return false ; } static void printPalindromes ( int d ) { if ( d <= 0 ) return ; int smallest = ( int ) Math . Pow ( 10 , d - 1 ) ; int largest = ( int ) Math . Pow ( 10 , d ) - 1 ; for ( int i = smallest ; i <= largest ; i ++ ) { if ( isPalindrome ( i ) ) Console . Write ( i + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int d = 2 ; printPalindromes ( d ) ; } }
Find X and Y intercepts of a line passing through the given points | C # implementation of the approach ; Function to find the X and Y intercepts of the line passing through the given points ; if line is parallel to y axis ; x - intercept will be p [ 0 ] ; y - intercept will be infinity ; if line is parallel to x axis ; x - intercept will be infinity ; y - intercept will be p [ 1 ] ; Slope of the line ; y = mx + c in where c is unknown Use any of the given point to find c ; For finding the x - intercept put y = 0 ; For finding the y - intercept put x = 0 ; Driver code
using System ; class GFG { static void getXandYintercept ( int [ ] P , int [ ] Q ) { int a = P [ 1 ] - Q [ 1 ] ; int b = P [ 0 ] - Q [ 0 ] ; if ( b == 0 ) { Console . WriteLine ( P [ 0 ] ) ; Console . WriteLine ( " infinity " ) ; return ; } if ( a == 0 ) { Console . WriteLine ( " infinity " ) ; Console . WriteLine ( P [ 1 ] ) ; return ; } double m = a / ( b * 1.0 ) ; int x = P [ 0 ] ; int y = P [ 1 ] ; double c = y - m * x ; y = 0 ; double r = ( y - c ) / ( m * 1.0 ) ; Console . WriteLine ( r ) ; x = 0 ; y = ( int ) ( m * x + c ) ; Console . WriteLine ( c ) ; } public static void Main ( ) { int [ ] p1 = { 5 , 2 } ; int [ ] p2 = { 2 , 7 } ; getXandYintercept ( p1 , p2 ) ; } }
Minimum number of moves to reach N starting from ( 1 , 1 ) | C # implementation of the approach ; Function to return the minimum number of moves required to reach the cell containing N starting from ( 1 , 1 ) ; To store the required answer ; For all possible values of divisors ; If i is a divisor of n ; Get the moves to reach n ; Return the required answer ; Driver code
using System ; class GFG { static int min_moves ( int n ) { int ans = int . MaxValue ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { ans = Math . Min ( ans , i + n / i - 2 ) ; } } return ans ; } public static void Main ( String [ ] args ) { int n = 10 ; Console . WriteLine ( min_moves ( n ) ) ; } }
Minimum possible value of ( i * j ) % 2019 | C # implementation of the approach ; Function to return the minimum possible value of ( i * j ) % 2019 ; If we can get a number divisible by 2019 ; Find the minimum value by running nested loops ; Driver code
using System ; class GFG { static int MOD = 2019 ; static int min_modulo ( int l , int r ) { if ( r - l >= MOD ) return 0 ; else { int ans = MOD - 1 ; for ( int i = l ; i <= r ; i ++ ) { for ( int j = i + 1 ; j <= r ; j ++ ) { ans = Math . Min ( ans , ( i * j ) % MOD ) ; } } return ans ; } } public static void Main ( String [ ] args ) { int l = 2020 , r = 2040 ; Console . WriteLine ( min_modulo ( l , r ) ) ; } }
Represent ( 2 / N ) as the sum of three distinct positive integers of the form ( 1 / m ) | C # implementation of the approach ; Function to find the required fractions ; Base condition ; For N > 1 ; Driver code
using System ; class GFG { static void find_numbers ( int N ) { if ( N == 1 ) { Console . Write ( - 1 ) ; } else { Console . Write ( N + " ▁ " + ( N + 1 ) + " ▁ " + ( N * ( N + 1 ) ) ) ; } } public static void Main ( String [ ] args ) { int N = 5 ; find_numbers ( N ) ; } }
Count the pairs in an array such that the difference between them and their indices is equal | C # implementation of the approach ; Function to return the count of all valid pairs ; To store the frequencies of ( arr [ i ] - i ) ; To store the required count ; If cnt is the number of elements whose difference with their index is same then ( ( cnt * ( cnt - 1 ) ) / 2 ) such pairs are possible ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int countPairs ( int [ ] arr , int n ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) map [ arr [ i ] - i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) map [ arr [ i ] - i ] ++ ; int res = 0 ; foreach ( KeyValuePair < int , int > x in map ) { int cnt = x . Value ; res += ( ( cnt * ( cnt - 1 ) ) / 2 ) ; } return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 6 , 7 , 9 } ; int n = arr . Length ; Console . WriteLine ( countPairs ( arr , n ) ) ; } }
Minimum possible number with the given operation | C # implementation of the above approach ; Function to return the minimum possible integer that can be obtained from the given integer after performing the given operations ; For every digit ; Digits less than 5 need not to be changed as changing them will lead to a larger number ; The resulting integer cannot have leading zero ; Driver code
using System ; class GFG { static string minInt ( char [ ] str ) { for ( int i = 0 ; i < str . Length ; i ++ ) { if ( ( int ) str [ i ] >= ( int ) ( '5' ) ) { str [ i ] = ( char ) ( ( ( int ) ( '9' ) - ( int ) ( str [ i ] ) ) + ( int ) ( '0' ) ) ; } } if ( str [ 0 ] == '0' ) str [ 0 ] = '9' ; string s = new string ( str ) ; return s ; } static public void Main ( ) { string str = "589" ; Console . WriteLine ( minInt ( str . ToCharArray ( ) ) ) ; } }
Reduce N to 1 with minimum number of given operations | C # implementation of the approach ; Function to return the minimum number of given operations required to reduce n to 1 ; To store the count of operations ; To store the digit ; If n is already then no operation is required ; Extract all the digits except the first digit ; Store the maximum of that digits ; for each digit ; First digit ; Add the value to count ; Driver code
using System ; class GFG { static long minOperations ( long n ) { long count = 0 ; long d = 0 ; if ( n == 1 ) return 0 ; while ( n > 9 ) { d = Math . Max ( n % 10 , d ) ; n /= 10 ; count += 10 ; } d = Math . Max ( d , n - 1 ) ; count += Math . Abs ( d ) ; return count - 1 ; } public static void Main ( String [ ] args ) { long n = 240 ; Console . WriteLine ( minOperations ( n ) ) ; } }
Find the largest number that can be formed by changing at most K digits | C # implementation of the approach ; Function to return the maximum number that can be formed by changing at most k digits in str ; For every digit of the number ; If no more digits can be replaced ; If current digit is not already 9 ; Replace it with 9 ; One digit has been used ; Driver code
using System ; using System . Text ; class GFG { static StringBuilder findMaximumNum ( StringBuilder str , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( k < 1 ) break ; if ( str [ i ] != '9' ) { str [ i ] = '9' ; k -- ; } } return str ; } public static void Main ( ) { StringBuilder str = new StringBuilder ( "569431" ) ; int n = str . Length ; int k = 3 ; Console . WriteLine ( findMaximumNum ( str , n , k ) ) ; } }
Number of occurrences of a given angle formed using 3 vertices of a n | C # implementation of the approach ; Function that calculates occurrences of given angle that can be created using any 3 sides ; Maximum angle in a regular n - gon is equal to the interior angle If the given angle is greater than the interior angle then the given angle cannot be created ; The given angle times n should be divisible by 180 else it cannot be created ; Initialise answer ; Calculate the frequency of given angle for each vertex ; Multiply answer by frequency . ; Multiply answer by the number of vertices . ; Driver code
using System ; class GFG { static int solve ( int ang , int n ) { if ( ( ang * n ) > ( 180 * ( n - 2 ) ) ) { return 0 ; } else if ( ( ang * n ) % 180 != 0 ) { return 0 ; } int ans = 1 ; int freq = ( ang * n ) / 180 ; ans = ans * ( n - 1 - freq ) ; ans = ans * n ; return ans ; } public static void Main ( String [ ] args ) { int ang = 90 , n = 4 ; Console . WriteLine ( solve ( ang , n ) ) ; } }
Find third number such that sum of all three number becomes prime | C # implementation of the above approach ; Function that will check whether number is prime or not ; Function to print the 3 rd number ; If the sum is odd ; If sum is not prime ; Driver code
using System ; class GFG { static bool prime ( int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static void thirdNumber ( int a , int b ) { int sum = 0 , temp = 0 ; sum = a + b ; temp = 1 ; if ( sum == 0 ) { temp = 2 ; } while ( ! prime ( sum + temp ) ) { temp += 2 ; } Console . Write ( temp ) ; } static public void Main ( ) { int a = 3 , b = 5 ; thirdNumber ( a , b ) ; } }
Total ways of selecting a group of X men from N men with or without including a particular man | C # implementation of the approach ; Function to return the value of nCr ; Initialize the answer ; Divide simultaneously by i to avoid overflow ; Function to return the count of ways ; Driver code
using System ; class GFG { static int nCr ( int n , int r ) { int ans = 1 ; for ( int i = 1 ; i <= r ; i += 1 ) { ans *= ( n - r + i ) ; ans /= i ; } return ans ; } static int total_ways ( int N , int X ) { return ( nCr ( N - 1 , X - 1 ) + nCr ( N - 1 , X ) ) ; } public static void Main ( String [ ] args ) { int N = 5 , X = 3 ; Console . WriteLine ( total_ways ( N , X ) ) ; } }
Compute the maximum power with a given condition | C # program for Compute maximum power to which K can be raised so that given condition remains true ; Function to return the largest power ; If n is greater than given M ; If n == m ; Checking for the next power ; Driver Code
using System ; class GFG { static int calculate ( int n , int k , int m , int power ) { if ( n > m ) { if ( power == 0 ) return 0 ; else return power - 1 ; } else if ( n == m ) return power ; else return calculate ( n * k , k , m , power + 1 ) ; } public static void Main ( String [ ] args ) { int N = 1 , K = 2 , M = 5 ; Console . WriteLine ( calculate ( N , K , M , 0 ) ) ; } }
Program to find the number from given holes | C # implementation of the above approach ; Function that will find out the number ; If number of holes equal 0 then return 1 ; If number of holes equal 0 then return 0 ; If number of holes is more than 0 or 1. ; If number of holes is odd ; Driver code ; Calling Function
using System ; class GFG { static void printNumber ( int holes ) { if ( holes == 0 ) Console . Write ( "1" ) ; else if ( holes == 1 ) Console . Write ( "0" ) ; else { int rem = 0 , quo = 0 ; rem = holes % 2 ; quo = holes / 2 ; if ( rem == 1 ) Console . Write ( "4" ) ; for ( int i = 0 ; i < quo ; i ++ ) Console . Write ( "8" ) ; } } static public void Main ( ) { int holes = 3 ; printNumber ( holes ) ; } }
Minimum cost to make all array elements equal | C # implementation of the approach ; Function to return the minimum cost to make each array element equal ; To store the count of even numbers present in the array ; To store the count of odd numbers present in the array ; Iterate through the array and find the count of even numbers and odd numbers ; Driver code
using System ; class GFG { static int minCost ( int [ ] arr , int n ) { int count_even = 0 ; int count_odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) count_even ++ ; else count_odd ++ ; } return Math . Min ( count_even , count_odd ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 3 , 1 , 5 } ; int n = arr . Length ; Console . WriteLine ( minCost ( arr , n ) ) ; } }
Number of Subarrays with positive product | C # implementation of the approach ; Function to return the count of subarrays with negative product ; Replace current element with 1 if it is positive else replace it with - 1 instead ; Take product with previous element to form the prefix product ; Count positive and negative elements in the prefix product array ; Return the required count of subarrays ; Function to return the count of subarrays with positive product ; Total subarrays possible ; Count to subarrays with negative product ; Return the count of subarrays with positive product ; Driver code
using System ; class GFG { static int negProdSubArr ( int [ ] arr , int n ) { int positive = 1 , negative = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) arr [ i ] = 1 ; else arr [ i ] = - 1 ; if ( i > 0 ) arr [ i ] *= arr [ i - 1 ] ; if ( arr [ i ] == 1 ) positive ++ ; else negative ++ ; } return ( positive * negative ) ; } static int posProdSubArr ( int [ ] arr , int n ) { int total = ( n * ( n + 1 ) ) / 2 ; int cntNeg = negProdSubArr ( arr , n ) ; return ( total - cntNeg ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , - 4 , - 3 , 2 , - 5 } ; int n = arr . Length ; Console . WriteLine ( posProdSubArr ( arr , n ) ) ; } }
Find the XOR of first N Prime Numbers | C # implementation of the approach ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Set all multiples of p to non - prime ; Function to return the xor of 1 st N prime numbers ; Count of prime numbers ; XOR of prime numbers ; If the number is prime xor it ; Increment the count ; Get to the next number ; Driver code ; Create the sieve ; Find the xor of 1 st n prime numbers
using System ; class GFG { static int MAX = 10000 ; static bool [ ] prime = new bool [ MAX + 1 ] ; static void SieveOfEratosthenes ( ) { int i ; for ( i = 0 ; i < MAX + 1 ; i ++ ) { prime [ i ] = true ; } prime [ 1 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } } static int xorFirstNPrime ( int n ) { int count = 0 , num = 1 ; int xorVal = 0 ; while ( count < n ) { if ( prime [ num ] ) { xorVal ^= num ; count ++ ; } num ++ ; } return xorVal ; } static public void Main ( ) { SieveOfEratosthenes ( ) ; int n = 4 ; Console . Write ( xorFirstNPrime ( n ) ) ; } }
Sum of all natural numbers from L to R ( for large values of L and R ) | C # implementation of the approach ; Value of inverse modulo 2 with 10 ^ 9 + 7 ; Function to return num % 1000000007 where num is a large number ; Initialize result ; One by one process all the digits of string ' num ' ; Function to return the sum of the longegers from the given range modulo 1000000007 ; a stores the value of L modulo 10 ^ 9 + 7 ; b stores the value of R modulo 10 ^ 9 + 7 ; l stores the sum of natural numbers from 1 to ( a - 1 ) ; r stores the sum of natural numbers from 1 to b ; If the result is negative ; Driver code
using System ; class GFG { static long mod = 1000000007 ; static long inv2 = 500000004 ; static long modulo ( String num ) { long res = 0 ; for ( int i = 0 ; i < num . Length ; i ++ ) res = ( res * 10 + ( long ) num [ i ] - '0' ) % mod ; return res ; } static long findSum ( String L , String R ) { long a , b , l , r , ret ; a = modulo ( L ) ; b = modulo ( R ) ; l = ( ( a * ( a - 1 ) ) % mod * inv2 ) % mod ; r = ( ( b * ( b + 1 ) ) % mod * inv2 ) % mod ; ret = ( r % mod - l % mod ) ; if ( ret < 0 ) ret = ret + mod ; else ret = ret % mod ; return ret ; } public static void Main ( String [ ] args ) { String L = "88949273204" ; String R = "98429729474298592" ; Console . WriteLine ( findSum ( L , R ) ) ; } }
Maximum subsequence sum such that all elements are K distance apart | C # implementation of the approach ; Function to return the maximum subarray sum for the array { a [ i ] , a [ i + k ] , a [ i + 2 k ] , ... } ; Function to return the sum of the maximum required subsequence ; To store the result ; Run a loop from 0 to k ; Find the maximum subarray sum for the array { a [ i ] , a [ i + k ] , a [ i + 2 k ] , ... } ; Return the maximum value ; Driver code
using System ; class GFG { static int maxSubArraySum ( int [ ] a , int n , int k , int i ) { int max_so_far = int . MinValue , max_ending_here = 0 ; while ( i < n ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; i += k ; } return max_so_far ; } static int find ( int [ ] arr , int n , int k ) { int maxSum = 0 ; for ( int i = 0 ; i <= Math . Min ( n , k ) ; i ++ ) { maxSum = Math . Max ( maxSum , maxSubArraySum ( arr , n , k , i ) ) ; } return maxSum ; } public static void Main ( ) { int [ ] arr = { 2 , - 3 , - 1 , - 1 , 2 } ; int n = arr . Length ; int k = 2 ; Console . WriteLine ( find ( arr , n , k ) ) ; } }
Generate N integers satisfying the given conditions | C # implementation of the above approach ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Set all multiples of p to non - prime ; Function to find the first n odd prime numbers ; To store the current count of prime numbers ; Starting with 3 as 2 is an even prime number ; If i is prime ; Print i and increment count ; Driver code ; Create the sieve
using System ; class GFG { static int MAX = 1000000 ; static bool [ ] prime = new bool [ MAX + 1 ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i <= MAX ; i ++ ) prime [ i ] = true ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } } static void solve ( int n ) { int count = 0 ; for ( int i = 3 ; count < n ; i ++ ) { if ( prime [ i ] ) { Console . Write ( i + " ▁ " ) ; count ++ ; } } } public static void Main ( String [ ] args ) { SieveOfEratosthenes ( ) ; int n = 6 ; solve ( n ) ; } }
Probability that a N digit number is palindrome | C # implementation of the approach ; Find the probability that a n digit number is palindrome ; Denominator ; Assign 10 ^ ( floor ( n / 2 ) ) to denominator ; Display the answer ; Driver code
using System ; class GFG { static void solve ( int n ) { int n_2 = n / 2 ; String den ; den = "1" ; while ( n_2 -- > 0 ) den += '0' ; Console . WriteLine ( 1 + " / " + den ) ; } public static void Main ( String [ ] args ) { int N = 5 ; solve ( N ) ; } }
Ways to choose balls such that at least one ball is chosen | C # implementation of the approach ; Function to return the count of ways to choose the balls ; Calculate ( 2 ^ n ) % MOD ; Subtract the only where no ball was chosen ; Driver code
using System ; class GFG { static int MOD = 1000000007 ; static int countWays ( int n ) { int ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) { ans *= 2 ; ans %= MOD ; } return ( ( ans - 1 + MOD ) % MOD ) ; } public static void Main ( String [ ] args ) { int n = 3 ; Console . WriteLine ( countWays ( n ) ) ; } }