codeparrot/xlcost-text-to-code huggingface dataset:
Midpoint ellipse drawing algorithm | C # program for implementing Mid - Point Ellipse Drawing Algorithm ; Initial decision parameter of region 1 ; For region 1 ; Print points based on 4 - way symmetry ; Checking and updating value of decision parameter based on algorithm ; Decision parameter of region 2 ; Plotting points of region 2 ; printing points based on 4 - way symmetry ; Checking and updating parameter value based on algorithm ; Driver code ; To draw a ellipse of major and minor radius 15 , 10 centred at ( 50 , 50 )
using System ; class GFG { static void midptellipse ( double rx , double ry , double xc , double yc ) { double dx , dy , d1 , d2 , x , y ; x = 0 ; y = ry ; d1 = ( ry * ry ) - ( rx * rx * ry ) + ( 0.25f * rx * rx ) ; dx = 2 * ry * ry * x ; dy = 2 * rx * rx * y ; while ( dx < dy ) { Console . WriteLine ( String . Format ( " { 0:0.000000 } " , ( x + xc ) ) + " , ▁ " + String . Format ( " { 0:0.000000 } " , ( y + yc ) ) ) ; Console . WriteLine ( String . Format ( " { 0:0.000000 } " , ( - x + xc ) ) + " , ▁ " + String . Format ( " { 0:0.000000 } " , ( y + yc ) ) ) ; Console . WriteLine ( String . Format ( " { 0:0.000000 } " , ( x + xc ) ) + " , ▁ " + String . Format ( " { 0:0.000000 } " , ( - y + yc ) ) ) ; Console . WriteLine ( String . Format ( " { 0:0.000000 } " , ( - x + xc ) ) + " , ▁ " + String . Format ( " { 0:0.000000 } " , ( - y + yc ) ) ) ; if ( d1 < 0 ) { x ++ ; dx = dx + ( 2 * ry * ry ) ; d1 = d1 + dx + ( ry * ry ) ; } else { x ++ ; y -- ; dx = dx + ( 2 * ry * ry ) ; dy = dy - ( 2 * rx * rx ) ; d1 = d1 + dx - dy + ( ry * ry ) ; } } d2 = ( ( ry * ry ) * ( ( x + 0.5f ) * ( x + 0.5f ) ) ) + ( ( rx * rx ) * ( ( y - 1 ) * ( y - 1 ) ) ) - ( rx * rx * ry * ry ) ; while ( y >= 0 ) { Console . WriteLine ( String . Format ( " { 0:0.000000 } " , ( x + xc ) ) + " , ▁ " + String . Format ( " { 0:0.000000 } " , ( y + yc ) ) ) ; Console . WriteLine ( String . Format ( " { 0:0.000000 } " , ( - x + xc ) ) + " , ▁ " + String . Format ( " { 0:0.000000 } " , ( y + yc ) ) ) ; Console . WriteLine ( String . Format ( " { 0:0.000000 } " , ( x + xc ) ) + " , ▁ " + String . Format ( " { 0:0.000000 } " , ( - y + yc ) ) ) ; Console . WriteLine ( String . Format ( " { 0:0.000000 } " , ( - x + xc ) ) + " , ▁ " + String . Format ( " { 0:0.000000 } " , ( - y + yc ) ) ) ; if ( d2 > 0 ) { y -- ; dy = dy - ( 2 * rx * rx ) ; d2 = d2 + ( rx * rx ) - dy ; } else { y -- ; x ++ ; dx = dx + ( 2 * ry * ry ) ; dy = dy - ( 2 * rx * rx ) ; d2 = d2 + dx - dy + ( rx * rx ) ; } } } static void Main ( ) { midptellipse ( 10 , 15 , 50 , 50 ) ; } }
Program to check if a number is divisible by sum of its digits | C # implementation of above approach ; Function to check if the given number is divisible by sum of its digits ; Find sum of digits ; check if sum of digits divides n ; Driver Code
using System ; class GFG { static String isDivisible ( long n ) { long temp = n ; int sum = 0 ; while ( n != 0 ) { int k = ( int ) n % 10 ; sum += k ; n /= 10 ; } if ( temp % sum == 0 ) return " YES " ; return " NO " ; } public static void Main ( ) { long n = 123 ; Console . WriteLine ( isDivisible ( n ) ) ; } }
Program to check if a number is divisible by sum of its digits | C # implementation of above approach ; Converting integer to String ; Initialising sum to 0 ; Traversing through the String ; Converting character to int ; Comparing number and sum ; Driver Code ; Passing this number to get result function
using System ; public class GFG { static String getResult ( int n ) { String st = String . Join ( " " , n ) ; int sum = 0 ; foreach ( char i in st . ToCharArray ( ) ) { sum = sum + ( int ) i ; } if ( n % sum == 0 ) return " Yes " ; else return " No " ; } public static void Main ( String [ ] args ) { int n = 123 ; Console . WriteLine ( getResult ( n ) ) ; } }
Find the final X and Y when they are Altering under given condition | C # implementation of the approach ; Function to get final value of X and Y ; Following the sequence but by replacing minus with modulo ; Step 1 ; Step 2 ; Step 3 ; Otherwise terminate ; Driver Code ; Get the initial X and Y values ; Find the result
using System ; class GFG { static void alter ( long x , long y ) { while ( true ) { if ( x == 0 y == 0 ) break ; if ( x >= 2 * y ) x = x % ( 2 * y ) ; else if ( y >= 2 * x ) y = y % ( 2 * x ) ; else break ; } Console . WriteLine ( " X ▁ = ▁ " + x + " , ▁ " + " Y ▁ = ▁ " + y ) ; } public static void Main ( ) { long x = 12 , y = 5 ; alter ( x , y ) ; } }
Print all multiplicative primes <= N | C # implementation of the approach ; Function to return the digit product of n ; Function to print all multiplicative primes <= n ; Create a boolean array " prime [ 0 . . n + 1 ] " . 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 ; Update all multiples of p ; If i is prime and its digit sum is also prime i . e . i is a multiplicative prime ; Driver code
class GFG { static int digitProduct ( int n ) { int prod = 1 ; while ( n > 0 ) { prod = prod * ( n % 10 ) ; n = n / 10 ; } return prod ; } static void printMultiplicativePrimes ( int n ) { bool [ ] prime = new bool [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) prime [ i ] = true ; prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] && prime [ digitProduct ( i ) ] ) System . Console . Write ( i + " ▁ " ) ; } } static void Main ( ) { int n = 10 ; printMultiplicativePrimes ( n ) ; } }
Largest number less than or equal to N / 2 which is coprime to N | C # implementation of the above approacdh ; Function to calculate gcd of two number ; Function to check if two numbers are coprime or not ; two numbers are coprime if their gcd is 1 ; Function to find largest integer less than or equal to N / 2 and coprime with N ; Check one by one all numbers less than or equal to N / 2 ; Driver code
using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } static bool coPrime ( int n1 , int n2 ) { if ( gcd ( n1 , n2 ) == 1 ) return true ; else return false ; } static int largestCoprime ( int N ) { int half = ( int ) ( N / 2 ) ; while ( coPrime ( N , half ) == false ) half -- ; return half ; } static void Main ( ) { int n = 50 ; Console . WriteLine ( largestCoprime ( n ) ) ; } }
Largest number less than or equal to N / 2 which is coprime to N | C # implementation of the above approach ; Function to find largest integer less than or equal to N / 2 and is coprime with N ; Handle the case for N = 6 ; Driver code
using System ; class GfG { static int largestCoprime ( int N ) { if ( N == 6 ) return 1 ; else if ( N % 4 == 0 ) return ( N / 2 ) - 1 ; else if ( N % 2 == 0 ) return ( N / 2 ) - 2 ; else return ( ( N - 1 ) / 2 ) ; } public static void Main ( ) { int n = 50 ; Console . WriteLine ( largestCoprime ( n ) ) ; } }
Print all safe primes below N | C # implementation of the approach ; Function to print first n safe primes ; Initialize all entries of integer array as 1. A value in prime [ i ] will finally be 0 if i is Not a prime , else 1 ; 0 and 1 are not primes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; If i is prime ; 2 p + 1 ; If 2 p + 1 is also a prime then set prime [ 2 p + 1 ] = 2 ; i is a safe prime ; Driver code
using System ; class GFG { static void printSafePrimes ( int n ) { int [ ] prime = new int [ n + 1 ] ; for ( int i = 2 ; i <= n ; i ++ ) prime [ i ] = 1 ; prime [ 0 ] = prime [ 1 ] = 0 ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == 1 ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = 0 ; } } for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] != 0 ) { int temp = ( 2 * i ) + 1 ; if ( temp <= n && prime [ temp ] != 0 ) prime [ temp ] = 2 ; } } for ( int i = 5 ; i <= n ; i ++ ) if ( prime [ i ] == 2 ) Console . Write ( i + " ▁ " ) ; } public static void Main ( ) { int n = 20 ; printSafePrimes ( n ) ; } }
Minimum multiplications with { 2 , 3 , 7 } to make two numbers equal | C # implementation of above approach ; Function to find powers of 2 , 3 and 7 in x ; To keep count of each divisor ; To store the result ; Count powers of 2 in x ; Count powers of 3 in x ; Count powers of 7 in x ; Remaining number which is not divisible by 2 , 3 or 7 ; Function to return the minimum number of given operations required to make a and b equal ; a = x * 2 ^ a1 * 3 ^ a2 * 7 ^ a3 va [ 0 ] = a1 va [ 1 ] = a2 va [ 2 ] = a3 va [ 3 ] = x ; Similarly for b ; If a and b cannot be made equal with the given operation . Note that va [ 3 ] and vb [ 3 ] contain remaining numbers after repeated divisions with 2 , 3 and 7. If remaining numbers are not same then we cannot make them equal . ; Minimum number of operations required ; Driver code
using System ; using System . Collections . Generic ; class GFG { static List < int > Divisors ( int x ) { int c = 0 ; List < int > v = new List < int > ( ) ; while ( x % 2 == 0 ) { c ++ ; x /= 2 ; } v . Add ( c ) ; c = 0 ; while ( x % 3 == 0 ) { c ++ ; x /= 3 ; } v . Add ( c ) ; c = 0 ; while ( x % 7 == 0 ) { c ++ ; x /= 7 ; } v . Add ( c ) ; v . Add ( x ) ; return v ; } static int MinOperations ( int a , int b ) { List < int > va = Divisors ( a ) ; List < int > vb = Divisors ( b ) ; if ( va [ 3 ] != vb [ 3 ] ) { return - 1 ; } int minOperations = Math . Abs ( va [ 0 ] - vb [ 0 ] ) + Math . Abs ( va [ 1 ] - vb [ 1 ] ) + Math . Abs ( va [ 2 ] - vb [ 2 ] ) ; return minOperations ; } public static void Main ( String [ ] args ) { int a = 14 , b = 28 ; Console . WriteLine ( MinOperations ( a , b ) ) ; } }
Product of N with its largest odd digit | C # program to find the product of N with its largest odd digit ; Function to return the largest odd digit in n ; If all digits are even then - 1 will be returned ; Last digit from n ; If current digit is odd and > maxOdd ; Remove last digit ; Return the maximum odd digit ; Function to return the product of n with its largest odd digit ; If there are no odd digits in n ; Product of n with its largest odd digit ; Driver code
using System ; class GFG { static int largestOddDigit ( int n ) { int maxOdd = - 1 ; while ( n > 0 ) { int digit = n % 10 ; if ( digit % 2 == 1 && digit > maxOdd ) maxOdd = digit ; n = n / 10 ; } return maxOdd ; } static int getProduct ( int n ) { int maxOdd = largestOddDigit ( n ) ; if ( maxOdd == - 1 ) return - 1 ; return ( n * maxOdd ) ; } public static void Main ( ) { int n = 12345 ; Console . Write ( getProduct ( n ) ) ; } }
Check if a number is perfect square without finding square root | C # program for above approach ; Program to find if x is a perfect square . ; Check if mid is perfect square ; Mid is small -> go right to increase mid ; Mid is large -> to left to decrease mid ; Driver code ; Function Call
using System ; class GFG { static bool isPerfectSquare ( int x ) { long left = 1 , right = x ; while ( left <= right ) { long mid = ( left + right ) / 2 ; if ( mid * mid == x ) { return true ; } if ( mid * mid < x ) { left = mid + 1 ; } else { right = mid - 1 ; } } return false ; } public static void Main ( string [ ] args ) { int x = 2500 ; if ( isPerfectSquare ( x ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Sum of LCM ( 1 , n ) , LCM ( 2 , n ) , LCM ( 3 , n ) , ... , LCM ( n , n ) | C # implementation of the approach ; Euler totient Function ; Function to return the required LCM sum ; Summation of d * ETF ( d ) where d belongs to set of divisors of n ; Driver code
using System ; class GFG { static int n = 1000002 ; static int [ ] phi = new int [ n + 2 ] ; static int [ ] ans = new int [ n + 2 ] ; static void ETF ( ) { for ( int i = 1 ; i <= n ; i ++ ) { phi [ i ] = i ; } for ( int i = 2 ; i <= n ; i ++ ) { if ( phi [ i ] == i ) { phi [ i ] = i - 1 ; for ( int j = 2 * i ; j <= n ; j += i ) { phi [ j ] = ( phi [ j ] * ( i - 1 ) ) / i ; } } } } static int LcmSum ( int m ) { ETF ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = i ; j <= n ; j += i ) { ans [ j ] += ( i * phi [ i ] ) ; } } int answer = ans [ m ] ; answer = ( answer + 1 ) * m ; answer = answer / 2 ; return answer ; } static void Main ( ) { int m = 5 ; Console . WriteLine ( LcmSum ( m ) ) ; } }
Permutations of string such that no two vowels are adjacent | C # program to count permutations of string such that no two vowels are adjacent ; Factorial of a number ; Function to find c ( n , r ) ; Function to count permutations of string such that no two vowels are adjacent ; Finding the frequencies of the characters ; finding the no . of vowels and consonants in given word ; finding places for the vowels ; ways to fill consonants 6 ! / 2 ! ; ways to put vowels 7 C5 x 5 ! ; Driver code
using System ; class GFG { static int factorial ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) fact = fact * i ; return fact ; } static int ncr ( int n , int r ) { return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) ) ; } static int countWays ( String str ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { freq [ i ] = 0 ; } int nvowels = 0 , nconsonants = 0 ; int vplaces , cways , vways ; for ( int i = 0 ; i < str . Length ; i ++ ) ++ freq [ str [ i ] - ' a ' ] ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( i == 0 i == 4 i == 8 i == 14 i == 20 ) nvowels += freq [ i ] ; else nconsonants += freq [ i ] ; } vplaces = nconsonants + 1 ; cways = factorial ( nconsonants ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( i != 0 && i != 4 && i != 8 && i != 14 && i != 20 && freq [ i ] > 1 ) { cways = cways / factorial ( freq [ i ] ) ; } } vways = ncr ( vplaces , nvowels ) * factorial ( nvowels ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( i == 0 i == 4 i == 8 i == 14 i == 20 && freq [ i ] > 1 ) { vways = vways / factorial ( freq [ i ] ) ; } } return cways * vways ; } public static void Main ( ) { String str = " permutation " ; Console . WriteLine ( countWays ( str ) ) ; } }
Pairs from an array that satisfy the given condition | C # implementation of the approach ; Function to return the number of set bits in n ; Function to return the count of required pairs ; Set bits for first element of the pair ; Set bits for second element of the pair ; Set bits of the resultant number which is the XOR of both the elements of the pair ; If the condition is satisfied ; Increment the count ; Return the total count ; Driver code
using System ; class GFG { static int setBits ( int n ) { int count = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } static int countPairs ( int [ ] a , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int setbits_x = setBits ( a [ i ] ) ; for ( int j = i + 1 ; j < n ; j ++ ) { int setbits_y = setBits ( a [ j ] ) ; int setbits_xor_xy = setBits ( a [ i ] ^ a [ j ] ) ; if ( setbits_x + setbits_y == setbits_xor_xy ) count ++ ; } } return count ; } public static void Main ( ) { int [ ] a = { 2 , 3 , 4 , 5 , 6 } ; int n = a . Length ; Console . Write ( countPairs ( a , n ) ) ; } }
Number of array elements derivable from D after performing certain operations | C # program to find the number of array elements which can be derived by perming ( + A , - A , + B , - B ) operations on D ; Function to return gcd of a and b ; Function to Return the number of elements of arr [ ] which can be derived from D by performing ( + A , - A , + B , - B ) ; find the gcd of A and B ; counter stores the number of array elements which can be derived from D ; arr [ i ] can be derived from D only if | arr [ i ] - D | is divisible by gcd of A and B ; Driver Code
using System ; public class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int findPossibleDerivables ( int [ ] arr , int n , int D , int A , int B ) { int gcdAB = gcd ( A , B ) ; int counter = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( Math . Abs ( arr [ i ] - D ) % gcdAB ) == 0 ) { counter ++ ; } } return counter ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 7 , 13 } ; int n = arr . Length ; int D = 5 , A = 4 , B = 2 ; Console . WriteLine ( findPossibleDerivables ( arr , n , D , A , B ) ) ; int [ ] a = { 1 , 2 , 3 } ; n = a . Length ; D = 6 ; A = 3 ; B = 2 ; Console . WriteLine ( findPossibleDerivables ( a , n , D , A , B ) ) ; } }
Find the sum of first N terms of the series 2 * 3 * 5 , 3 * 5 * 7 , 4 * 7 * 9 , ... | C # program to find sum of the first n terms of the given series ; Function to return the sum of the first n terms of the given series ; As described in the approach ; Driver code
using System ; class GFG { static int calSum ( int n ) { return ( n * ( 2 * n * n * n + 12 * n * n + 25 * n + 21 ) ) / 2 ; } static public void Main ( ) { int n = 3 ; Console . WriteLine ( calSum ( n ) ) ; } }
Find elements of array using XOR of consecutive elements | C # program to find the array elements using XOR of consecutive elements ; Function to find the array elements using XOR of consecutive elements ; array to store the original elements ; first element a i . e elements [ 0 ] = a ; To get the next elements we have to calculate xor of previous elements with given xor of 2 consecutive elements . e . g . if a ^ b = k1 so to get b xor a both side . b = k1 ^ a ; Printing the original array elements ; Driver Code
using System ; class GFG { static void getElements ( int a , int [ ] arr , int n ) { int [ ] elements = new int [ n + 1 ] ; elements [ 0 ] = a ; for ( int i = 0 ; i < n ; i ++ ) { elements [ i + 1 ] = arr [ i ] ^ elements [ i ] ; } for ( int i = 0 ; i < n + 1 ; i ++ ) Console . Write ( elements [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 13 , 2 , 6 , 1 } ; int n = arr . Length ; int a = 5 ; getElements ( a , arr , n ) ; } }
Maximum GCD of N integers with given product | C # implementation of above approach ; Function to find maximum GCD of N integers with product P ; map to store prime factors of P ; prime factorization of P ; traverse all prime factors and multiply its 1 / N power to the result ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int maxGCD ( int N , int P ) { int ans = 1 ; Dictionary < int , int > prime_factors = new Dictionary < int , int > ( ) ; for ( int i = 2 ; i * i <= P ; i ++ ) { while ( P % i == 0 ) { if ( ! prime_factors . ContainsKey ( i ) ) prime_factors . Add ( i , 1 ) ; else prime_factors [ i ] = prime_factors [ i ] + 1 ; P /= i ; } } if ( P != 1 ) if ( ! prime_factors . ContainsKey ( P ) ) prime_factors . Add ( P , 1 ) ; else prime_factors [ P ] = prime_factors [ P ] + 1 ; foreach ( KeyValuePair < int , int > me in prime_factors ) { ans *= ( int ) Math . Pow ( me . Key , me . Value / N ) ; } return ans ; } public static void Main ( String [ ] args ) { int N = 3 , P = 24 ; Console . WriteLine ( maxGCD ( N , P ) ) ; } }
Check if the sum of distinct digits of two integers are equal | Function to return the sum of distinct digits of a number ; Take last digit ; If digit has not been used before ; Set digit as used ; Remove last digit ; Function to check whether the sum of distinct digits of two numbers are equal ; Driver code
using System ; public class GFG { static int distinctDigitSum ( int n ) { bool [ ] used = new bool [ 10 ] ; int sum = 0 ; while ( n > 0 ) { int digit = n % 10 ; if ( ! used [ digit ] ) { used [ digit ] = true ; sum += digit ; } n = n / 10 ; } return sum ; } static String checkSum ( int m , int n ) { int sumM = distinctDigitSum ( m ) ; int sumN = distinctDigitSum ( n ) ; if ( sumM == sumN ) return " YES " ; return " NO " ; } static public void Main ( ) { int m = 2452 , n = 9222 ; Console . WriteLine ( checkSum ( m , n ) ) ; } }
All pairs whose xor gives unique prime | C # implementation of the approach ; Function that returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to return the count of valid pairs ; If xor ( a [ i ] , a [ j ] ) is prime and unique ; Driver code
using System ; using System . Collections ; class solution { 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 countPairs ( int [ ] a , int n ) { int count = 0 ; Hashtable m = new Hashtable ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( isPrime ( a [ i ] ^ a [ j ] ) && m [ a [ i ] ^ a [ j ] ] == null ) { m . Add ( ( a [ i ] ^ a [ j ] ) , 1 ) ; count ++ ; } } } return count ; } public static void Main ( ) { int [ ] a = { 10 , 12 , 23 , 45 , 5 , 6 } ; int n = a . Length ; Console . WriteLine ( countPairs ( a , n ) ) ; } }
Sum of element whose prime factors are present in array | C # program to find the sum of the elements of an array whose prime factors are present in the same array ; Stores smallest prime factor for every number ; Function to calculate SPF ( Smallest Prime Factor ) for every number till MAXN ; Marking smallest prime factor for every number to be itself . ; Separately marking spf for every even number as 2 ; If i is prime ; Marking SPF for all numbers divisible by i ; Marking spf [ j ] if it is not previously marked ; Function to return the sum of the elements of an array whose prime factors are present in the same array ; Function call to calculate smallest prime factors of all the numbers upto MAXN ; Create map for each element ; If smallest prime factor of num is present in array ; Each factor of arr [ i ] is present in the array ; Driver Code ; Function call to print required answer
using System ; using System . Collections . Generic ; class GFG { static int MAXN = 1000001 ; static int [ ] spf = new int [ MAXN ] ; static void sieve ( ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } static int sumFactors ( int [ ] arr , int n ) { sieve ( ) ; Dictionary < int , int > map = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < MAXN ; ++ i ) map . Add ( i , 0 ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( map . ContainsKey ( arr [ i ] ) ) { map [ arr [ i ] ] = 1 ; } else { map . Add ( arr [ i ] , 1 ) ; } } int sum = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int num = arr [ i ] ; while ( num != 1 && ( int ) ( map [ spf [ num ] ] ) == 1 ) { num /= spf [ num ] ; } if ( num == 1 ) sum += arr [ i ] ; } return sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 11 , 55 , 25 , 100 } ; int n = arr . Length ; Console . WriteLine ( sumFactors ( arr , n ) ) ; } }
Find nth Hermite number | C # program to find nth Hermite number ; Utility function to calculate double factorial of a number ; Function to return nth Hermite number ; If n is even then return 0 ; If n is odd ; Calculate double factorial of ( n - 1 ) and multiply it with 2 ^ ( n / 2 ) ; If n / 2 is odd then nth Hermite number will be negative ; Return nth Hermite number ; Driver Code ; Print nth Hermite number
using System ; class GFG { static int doubleFactorial ( int n ) { int fact = 1 ; for ( int i = 1 ; i <= n ; i = i + 2 ) { fact = fact * i ; } return fact ; } static int hermiteNumber ( int n ) { if ( n % 2 == 1 ) return 0 ; else { int number = ( int ) ( Math . Pow ( 2 , n / 2 ) ) * doubleFactorial ( n - 1 ) ; if ( ( n / 2 ) % 2 == 1 ) number = number * - 1 ; return number ; } } public static void Main ( ) { int n = 6 ; Console . WriteLine ( hermiteNumber ( n ) ) ; } }