codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of given moves required to make N divisible by 25 | C # implementation of the approach ; Function to return the minimum number of moves required to make n divisible by 25 ; Convert number into string ; To store required answer ; Length of the string ; To check all possible pairs ; Make a duplicate string ; Number of swaps required to place ith digit in last position ; Number of swaps required to place jth digit in 2 nd last position ; Find first non zero digit ; Place first non zero digit in the first position ; Convert string to number ; If this number is divisible by 25 then cur is one of the possible answer ; If not possible ; Driver code
using System ; class GFG { static int minMoves ( int n ) { string s = n . ToString ( ) ; int ans = Int32 . MaxValue ; int len = s . Length ; for ( int i = 0 ; i < len ; ++ i ) { for ( int j = 0 ; j < len ; ++ j ) { if ( i == j ) continue ; char [ ] t = s . ToCharArray ( ) ; int cur = 0 ; for ( int k = i ; k < len - 1 ; ++ k ) { swap ( t , k , k + 1 ) ; ++ cur ; } for ( int k = j - ( ( j > i ) ? 1 : 0 ) ; k < len - 2 ; ++ k ) { swap ( t , k , k + 1 ) ; ++ cur ; } int pos = - 1 ; for ( int k = 0 ; k < len ; ++ k ) { if ( t [ k ] != '0' ) { pos = k ; break ; } } for ( int k = pos ; k > 0 ; -- k ) { swap ( t , k , k - 1 ) ; ++ cur ; } int nn = Convert . ToInt32 ( new String ( t ) ) ; if ( nn % 25 == 0 ) ans = Math . Min ( ans , cur ) ; } } if ( ans == Int32 . MaxValue ) return - 1 ; return ans ; } static void swap ( char [ ] t , int i , int j ) { char temp = t [ i ] ; t [ i ] = t [ j ] ; t [ j ] = temp ; } static void Main ( ) { int n = 509201 ; Console . WriteLine ( minMoves ( n ) ) ; } }
Maximum positive integer divisible by C and is in the range [ A , B ] | C # implementation of the above approach ; Function to return the required number ; If b % c = 0 then b is the required number ; Else get the maximum multiple of c smaller than b ; Driver code
using System ; class GFG { static int getMaxNum ( int a , int b , int c ) { if ( b % c == 0 ) return b ; int x = ( ( b / c ) * c ) ; if ( x >= a && x <= b ) return x ; else return - 1 ; } public static void Main ( ) { int a = 2 , b = 10 , c = 3 ; Console . WriteLine ( getMaxNum ( a , b , c ) ) ; } }
Count of pairs ( x , y ) in an array such that x < y | C # implementation of the approach ; Function to return the number of pairs ( x , y ) such that x < y ; Length of the array ; Calculate the number of valid pairs ; Return the count of valid pairs ; Driver code
using System ; class GFG { static int getPairs ( int [ ] a ) { int n = a . Length ; int count = ( n * ( n - 1 ) ) / 2 ; return count ; } public static void Main ( ) { int [ ] a = { 2 , 4 , 3 , 1 } ; Console . Write ( getPairs ( a ) ) ; } }
Count the total number of squares that can be visited by Bishop in one move | C # implementation of above approach ; Function to return the count of total positions the Bishop can visit in a single move ; Count top left squares ; Count bottom right squares ; Count top right squares ; Count bottom left squares ; Return total count ; Driver code ; Bishop 's Position
using System ; class GFG { static int countSquares ( int row , int column ) { int topLeft = Math . Min ( row , column ) - 1 ; int bottomRight = 8 - Math . Max ( row , column ) ; int topRight = Math . Min ( row , 9 - column ) - 1 ; int bottomLeft = 8 - Math . Max ( row , 9 - column ) ; return ( topLeft + topRight + bottomRight + bottomLeft ) ; } public static void Main ( ) { int row = 4 , column = 4 ; Console . WriteLine ( countSquares ( row , column ) ) ; } }
Check whether Bishop can take down Pawn or not | C # implementation of above approach ; Function that return true if the Bishop can take down the pawn ; If pawn is at angle 45 or 225 degree from bishop 's Position ; If pawn is at angle 135 or 315 degree from bishop 's Position ; Driver code ; Bishop 's Position ; Pawn 's Position
using System ; class GFG { static bool canTakeDown ( int bishopX , int bishopY , int pawnX , int pawnY ) { if ( pawnX - bishopX == pawnY - bishopY ) return true ; else if ( - pawnX + bishopX == pawnY - bishopY ) return true ; else return false ; } public static void Main ( ) { int bishopX = 5 , bishopY = 5 ; int pawnX = 1 , pawnY = 1 ; if ( canTakeDown ( bishopX , bishopY , pawnX , pawnY ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find maximum operations to reduce N to 1 | C # program to find maximum number moves possible ; To store number of prime factors of each number ; Function to find number of prime factors of each number ; if i is a prime number ; increase value by one from it 's preveious multiple ; make prefix sum this will be helpful for multiple test cases ; Driver Code ; Generate primeFactors array ; required answer
using System ; class GFG { static int N = 1000005 ; static int [ ] primeFactors = new int [ N ] ; static void findPrimeFactors ( ) { for ( int i = 2 ; i < N ; i ++ ) if ( primeFactors [ i ] == 0 ) for ( int j = i ; j < N ; j += i ) primeFactors [ j ] = primeFactors [ j / i ] + 1 ; for ( int i = 1 ; i < N ; i ++ ) primeFactors [ i ] += primeFactors [ i - 1 ] ; } static public void Main ( ) { findPrimeFactors ( ) ; int a = 6 , b = 3 ; Console . WriteLine ( primeFactors [ a ] - primeFactors [ b ] ) ; } }
Smallest integer with digit sum M and multiple of N | C # implementation of the above approach ; Function to return digit sum ; Function to find out the smallest integer ; Start of the iterator ( Smallest multiple of n ) ; Driver code
using System ; class GFG { static int digitSum ( int n ) { int ans = 0 ; while ( n != 0 ) { ans += n % 10 ; n /= 10 ; } return ans ; } static int findInt ( int n , int m ) { int minDigit = ( int ) Math . Floor ( ( double ) ( m / 9 ) ) ; int start = ( int ) Math . Pow ( 10 , minDigit ) - ( int ) Math . Pow ( 10 , minDigit ) % n ; while ( start < int . MaxValue ) { if ( digitSum ( start ) == m ) return start ; else start += n ; } return - 1 ; } static public void Main ( ) { int n = 13 , m = 32 ; Console . WriteLine ( findInt ( n , m ) ) ; } }
Maximum sum after repeatedly dividing N by a divisor | C # implementation of the above approach ; Function to find the smallest divisor ; Function to find the maximum sum ; Driver Code
using System ; class GFG { static double smallestDivisor ( int n ) { double mx = Math . Sqrt ( n ) ; for ( int i = 2 ; i <= mx ; i ++ ) if ( n % i == 0 ) return i ; return n ; } static double maxSum ( int n ) { long res = n ; while ( n > 1 ) { double divi = smallestDivisor ( n ) ; n /= ( int ) divi ; res += n ; } return res ; } public static void Main ( ) { int n = 34 ; Console . WriteLine ( maxSum ( n ) ) ; } }
Make all elements of an array equal with the given operation | C # implementation of the approach ; Function that returns true if all the elements of the array can be made equal with the given operation ; To store the sum of the array elements and the maximum element from the array ; Driver code
using System ; class GFG { static bool isPossible ( int n , int k , int [ ] arr ) { int sum = arr [ 0 ] ; int maxVal = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { sum += arr [ i ] ; maxVal = Math . Max ( maxVal , arr [ i ] ) ; } if ( ( float ) maxVal > ( float ) ( sum + k ) / n ) return false ; return true ; } public static void Main ( ) { int k = 8 ; int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; if ( isPossible ( n , k , arr ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Maximize the value of x + y + z such that ax + by + cz = n | C # implementation of the approach ; Function to return the maximum value of ( x + y + z ) such that ( ax + by + cz = n ) ; i represents possible values of a * x ; j represents possible values of b * y ; If z is an integer ; Driver code ; Function Call
using System ; class GFG { static int maxResult ( int n , int a , int b , int c ) { int maxVal = 0 ; for ( int i = 0 ; i <= n ; i += a ) for ( int j = 0 ; j <= n - i ; j += b ) { float z = ( float ) ( n - ( i + j ) ) / ( float ) c ; if ( Math . Floor ( z ) == Math . Ceiling ( z ) ) { int x = i / a ; int y = j / b ; maxVal = Math . Max ( maxVal , x + y + ( int ) z ) ; } } return maxVal ; } public static void Main ( String [ ] args ) { int n = 10 , a = 5 , b = 3 , c = 4 ; Console . WriteLine ( maxResult ( n , a , b , c ) ) ; } }
Make all numbers of an array equal | C # implementation of above approach ; Function that returns true if all the array elements can be made equal with the given operation ; Divide number by 2 ; Divide number by 3 ; Driver code
using System ; class GFG { static bool EqualNumbers ( int [ ] a , int n ) { for ( int i = 0 ; i < n ; i ++ ) { while ( a [ i ] % 2 == 0 ) { a [ i ] /= 2 ; } while ( a [ i ] % 3 == 0 ) { a [ i ] /= 3 ; } if ( a [ i ] != a [ 0 ] ) { return false ; } } return true ; } public static void Main ( ) { int [ ] a = { 50 , 75 , 150 } ; int n = a . Length ; if ( EqualNumbers ( a , n ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Maximum GCD from Given Product of Unknowns | C # implementation of the approach ; Function to return the required gcd ; Count the number of times 2 divides p ; Equivalent to p = p / 2 ; ; If 2 divides p ; Check all the possible numbers that can divide p ; If n in the end is a prime number ; Return the required gcd ; Driver code
using System ; class GFG { static long max_gcd ( long n , long p ) { int count = 0 ; long gcd = 1 ; while ( p % 2 == 0 ) { p >>= 1 ; count ++ ; } if ( count > 0 ) gcd *= ( long ) Math . Pow ( 2 , count / n ) ; for ( long i = 3 ; i <= Math . Sqrt ( p ) ; i += 2 ) { count = 0 ; while ( p % i == 0 ) { count ++ ; p = p / i ; } if ( count > 0 ) { gcd *= ( long ) Math . Pow ( i , count / n ) ; } } if ( p > 2 ) gcd *= ( long ) Math . Pow ( p , 1 / n ) ; return gcd ; } public static void Main ( ) { long n = 3 ; long p = 80 ; Console . WriteLine ( max_gcd ( n , p ) ) ; } }
Minimum positive integer divisible by C and is not in range [ A , B ] | C # implementation of the approach ; Function to return the required number ; If doesn 't belong to the range  then c is the required number ; Else get the next multiple of c starting from b + 1 ; Driver code
using System ; class GFG { static int getMinNum ( int a , int b , int c ) { if ( c < a c > b ) { return c ; } int x = ( ( b / c ) * c ) + c ; return x ; } static public void Main ( ) { int a = 2 , b = 4 , c = 4 ; Console . WriteLine ( getMinNum ( a , b , c ) ) ; } }
Count of pairs of ( i , j ) such that ( ( n % i ) % j ) % n is maximized | C # implementation of above approach ; Function to return the count of required pairs ; Special case ; Number which will give the max value for ( ( n % i ) % j ) % n ; To store the maximum possible value of ( ( n % i ) % j ) % n ; Count of possible pairs ; Driver code
using System ; class GFG { static int countPairs ( int n ) { if ( n == 2 ) return 4 ; int num = ( ( n / 2 ) + 1 ) ; int max = n % num ; int count = n - max ; return count ; } static public void Main ( ) { int n = 5 ; Console . WriteLine ( countPairs ( n ) ) ; } }
Remove characters from a numeric string such that string becomes divisible by 8 | C # program to remove digits from a numeric string such that the number becomes divisible by 8 ; Function that return true if sub is a sub - sequence in s ; Function to return a multiple of 8 formed after removing 0 or more characters from the given string ; Iterate over all multiples of 8 ; If current multiple exists as a subsequence in the given string ; Driver Code
using System ; class GFG { static bool checkSub ( string sub , string s ) { int j = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) if ( sub [ j ] == s [ i ] ) j ++ ; return j == sub . Length ; } static int getMultiple ( string s ) { for ( int i = 0 ; i < 1e3 ; i += 8 ) { if ( checkSub ( i . ToString ( ) , s ) ) return i ; } return - 1 ; } static void Main ( ) { string s = "3454" ; Console . WriteLine ( getMultiple ( s ) ) ; } }
Program to check if a number is divisible by any of its digits | C # implementation of above approach ; Converting integer to string ; Traversing the string ; find the actual digit ; If the number is divisible by digits then return yes ; If no digits are dividing the number then return no ; Driver Code ; passing this number to get result function
using System ; public class GFG { static String getResult ( int n ) { string st = n . ToString ( ) ; for ( int i = 0 ; i < st . Length ; i ++ ) { int d = st [ i ] - 48 ; if ( n % d == 0 ) { return " Yes " ; } } return " No " ; } public static void Main ( String [ ] args ) { int n = 9876543 ; Console . Write ( getResult ( n ) ) ; } }
Program to find sum of harmonic series | C # program to find sum of harmonic series using recursion ; Base condition ; Driven Code
using System ; class GFG { static float sum ( float n ) { if ( n < 2 ) return 1 ; else return 1 / n + ( sum ( n - 1 ) ) ; } public static void Main ( ) { Console . WriteLine ( sum ( 8 ) ) ; Console . WriteLine ( sum ( 10 ) ) ; } }
Sum of P terms of an AP if Mth and Nth terms are given | C # implementation of the above approach ; Function to calculate the value of the ; Calculate value of d using formula ; Calculate value of a using formula ; Return pair ; Function to calculate value sum of first p numbers of the series ; First calculate value of a and d ; Calculate the sum by using formula ; Return the sum ; Driver Code
using System ; using System . Collections ; class GFG { static ArrayList findingValues ( int m , int n , int mth , int nth ) { int d = ( Math . Abs ( mth - nth ) ) / Math . Abs ( ( m - 1 ) - ( n - 1 ) ) ; int a = mth - ( ( m - 1 ) * d ) ; ArrayList res = new ArrayList ( ) ; res . Add ( a ) ; res . Add ( d ) ; return res ; } static int findSum ( int m , int n , int mth , int nth , int p ) { ArrayList ad = findingValues ( m , n , mth , nth ) ; int a = ( int ) ad [ 0 ] ; int d = ( int ) ad [ 1 ] ; int sum = ( p * ( 2 * a + ( p - 1 ) * d ) ) / 2 ; return sum ; } public static void Main ( ) { int m = 6 , n = 10 , mTerm = 12 , nTerm = 20 , p = 5 ; Console . WriteLine ( findSum ( m , n , mTerm , nTerm , p ) ) ; } }
Print all integers that are sum of powers of two given numbers | C # implementation of the approach ; Function to print powerful integers ; Set is used to store distinct numbers in sorted order ; Store all the powers of y < bound in a vector to avoid calculating them again and again ; x ^ i ; If num is within limits insert it into the set ; Break out of the inner loop ; Adding any number to it will be out of bounds ; Increment i ; Print the contents of the set ; Driver code ; Print powerful integers
using System ; using System . Linq ; using System . Collections . Generic ; using System . Collections ; class GFG { static void powerfulIntegers ( int x , int y , int bound ) { HashSet < int > s = new HashSet < int > ( ) ; ArrayList powersOfY = new ArrayList ( ) ; int i ; powersOfY . Add ( 1 ) ; for ( i = y ; i < bound && y != 1 ; i = i * y ) powersOfY . Add ( i ) ; i = 0 ; while ( true ) { int xPowI = ( int ) Math . Pow ( x , i ) ; for ( int j = 0 ; j != powersOfY . Count ; ++ j ) { int num = xPowI + ( int ) powersOfY [ j ] ; if ( num <= bound ) s . Add ( num ) ; else break ; } if ( xPowI >= bound x == 1 ) break ; i ++ ; } int [ ] ar = s . ToArray ( ) ; Array . Sort ( ar ) ; s . Clear ( ) ; s . UnionWith ( ar ) ; foreach ( int t in s ) { Console . Write ( t + " ▁ " ) ; } } static void Main ( ) { int x = 2 , y = 3 , bound = 10 ; powerfulIntegers ( x , y , bound ) ; } }
Distribute N candies among K people | C # code for better approach to distribute candies ; Function to find out the number of candies every person received ; Count number of complete turns ; Get the last term ; Stores the number of candies ; Last term of last and current series ; Sum of current and last series ; Sum of current series only ; If sum of current is less than N ; else Individually distribute ; First term ; Distribute candies till there ; Candies available ; Not available ; Count the total candies ; Print the total candies ; Driver Code
using System ; class GFG { static void candies ( int n , int k ) { int count = 0 ; int ind = 1 ; int [ ] arr = new int [ k ] ; for ( int i = 0 ; i < k ; i ++ ) arr [ i ] = 0 ; while ( n > 0 ) { int f1 = ( ind - 1 ) * k ; int f2 = ind * k ; int sum1 = ( f1 * ( f1 + 1 ) ) / 2 ; int sum2 = ( f2 * ( f2 + 1 ) ) / 2 ; int res = sum2 - sum1 ; if ( res <= n ) { count ++ ; n -= res ; ind ++ ; } { int i = 0 ; int term = ( ( ind - 1 ) * k ) + 1 ; while ( n > 0 ) { if ( term <= n ) { arr [ i ++ ] = term ; n -= term ; term ++ ; } else { arr [ i ++ ] = n ; n = 0 ; } } } } for ( int i = 0 ; i < k ; i ++ ) arr [ i ] += ( count * ( i + 1 ) ) + ( k * ( count * ( count - 1 ) ) / 2 ) ; for ( int i = 0 ; i < k ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int n = 10 , k = 3 ; candies ( n , k ) ; } }
Distribute N candies among K people | C # implementation of the above approach ; Function to find out the number of candies every person received ; Count number of complete turns ; Get the last term ; Stores the number of candies ; Do a binary search to find the number whose sum is less than N . ; Get mide ; If sum is below N ; Find number of complete turns ; Right halve ; Left halve ; Last term of last complete series ; Subtract the sum till ; First term of incomplete series ; Count the total candies ; Print the total candies ; Driver Code
using System ; class GFG { static void candies ( int n , int k ) { int count = 0 ; int ind = 1 ; int [ ] arr = new int [ k ] ; for ( int i = 0 ; i < k ; i ++ ) arr [ i ] = 0 ; int low = 0 , high = n ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; int sum = ( mid * ( mid + 1 ) ) >> 1 ; if ( sum <= n ) { count = mid / k ; low = mid + 1 ; } else { high = mid - 1 ; } } int last = ( count * k ) ; n -= ( last * ( last + 1 ) ) / 2 ; int j = 0 ; int term = ( count * k ) + 1 ; while ( n > 0 ) { if ( term <= n ) { arr [ j ++ ] = term ; n -= term ; term ++ ; } else { arr [ j ] += n ; n = 0 ; } } for ( int i = 0 ; i < k ; i ++ ) arr [ i ] += ( count * ( i + 1 ) ) + ( k * ( count * ( count - 1 ) ) / 2 ) ; for ( int i = 0 ; i < k ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int n = 7 , k = 4 ; candies ( n , k ) ; } }
Smallest multiple of 3 which consists of three given non | C # implementation of the approach ; Function to return the minimum number divisible by 3 formed by the given digits ; Sort the given array in ascending ; Check if any single digit is divisible by 3 ; Check if any two digit number formed by the given digits is divisible by 3 starting from the minimum ; Generate the two digit number ; If none of the above is true , we can form three digit number by taking a [ 0 ] three times . ; Driver code
using System ; public class GFG { static int printSmallest ( int [ ] a ) { Array . Sort ( a ) ; int i , j , num ; for ( i = 0 ; i < 3 ; i ++ ) { if ( a [ i ] % 3 == 0 ) { return a [ i ] ; } } for ( i = 0 ; i < 3 ; i ++ ) { for ( j = 0 ; j < 3 ; j ++ ) { num = ( a [ i ] * 10 ) + a [ j ] ; if ( num % 3 == 0 ) { return num ; } } } return a [ 0 ] * 100 + a [ 0 ] * 10 + a [ 0 ] ; } public static void Main ( ) { int [ ] arr = { 7 , 7 , 1 } ; Console . Write ( printSmallest ( arr ) ) ; } }
Print matrix after applying increment operations in M ranges | C # implementation of the above approach ; Function to update and print the matrix after performing queries ; Add 1 to the first element of the sub - matrix ; If there is an element after the last element of the sub - matrix then decrement it by 1 ; Calculate the running sum ; Print the updated element ; Next line ; Driver code ; Size of the matrix ; Queries
using System ; public class GFG { static void updateMatrix ( int n , int [ , ] q , int [ , ] mat ) { int i , j ; for ( i = 0 ; i < q . GetLength ( 0 ) ; i ++ ) { int X1 = q [ i , 0 ] ; int Y1 = q [ i , 1 ] ; int X2 = q [ i , 2 ] ; int Y2 = q [ i , 3 ] ; mat [ X1 , Y1 ] ++ ; if ( Y2 + 1 < n ) mat [ X2 , Y2 + 1 ] -- ; else if ( X2 + 1 < n ) mat [ X2 + 1 , 0 ] -- ; } int sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { sum += mat [ i , j ] ; Console . Write ( sum + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( ) { int n = 5 ; int [ , ] mat = new int [ n , n ] ; int [ , ] q = { { 0 , 0 , 1 , 2 } , { 1 , 2 , 3 , 4 } , { 1 , 4 , 3 , 4 } } ; updateMatrix ( n , q , mat ) ; } }
Replace the maximum element in the array by coefficient of range | C # implementation to replace maximum element by coefficient of range ; Utility function to print the contents of the array ; Function to replace the maximum element from the array with the coefficient of range of the array ; Maximum element from the array ; Minimum element from the arra ; Calculate the coefficient of range for the array ; Assuming all the array elements are distinc Replace the maximum element with the coefficient of range of the array ; Print the updated array ; Driver code
using System ; class GFG { static void printArr ( float [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void replaceMax ( float [ ] arr , int n ) { float max = arr [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } float min = arr [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < min ) min = arr [ i ] ; } float range = max - min ; float coeffOfRange = range / ( max + min ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == max ) { arr [ i ] = coeffOfRange ; break ; } } printArr ( arr , n ) ; } public static void Main ( ) { float [ ] arr = { 15 , 16 , 10 , 9 , 6 , 7 , 17 } ; int n = arr . Length ; replaceMax ( arr , n ) ; } }
Divide the two given numbers by their common divisors | C # implementation of above approach ; print the numbers after dividing them by their common factors ; iterate from 1 to minimum of a and b ; if i is the common factor of both the numbers ; Driver code ; divide A and B by their common factors
using System ; class GFG { static void divide ( int a , int b ) { for ( int i = 2 ; i <= Math . Min ( a , b ) ; i ++ ) { while ( a % i == 0 && b % i == 0 ) { a = a / i ; b = b / i ; } } Console . WriteLine ( " A ▁ = ▁ " + a + " , ▁ B ▁ = ▁ " + b ) ; } static public void Main ( ) { int A = 10 , B = 15 ; divide ( A , B ) ; } }
Divide the two given numbers by their common divisors | C # implementation of above approach ; Function to calculate gcd of two numbers ; Function to calculate all common divisors of two given numbers a , b -- > input integer numbers ; find gcd of a , b ; Driver code
using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void commDiv ( int a , int b ) { int n = gcd ( a , b ) ; a = a / n ; b = b / n ; Console . WriteLine ( " A ▁ = ▁ " + a + " , ▁ B ▁ = ▁ " + b ) ; } public static void Main ( ) { int a = 10 , b = 15 ; commDiv ( a , b ) ; } }
Minimum absolute difference between N and a power of 2 | C # implementation of the above approach ; Function to return the minimum difference between N and a power of 2 ; Power of 2 closest to n on its left ; Power of 2 closest to n on its right ; Return the minimum abs difference ; Driver code
using System ; public class GFG { static int minAbsDiff ( int n ) { int left = 1 << ( ( int ) Math . Floor ( Math . Log ( n ) / Math . Log ( 2 ) ) ) ; int right = left * 2 ; return Math . Min ( ( n - left ) , ( right - n ) ) ; } static public void Main ( ) { int n = 15 ; Console . WriteLine ( minAbsDiff ( n ) ) ; } }
Find probability that a player wins when probabilities of hitting the target are given | C # mplementation of the approach ; Function to return the probability of the winner ; Driver Code ; Will print 9 digits after the decimal point
using System ; class GFG { static double find_probability ( double p , double q , double r , double s ) { double t = ( 1 - p / q ) * ( 1 - r / s ) ; double ans = ( p / q ) / ( 1 - t ) ; return ans ; } public static void Main ( ) { double p = 1 , q = 2 , r = 1 , s = 2 ; Console . WriteLine ( find_probability ( p , q , r , s ) ) ; } }