codeparrot/xlcost-text-to-code huggingface dataset:
Sum of Area of all possible square inside a rectangle | C # program to calculate the sum of area of all possible squares that comes inside the rectangle ; Function to calculate the sum of area of all possible squares that comes inside the rectangle ; Square with max size possible ; calculate total square of a given size ; calculate area of squares of a particular size ; total area ; increment size ; Driver Code
using System ; class GFG { static int calculateAreaSum ( int l , int b ) { int size = 1 ; int maxSize = Math . Min ( l , b ) ; int totalArea = 0 ; for ( int i = 1 ; i <= maxSize ; i ++ ) { int totalSquares = ( l - size + 1 ) * ( b - size + 1 ) ; int area = totalSquares * size * size ; totalArea += area ; size ++ ; } return totalArea ; } public static void Main ( ) { int l = 4 , b = 3 ; Console . Write ( calculateAreaSum ( l , b ) ) ; } }
Hyperfactorial of a number | C # program to find the hyperfactorial of a number ; function to calculate the value of hyperfactorial ; initialise the val to 1 ; returns the hyperfactorial of a number ; Driver code
using System ; class GFG { static long boost_hyperfactorial ( long num ) { long val = 1 ; for ( long i = 1 ; i <= num ; i ++ ) { val = val * ( long ) Math . Pow ( i , i ) ; } return val ; } public static void Main ( ) { int num = 5 ; Console . WriteLine ( boost_hyperfactorial ( num ) ) ; } }
Hyperfactorial of a number | C # program to find the hyperfactorial of a number using boost libraries ; function to calculate the value of hyperfactorial ; initialise the val to 1 ; 1 ^ 1 * 2 ^ 2 * 3 ^ 3. . . . ; returns the hyperfactorial of a number ; Driver code
using System ; class GFG { static int boost_hyperfactorial ( int num ) { int val = 1 ; for ( int i = 1 ; i <= num ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { val *= i ; } } return val ; } public static void Main ( ) { int num = 5 ; Console . WriteLine ( boost_hyperfactorial ( num ) ) ; } }
Subtract 1 without arithmetic operators | C # code to subtract one from a given number ; Flip all the set bits until we find a 1 ; flip the rightmost 1 bit ; Driver Code
using System ; class GFG { static int subtractOne ( int x ) { int m = 1 ; while ( ! ( ( x & m ) > 0 ) ) { x = x ^ m ; m <<= 1 ; } x = x ^ m ; return x ; } public static void Main ( ) { Console . WriteLine ( subtractOne ( 13 ) ) ; } }
Find the mean vector of a Matrix | C # program to find mean vector of given matrix ; Function to find mean vector ; loop to traverse each column ; to calculate mean of each row ; to store sum of elements of a column ; Driver code
using System ; class GFG { static int rows = 3 ; static int cols = 3 ; static void meanVector ( int [ , ] mat ) { Console . Write ( " [ ▁ " ) ; for ( int i = 0 ; i < rows ; i ++ ) { double mean = 0.00 ; int sum = 0 ; for ( int j = 0 ; j < cols ; j ++ ) sum += mat [ j , i ] ; mean = sum / rows ; Console . Write ( ( int ) mean + " ▁ " ) ; } Console . Write ( " ] " ) ; } public static void Main ( ) { int [ , ] mat = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; meanVector ( mat ) ; } }
Hoax Number | C # code to check if a number is a hoax number or not . ; Function to find distinct prime factors of given number n ; n is odd at this point , since it is no longer divisible by 2. So we can test only for the odd numbers , whether they are factors of n ; Check if i is prime factor ; This condition is to handle the case when n is a prime number greater than 2 ; Function to calculate sum of digits of distinct prime factors of given number n and sum of digits of number n and compare the sums obtained ; Distinct prime factors of n are being stored in vector pf ; If n is a prime number , it cannot be a hoax number ; Finding sum of digits of distinct prime factors of the number n ; Finding sum of digits in current prime factor pf [ i ] . ; Finding sum of digits of number n ; Comparing the two calculated sums ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static List < int > primeFactors ( int n ) { List < int > res = new List < int > ( ) ; if ( n % 2 == 0 ) { while ( n % 2 == 0 ) n = n / 2 ; res . Add ( 2 ) ; } for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { while ( n % i == 0 ) n = n / i ; res . Add ( i ) ; } } if ( n > 2 ) res . Add ( n ) ; return res ; } static bool isHoax ( int n ) { List < int > pf = primeFactors ( n ) ; if ( pf [ 0 ] == n ) return false ; int all_pf_sum = 0 ; for ( int i = 0 ; i < pf . Count ; i ++ ) { int pf_sum ; for ( pf_sum = 0 ; pf [ i ] > 0 ; pf_sum += pf [ i ] % 10 , pf [ i ] /= 10 ) ; all_pf_sum += pf_sum ; } int sum_n ; for ( sum_n = 0 ; n > 0 ; sum_n += n % 10 , n /= 10 ) ; return sum_n == all_pf_sum ; } public static void Main ( ) { int n = 84 ; if ( isHoax ( n ) ) Console . Write ( " A ▁ Hoax ▁ Number STRNEWLINE " ) ; else Console . Write ( " Not ▁ a ▁ Hoax ▁ Number STRNEWLINE " ) ; } }
Primality Test | Set 5 ( Using Lucas | C # program to find out Lucas - Lehmer series . ; Function to find out first n terms ( considering4 as 0 th term ) of Lucas - Lehmer series . ; the 0 th term of the series is 4. ; create an array to store the terms . ; compute each term and add it to the array . ; print out the terms one by one . ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void LucasLehmer ( int n ) { long current_val = 4 ; List < long > series = new List < long > ( ) ; series . Add ( current_val ) ; for ( int i = 0 ; i < n ; i ++ ) { current_val = current_val * current_val - 2 ; series . Add ( current_val ) ; } for ( int i = 0 ; i <= n ; i ++ ) Console . WriteLine ( " Term ▁ " + i + " : ▁ " + series [ i ] ) ; } static void Main ( ) { int n = 5 ; LucasLehmer ( n ) ; } }
Modular multiplicative inverse from 1 to n | C # program to find modular inverse of all numbers from 1 to n using naive method ; A naive method to find modular multiplicative inverse of ' a ' under modulo ' prime ' ; Driver Program
using System ; class GFG { static int modInverse ( int a , int prime ) { a = a % prime ; for ( int x = 1 ; x < prime ; x ++ ) if ( ( a * x ) % prime == 1 ) return x ; return - 1 ; } static void printModIverses ( int n , int prime ) { for ( int i = 1 ; i <= n ; i ++ ) Console . Write ( modInverse ( i , prime ) + " ▁ " ) ; } public static void Main ( ) { int n = 10 , prime = 17 ; printModIverses ( n , prime ) ; } }
Convert to number with digits as 3 and 8 only | C # to find min operations required to convert into charming number ; function for minimum operation ; remainder and operations count ; count digits not equal to 3 or 8 ; Driver code
using System ; class GFG { static int minOp ( int num ) { int rem ; int count = 0 ; while ( num > 0 ) { rem = num % 10 ; if ( ! ( rem == 3 rem == 8 ) ) count ++ ; num /= 10 ; } return count ; } public static void Main ( ) { int num = 234198 ; Console . WriteLine ( " Minimum ▁ Operations ▁ = " + minOp ( num ) ) ; } }
Biggest integer which has maximum digit sum in range from 1 to n | C # program to find the number with maximum digit sum . ; function to calculate the sum of digits of a number . ; Returns the maximum number with maximum sum of digits . ; initializing b as 1 and initial max sum to be of n ; iterates from right to left in a digit ; while iterating this is the number from from right to left ; calls the function to check if sum of cur is more then of ans ; reduces the number to one unit less ; driver program
using System ; class GFG { static int sumOfDigits ( int a ) { int sum = 0 ; while ( a != 0 ) { sum += a % 10 ; a /= 10 ; } return sum ; } static int findMax ( int x ) { int b = 1 , ans = x ; while ( x != 0 ) { int cur = ( x - 1 ) * b + ( b - 1 ) ; if ( sumOfDigits ( cur ) > sumOfDigits ( ans ) || ( sumOfDigits ( cur ) == sumOfDigits ( ans ) && cur > ans ) ) ans = cur ; x /= 10 ; b *= 10 ; } return ans ; } public static void Main ( ) { int n = 521 ; Console . WriteLine ( findMax ( n ) ) ; } }
Interquartile Range ( IQR ) | C # program to find IQR of a data set ; Function to give index of the median ; Function to calculate IQR ; Index of median of entire data ; Median of first half ; Median of second half ; IQR calculation ; Driver Code
using System ; class GFG { static int median ( int [ ] a , int l , int r ) { int n = r - l + 1 ; n = ( n + 1 ) / 2 - 1 ; return n + l ; } static int IQR ( int [ ] a , int n ) { Array . Sort ( a ) ; int mid_index = median ( a , 0 , n ) ; int Q1 = a [ median ( a , 0 , mid_index ) ] ; int Q3 = a [ mid_index + median ( a , mid_index + 1 , n ) ] ; return ( Q3 - Q1 ) ; } public static void Main ( ) { int [ ] a = { 1 , 19 , 7 , 6 , 5 , 9 , 12 , 27 , 18 , 2 , 15 } ; int n = a . Length ; Console . WriteLine ( IQR ( a , n ) ) ; } }
Largest palindromic number in an array | C # implementation of above approach ; Function to check if n is palindrome ; Find the appropriate divisor to extract the leading digit ; If first and last digits are not same then return false ; Removing the leading and trailing digits from the number ; Reducing divisor by a factor of 2 as 2 digits are dropped ; Function to find the largest palindromic number ; Sort the array ; If number is palindrome ; If no palindromic number found ; Driver program ; print required answer
using System ; class GFG { static bool isPalindrome ( int n ) { int divisor = 1 ; while ( n / divisor >= 10 ) divisor *= 10 ; while ( n != 0 ) { int leading = n / divisor ; int trailing = n % 10 ; if ( leading != trailing ) return false ; n = ( n % divisor ) / 10 ; divisor = divisor / 100 ; } return true ; } static int largestPalindrome ( int [ ] A , int n ) { Array . Sort ( A ) ; for ( int i = n - 1 ; i >= 0 ; -- i ) { if ( isPalindrome ( A [ i ] ) ) return A [ i ] ; } return - 1 ; } public static void Main ( ) { int [ ] A = { 1 , 232 , 54545 , 999991 } ; int n = A . Length ; Console . WriteLine ( largestPalindrome ( A , n ) ) ; } }
Sum of the multiples of two numbers below N | C # program to find the sum of all the integers below N which are multiples of either A or B ; Function to return the sum of all the integers below N which are multiples of either A or B ; If i is a multiple of a or b ; Driver code
using System ; class GFG { static int findSum ( int n , int a , int b ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( i % a == 0 i % b == 0 ) sum += i ; return sum ; } static void Main ( ) { int n = 10 , a = 3 , b = 5 ; Console . WriteLine ( findSum ( n , a , b ) ) ; } }
Subtract 1 without arithmetic operators | Driver code
using System ; class GFG { static int subtractOne ( int x ) { return ( ( x << 1 ) + ( ~ x ) ) ; } public static void Main ( String [ ] args ) { Console . Write ( " { 0 } " , subtractOne ( 13 ) ) ; } }
Pell Number | Pell Number Series using Recursion in C # ; calculate n - th Pell number ; Driver function
using System ; class PellNumber { public static int pell ( int n ) { if ( n <= 2 ) return n ; return 2 * pell ( n - 1 ) + pell ( n - 2 ) ; } public static void Main ( ) { int n = 4 ; Console . Write ( pell ( n ) ) ; } }
Finding LCM of more than two ( or array ) numbers without using GCD | C # program to find LCM of array without using GCD . ; Returns LCM of arr [ 0. . n - 1 ] ; Find the maximum value in arr [ ] ; Initialize result ; Find all factors that are present in two or more array elements . int x = 2 ; Current factor . ; To store indexes of all array elements that are divisible by x . ; If there are 2 or more array elements that are divisible by x . ; Reduce all array elements divisible by x . ; Then multiply all reduced array elements ; Driver code
using System ; using System . Collections ; class GFG { static long LCM ( int [ ] arr , int n ) { int max_num = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( max_num < arr [ i ] ) { max_num = arr [ i ] ; } } long res = 1 ; while ( x <= max_num ) { ArrayList indexes = new ArrayList ( ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] % x == 0 ) { indexes . Add ( j ) ; } } if ( indexes . Count >= 2 ) { for ( int j = 0 ; j < indexes . Count ; j ++ ) { arr [ ( int ) indexes [ j ] ] = arr [ ( int ) indexes [ j ] ] / x ; } res = res * x ; } else { x ++ ; } } for ( int i = 0 ; i < n ; i ++ ) { res = res * arr [ i ] ; } return res ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 10 , 20 , 35 } ; int n = arr . Length ; Console . WriteLine ( LCM ( arr , n ) ) ; } }
Find politeness of a number | C # program for the above approach ; Function to find politeness ; sqrt ( 2 * n ) as max length will be when the sum starts from 1 which follows the equation n ^ 2 - n - ( 2 * sum ) = 0 ; Driver Code
using System ; public class GFG { static int politness ( int n ) { int count = 0 ; for ( int i = 2 ; i <= Math . Sqrt ( 2 * n ) ; i ++ ) { int a ; if ( ( 2 * n ) % i != 0 ) continue ; a = 2 * n ; a /= i ; a -= ( i - 1 ) ; if ( a % 2 != 0 ) continue ; a /= 2 ; if ( a > 0 ) { count ++ ; } } return count ; } public static void Main ( String [ ] args ) { int n = 90 ; Console . WriteLine ( " Politness ▁ of ▁ " + n + " ▁ = ▁ " + politness ( n ) ) ; n = 15 ; Console . WriteLine ( " Politness ▁ of ▁ " + n + " ▁ = ▁ " + politness ( n ) ) ; } }
Program for Goldbachâ €™ s Conjecture ( Two Primes with given Sum ) | C # program to implement Goldbach 's conjecture ; Array to store all prime less than and equal to 10 ^ 6 ; Utility function for Sieve of Sundaram ; In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x . Since we want primes smaller than MAX , we reduce MAX to half This array is used to separate numbers of the form i + j + 2 * i * j from others where 1 <= i <= j ; Main logic of Sundaram . Mark all numbers which do not generate prime number by doing 2 * i + 1 ; Since 2 is a prime number ; Print other primes . Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; Function to perform Goldbach 's conjecture ; Return if number is not even or less than 3 ; Check only upto half of number ; find difference by subtracting current prime from n ; Search if the difference is also a prime number ; Express as a sum of primes ; Driver code ; Finding all prime numbers before limit ; Express number as a sum of two primes
using System ; using System . Collections . Generic ; class GFG { static int MAX = 10000 ; static List < int > primes = new List < int > ( ) ; static void sieveSundaram ( ) { Boolean [ ] marked = new Boolean [ MAX / 2 + 100 ] ; for ( int i = 1 ; i <= ( Math . Sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked [ j ] = true ; primes . Add ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked [ i ] == false ) primes . Add ( 2 * i + 1 ) ; } static void findPrimes ( int n ) { if ( n <= 2 n % 2 != 0 ) { Console . WriteLine ( " Invalid ▁ Input ▁ " ) ; return ; } for ( int i = 0 ; primes [ i ] <= n / 2 ; i ++ ) { int diff = n - primes [ i ] ; if ( primes . Contains ( diff ) ) { Console . WriteLine ( primes [ i ] + " ▁ + ▁ " + diff + " ▁ = ▁ " + n ) ; return ; } } } public static void Main ( String [ ] args ) { sieveSundaram ( ) ; findPrimes ( 4 ) ; findPrimes ( 38 ) ; findPrimes ( 100 ) ; } }
k | C # Program to print kth prime factor . ; A function to generate prime factors of a given number n and return k - th prime factor ; Find the number of 2 's that  divide k ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , store i and divide n ; This condition is to handle the case where n is a prime number greater than 2 ; Driver Program
using System ; class GFG { static int kPrimeFactor ( int n , int k ) { while ( n % 2 == 0 ) { k -- ; n = n / 2 ; if ( k == 0 ) return 2 ; } for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { if ( k == 1 ) return i ; k -- ; n = n / i ; } } if ( n > 2 && k == 1 ) return n ; return - 1 ; } public static void Main ( ) { int n = 12 , k = 3 ; Console . WriteLine ( kPrimeFactor ( n , k ) ) ; n = 14 ; k = 3 ; Console . WriteLine ( kPrimeFactor ( n , k ) ) ; } }
k | C # program to find k - th prime factor using Sieve Of Eratosthenes . This program is efficient when we have a range of numbers and we ; Using SieveOfEratosthenes to find smallest prime factor of all the numbers . For example , if MAX is 10 , s [ 2 ] = s [ 4 ] = s [ 6 ] = s [ 10 ] = 2 s [ 3 ] = s [ 9 ] = 3 s [ 5 ] = 5 s [ 7 ] = 7 ; Create a boolean array " prime [ 0 . . MAX ] " and initialize all entries in it as false . ; Initializing smallest factor equal to 2 for all the even numbers ; For odd numbers less then equal to n ; s ( i ) for a prime is the number itself ; For all multiples of current prime number ; i is the smallest prime factor for number " i * j " . ; Function to generate prime factors and return its k - th prime factor . s [ i ] stores least prime factor of i . ; Keep dividing n by least prime factor while either n is not 1 or count of prime factors is not k . ; To keep track of count of prime factors ; Divide n to find next prime factor ; Driver Code ; s [ i ] is going to store prime factor of i .
using System ; class GFG { static int MAX = 10001 ; static void sieveOfEratosthenes ( int [ ] s ) { bool [ ] prime = new bool [ MAX + 1 ] ; for ( int i = 2 ; i <= MAX ; i += 2 ) s [ i ] = 2 ; for ( int i = 3 ; i <= MAX ; i += 2 ) { if ( prime [ i ] == false ) { s [ i ] = i ; for ( int j = i ; j * i <= MAX ; j += 2 ) { if ( prime [ i * j ] == false ) { prime [ i * j ] = true ; s [ i * j ] = i ; } } } } } static int kPrimeFactor ( int n , int k , int [ ] s ) { while ( n > 1 ) { if ( k == 1 ) return s [ n ] ; k -- ; n /= s [ n ] ; } return - 1 ; } static void Main ( ) { int [ ] s = new int [ MAX + 1 ] ; sieveOfEratosthenes ( s ) ; int n = 12 , k = 3 ; Console . WriteLine ( kPrimeFactor ( n , k , s ) ) ; n = 14 ; k = 3 ; Console . WriteLine ( kPrimeFactor ( n , k , s ) ) ; } }
Find sum of divisors of all the divisors of a natural number | C # program to find sum of divisors of all the divisors of a natural number . ; Returns sum of divisors of all the divisors of n ; Calculating powers of prime factors and storing them in a map mp [ ] . ; If n is a prime number ; For each prime factor , calculating ( p ^ ( a + 1 ) - 1 ) / ( p - 1 ) and adding it to answer . ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static int sumDivisorsOfDivisors ( int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int j = 2 ; j <= Math . Sqrt ( n ) ; j ++ ) { int count = 0 ; while ( n % j == 0 ) { n /= j ; count ++ ; } if ( count != 0 ) mp . Add ( j , count ) ; } if ( n != 1 ) mp . Add ( n , 1 ) ; int ans = 1 ; foreach ( KeyValuePair < int , int > entry in mp ) { int pw = 1 ; int sum = 0 ; for ( int i = entry . Value + 1 ; i >= 1 ; i -- ) { sum += ( i * pw ) ; pw = entry . Key ; } ans *= sum ; } return ans ; } public static void Main ( String [ ] args ) { int n = 10 ; Console . WriteLine ( sumDivisorsOfDivisors ( n ) ) ; } }
Nearest prime less than given number n | C # program for the above approach ; Function to return nearest prime number ; All prime numbers are odd except two ; It will only be executed when n is 3 ; Driver Code
using System ; class GFG { static int prime ( int n ) { if ( n % 2 != 0 ) n -= 2 ; else n -- ; int i , j ; for ( i = n ; i >= 2 ; i -= 2 ) { if ( i % 2 == 0 ) continue ; for ( j = 3 ; j <= Math . Sqrt ( i ) ; j += 2 ) { if ( i % j == 0 ) break ; } if ( j > Math . Sqrt ( i ) ) return i ; } return 2 ; } public static void Main ( ) { int n = 17 ; Console . Write ( prime ( n ) ) ; } }
Find Recurring Sequence in a Fraction | C # program to find repeating sequence in a fraction ; This function returns repeating sequence of a fraction . If repeating sequence doesn 't  exits, then returns empty String ; Initialize result ; Create a map to store already seen remainders . Remainder is used as key and its position in result is stored as value . Note that we need position for cases like 1 / 6. In this case , the recurring sequence doesn 't  start from first remainder. ; Find first remainder ; Keep finding remainder until either remainder becomes 0 or repeats ; Store this remainder ; Multiply remainder with 10 ; Append rem / denr to result ; Update remainder ; Driver code
using System ; using System . Collections . Generic ; class GFG { static string fractionToDecimal ( int numr , int denr ) { string res = " " ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int rem = numr % denr ; while ( ( rem != 0 ) && ( ! mp . ContainsValue ( rem ) ) ) { mp [ rem ] = res . Length ; rem = rem * 10 ; int res_part = rem / denr ; res += res_part . ToString ( ) ; rem = rem % denr ; } if ( rem == 0 ) return " " ; else if ( mp . ContainsKey ( rem ) ) return res . Substring ( mp [ rem ] ) ; return " " ; } public static void Main ( string [ ] args ) { int numr = 50 , denr = 22 ; string res = fractionToDecimal ( numr , denr ) ; if ( res == " " ) Console . Write ( " No ▁ recurring ▁ sequence " ) ; else Console . Write ( " Recurring ▁ sequence ▁ is ▁ " + res ) ; } }
Count numbers having 0 as a digit | C # program to count numbers from 1 to n with 0 as a digit ; Returns 1 if x has 0 , else 0 ; Traverse through all digits of x to check if it has 0. ; If current digit is 0 , return true ; Returns count of numbers from 1 to n with 0 as digit ; Initialize count of numbers having 0 as digit ; Traverse through all numbers and for every number check if it has 0. ; Driver Code
using System ; class GFG { static int has0 ( int x ) { while ( x != 0 ) { if ( x % 10 == 0 ) return 1 ; x /= 10 ; } return 0 ; } static int getCount ( int n ) { int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) count += has0 ( i ) ; return count ; } public static void Main ( ) { int n = 107 ; Console . WriteLine ( " Count ▁ of ▁ numbers ▁ from ▁ 1" + " ▁ to ▁ " + n + " ▁ is ▁ " + getCount ( n ) ) ; } }
Euler 's criterion (Check if square root under modulo p exists) | A Simple C # program to check if square root of a number under modulo p exists or not ; Returns true if square root of n under modulo p exists ; One by one check all numbers from 2 to p - 1 ; Driver code
using System ; class GFG { static bool squareRootExists ( int n , int p ) { n = n % p ; for ( int x = 2 ; x < p ; x ++ ) if ( ( x * x ) % p == n ) return true ; return false ; } public static void Main ( ) { int p = 7 ; int n = 2 ; if ( squareRootExists ( n , p ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Legendre 's formula (Given p and n, find the largest x such that p^x divides n!) | C # program to find largest x such that p * x divides n ! ; Function that returns largest power of p that divides n ! ; Initialize result ; Calculate x = n / p + n / ( p ^ 2 ) + n / ( p ^ 3 ) + ... . ; Driver Code
using System ; public class GFG { static int Largestpower ( int n , int p ) { int ans = 0 ; while ( n > 0 ) { n /= p ; ans += n ; } return ans ; } public static void Main ( ) { int n = 10 ; int p = 3 ; Console . Write ( " ▁ The ▁ largest ▁ power ▁ of ▁ " + p + " ▁ that ▁ divides ▁ " + n + " ! ▁ is ▁ " + Largestpower ( n , p ) ) ; } }
Program for factorial of a number | C # program to find factorial of the given number ; single line to find factorial ; Driver Code
using System ; class Factorial { int factorial ( int n ) { return ( n == 1 n == 0 ) ? 1 : n * factorial ( n - 1 ) ; } public static void Main ( ) { Factorial obj = new Factorial ( ) ; int num = 5 ; Console . WriteLine ( " Factorial ▁ of ▁ " + num + " ▁ is ▁ " + obj . factorial ( num ) ) ; } }
All about Bit Manipulation | Function to get the bit at the ith position ; Return true if the bit is set . Otherwise return false
static bool getBit ( int num , int i ) { return ( ( num & ( 1 << i ) ) != 0 ) ; }
All about Bit Manipulation | Function to clear the ith bit of the given number N ; Create the mask for the ith bit unset ; Return the update value
static int clearBit ( int num , int i ) { int mask = ~ ( 1 << i ) ; return num & mask ; }