codeparrot/xlcost-text-to-code huggingface dataset:
Find nth Fibonacci number using Golden ratio | C # program to find n - th Fibonacci number ; Approximate value of golden ratio ; Fibonacci numbers upto n = 5 ; Function to find nth Fibonacci number ; Fibonacci numbers for n < 6 ; Else start counting from 5 th term ; Driver code
using System ; class GFG { static double PHI = 1.6180339 ; static int [ ] f = { 0 , 1 , 1 , 2 , 3 , 5 } ; static int fib ( int n ) { if ( n < 6 ) return f [ n ] ; int t = 5 ; int fn = 5 ; while ( t < n ) { fn = ( int ) Math . Round ( fn * PHI ) ; t ++ ; } return fn ; } public static void Main ( ) { int n = 9 ; Console . WriteLine ( n + " th ▁ Fibonacci " + " ▁ Number ▁ = ▁ " + fib ( n ) ) ; } }
Euler Method for solving differential equation | C # program to find approximation of an ordinary differential equation using euler method ; Consider a differential equation dy / dx = ( x + y + xy ) ; Function for Euler formula ; Iterating till the point at which we need approximation ; Printing approximation ; Driver program ; Initial Values ; Value of x at which we need approximation
using System ; class GFG { static float func ( float x , float y ) { return ( x + y + x * y ) ; } static void euler ( float x0 , float y , float h , float x ) { while ( x0 < x ) { y = y + h * func ( x0 , y ) ; x0 = x0 + h ; } Console . WriteLine ( " Approximate ▁ solution ▁ at ▁ x ▁ = ▁ " + x + " ▁ is ▁ " + y ) ; } public static void Main ( ) { float x0 = 0 ; float y0 = 1 ; float h = 0.025f ; float x = 0.1f ; euler ( x0 , y0 , h , x ) ; } }
Find x and y satisfying ax + by = n | C # program to find solution of ax + by = n ; function to find the solution ; traverse for all possible values ; check if it is satisfying the equation ; Driver code
using System ; class GfG { static void solution ( int a , int b , int n ) { for ( int i = 0 ; i * a <= n ; i ++ ) { if ( ( n - ( i * a ) ) % b == 0 ) { Console . Write ( " x ▁ = ▁ " + i + " , ▁ y ▁ = ▁ " + ( n - ( i * a ) ) / b ) ; return ; } } Console . Write ( " No ▁ solution " ) ; } public static void Main ( ) { int a = 2 , b = 3 , n = 7 ; solution ( a , b , n ) ; } }
Sum of Binomial coefficients | C # Program to find sum of Binomial Coefficient . ; Returns value of Binomial Coefficient Sum which is 2 raised to power n . ; Driver program to test above function
using System ; class GFG { static int binomialCoeffSum ( int n ) { return ( 1 << n ) ; } static public void Main ( ) { int n = 4 ; Console . WriteLine ( binomialCoeffSum ( n ) ) ; } }
Program to compute division upto n decimal places | C # program to compute division upto n decimal places . ; Base cases ; Since n <= 0 , don 't compute after  the decimal ; Handling negative numbers ; Integral division ; Now one by print digits after dot using school division method . ; Driver code
using System ; class Eulerian { public static void precisionCompute ( int x , int y , int n ) { if ( y == 0 ) { Console . WriteLine ( " Infinite " ) ; return ; } if ( x == 0 ) { Console . WriteLine ( "0" ) ; return ; } if ( n <= 0 ) { Console . WriteLine ( x / y ) ; return ; } if ( ( ( x > 0 ) && ( y < 0 ) ) || ( ( x < 0 ) && ( y > 0 ) ) ) { Console . WriteLine ( " - " ) ; x = x > 0 ? x : - x ; y = y > 0 ? y : - y ; } int d = x / y ; for ( int i = 0 ; i <= n ; i ++ ) { Console . Write ( d ) ; x = x - ( y * d ) ; if ( x == 0 ) break ; x = x * 10 ; d = x / y ; if ( i == 0 ) Console . Write ( " . " ) ; } } public static void Main ( ) { int x = 22 , y = 7 , n = 15 ; precisionCompute ( x , y , n ) ; } }
Program to determine the quadrant of the cartesian plane | C # program to check quadrant ; Function to check quadrant ; Driver Code ; Function Calling
using System ; class GFG { static void quadrant ( int x , int y ) { if ( x > 0 && y > 0 ) Console . WriteLine ( " lies ▁ in ▁ First ▁ quadrant " ) ; else if ( x < 0 && y > 0 ) Console . WriteLine ( " lies ▁ in ▁ Second ▁ quadrant " ) ; else if ( x < 0 && y < 0 ) Console . WriteLine ( " lies ▁ in ▁ Third ▁ quadrant " ) ; else if ( x > 0 && y < 0 ) Console . WriteLine ( " lies ▁ in ▁ Fourth ▁ quadrant " ) ; else if ( x == 0 && y > 0 ) Console . WriteLine ( " lies ▁ at ▁ positive ▁ y ▁ axis " ) ; else if ( x == 0 && y < 0 ) Console . WriteLine ( " lies ▁ at ▁ negative ▁ y ▁ axis " ) ; else if ( y == 0 && x < 0 ) Console . WriteLine ( " lies ▁ at ▁ negative ▁ x ▁ axis " ) ; else if ( y == 0 && x > 0 ) Console . WriteLine ( " lies ▁ at ▁ positive ▁ x ▁ axis " ) ; else Console . WriteLine ( " lies ▁ at ▁ origin " ) ; } public static void Main ( ) { int x = 1 , y = 1 ; quadrant ( x , y ) ; } }
Check if a number is Full Prime | C # program for checking of full prime ; function to check digits ; check all digits are prime or not ; check if digits are prime or not ; To check if n is prime or not ; check for all factors ; To check if n is Full Prime ; The order is important here for efficiency ; Driver code
using System ; class Prime { public static bool checkDigits ( int n ) { while ( n > 0 ) { int dig = n % 10 ; if ( dig != 2 && dig != 3 && dig != 5 && dig != 7 ) return false ; n /= 10 ; } return true ; } public static bool prime ( int n ) { if ( n == 1 ) return false ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } public static bool isFullPrime ( int n ) { return ( checkDigits ( n ) && prime ( n ) ) ; } public static void Main ( ) { int n = 53 ; if ( isFullPrime ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Narcissistic number | C # program for checking of Narcissistic number ; Traversing through the string ; Since ascii value of numbers starts from 48 so we subtract it from sum ; Converting string to integer ; Comparing number and sum ; Driver Code
using System ; class GFG { static string getResult ( string st ) { int sum = 0 ; int length = st . Length ; for ( int i = 0 ; i < length ; i ++ ) { sum = sum + ( int ) Math . Pow ( st [ i ] - '0' , length ) ; } int number = int . Parse ( st ) ; if ( number == sum ) return " yes " ; else return " no " ; } public static void Main ( string [ ] args ) { string st = "153" ; Console . Write ( getResult ( st ) ) ; } }
SchrÃ ¶ derâ €“ Hipparchus number | A memoization based optimized C # program to find n - th Schroder - Hipparchus number ; Driver code
using System ; class GFG { static int MAX = 500 ; static int nthSHN ( int n , int [ ] dp ) { if ( n == 1 n == 2 ) return dp [ n ] = 1 ; if ( dp [ n ] != - 1 ) return dp [ n ] ; return dp [ n ] = ( ( 6 * n - 9 ) * nthSHN ( n - 1 , dp ) - ( n - 3 ) * nthSHN ( n - 2 , dp ) ) / n ; } public static void Main ( ) { int n = 6 ; int [ ] dp = new int [ MAX ] ; for ( int i = 0 ; i < dp . Length ; i ++ ) dp [ i ] = - 1 ; Console . Write ( nthSHN ( n , dp ) ) ; } }
Sum of first n even numbers | C # implementation to find sum of first n even numbers ; function to find sum of first n even numbers ; sum of first n even numbers ; next even number ; required sum ; driver function
using System ; public class GfG { static int evenSum ( int n ) { int curr = 2 , sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { sum += curr ; curr += 2 ; } return sum ; } public static void Main ( ) { int n = 20 ; Console . WriteLine ( " Sum ▁ of ▁ first ▁ " + n + " ▁ Even ▁ numbers ▁ is : ▁ " + evenSum ( n ) ) ; } }
Sum of first n even numbers | C # implementation to find sum of first n even numbers ' ; function to find sum of first n even numbers ; required sum ; driver function
using System ; public class GfG { static int evenSum ( int n ) { return ( n * ( n + 1 ) ) ; } public static void Main ( ) { int n = 20 ; Console . WriteLine ( " Sum ▁ of ▁ first ▁ " + n + " ▁ Even ▁ numbers ▁ is : ▁ " + evenSum ( n ) ) ; } }
Program to Convert Km / hr to miles / hr and vice versa | C # program for the conversion kmph to mph and vice versa ; Function to convert kmph to mph ; Function to convert mph to kmph ; Driver code to check the above function
using System ; class GFG { static double kmphTOmph ( double kmph ) { return 0.6214 * kmph ; } static double mphTOkmph ( double mph ) { return mph * 1.60934 ; } public static void Main ( ) { double kmph = 150 ; double mph = 100 ; Console . WriteLine ( " speed ▁ in ▁ miles / hr ▁ is ▁ " + kmphTOmph ( kmph ) ) ; Console . WriteLine ( " speed ▁ in ▁ km / hr ▁ is ▁ " + mphTOkmph ( mph ) ) ; } }
Number of GP ( Geometric Progression ) subsequences of size 3 | C # program to count GP subsequences of size 3. ; Returns count of G . P . subsequences with length 3 and common ratio r ; Hashing to maintain left and right array elements to the main count ; Stores the answer ; Traverse through the elements ; Keep the count in the hash ; Traverse through all elements and find out the number of elements as k1 * k2 ; Keep the count of left and right elements left is a [ i ] / r and right a [ i ] * r ; If the current element is divisible by k , count elements in left hash . ; Decrease the count in right hash ; Number of right elements ; Calculate the answer ; left count of a [ i ] ; Returns answer ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static long subsequences ( int [ ] a , int n , int r ) { Dictionary < int , int > left = new Dictionary < int , int > ( ) , right = new Dictionary < int , int > ( ) ; long ans = - 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( right . ContainsKey ( a [ i ] ) ) right [ a [ i ] ] = right [ a [ i ] ] + 1 ; else right . Add ( a [ i ] , 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { long c1 = 0 , c2 = 0 ; if ( a [ i ] % r == 0 ) if ( left . ContainsKey ( a [ i ] / r ) ) c1 = right [ a [ i ] / r ] ; else c1 = 0 ; if ( right . ContainsKey ( a [ i ] ) ) right [ a [ i ] ] = right [ a [ i ] ] ; else right . Add ( a [ i ] , - 1 ) ; if ( right . ContainsKey ( a [ i ] * r ) ) c2 = right [ a [ i ] * r ] ; else c2 = 0 ; ans += ( c1 * c2 ) ; if ( left . ContainsKey ( a [ i ] ) ) left [ a [ i ] ] = 0 ; else left . Add ( a [ i ] , 1 ) ; } return ans - 1 ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 6 , 2 , 3 , 6 , 9 , 18 , 3 , 9 } ; int n = a . Length ; int r = 3 ; Console . WriteLine ( subsequences ( a , n , r ) ) ; } }
Find element in array that divides all array elements | C # program to find such number in the array that all array elements are divisible by it ; Returns GCD of two numbers ; Function to return the desired number if exists ; Find GCD of array ; Check if GCD is present in array ; Driver Code
using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int findNumber ( int [ ] arr , int n ) { int ans = arr [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) ans = gcd ( ans , arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == ans ) return ans ; return - 1 ; } public static void Main ( ) { int [ ] arr = { 2 , 2 , 4 } ; int n = arr . Length ; Console . WriteLine ( findNumber ( arr , n ) ) ; } }
Special prime numbers | C # program to check whether there exist at least k or not in range [ 2. . n ] ; Generating all the prime numbers from 2 to n . ; If a prime number is Special prime number , then we increments the value of k . ; If at least k Special prime numbers are present , then we return 1. else we return 0 from outside of the outer loop . ; Driver function
using System ; using System . Collections ; class GFG { static ArrayList primes = new ArrayList ( ) ; static void SieveofEratosthenes ( int n ) { bool [ ] visited = new bool [ n * n + 2 ] ; for ( int i = 2 ; i <= n + 1 ; i ++ ) if ( ! visited [ i ] ) { for ( int j = i * i ; j <= n + 1 ; j += i ) visited [ j ] = true ; primes . Add ( i ) ; } } static bool specialPrimeNumbers ( int n , int k ) { SieveofEratosthenes ( n ) ; int count = 0 ; for ( int i = 0 ; i < primes . Count ; i ++ ) { for ( int j = 0 ; j < i - 1 ; j ++ ) { if ( ( int ) primes [ j ] + ( int ) primes [ j + 1 ] + 1 == ( int ) primes [ i ] ) { count ++ ; break ; } } if ( count == k ) return true ; } return false ; } public static void Main ( ) { int n = 27 , k = 2 ; if ( specialPrimeNumbers ( n , k ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Prime factors of a big number | C # program to print prime factors and their powers . ; function to calculate all the prime factors and count of every prime factor ; count the number of times 2 divides ; equivalent to n = n / 2 ; ; if 2 divides it ; check for all the possible numbers that can divide it ; if n at the end is a prime number . ; Driver Code
using System ; public class GFG { static void factorize ( long n ) { int count = 0 ; while ( ! ( n % 2 > 0 ) ) { n >>= 1 ; count ++ ; } if ( count > 0 ) Console . WriteLine ( "2" + " ▁ " + count ) ; for ( long i = 3 ; i <= ( long ) Math . Sqrt ( n ) ; i += 2 ) { count = 0 ; while ( n % i == 0 ) { count ++ ; n = n / i ; } if ( count > 0 ) Console . WriteLine ( i + " ▁ " + count ) ; } if ( n > 2 ) Console . WriteLine ( n + " ▁ " + "1" ) ; } static public void Main ( ) { long n = 1000000000000000000 ; factorize ( n ) ; } }
Minimum gcd operations to make all array elements one | C # program to find minimum GCD operations to make all array elements one . ; __gcd function ; Function to count number of moves . ; Counting Number of ones . ; If there is a one ; Find smallest subarray with GCD equals to one . ; to calculate GCD ; Not Possible ; Final answer ; Driver code
using System ; class GFG { static int __gcd ( int a , int b ) { if ( a == 0 ) return b ; return __gcd ( b % a , a ) ; } static int minimumMoves ( int [ ] A , int N ) { int one = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( A [ i ] == 1 ) one ++ ; if ( one != 0 ) return N - one ; int minimum = int . MaxValue ; for ( int i = 0 ; i < N ; i ++ ) { int g = A [ i ] ; for ( int j = i + 1 ; j < N ; j ++ ) { g = __gcd ( A [ j ] , g ) ; if ( g == 1 ) { minimum = Math . Min ( minimum , j - i ) ; break ; } } } if ( minimum == int . MaxValue ) return - 1 ; else return N + minimum - 1 ; } public static void Main ( ) { int [ ] A = { 2 , 4 , 3 , 9 } ; int N = A . Length ; Console . WriteLine ( minimumMoves ( A , N ) ) ; } }
Given N and Standard Deviation , find N elements | C # program to find n elements ; function to print series of n elements ; if S . D . is 0 then print all elements as 0. ; print n 0 's ; if S . D . is even ; print - SD , + SD , - SD , + SD ; if odd ; convert n to a float integer ; print one element to be 0 ; print ( n - 1 ) elements as xi derived from the formula ; driver function
using System ; public class GfG { public static void series ( int n , int d ) { if ( d == 0 ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( "0" ) ; Console . WriteLine ( ) ; return ; } if ( n % 2 == 0 ) { for ( int i = 1 ; i <= n ; i ++ ) { Console . Write ( Math . Pow ( - 1 , i ) * d + " ▁ " ) ; } Console . WriteLine ( ) ; } else { float m = n ; float r = ( m / ( m - 1 ) ) ; float g = ( float ) ( d * ( float ) ( Math . Sqrt ( r ) ) ) ; Console . Write ( "0 ▁ " ) ; for ( int i = 1 ; i < n ; i ++ ) { Console . Write ( Math . Pow ( - 1 , i ) * g + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( ) { int n = 3 , d = 3 ; series ( n , d ) ; } }
Total no of 1 's in numbers | C # code to count the frequency of 1 in numbers less than or equal to the given number . ; Driver Code
using System ; class GFG { static int countDigitOne ( int n ) { int countr = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { string str = i . ToString ( ) ; countr += str . Split ( "1" ) . Length - 1 ; } return countr ; } public static void Main ( ) { int n = 13 ; Console . WriteLine ( countDigitOne ( n ) ) ; n = 131 ; Console . WriteLine ( countDigitOne ( n ) ) ; n = 159 ; Console . WriteLine ( countDigitOne ( n ) ) ; } }
Exponential Squaring ( Fast Modulo Multiplication ) | C # program to compute exponential value using ( 2 ^ k ) - ary method . ; prime modulo value ; for cases where exponent is not an even value ; Driver Code
using System ; class GFG { static long N = 1000000007L ; static long exponentiation ( long bas , long exp ) { long t = 1L ; while ( exp > 0 ) { if ( exp % 2 != 0 ) t = ( t * bas ) % N ; bas = ( bas * bas ) % N ; exp /= 2 ; } return t % N ; } public static void Main ( ) { long bas = 5 ; long exp = 100000 ; long modulo = exponentiation ( bas , exp ) ; Console . WriteLine ( modulo ) ; } }
GCD of factorials of two numbers | C # program to find GCD of factorial of two numbers . ; Driver program to test above functions
using System ; public class GFG { static int factorial ( int x ) { if ( x <= 1 ) return 1 ; int res = 2 ; for ( int i = 3 ; i <= x ; i ++ ) res = res * i ; return res ; } static int gcdOfFactorial ( int m , int n ) { int min = m < n ? m : n ; return factorial ( min ) ; } public static void Main ( ) { int m = 5 , n = 9 ; Console . WriteLine ( gcdOfFactorial ( m , n ) ) ; } }
Recursive sum of digits of a number is prime or not | C # code to check if recursive sum of digits is prime or not . ; Function for recursive digit sum ; function to check if prime or not the single digit ; calls function which returns sum till single digit ; checking prime ; Driver code
using System ; class GFG { static int recDigSum ( int n ) { if ( n == 0 ) return 0 ; else { if ( n % 9 == 0 ) return 9 ; else return n % 9 ; } } static void check ( int n ) { n = recDigSum ( n ) ; if ( n == 2 n == 3 n == 5 n == 7 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( ) { int n = 5602 ; check ( n ) ; } }
Find n | C # program to find the value at n - th place in the given sequence ; Definition of findNumber function ; Finding x from equation n = x ( x + 1 ) / 2 + 1 ; Base of current block ; Value of n - th element ; Driver code
using System ; class GFG { static int findNumber ( int n ) { int x = ( int ) Math . Floor ( ( - 1 + Math . Sqrt ( 1 + 8 * n - 8 ) ) / 2 ) ; int Base = ( x * ( x + 1 ) ) / 2 + 1 ; return n - Base + 1 ; } public static void Main ( ) { int n = 55 ; Console . WriteLine ( findNumber ( n ) ) ; } }
Program for weighted mean of natural numbers . | Program to find weighted mean of first n natural numbers using formula . ; Returns weighted mean assuming for numbers { 1 , 2 , . . n } and weights { 1 , 2 , . . n } ; Driver program to test the function .
using System ; public class GFG { static int weightedMean ( int n ) { return ( 2 * n + 1 ) / 3 ; } static public void Main ( ) { int n = 10 ; Console . WriteLine ( weightedMean ( n ) ) ; } }
Divide every element of one array by other array elements | C # program to find quotient of array elements ; Function to calculate the quotient of every element of the array ; Calculate the product of all elements ; To calculate the quotient of every array element ; Driver code
using System ; class GFG { static void calculate ( int [ ] a , int [ ] b , int n , int m ) { int mul = 1 ; for ( int i = 0 ; i < m ; i ++ ) if ( b [ i ] != 0 ) mul = mul * b [ i ] ; for ( int i = 0 ; i < n ; i ++ ) { int x = ( int ) Math . Floor ( ( double ) ( a [ i ] / mul ) ) ; Console . Write ( x + " ▁ " ) ; } } public static void Main ( ) { int [ ] a = { 5 , 100 , 8 } ; int [ ] b = { 2 , 3 } ; int n = a . Length ; int m = b . Length ; calculate ( a , b , n , m ) ; } }
Largest power of k in n ! ( factorial ) where k may not be prime | C # program to find the largest power of k that divides n ! ; To find the power of a prime p in factorial N ; calculating Math . Floor ( n / r ) and adding to the count ; increasing the power of p from 1 to 2 to 3 and so on ; returns all the prime factors of k ; vector to store all the prime factors along with their number of occurrence in factorization of k ; Returns largest power of k that divides n ! ; calculating minimum power of all the prime factors of k ; Driver code
using System ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int findPowerOfP ( int n , int p ) { int count = 0 ; int r = p ; while ( r <= n ) { count += ( n / r ) ; r = r * p ; } return count ; } static List < pair > primeFactorsofK ( int k ) { List < pair > ans = new List < pair > ( ) ; for ( int i = 2 ; k != 1 ; i ++ ) { if ( k % i == 0 ) { int count = 0 ; while ( k % i == 0 ) { k = k / i ; count ++ ; } ans . Add ( new pair ( i , count ) ) ; } } return ans ; } static int largestPowerOfK ( int n , int k ) { List < pair > vec = new List < pair > ( ) ; vec = primeFactorsofK ( k ) ; int ans = int . MaxValue ; for ( int i = 0 ; i < vec . Count ; i ++ ) ans = Math . Min ( ans , findPowerOfP ( n , vec [ i ] . first ) / vec [ i ] . second ) ; return ans ; } public static void Main ( String [ ] args ) { Console . Write ( largestPowerOfK ( 7 , 2 ) + " STRNEWLINE " ) ; Console . Write ( largestPowerOfK ( 10 , 9 ) + " STRNEWLINE " ) ; } }
Minimum number of bombs | C # program to find number of bombings required to kill all aliens . ; function to print where to shoot ; no . of bombs required ; bomb all the even positions ; bomb all the odd positions ; bomb all the even positions again ; Driver code
using System ; class GFG { static void print ( int n ) { Console . WriteLine ( n + n / 2 ) ; for ( int i = 2 ; i <= n ; i += 2 ) Console . Write ( i + " ▁ " ) ; for ( int i = 1 ; i <= n ; i += 2 ) Console . Write ( i + " ▁ " ) ; for ( int i = 2 ; i <= n ; i += 2 ) Console . Write ( i + " ▁ " ) ; } public static void Main ( ) { int n = 3 ; print ( n ) ; } }
LCM of digits of a given number | C # program to find LCM of digits of a number ; define lcm function ; If at any point LCM become 0. return it ; Driver Code
class GFG { static int lcm_fun ( int a , int b ) { if ( b == 0 ) return a ; return lcm_fun ( b , a % b ) ; } static int digitLCM ( int n ) { int lcm = 1 ; while ( n > 0 ) { lcm = ( n % 10 * lcm ) / lcm_fun ( n % 10 , lcm ) ; if ( lcm == 0 ) return 0 ; n = n / 10 ; } return lcm ; } public static void Main ( ) { int n = 397 ; System . Console . WriteLine ( digitLCM ( n ) ) ; } }
Secretary Problem ( A Optimal Stopping Problem ) | C # Program to test 1 / e law for Secretary Problem ; To find closest integer of num . ; Finds best candidate using n / e rule . candidate [ ] represents talents of n candidates . ; Calculating sample size for benchmarking . ; Finding best candidate in sample size ; Finding the first best candidate that is better than benchmark set . ; Driver Code ; n = 8 candidates and candidate array contains talents of n candidate where the largest number means highest talented candidate . ; generating random numbers between 1 to 8 for talent of candidate
using System ; class GFG { static double e = 2.71828 ; static int roundNo ( float num ) { return ( int ) ( num < 0 ? num - 0.5 : num + 0.5 ) ; } static void printBestCandidate ( int [ ] candidate , int n ) { int sample_size = roundNo ( ( float ) ( n / e ) ) ; Console . WriteLine ( " STRNEWLINE STRNEWLINE Sample ▁ size ▁ is ▁ " + sample_size ) ; int best = 0 ; for ( int i = 1 ; i < sample_size ; i ++ ) if ( candidate [ i ] > candidate [ best ] ) best = i ; for ( int i = sample_size ; i < n ; i ++ ) if ( candidate [ i ] >= candidate [ best ] ) { best = i ; break ; } if ( best >= sample_size ) Console . WriteLine ( " found is " ( best + 1 ) + " ▁ with ▁ talent ▁ " + candidate [ best ] ) ; else Console . Write ( " Couldn ' t ▁ find ▁ a ▁ best ▁ candidate STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int n = 8 ; int [ ] candidate = new int [ n ] ; Random rand = new Random ( ) ; for ( int i = 0 ; i < n ; i ++ ) candidate [ i ] = 1 + rand . Next ( 1 , 8 ) ; Console . Write ( " Candidate ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( i + 1 + " ▁ " ) ; Console . WriteLine ( ) ; Console . Write ( " Talents ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( candidate [ i ] + " ▁ " ) ; printBestCandidate ( candidate , n ) ; } }