codeparrot/xlcost-text-to-code huggingface dataset:
Nth term of a recurrence relation generated by two given arrays | C # program for the above approach ; Declare T [ , ] as global matrix ; Result matrix ; Function to multiply two matrices ; Create an auxiliary matrix to store elements of the multiplication matrix ; Iterate over range [ 0 , K ] ; Update temp [ i , j ] ; Update the readonly matrix ; Function to multiply two matrices ; Create an auxiliary matrix to store elements of the multiplication matrix ; Iterate over range [ 0 , K ] ; Update temp [ i , j ] ; Update the readonly matrix ; Function to calculate matrix ^ n using binary exponentaion ; Initialize result matrix and unity matrix ; Function to calculate nth term of general recurrence ; Fill T [ , ] with appropriate value ; Function Call to calculate T ^ n ; Calculate nth term as first element of F * ( T ^ n ) ; Print the result ; Driver Code ; Given Initial terms ; Given coefficients ; Given K ; Given N ; Function Call
using System ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int [ , ] T = new int [ 2000 , 2000 ] ; static int [ , ] result = new int [ 2000 , 2000 ] ; static void mul_2 ( int K ) { int [ , ] temp = new int [ K + 1 , K + 1 ] ; for ( int i = 1 ; i <= K ; i ++ ) { for ( int j = 1 ; j <= K ; j ++ ) { for ( int k = 1 ; k <= K ; k ++ ) { temp [ i , j ] = ( temp [ i , j ] + ( T [ i , k ] * T [ k , j ] ) % mod ) % mod ; } } } for ( int i = 1 ; i <= K ; i ++ ) { for ( int j = 1 ; j <= K ; j ++ ) { T [ i , j ] = temp [ i , j ] ; } } } static void mul_1 ( int K ) { int [ , ] temp = new int [ K + 1 , K + 1 ] ; for ( int i = 1 ; i <= K ; i ++ ) { for ( int j = 1 ; j <= K ; j ++ ) { for ( int k = 1 ; k <= K ; k ++ ) { temp [ i , j ] = ( temp [ i , j ] + ( result [ i , k ] * T [ k , j ] ) % mod ) % mod ; } } } for ( int i = 1 ; i <= K ; i ++ ) { for ( int j = 1 ; j <= K ; j ++ ) { result [ i , j ] = temp [ i , j ] ; } } } static void matrix_pow ( int K , int n ) { for ( int i = 1 ; i <= K ; i ++ ) { for ( int j = 1 ; j <= K ; j ++ ) { if ( i == j ) result [ i , j ] = 1 ; } } while ( n > 0 ) { if ( n % 2 == 1 ) mul_1 ( K ) ; mul_2 ( K ) ; n /= 2 ; } } static int NthTerm ( int [ ] F , int [ ] C , int K , int n ) { for ( int i = 1 ; i <= K ; i ++ ) T [ i , K ] = C [ K - i ] ; for ( int i = 1 ; i <= K ; i ++ ) T [ i + 1 , i ] = 1 ; matrix_pow ( K , n ) ; int answer = 0 ; for ( int i = 1 ; i <= K ; i ++ ) { answer += F [ i - 1 ] * result [ i , 1 ] ; } Console . Write ( answer + " STRNEWLINE " ) ; return 0 ; } public static void Main ( String [ ] args ) { int [ ] F = { 1 , 2 , 3 } ; int [ ] C = { 1 , 1 , 1 } ; int K = 3 ; int N = 10 ; NthTerm ( F , C , K , N ) ; } }
Calculate Root Mean Kth power of all array elements | C # program for the above approach ; Function to find the Nth root ; Instantiate random number generator ; Initially guessing random numberbetween 0 and 9 ; Smaller eps for more accuracy ; Initialize difference between the two roots by Integer . MAX_VALUE ; xK denotes current value of x ; Iterate until desired accuracy is reached ; Find the current value from previous value by newton 's method ; Function to calculate the Root Mean kth power of array elements ; Calculate sum of kth power ; Calculate Mean ; Calculate kth Root of mean ; Driver Code ; Function call
using System ; class GFG { static double nthRoot ( int A , int N ) { Random rand = new Random ( ) ; double xPre = ( rand . Next ( ) * 10 ) % 10 ; double eps = 1e-3 ; double delX = Int32 . MaxValue ; double xK = 0 ; while ( delX > eps ) { xK = ( ( N - 1.0 ) * xPre + ( double ) A / Math . Pow ( xPre , N - 1 ) ) / ( double ) N ; delX = Math . Abs ( xK - xPre ) ; xPre = xK ; } return xK ; } static float RMNValue ( int [ ] arr , int n , int k ) { int Nth = 0 ; float mean = 0 , root = 0 ; for ( int i = 0 ; i < n ; i ++ ) { Nth += ( int ) Math . Pow ( arr [ i ] , k ) ; } mean = ( Nth / ( float ) ( n ) ) ; root = ( float ) nthRoot ( ( int ) mean , k ) ; return root ; } public static void Main ( ) { int [ ] arr = { 10 , 4 , 6 , 8 } ; int N = arr . Length ; int K = 3 ; Console . Write ( RMNValue ( arr , N , K ) ) ; } }
Add two numbers represented by Stacks | C # program to implement the above approach ; Function to return the stack that contains the sum of two numbers ; Calculate the sum of the top elements of both the stacks ; Push the sum into the stack ; Store the carry ; Pop the top elements ; If N1 is not empty ; If N2 is not empty ; If carry remains ; Reverse the stack . so that most significant digit is at the bottom of the stack ; Function to display the resultamt stack ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static Stack < int > PushStack ( Stack < int > N1 , Stack < int > N2 ) { Stack < int > res = new Stack < int > ( ) ; int sum = 0 , rem = 0 ; while ( N1 . Count != 0 && N2 . Count != 0 ) { sum = ( rem + N1 . Peek ( ) + N2 . Peek ( ) ) ; res . Push ( ( int ) sum % 10 ) ; rem = sum / 10 ; N1 . Pop ( ) ; N2 . Pop ( ) ; } while ( N1 . Count != 0 ) { sum = ( rem + N1 . Peek ( ) ) ; res . Push ( sum % 10 ) ; rem = sum / 10 ; N1 . Pop ( ) ; } while ( N2 . Count != 0 ) { sum = ( rem + N2 . Peek ( ) ) ; res . Push ( sum % 10 ) ; rem = sum / 10 ; N2 . Pop ( ) ; } while ( rem > 0 ) { res . Push ( rem ) ; rem /= 10 ; } while ( res . Count != 0 ) { N1 . Push ( res . Peek ( ) ) ; res . Pop ( ) ; } res = N1 ; return res ; } static void display ( Stack < int > res ) { int N = res . Count ; String s = " " ; while ( res . Count != 0 ) { s = String . Join ( " " , res . Peek ( ) ) + s ; res . Pop ( ) ; } Console . Write ( s + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { Stack < int > N1 = new Stack < int > ( ) ; N1 . Push ( 5 ) ; N1 . Push ( 8 ) ; N1 . Push ( 7 ) ; N1 . Push ( 4 ) ; Stack < int > N2 = new Stack < int > ( ) ; N2 . Push ( 2 ) ; N2 . Push ( 1 ) ; N2 . Push ( 3 ) ; Stack < int > res = PushStack ( N1 , N2 ) ; display ( res ) ; } }
Check if a number has an odd count of odd divisors and even count of even divisors | C # implementation of the above approach ; Function to check if the number is a perfect square ; Find floating point value of square root of x . ; If square root is an integer ; Function to check if count of even divisors is even and count of odd divisors is odd ; Driver code
using System ; class GFG { static bool isPerfectSquare ( double x ) { double sr = Math . Sqrt ( x ) ; return ( ( sr - Math . Floor ( sr ) ) == 0 ) ; } static void checkFactors ( int x ) { if ( isPerfectSquare ( x ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( String [ ] args ) { int N = 36 ; checkFactors ( N ) ; } }
Queries to find the XOR of an Array after replacing all occurrences of X by Y | C # program to implement the above approach ; Stores the bitwise XOR of array elements ; Function to find the total xor ; Loop to find the xor of all the elements ; Function to find the XOR after replacing all occurrences of X by Y for Q queries ; Remove contribution of X from total_xor ; Adding contribution of Y to total_xor ; Print total_xor ; Driver Code
using System ; class GFG { static int total_xor ; static void initialize_xor ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { total_xor = total_xor ^ arr [ i ] ; } } static void find_xor ( int X , int Y ) { total_xor = total_xor ^ X ; total_xor = total_xor ^ Y ; Console . Write ( total_xor + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 7 , 8 , 9 } ; int n = arr . Length ; initialize_xor ( arr , n ) ; int [ , ] Q = { { 5 , 6 } , { 8 , 1 } } ; for ( int i = 0 ; i < Q . GetLength ( 0 ) ; i ++ ) { find_xor ( Q [ i , 0 ] , Q [ i , 1 ] ) ; } } }
Check whether a given number is an ugly number or not | C # implementation to check if a number is ugly number ; Function to check the ugly number ; Base Cases ; Condition to check if a number is divide by 2 , 3 , or 5 ; Driver Code
using System ; class GFG { static int isUgly ( int n ) { if ( n == 1 ) return 1 ; if ( n <= 0 ) return 0 ; if ( n % 2 == 0 ) { return ( isUgly ( n / 2 ) ) ; } if ( n % 3 == 0 ) { return ( isUgly ( n / 3 ) ) ; } if ( n % 5 == 0 ) { return ( isUgly ( n / 5 ) ) ; } return 0 ; } public static void Main ( String [ ] args ) { int no = isUgly ( 14 ) ; if ( no == 1 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Maximum possible GCD for a pair of integers with sum N | C # program to find the maximum possible GCD of any pair with sum N ; Function to find the required GCD value ; If i is a factor of N ; Return the largest factor possible ; If N is a prime number ; Driver Code
using System ; class GFG { static int maxGCD ( int N ) { for ( int i = 2 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { return N / i ; } } return 1 ; } public static void Main ( String [ ] args ) { int N = 33 ; Console . WriteLine ( " Maximum ▁ Possible ▁ GCD ▁ " + " value ▁ is ▁ : ▁ " + maxGCD ( N ) ) ; } }
Minimize K whose XOR with given array elements leaves array unchanged | C # program for the above approach ; Function to find the minimum value of K in given range ; Declare a set ; Initialize count variable ; Iterate in range [ 1 , 1024 ] ; counter set as 0 ; Iterating through the Set ; Check if the XOR calculated is present in the Set ; If the value of Bitwise XOR inside the given set then increment count ; Check if the value of count is equal to the size of set ; Return minimum value of K ; If no such K is found ; Driver code ; Given array ; Function call
using System ; using System . Collections . Generic ; class GFG { static int min_value ( int [ ] arr , int N ) { int X , K ; HashSet < int > S = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { S . Add ( arr [ i ] ) ; } int count = 0 ; for ( int i = 1 ; i <= 1024 ; i ++ ) { count = 0 ; foreach ( int it in S ) { X = ( ( i it ) - ( i & it ) ) ; if ( S . Contains ( X ) ) { count ++ ; } } if ( count == S . Count ) { K = i ; return K ; } } return - 1 ; } static void Main ( ) { int [ ] arr = { 1 , 0 , 3 , 3 , 0 , 2 } ; int N = arr . Length ; Console . Write ( min_value ( arr , N ) ) ; } }
Junction Numbers | C # program for above approach ; Function to find the sum Of digits of N ; To store sum of N and sumOfdigitsof ( N ) ; extracting digit ; Function to check Junction numbers ; To store count of ways n can be represented as i + SOD ( i ) ; Driver Code ; Function Call
using System ; class GFG { static int sum ( int n ) { int sum = 0 ; while ( n != 0 ) { int r = n % 10 ; sum = sum + r ; n = n / 10 ; } return sum ; } static bool isJunction ( int n ) { int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i + sum ( i ) == n ) count ++ ; } return count >= 2 ; } public static void Main ( ) { int N = 111 ; if ( isJunction ( N ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Pointer | C # program for above approach ; Function to find the product of digits of a number N ; Function that returns true if n is prime else returns false ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to return the smallest prime number greater than N ; Base case ; Loop continuously until isPrime returns true for a number greater than n ; Function to check Pointer - Prime numbers ; Driver Code ; Given Number N ; Function Call
using System ; class GFG { static int digProduct ( int n ) { int product = 1 ; while ( n != 0 ) { product = product * ( n % 10 ) ; n = n / 10 ; } return product ; } static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static int nextPrime ( int N ) { if ( N <= 1 ) return 2 ; int prime = N ; bool found = false ; while ( ! found ) { prime ++ ; if ( isPrime ( prime ) ) found = true ; } return prime ; } static bool isPointerPrime ( int n ) { if ( isPrime ( n ) && ( n + digProduct ( n ) == nextPrime ( n ) ) ) return true ; else return false ; } public static void Main ( ) { int N = 23 ; if ( isPointerPrime ( N ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Perfect Digital Invariants number | C # program for the above approach ; Function to calculate x raised to the power y ; Function to check whether the given number is Perfect Digital Invariant number or not ; For each digit in temp ; If satisfies Perfect Digital Invariant condition ; If sum exceeds n , then not possible ; Driver Code ; Given number N ; Function call
using System ; class GFG { static int power ( int x , int y ) { if ( y == 0 ) { return 1 ; } if ( y % 2 == 0 ) { return ( power ( x , y / 2 ) * power ( x , y / 2 ) ) ; } return ( x * power ( x , y / 2 ) * power ( x , y / 2 ) ) ; } static bool isPerfectDigitalInvariant ( int x ) { for ( int fixed_power = 1 ; ; fixed_power ++ ) { int temp = x , sum = 0 ; while ( temp > 0 ) { int r = temp % 10 ; sum += power ( r , fixed_power ) ; temp = temp / 10 ; } if ( sum == x ) { return true ; } if ( sum > x ) { return false ; } } } public static void Main ( String [ ] args ) { int N = 4150 ; if ( isPerfectDigitalInvariant ( N ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Brilliant Numbers | C # implementation for the above approach ; Function to generate all prime numbers less than n ; Initialize all entries of boolean array as true . A value in isPrime [ i ] will finally be false if i is Not a prime ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to return the number of digits in a number ; Function to check if N is a Brilliant number ; Generating primes using Sieve ; Traversing all numbers to find first pair ; Driver Code ; Given Number ; Function Call
using System ; class GFG { static void SieveOfEratosthenes ( int n , bool [ ] isPrime ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) isPrime [ i ] = false ; } } } static int countDigit ( int n ) { int count = 0 ; while ( n != 0 ) { n = n / 10 ; ++ count ; } return count ; } static bool isBrilliant ( int n ) { bool [ ] isPrime = new bool [ n + 1 ] ; SieveOfEratosthenes ( n , isPrime ) ; for ( int i = 2 ; i < n ; i ++ ) { int x = n / i ; if ( isPrime [ i ] && isPrime [ x ] && ( x * i ) == n ) { if ( countDigit ( i ) == countDigit ( x ) ) return true ; } } return false ; } public static void Main ( ) { int n = 1711 ; if ( isBrilliant ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Icosikaienneagonal Number | C # implementation for above approach ; Function to Find the Nth icosikaienneagonal Number ; Driver Code
using System ; class GFG { static int icosikaienneagonalNum ( int n ) { return ( 27 * n * n - 25 * n ) / 2 ; } public static void Main ( ) { int n = 3 ; Console . Write ( icosikaienneagonalNum ( n ) ) ; } }
Find the largest contiguous pair sum in given Array | C # program to find the a contiguous pair from the which has the largest sum ; Function to find and return the largest sum contiguous pair ; Stores the contiguous pair ; Initialize maximum sum ; Compare sum of pair with max_sum ; Insert the pair ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { public static ArrayList largestSumpair ( int [ ] arr , int n ) { ArrayList pair = new ArrayList ( ) ; int max_sum = int . MinValue , i ; for ( i = 1 ; i < n ; i ++ ) { if ( max_sum < ( arr [ i ] + arr [ i - 1 ] ) ) { max_sum = arr [ i ] + arr [ i - 1 ] ; if ( pair . Count == 0 ) { pair . Add ( arr [ i - 1 ] ) ; pair . Add ( arr [ i ] ) ; } else { pair [ 0 ] = arr [ i - 1 ] ; pair [ 1 ] = arr [ i ] ; } } } return pair ; } public static void Main ( string [ ] args ) { int [ ] arr = { 11 , - 5 , 9 , - 3 , 2 } ; int N = arr . Length ; ArrayList pair = new ArrayList ( ) ; pair = largestSumpair ( arr , N ) ; foreach ( int it in pair ) { Console . Write ( it + " ▁ " ) ; } } }
Sum of minimum value of x and y satisfying the equation ax + by = c | C # program for the above approach ; x and y store solution of equation ax + by = g ; Euclidean Algorithm ; store_gcd returns the gcd of a and b ; Function to find any possible solution ; Condition if solution does not exists ; Adjusting the sign of x0 and y0 ; Function to shift solution ; Shifting to obtain another solution ; Function to find minimum value of x and y ; g is the gcd of a and b ; Store sign of a and b ; If x is less than 0 , then shift solution ; If y is less than 0 , then shift solution ; Find intersection such that both x and y are positive ; miny is value of y corresponding to minx ; Returns minimum value of x + y ; Driver Code ; Given a , b , and c ; Function call
using System ; class GFG { public static int x = 0 , y = 0 , x1 = 0 , y1 = 0 ; public static int x0 = 0 , y0 = 0 , g = 0 ; public static int gcd ( int a , int b ) { if ( b == 0 ) { x = 1 ; y = 0 ; return a ; } int store_gcd = gcd ( b , a % b ) ; x = y1 ; y = x1 - y1 * ( a / b ) ; return store_gcd ; } public static int possible_solution ( int a , int b , int c ) { g = gcd ( Math . Abs ( a ) , Math . Abs ( b ) ) ; if ( c % g != 0 ) { return 0 ; } x0 *= c / g ; y0 *= c / g ; if ( a < 0 ) x0 *= - 1 ; if ( b < 0 ) y0 *= - 1 ; return 1 ; } public static void shift_solution ( int a , int b , int shift_var ) { x += shift_var * b ; y -= shift_var * a ; } public static int find_min_sum ( int a , int b , int c ) { int x = 0 , y = 0 , g = 0 ; if ( possible_solution ( a , b , c ) == 0 ) return - 1 ; if ( g != 0 ) { a /= g ; b /= g ; } int sign_a = a > 0 ? + 1 : - 1 ; int sign_b = b > 0 ? + 1 : - 1 ; shift_solution ( a , b , - x / b ) ; if ( x < 0 ) shift_solution ( a , b , sign_b ) ; int minx1 = x ; shift_solution ( a , b , y / a ) ; if ( y < 0 ) shift_solution ( a , b , - sign_a ) ; int minx2 = x ; if ( minx2 > x ) { int temp = minx2 ; minx2 = x ; x = temp ; } int minx = Math . Max ( minx1 , minx2 ) ; if ( minx > x ) return - 1 ; int miny = ( c - a * x ) / b ; return ( miny + minx ) ; } public static void Main ( String [ ] args ) { int a = 2 , b = 2 , c = 0 ; Console . Write ( find_min_sum ( a , b , c ) ) ; } }
Super | C # implementation to check if N is a super - d number ; Function to check if N is a super - d number ; Utility function ; Driver Code
using System ; class GFG { static bool isSuperdNum ( int n ) { for ( int d = 2 ; d < 10 ; d ++ ) { String subString = newString ( d ) ; if ( String . Join ( " " , ( d * Math . Pow ( n , d ) ) ) . Contains ( subString ) ) return true ; } return false ; } private static String newString ( int d ) { String ans = " " ; for ( int i = 0 ; i < d ; i ++ ) { ans += String . Join ( " " , d ) ; } return ans ; } public static void Main ( String [ ] args ) { int n = 261 ; if ( isSuperdNum ( n ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Second hexagonal numbers | C # implementation to find N - th term in the series ; Function to find N - th term in the series ; Driver code
using System ; class GFG { static void findNthTerm ( int n ) { Console . Write ( n * ( 2 * n + 1 ) ) ; } public static void Main ( ) { int N = 4 ; findNthTerm ( N ) ; } }
Apocalyptic Number | C # program for the above approach ; Function to check if a number N is Apocalyptic ; Driver Code ; Given Number N ; Function Call
using System ; class GFG { static bool isApocalyptic ( int n ) { if ( Math . Pow ( 2 , n ) . ToString ( ) . Contains ( "666" ) ) return true ; return false ; } static public void Main ( ) { int N = 157 ; if ( isApocalyptic ( N ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Duffinian Numbers | C # program for the above approach ; Recursive function to return gcd of a and b ; Function to calculate the sum of all divisors of a given number ; Sum of divisors ; Find all divisors of num ; If ' i ' is divisor of ' n ' ; If both divisors are same then add it once ; Add 1 and n to result as above loop considers proper divisors greater than 1. ; Function to check if n is an Duffinian number ; Calculate the sum of divisors ; If number is prime return false ; Find the gcd of n and sum of divisors of n ; Returns true if N and sumDivisors are relatively prime ; Driver code ; Given number ; Function call
using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int divSum ( int n ) { int result = 0 ; for ( int i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == ( n / i ) ) result += i ; else result += ( i + n / i ) ; } } return ( result + n + 1 ) ; } static bool isDuffinian ( int n ) { int sumDivisors = divSum ( n ) ; if ( sumDivisors == n + 1 ) return false ; int hcf = gcd ( n , sumDivisors ) ; return hcf == 1 ; } public static void Main ( string [ ] args ) { int n = 36 ; if ( isDuffinian ( n ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Kummer Numbers | C # program to find Kummer number upto n terms ; vector to store all prime less than and equal to 10 ^ 6 ; Function for the Sieve of Sundaram . This function stores all prime numbers less than MAX in primes ; In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x . Since we want primes smaller than MAX , we reduce MAX to half This array is used to separate numbers of the form i + j + 2 ij from others where 1 <= i <= j ; Main logic of Sundaram . Mark all numbers which do not generate prime number by doing 2 * i + 1 ; Since 2 is a prime number ; Print other primes . Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; Function to calculate nth Kummer number ; Multiply first n primes ; return nth Kummer number ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int MAX = 1000000 ; static List < int > primes = new List < int > ( ) ; static void sieveSundaram ( ) { bool [ ] marked = new bool [ MAX / 2 + 1 ] ; for ( int i = 1 ; i <= ( Math . Sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j += 2 * i + 1 ) marked [ j ] = true ; primes . Add ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked [ i ] == false ) primes . Add ( 2 * i + 1 ) ; } static int calculateKummer ( int n ) { int result = 1 ; for ( int i = 0 ; i < n ; i ++ ) result = result * primes [ i ] ; return - 1 + result ; } public static void Main ( String [ ] args ) { int n = 5 ; sieveSundaram ( ) ; for ( int i = 1 ; i <= n ; i ++ ) Console . Write ( calculateKummer ( i ) + " , ▁ " ) ; } }
Check if given number contains a digit which is the average of all other digits | C # program to check whether a given number contains a digit which is the average of all other digits ; Function which checks if a digits exists in n which is the average of all other digits ; Calculate sum of digits in n ; Store the digits ; Increase the count of digits in n ; If average of all digits is an integer ; Otherwise ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void check ( int n ) { HashSet < int > digits = new HashSet < int > ( ) ; int temp = n ; int sum = 0 ; int count = 0 ; while ( temp > 0 ) { sum += temp % 10 ; digits . Add ( temp % 10 ) ; count ++ ; temp = temp / 10 ; } if ( sum % count == 0 && digits . Contains ( sum / count ) ) Console . Write ( " Yes " + " STRNEWLINE " ) ; else Console . Write ( " No " + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int n = 42644 ; check ( n ) ; } }
Count of lexicographically smaller characters on right |  ; function to count the smaller characters at the right of index i ; store the length of String ; for each index initialize count as zero ; increment the count if characters are smaller than at ith index ; print the count of characters smaller than the index i ; Driver code ; input String
using System ; class GFG { static void countSmaller ( String str ) { int n = str . Length ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( str [ j ] < str [ i ] ) { cnt += 1 ; } } Console . Write ( cnt + " ▁ " ) ; } } public static void Main ( String [ ] args ) { String str = " edcba " ; countSmaller ( str ) ; } }