codeparrot/xlcost-text-to-code huggingface dataset:
Bitonic point in the given linked list | C # implementation of the approach ; Node for linked list ; Function to insert a node at the head of the linked list ; Function to return the bitonic of the given linked list ; If list is empty ; If list contains only a single node ; Invalid bitonic sequence ; If current node is the bitonic point ; Get to the next node in the list ; Nodes must be in descending starting from here ; Out of order node ; Get to the next node in the list ; Driver code
using System ; class GFG { public class Node { public int data ; public Node next ; } ; static Node push ( Node head_ref , int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } static int bitonic_point ( Node node ) { if ( node == null ) return - 1 ; if ( node . next == null ) return - 1 ; if ( node . data > node . next . data ) return - 1 ; while ( node . next != null ) { if ( node . data > node . next . data ) break ; node = node . next ; } int bitonicPoint = node . data ; while ( node . next != null ) { if ( node . data < node . next . data ) return - 1 ; node = node . next ; } return bitonicPoint ; } public static void Main ( String [ ] args ) { Node head = null ; head = push ( head , 100 ) ; head = push ( head , 201 ) ; head = push ( head , 399 ) ; head = push ( head , 490 ) ; head = push ( head , 377 ) ; head = push ( head , 291 ) ; head = push ( head , 100 ) ; Console . WriteLine ( bitonic_point ( head ) ) ; } }
Find the minimum number of elements that should be removed to make an array good | C # program to remove minimum elements to make the given array good ; Function to remove minimum elements to make the given array good ; To store count of each subsequence ; Increase the count of subsequence [ 0 ] ; If Previous element subsequence count is greater than zero then increment subsequence count of current element and decrement subsequence count of the previous element . ; Return the required answer ; Driver code ; Function call
using System ; class GFG { static int MinRemove ( int [ ] a , int n , int k ) { int [ ] cnt = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) cnt [ 0 ] ++ ; else if ( cnt [ a [ i ] - 1 ] > 0 ) { cnt [ a [ i ] - 1 ] -- ; cnt [ a [ i ] ] ++ ; } } return n - ( k * cnt [ k - 1 ] ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 0 , 1 , 2 , 3 , 4 , 0 , 1 , 0 , 1 , 2 , 3 , 4 } ; int k = 5 ; int n = a . Length ; Console . WriteLine ( MinRemove ( a , n , k ) ) ; } }
Print first N Mosaic numbers | C # implementation of the approach ; Function to return the nth mosaic number ; Iterate from 2 to the number ; If i is the factor of n ; Find the count where i ^ count is a factor of n ; Divide the number by i ; Increase the count ; Multiply the answer with count and i ; Return the answer ; Function to print first N Mosaic numbers ; Driver code
using System ; class GFG { static int mosaic ( int n ) { int i , ans = 1 ; for ( i = 2 ; i <= n ; i ++ ) { if ( n % i == 0 && n > 0 ) { int count = 0 ; while ( n % i == 0 ) { n /= i ; count ++ ; } ans *= count * i ; } } return ans ; } static void nMosaicNumbers ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) Console . Write ( mosaic ( i ) + " ▁ " ) ; } public static void Main ( ) { int n = 10 ; nMosaicNumbers ( n ) ; } }
Count of nodes which are at a distance X from root and leaves | C # implementation of the approach ; Function to find the count of the required nodes ; Height of the complete binary tree with n nodes ; If X > height then no node can be present at that level ; Corner case ; Maximum total nodes that are possible in complete binary tree with height h ; Nodes at the last level ; To store the count of nodes x dist away from root ; To store the count of nodes x dist away from leaf ; If X = h then print nodes at last level else nodes at Xth level ; 2 ^ X ; Number of left leaf nodes at ( h - 1 ) th level observe that if nodes are not present at last level then there are a / 2 leaf nodes at ( h - 1 ) th level ; If X = h then print leaf nodes at the last h level + leaf nodes at ( h - 1 ) th level ; First calculate nodes for leaves present at height h ; Then calculate nodes for leaves present at height h - 1 ; Add both the resuls ; Driver Code
using System ; class GFG { static void countNodes ( int N , int X ) { int height = ( int ) Math . Floor ( Math . Log ( N ) / Math . Log ( 2 ) ) ; if ( X > height ) { Console . Write ( "0 STRNEWLINE 0" ) ; return ; } if ( N == 1 ) { Console . Write ( "1 STRNEWLINE 1" ) ; return ; } int max_total_nodes = ( 1 << ( height + 1 ) ) - 1 ; int nodes_last_level = ( 1 << height ) - ( max_total_nodes - N ) ; int from_root ; int from_leaf ; if ( X == height ) from_root = nodes_last_level ; else from_root = 1 << X ; int left_leaf_nodes = ( ( 1 << height ) - nodes_last_level ) / 2 ; if ( X == 0 ) { from_leaf = nodes_last_level + left_leaf_nodes ; } else { int i = X ; while ( nodes_last_level > 1 && i > 0 ) { nodes_last_level = ( int ) Math . Ceiling ( ( float ) nodes_last_level / ( float ) 2 ) ; i -- ; } from_leaf = nodes_last_level ; i = X ; while ( left_leaf_nodes > 1 && i > 0 ) { left_leaf_nodes = ( int ) Math . Ceiling ( ( float ) left_leaf_nodes / ( float ) 2 ) ; i -- ; } from_leaf += left_leaf_nodes ; } Console . Write ( from_root + " STRNEWLINE " + from_leaf ) ; } public static void Main ( ) { int N = 38 , X = 3 ; countNodes ( N , X ) ; } }
Number of ways of writing N as a sum of 4 squares | C # implementation of above approach ; Number of ways of writing n as a sum of 4 squares ; sum of odd and even factor ; iterate from 1 to the number ; if i is the factor of n ; if factor is even ; if factor is odd ; n / i is also a factor ; if factor is even ; if factor is odd ; if n is odd ; if n is even ; Driver code
using System ; class GFG { static int sum_of_4_squares ( int n ) { int i , odd = 0 , even = 0 ; for ( i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i % 2 == 0 ) even += i ; else odd += i ; if ( ( n / i ) != i ) { if ( ( n / i ) % 2 == 0 ) even += ( n / i ) ; else odd += ( n / i ) ; } } } if ( n % 2 == 1 ) return 8 * ( odd + even ) ; else return 24 * ( odd ) ; } static public void Main ( ) { int n = 4 ; Console . WriteLine ( sum_of_4_squares ( n ) ) ; } }
Find the Nth Mosaic number | C # implementation of the approach ; Function to return the nth mosaic number ; Iterate from 2 to the number ; If i is the factor of n ; Find the count where i ^ count is a factor of n ; Divide the number by i ; Increase the count ; Multiply the answer with count and i ; Return the answer ; Driver code
using System ; class GFG { static int mosaic ( int n ) { int i , ans = 1 ; for ( i = 2 ; i <= n ; i ++ ) { if ( n % i == 0 && n > 0 ) { int count = 0 ; while ( n % i == 0 ) { n /= i ; count ++ ; } ans *= count * i ; } } return ans ; } static public void Main ( ) { int n = 36 ; Console . WriteLine ( mosaic ( n ) ) ; } }
Seating arrangement of N boys sitting around a round table such that two particular boys sit together | C # implementation of the approach ; Function to return the total count of ways ; Find ( n - 1 ) factorial ; Return ( n - 1 ) ! * 2 ! ; Driver code
using System ; class GFG { static int Total_Ways ( int n ) { int fac = 1 ; for ( int i = 2 ; i <= n - 1 ; i ++ ) { fac = fac * i ; } return ( fac * 2 ) ; } static public void Main ( ) { int n = 5 ; Console . Write ( Total_Ways ( n ) ) ; } }
Find the maximum number of elements divisible by 3 | C # program to find the maximum number of elements divisible by 3 ; Function to find the maximum number of elements divisible by 3 ; To store frequency of each number ; Store modulo value ; Store frequency ; Add numbers with zero modulo to answer ; Find minimum of elements with modulo frequency one and zero ; Add k to the answer ; Remove them from frequency ; Add numbers possible with remaining frequency ; Return the required answer ; Driver code ; Function call
using System ; class GFG { static int MaxNumbers ( int [ ] a , int n ) { int [ ] fre = { 0 , 0 , 0 } ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] %= 3 ; fre [ a [ i ] ] ++ ; } int ans = fre [ 0 ] ; int k = Math . Min ( fre [ 1 ] , fre [ 2 ] ) ; ans += k ; fre [ 1 ] -= k ; fre [ 2 ] -= k ; ans += fre [ 1 ] / 3 + fre [ 2 ] / 3 ; return ans ; } static public void Main ( ) { int [ ] a = { 1 , 4 , 10 , 7 , 11 , 2 , 8 , 5 , 9 } ; int n = a . Length ; Console . WriteLine ( MaxNumbers ( a , n ) ) ; } }
Count pairs with set bits sum equal to K | C # implementation of the approach ; Function to return the count of set bits in n ; Function to return the count of required pairs ; To store the count ; Sum of set bits in both the integers ; If current pair satisfies the given condition ; Driver code
using System ; public class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static int pairs ( int [ ] arr , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int sum = countSetBits ( arr [ i ] ) + countSetBits ( arr [ j ] ) ; if ( sum == k ) count ++ ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; int k = 4 ; Console . WriteLine ( pairs ( arr , n , k ) ) ; } }
Count pairs with set bits sum equal to K | C # implementation of the approach ; Function to return the count of set bits in n ; Function to return the count of required pairs ; To store the count ; Frequency array ; If current pair satisfies the given condition ; ( arr [ i ] , arr [ i ] ) cannot be a valid pair ; Driver code
using System ; class GFG { static int MAX = 32 ; static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static int pairs ( int [ ] arr , int n , int k ) { int count = 0 ; int [ ] f = new int [ MAX + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) f [ countSetBits ( arr [ i ] ) ] ++ ; for ( int i = 0 ; i <= MAX ; i ++ ) { for ( int j = i ; j <= MAX ; j ++ ) { if ( i + j == k ) { if ( i == j ) count += ( ( f [ i ] * ( f [ i ] - 1 ) ) / 2 ) ; else count += ( f [ i ] * f [ j ] ) ; } } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; int k = 4 ; Console . WriteLine ( pairs ( arr , n , k ) ) ; } }
Print Lower Hessenberg matrix of order N | C # implementation of the approach ; Function to print the Lower Hessenberg matrix of order n ; If element is above super - diagonal then print 0 ; Print a random digit for every non - zero element ; Driver code
using System ; class GFG { static void LowerHessenbergMatrix ( int n ) { Random rand = new Random ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( j > i + 1 ) Console . Write ( 0 + " ▁ " ) ; else Console . Write ( rand . Next ( 1 , 10 ) + " ▁ " ) ; } Console . WriteLine ( ) ; } } static public void Main ( ) { int n = 4 ; LowerHessenbergMatrix ( n ) ; } }
Print Upper Hessenberg matrix of order N | C # implementation of the approach ; Function to print the Lower Hessenberg matrix of order n ; If element is above super - diagonal then print 0 ; Print a random digit for every non - zero element ; Driver code
using System ; class GFG { static void UpperHessenbergMatrix ( int n ) { Random rand = new Random ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( i > j + 1 ) Console . Write ( 0 + " ▁ " ) ; else Console . Write ( rand . Next ( 1 , 10 ) + " ▁ " ) ; } Console . WriteLine ( ) ; } } static public void Main ( ) { int n = 4 ; UpperHessenbergMatrix ( n ) ; } }
Find the total number of composite factor for a given number | C # implementation of the approach ; Function to return the count of prime factors of n ; Initialise array with 0 ; Stored i value into an array ; Every non - zero value at a [ i ] denotes that i is a factor of n ; Find if i is prime ; If i is a factor of n and i is not prime ; Driver code
using System ; class GFG { static int composite_factors ( int n ) { int count = 0 ; int i , j ; int [ ] a = new int [ n + 1 ] ; for ( i = 0 ; i < n ; i ++ ) { a [ i ] = 0 ; } for ( i = 1 ; i <= n ; ++ i ) { if ( n % i == 0 ) { a [ i ] = i ; } } for ( i = 2 ; i <= n ; i ++ ) { j = 2 ; int p = 1 ; while ( j < a [ i ] ) { if ( a [ i ] % j == 0 ) { p = 0 ; break ; } j += 1 ; } if ( p == 0 && a [ i ] != 0 ) { count += 1 ; } } return count ; } public static void Main ( ) { int n = 100 ; Console . WriteLine ( composite_factors ( n ) ) ; } }
Sum of all mersenne numbers present in an array | C # implementation of the approach ; Function that returns true if n is a Mersenne number ; Function to return the sum of all the Mersenne numbers from the given array ; To store the required sum ; If current element is a Mersenne number ; Driver code
using System ; class GFG { static bool isMersenne ( int n ) { while ( n != 0 ) { int r = n % 2 ; if ( r == 0 ) return false ; n /= 2 ; } return true ; } static int sumOfMersenne ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 && isMersenne ( arr [ i ] ) ) { sum += arr [ i ] ; } } return sum ; } static public void Main ( ) { int [ ] arr = { 17 , 6 , 7 , 63 , 3 } ; int n = arr . Length ; Console . WriteLine ( sumOfMersenne ( arr , n ) ) ; } }
Sum of all perfect numbers present in an array | C # implementation of the above approach ; Function to return the sum of all the proper factors of n ; f is the factor of n ; Function to return the required sum ; To store the sum ; If current element is non - zero and equal to the sum of proper factors of itself ; Driver code
using System ; class GFG { static int sumOfFactors ( int n ) { int sum = 0 ; for ( int f = 1 ; f <= n / 2 ; f ++ ) { if ( n % f == 0 ) { sum += f ; } } return sum ; } static int getSum ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 && arr [ i ] == sumOfFactors ( arr [ i ] ) ) { sum += arr [ i ] ; } } return sum ; } static public void Main ( ) { int [ ] arr = { 17 , 6 , 10 , 6 , 4 } ; int n = arr . Length ; Console . WriteLine ( getSum ( arr , n ) ) ; } }
Check if two numbers have same number of digits | C # implementation of the approach ; Function that return true if A and B have same number of digits ; Both must be 0 now if they had same lengths ; Driver code
using System ; class GFG { static bool sameLength ( int A , int B ) { while ( ( A > 0 ) && ( B > 0 ) ) { A = A / 10 ; B = B / 10 ; } if ( ( A == 0 ) && ( B == 0 ) ) return true ; return false ; } static public void Main ( ) { int A = 21 , B = 1 ; if ( sameLength ( A , B ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find a way to fill matrix with 1 ' s ▁ and ▁ 0' s in blank positions | C # implementation of the approach ; Function to generate and print the required matrix ; Replace the ' . ' ; Toggle number ; For each row , change the starting number ; Driver code
using System ; class GFG { static int N = 2 ; static int M = 2 ; static void Matrix ( char [ , ] a ) { char ch = '1' ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( a [ i , j ] == ' . ' ) a [ i , j ] = ch ; ch = ( ch == '1' ) ? '0' : '1' ; Console . Write ( a [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; if ( i % 2 == 0 ) ch = '0' ; else ch = '1' ; } } public static void Main ( String [ ] args ) { char [ , ] a = { { ' . ' , ' _ ' } , { ' _ ' , ' . ' } } ; Matrix ( a ) ; } }
Queries to check whether all the elements can be made positive by flipping signs exactly K times | C # implementation of the approach ; To store the count of negative integers ; Check if zero exists ; Function to find the count of negative integers and check if zero exists in the array ; Function that returns true if array elements can be made positive by changing signs exactly k times ; Driver code ; Pre - processing ; Perform queries
using System ; class GFG { static int cnt_neg ; static bool exists_zero ; static void preProcess ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) cnt_neg ++ ; if ( arr [ i ] == 0 ) exists_zero = true ; } } static bool isPossible ( int k ) { if ( ! exists_zero ) { if ( k >= cnt_neg && ( k - cnt_neg ) % 2 == 0 ) return true ; else return false ; } else { if ( k >= cnt_neg ) return true ; else return false ; } } static public void Main ( ) { int [ ] arr = { - 1 , 2 , - 3 , 4 , 5 } ; int n = arr . Length ; preProcess ( arr , n ) ; int [ ] queries = { 1 , 2 , 3 , 4 } ; int q = arr . Length ; for ( int i = 0 ; i < q ; i ++ ) { if ( isPossible ( queries [ i ] ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } } }
Sum of XOR of all sub | C # implementation of the above approach ; Sum of XOR of all K length sub - array of an array ; If the length of the array is less than k ; Array that will store xor values of subarray from 1 to i ; Traverse through the array ; If i is greater than zero , store xor of all the elements from 0 to i ; If it is the first element ; If i is greater than k ; Xor of values from 0 to i ; Now to find subarray of length k that ends at i , xor sum with x [ i - k ] ; Add the xor of elements from i - k + 1 to i ; Return the resultant sum ; ; Driver code
using System ; class GFG { static int FindXorSum ( int [ ] arr , int k , int n ) { if ( n < k ) return 0 ; int [ ] x = new int [ n ] ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i > 0 ) x [ i ] = x [ i - 1 ] ^ arr [ i ] ; else x [ i ] = arr [ i ] ; if ( i >= k - 1 ) { int sum = 0 ; sum = x [ i ] ; if ( i - k > - 1 ) sum ^= x [ i - k ] ; result += sum ; } } return result ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = 4 , k = 2 ; Console . WriteLine ( FindXorSum ( arr , k , n ) ) ; } }
Count the number of digits of palindrome numbers in an array | C # implementation of the approach ; Function to return the reverse of n ; Function that returns true if n is a palindrome ; Function to return the count of digits of n ; Function to return the count of digits in all the palindromic numbers of arr [ ] ; If arr [ i ] is a one digit number or it is a palindrome ; Driver code
using System ; class GFG { static int reverse ( int n ) { int rev = 0 ; while ( n > 0 ) { int d = n % 10 ; rev = rev * 10 + d ; n = n / 10 ; } return rev ; } static bool isPalin ( int n ) { return ( n == reverse ( n ) ) ; } static int countDigits ( int n ) { int c = 0 ; while ( n > 0 ) { n = n / 10 ; c ++ ; } return c ; } static int countPalinDigits ( int [ ] arr , int n ) { int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 10 || isPalin ( arr [ i ] ) ) { s += countDigits ( arr [ i ] ) ; } } return s ; } public static void Main ( ) { int [ ] arr = { 121 , 56 , 434 } ; int n = arr . Length ; Console . WriteLine ( countPalinDigits ( arr , n ) ) ; } }
Sum of all palindrome numbers present in an Array | C # program to calculate the sum of all palindromic numbers in array ; Function to reverse a number n ; Function to check if a number n is palindrome ; If n is equal to the reverse of n it is a palindrome ; Function to calculate sum of all array elements which are palindrome ; summation of all palindrome numbers present in array ; Driver Code
using System ; class GFG { static int reverse ( int n ) { int d = 0 , s = 0 ; while ( n > 0 ) { d = n % 10 ; s = s * 10 + d ; n = n / 10 ; } return s ; } static bool isPalin ( int n ) { return n == reverse ( n ) ; } static int sumOfArray ( int [ ] arr , int n ) { int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] > 10 ) && isPalin ( arr [ i ] ) ) { s += arr [ i ] ; } } return s ; } public static void Main ( String [ ] args ) { int n = 6 ; int [ ] arr = { 12 , 313 , 11 , 44 , 9 , 1 } ; Console . WriteLine ( sumOfArray ( arr , n ) ) ; } }
Queries for the difference between the count of composite and prime numbers in a given range | C # implementation of the approach ; Function to update prime [ ] such prime [ i ] stores the count of prime numbers <= i ; Initialization ; 0 and 1 are not primes ; Mark composite numbers as false and prime numbers as true ; Update prime [ ] such that prime [ i ] will store the count of all the prime numbers <= i ; Function to return the absolute difference between the number of primes and the number of composite numbers in the range [ l , r ] ; Total elements in the range ; Count of primes in the range [ l , r ] ; Count of composite numbers in the range [ l , r ] ; Return the sbsolute difference ; Driver code ; Perform queries
using System ; class GFG { static int MAX = 1000000 ; static int [ ] prime = new int [ MAX + 1 ] ; static void updatePrimes ( ) { for ( int i = 2 ; i <= MAX ; i ++ ) { prime [ i ] = 1 ; } prime [ 0 ] = prime [ 1 ] = 0 ; for ( int i = 2 ; i * i <= MAX ; i ++ ) { if ( prime [ i ] == 1 ) { for ( int j = i * i ; j <= MAX ; j += i ) { prime [ j ] = 0 ; } } } for ( int i = 1 ; i <= MAX ; i ++ ) { prime [ i ] += prime [ i - 1 ] ; } } static int getDifference ( int l , int r ) { int total = r - l + 1 ; int primes = prime [ r ] - prime [ l - 1 ] ; int composites = total - primes ; return ( Math . Abs ( primes - composites ) ) ; } public static void Main ( ) { int [ , ] queries = { { 1 , 10 } , { 5 , 30 } } ; int q = queries . GetLength ( 0 ) ; updatePrimes ( ) ; for ( int i = 0 ; i < q ; i ++ ) Console . WriteLine ( getDifference ( queries [ i , 0 ] , queries [ i , 1 ] ) ) ; } }
Check if the given Prufer sequence is valid or not | C # implementation of the approach ; Function that returns true if given Prufer sequence is valid ; Iterate in the Prufer sequence ; If out of range ; Driver code
using System ; class GFG { static Boolean isValidSeq ( int [ ] a , int n ) { int nodes = n + 2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] < 1 a [ i ] > nodes ) return false ; } return true ; } public static void Main ( String [ ] args ) { int [ ] a = { 4 , 1 , 3 , 4 } ; int n = a . Length ; if ( isValidSeq ( a , n ) ) Console . WriteLine ( " Valid " ) ; else Console . WriteLine ( " Invalid " ) ; } }
Program to Calculate e ^ x by Recursion ( using Taylor Series ) | C # implementation of the approach ; Recursive Function with static variables p and f ; Termination condition ; Recursive call ; Update the power of x ; Factorial ; Driver code
using System ; class GFG { static double p = 1 , f = 1 ; static double e ( int x , int n ) { double r ; if ( n == 0 ) return 1 ; r = e ( x , n - 1 ) ; p = p * x ; f = f * n ; return ( r + p / f ) ; } static void Main ( ) { int x = 4 , n = 15 ; Console . WriteLine ( Math . Round ( e ( x , n ) , 6 ) ) ; } }
Check if each element of the given array is the product of exactly K prime numbers | C # program to check if each element of the given array is a product of exactly K prime factors ; initialise the global sieve array ; Function to generate Sieve ; NOTE : k value is necessarily more than 1 hence , 0 , 1 and any prime number cannot be represented as product of two or more prime numbers ; Function to check if each number of array satisfies the given condition ; Driver Code ; first construct the sieve
using System ; class GFG { static int MAX = 1000000 ; static int [ ] Sieve = new int [ MAX + 1 ] ; static void constructSieve ( ) { for ( int i = 2 ; i <= MAX ; i ++ ) { if ( Sieve [ i ] == 0 ) { for ( int j = 2 * i ; j <= MAX ; j += i ) { int temp = j ; while ( temp > 1 && temp % i == 0 ) { Sieve [ j ] ++ ; temp = temp / i ; } } } } } static void checkElements ( int [ ] A , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( Sieve [ A [ i ] ] == k ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " No " ) ; } } } public static void Main ( ) { constructSieve ( ) ; int k = 3 ; int [ ] A = { 12 , 36 , 42 , 72 } ; int n = A . Length ; checkElements ( A , n , k ) ; } }