codeparrot/xlcost-text-to-code huggingface dataset:
Given a HUGE number check if it 's a power of two. | C # program to find whether a number is power of 2 or not ; returns 1 when str is power of 2 return 0 when str is not a power of 2 ; sum stores the intermediate dividend while dividing . ; if the input is "1" then return 0 because 2 ^ k = 1 where k >= 1 and here k = 0 ; Divide the number until it gets reduced to 1 if we are successfully able to reduce the number to 1 it means input string is power of two if in between an odd number appears at the end it means string is not divisible by two hence not a power of 2. ; if the last digit is odd then string is not divisible by 2 hence not a power of two return 0. ; divide the whole string by 2. i is used to track index in current number . j is used to track index for next iteration . ; if num < 2 then we have to take another digit to the right of A [ i ] to make it bigger than A [ i ] . E . g . 214 / 2 -- > 107 ; if it 's not the first index. E.g 214  then we have to include 0. ; for eg . "124" we will not write 064 so if it is the first index just ignore ; After every division by 2 the length of string is changed . ; if the string reaches to 1 then the str is a power of 2. ; Driver code .
using System ; class GFG { static int isPowerOf2 ( string s ) { char [ ] str = s . ToCharArray ( ) ; int len_str = str . Length ; int num = 0 ; if ( len_str == 1 && str [ len_str - 1 ] == '1' ) return 0 ; while ( len_str != 1 str [ len_str - 1 ] != '1' ) { if ( ( str [ len_str - 1 ] - '0' ) % 2 == 1 ) return 0 ; int j = 0 ; for ( int i = 0 ; i < len_str ; i ++ ) { num = num * 10 + ( int ) str [ i ] - ( int ) '0' ; if ( num < 2 ) { if ( i != 0 ) str [ j ++ ] = '0' ; continue ; } str [ j ++ ] = ( char ) ( ( int ) ( num / 2 ) + ( int ) '0' ) ; num = ( num ) - ( num / 2 ) * 2 ; } str [ j ] = ' \0' ; len_str = j ; } return 1 ; } static void Main ( ) { string str1 = "124684622466842024680246842024662202000002" ; string str2 = "1" ; string str3 = "128" ; Console . Write ( isPowerOf2 ( str1 ) + " STRNEWLINE " + isPowerOf2 ( str2 ) + " STRNEWLINE " + isPowerOf2 ( str3 ) ) ; } }
Given a HUGE number check if it 's a power of two. | C # program to find whether a number is power of 2 or not ; Function to check whether a number is power of 2 or not ; Driver Code
class GFG { static long ispowerof2 ( long num ) { if ( ( num & ( num - 1 ) ) == 0 ) return 1 ; return 0 ; } public static void Main ( ) { long num = 549755813888 ; System . Console . WriteLine ( ispowerof2 ( num ) ) ; } }
Count divisors of array multiplication | C # program to count divisors in array multiplication . ; To count number of factors in a number ; Initialize count with 0 ; Increment count for every factor of the given number X . ; Return number of factors ; Returns number of divisors in array multiplication ; Multipliying all elements of the given array . ; Calling function which count number of factors of the number ; Driver code
using System ; class GFG { static int counDivisors ( int X ) { int count = 0 ; for ( int i = 1 ; i <= X ; ++ i ) { if ( X % i == 0 ) { count ++ ; } } return count ; } static int countDivisorsMult ( int [ ] arr , int n ) { int mul = 1 ; for ( int i = 0 ; i < n ; ++ i ) mul *= arr [ i ] ; return counDivisors ( mul ) ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 6 } ; int n = arr . Length ; Console . Write ( countDivisorsMult ( arr , n ) ) ; } }
Count divisors of array multiplication | C # program to count divisors in array multiplication . ; Create a boolean array " isPrime [ 0 . . n ] " and initialize all entries it as true . A value in isPrime [ i ] will finally be false if i is Not a isPrime , else true . ; If isPrime [ p ] is not changed , then it is a isPrime ; Update all multiples of p ; Print all isPrime numbers ; Returns number of divisors in array multiplication ; Find all prime numbers smaller than the largest element . ; Find counts of occurrences of each prime factor ; Compute count of all divisors using counts prime factors . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void SieveOfEratosthenes ( int largest , List < int > prime ) { bool [ ] isPrime = new bool [ largest + 1 ] ; Array . Fill ( isPrime , true ) ; for ( int p = 2 ; p * p <= largest ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= largest ; i += p ) isPrime [ i ] = false ; } } for ( int p = 2 ; p <= largest ; p ++ ) if ( isPrime [ p ] ) prime . Add ( p ) ; } static long countDivisorsMult ( int [ ] arr , int n ) { int largest = 0 ; foreach ( int a in arr ) { largest = Math . Max ( largest , a ) ; } List < int > prime = new List < int > ( ) ; SieveOfEratosthenes ( largest , prime ) ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < prime . Count ; j ++ ) { while ( arr [ i ] > 1 && arr [ i ] % prime [ j ] == 0 ) { arr [ i ] /= prime [ j ] ; if ( mp . ContainsKey ( prime [ j ] ) ) { mp [ prime [ j ] ] ++ ; } else { mp . Add ( prime [ j ] , 1 ) ; } } } if ( arr [ i ] != 1 ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] ++ ; } else { mp . Add ( arr [ i ] , 1 ) ; } } } long res = 1 ; foreach ( KeyValuePair < int , int > it in mp ) res *= ( it . Value + 1L ) ; return res ; } static public void Main ( ) { int [ ] arr = { 2 , 4 , 6 } ; int n = arr . Length ; Console . WriteLine ( countDivisorsMult ( arr , n ) ) ; } }
K distant prime pairs in a given range | C # program for the above approach ; Function to generate prime numbers in the given range [ L , R ] ; Store all value in the range ; Erase 1 as its non - prime ; Perform Sieve of Eratosthenes ; Find current multiple ; Erase as it is a non - prime ; Increment multiple ; Traverse the Map M ; If it . first & ( it . first + K ) is prime then print this pair ; Function to print all the prime pairs in the given range that differs by K ; Generate all prime number ; Driver Code ; Given range ; Given K ; Function Call
using System ; using System . Collections . Generic ; class solution { static void findPrimeNos ( int L , int R , Dictionary < int , int > M , int K ) { for ( int i = L ; i <= R ; i ++ ) { if ( M . ContainsKey ( i ) ) M . Add ( i , M [ i ] + 1 ) ; else M . Add ( i , 1 ) ; } if ( M [ 1 ] != 0 ) { M . Remove ( 1 ) ; } for ( int i = 2 ; i <= Math . Sqrt ( R ) ; i ++ ) { int multiple = 2 ; while ( ( i * multiple ) <= R ) { if ( M . ContainsKey ( i * multiple ) ) { M . Remove ( i * multiple ) ; } multiple ++ ; } } foreach ( KeyValuePair < int , int > entry in M ) { if ( M . ContainsKey ( entry . Key + K ) ) { Console . Write ( " ( " + entry . Key + " , ▁ " + ( entry . Key + K ) + " ) ▁ " ) ; } } } static void getPrimePairs ( int L , int R , int K ) { Dictionary < int , int > M = new Dictionary < int , int > ( ) ; findPrimeNos ( L , R , M , K ) ; } public static void Main ( String [ ] args ) { int L = 1 , R = 19 ; int K = 6 ; getPrimePairs ( L , R , K ) ; } }
Enneacontahexagon numbers | C # program to find N - th Enneacontahexagon Number ; Function to find the nth Enneacontahexagon Number ; Driver code
using System ; class GFG { static int enneacontahexagonNum ( int n ) { return ( 94 * n * n - 92 * n ) / 2 ; } public static void Main ( ) { int n = 3 ; Console . Write ( enneacontahexagonNum ( n ) ) ; } }
Find two Composite Numbers such that there difference is N | C # code to Find two Composite Numbers such that there difference is N ; Function to find the two composite numbers ; Driver code
using System ; class GFG { static void find_composite_nos ( int n ) { Console . WriteLine ( 9 * n + " ▁ " + 8 * n ) ; } public static void Main ( ) { int n = 4 ; find_composite_nos ( n ) ; } }
Count the number of pairs ( i , j ) such that either arr [ i ] is divisible by arr [ j ] or arr [ j ] is divisible by arr [ i ] | C # implementation of the approach ; Function to find number of unordered pairs ; Maximum element from the array ; Array to store the frequency of each element ; Stores the number of unordered pairs ; Store the frequency of each element ; Find the number of unordered pairs ; If the number j divisible by ith element is present in the array ; If the ith element of the array has frequency more than one ; Driver code
using System ; using System . Linq ; class GFG { static int freqPairs ( int [ ] arr , int n ) { int max = arr . Max ( ) ; int [ ] freq = new int [ max + 1 ] ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 2 * arr [ i ] ; j <= max ; j += arr [ i ] ) { if ( freq [ j ] >= 1 ) { count += freq [ j ] ; } } if ( freq [ arr [ i ] ] > 1 ) { count += freq [ arr [ i ] ] - 1 ; freq [ arr [ i ] ] -- ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 4 , 2 , 6 } ; int n = arr . Length ; Console . WriteLine ( freqPairs ( arr , n ) ) ; } }
Find the Nth term of the series 1 + 2 + 6 + 15 + 31 + 56 + ... | C # program to find Nth term of the series : 1 + 2 + 6 + 15 + 31 + 56 + ... ; calculate Nth term of given series ; Driver code
using System ; class GFG { static double Nth_Term ( int n ) { return ( 2 * Math . Pow ( n , 3 ) - 3 * Math . Pow ( n , 2 ) + n + 6 ) / 6 ; } static public void Main ( ) { int N = 8 ; Console . WriteLine ( Nth_Term ( N ) ) ; } }
Program to find N | C # program to find n - th number in a series made of digits 3 and 5 ; Function to find n - th number in series made of 3 and 5 ; create an array of size ( n + 1 ) ; If i is odd ; Driver Code
using System ; class GFG { static int printNthElement ( int n ) { int [ ] arr = new int [ n + 1 ] ; arr [ 1 ] = 3 ; arr [ 2 ] = 5 ; for ( int i = 3 ; i <= n ; i ++ ) { if ( i % 2 != 0 ) arr [ i ] = arr [ i / 2 ] * 10 + 3 ; else arr [ i ] = arr [ ( i / 2 ) - 1 ] * 10 + 5 ; } return arr [ n ] ; } static void Main ( ) { int n = 6 ; Console . WriteLine ( printNthElement ( n ) ) ; } }
Program to find Nth term of the series 3 , 6 , 18 , 24 , ... | C # program to find N - th term of the series : 3 , 6 , 18 , 24 , 45 , 54. . . ; By using above formula ; Driver Code ; get the value of N ; create object of Class Nth ; Calculate and print the Nth term
using System ; class GFG { public int nthTerm ( int N ) { return ( N * ( ( N / 2 ) + ( ( N % 2 ) * 2 ) + N ) ) ; } public static void Main ( ) { int N = 5 ; GFG a = new GFG ( ) ; Console . WriteLine ( " Nth ▁ term ▁ for ▁ N ▁ = ▁ " + N + " ▁ : ▁ " + a . nthTerm ( N ) ) ; } }
Program to print binomial expansion series | C # program to print terms of binomial series and also calculate sum of series . ; function to print the series ; Calculating and printing first term ; Computing and printing remaining terms ; Find current term using previous terms We increment power of X by 1 , decrement power of A by 1 and compute nCi using previous term by multiplying previous term with ( n - i + 1 ) / i ; main function started
using System ; public class GFG { static void series ( int A , int X , int n ) { int term = ( int ) Math . Pow ( A , n ) ; Console . Write ( term + " ▁ " ) ; for ( int i = 1 ; i <= n ; i ++ ) { term = term * X * ( n - i + 1 ) / ( i * A ) ; Console . Write ( term + " ▁ " ) ; } } public static void Main ( ) { int A = 3 , X = 4 , n = 5 ; series ( A , X , n ) ; } }
Check if a number is divisible by 8 using bitwise operators | C # program to check whether the number is divisible by 8 or not using bitwise operator ; function to check number is div by 8 or not using bitwise operator ; Driver code
using System ; class GFG { static bool Div_by_8 ( int n ) { return ( ( ( n >> 3 ) << 3 ) == n ) ; } public static void Main ( ) { int n = 16 ; if ( Div_by_8 ( n ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Average of even numbers till a given even number | C # Program to find average of even numbers till a given even number . ; Function to calculate the average of even numbers ; count even numbers ; store the sum of even numbers ; driver function
using System ; class GFG { static int averageEven ( int n ) { if ( n % 2 != 0 ) { Console . Write ( " Invalid ▁ Input " ) ; return - 1 ; } int sum = 0 , count = 0 ; while ( n >= 2 ) { count ++ ; sum += n ; n = n - 2 ; } return sum / count ; } public static void Main ( ) { int n = 16 ; Console . Write ( averageEven ( n ) ) ; } }
Average of even numbers till a given even number | C # Program to find average of even numbers till a given even number . ; Function to calculate the average of even numbers ; driver function
using System ; class GFG { static int averageEven ( int n ) { if ( n % 2 != 0 ) { Console . Write ( " Invalid ▁ Input " ) ; return - 1 ; } return ( n + 2 ) / 2 ; } public static void Main ( ) { int n = 16 ; Console . Write ( averageEven ( n ) ) ; } }
Largest number that divides x and is co | C # program to find the Largest Coprime Divisor ; Recursive function to return gcd of a and b ; Everything divides 0 ; base case ; a is greater ; function to find largest coprime divisor ; divisor code
using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 b == 0 ) return 0 ; if ( a == b ) return a ; if ( a > b ) return gcd ( a - b , b ) ; return gcd ( a , b - a ) ; } static int cpFact ( int x , int y ) { while ( gcd ( x , y ) != 1 ) { x = x / gcd ( x , y ) ; } return x ; } public static void Main ( ) { int x = 15 ; int y = 3 ; Console . WriteLine ( cpFact ( x , y ) ) ; x = 14 ; y = 28 ; Console . WriteLine ( cpFact ( x , y ) ) ; x = 7 ; y = 3 ; Console . WriteLine ( cpFact ( x , y ) ) ; } }
Count numbers with unit digit k in given range | Simple C # program to count numbers with last digit as k in given range . ; Returns count of numbers with k as last digit . ; Driver function
using System ; public class GfG { public static int counLastDigitK ( int low , int high , int k ) { int count = 0 ; for ( int i = low ; i <= high ; i ++ ) if ( i % 10 == k ) count ++ ; return count ; } public static void Main ( ) { int low = 3 , high = 35 , k = 3 ; Console . WriteLine ( counLastDigitK ( low , high , k ) ) ; } }
Taxicab Numbers | C # Code for Taxicab Numbers ; Starting from 1 , check every number if it is Taxicab until count reaches N . ; Try all possible pairs ( j , k ) whose cube sums can be i . ; Taxicab ( 2 ) found ; Driver program
using System ; class GFG { public static void printTaxicab2 ( int N ) { int i = 1 , count = 0 ; while ( count < N ) { int int_count = 0 ; for ( int j = 1 ; j <= Math . Pow ( i , 1.0 / 3 ) ; j ++ ) for ( int k = j + 1 ; k <= Math . Pow ( i , 1.0 / 3 ) ; k ++ ) if ( j * j * j + k * k * k == i ) int_count ++ ; if ( int_count == 2 ) { count ++ ; Console . WriteLine ( count + " ▁ " + i ) ; } i ++ ; } } public static void Main ( ) { int N = 5 ; printTaxicab2 ( N ) ; } }
Composite Number | A optimized school method based C # program to check if a number is composite . ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver Code
using System ; namespace Composite { public class GFG { public static bool isComposite ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } public static void Main ( ) { if ( isComposite ( 11 ) ) Console . WriteLine ( " true " ) ; else Console . WriteLine ( " false " ) ; if ( isComposite ( 15 ) ) Console . WriteLine ( " true " ) ; else Console . WriteLine ( " false " ) ; } } }
Insert minimum number in array so that sum of array becomes prime | C # program to find minimum number to insert in array so their sum is prime ; function to check if a number is prime or not ; Corner case ; Check from 2 to n - 1 ; Find prime number greater than a number ; find prime greater than n ; check if num is prime ; increment num ; To find number to be added so sum of array is prime ; To find sum of array elements ; if sum is already prime return 0 ; To find prime number greater than sum ; Return difference of sum and num ; Driver Code
using System ; class GFG { static bool isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static int findPrime ( int n ) { int num = n + 1 ; while ( num > 0 ) { if ( isPrime ( num ) ) return num ; num = num + 1 ; } return 0 ; } static int minNumber ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; if ( isPrime ( sum ) ) return 0 ; int num = findPrime ( sum ) ; return num - sum ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 6 , 8 , 12 } ; int n = arr . Length ; Console . Write ( minNumber ( arr , n ) ) ; } }
Sum of divisors of factorial of a number | C # program to find sum of proper divisor of factorial of a number ; function to calculate factorial ; function to calculate sum of divisor ; Returns sum of divisors of n ! ; Driver Code
using System ; class Division { static int fac ( int n ) { if ( n == 0 ) return 1 ; return n * fac ( n - 1 ) ; } static int div ( int x ) { int ans = 0 ; for ( int i = 1 ; i <= x ; i ++ ) if ( x % i == 0 ) ans += i ; return ans ; } static int sumFactDiv ( int n ) { return div ( fac ( n ) ) ; } public static void Main ( ) { int n = 4 ; Console . Write ( sumFactDiv ( n ) ) ; } }
Sum of divisors of factorial of a number | C # program to find sum of divisors in n ! ; allPrimes [ ] stores all prime numbers less than or equal to n . ; Fills above vector allPrimes [ ] for a given n ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is not a prime , else true . ; Loop to update prime [ ] ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store primes in the vector allPrimes ; Function to find all result of factorial number ; Initialize result ; find exponents of all primes which divides n and less than n ; Current divisor ; Find the highest power ( stored in exp ) ' ▁ ▁ of ▁ allPrimes [ i ] ▁ that ▁ divides ▁ n ▁ using ▁ ▁ Legendre ' s formula . ; Using the divisor function to calculate the sum ; return total divisors ; Driver program to run the cases
using System ; using System . Collections ; class GFG { static ArrayList allPrimes = new ArrayList ( ) ; static void sieve ( int n ) { bool [ ] prime = new bool [ n + 1 ] ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == false ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = true ; } } for ( int p = 2 ; p <= n ; p ++ ) if ( prime [ p ] == false ) allPrimes . Add ( p ) ; } static int factorialDivisors ( int n ) { int result = 1 ; for ( int i = 0 ; i < allPrimes . Count ; i ++ ) { int p = ( int ) allPrimes [ i ] ; int exp = 0 ; while ( p <= n ) { exp = exp + ( n / p ) ; p = p * ( int ) allPrimes [ i ] ; } result = result * ( ( int ) Math . Pow ( ( int ) allPrimes [ i ] , exp + 1 ) - 1 ) / ( ( int ) allPrimes [ i ] - 1 ) ; } return result ; } static void Main ( ) { Console . WriteLine ( factorialDivisors ( 4 ) ) ; } }
Pandigital number in a given base | C # program to check if a number is pandigital in given base . ; Return true if n is pandigit else return false . ; Checking length is less than base ; Traversing each digit of the number . ; If digit is integer ; If digit is alphabet ; Checking hash array , if any index is unmarked . ; Driver code
using System ; class GFG { static bool checkPandigital ( int b , string n ) { if ( n . Length < b ) return false ; bool [ ] hash = new bool [ b ] ; for ( int i = 0 ; i < b ; i ++ ) hash [ i ] = false ; for ( int i = 0 ; i < n . Length ; i ++ ) { if ( n [ i ] >= '0' && n [ i ] <= '9' ) hash [ n [ i ] - '0' ] = true ; else if ( n [ i ] - ' A ' <= b - 11 ) hash [ n [ i ] - ' A ' + 10 ] = true ; } for ( int i = 0 ; i < b ; i ++ ) if ( hash [ i ] == false ) return false ; return true ; } public static void Main ( ) { int b = 13 ; String n = "1298450376ABC " ; if ( checkPandigital ( b , n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Convert a number m to n using minimum number of given operations | C # implementation to convert a number m to n using minimum number of given operations ; function to find minimum number of given operations to convert m to n ; only way is to do - 1 ( m - n ) times ; not possible ; n is greater and n is odd ; perform ' - 1' on m ( or + 1 on n ) ; n is even ; perform ' * 2' on m ( or n / 2 on n ) ; Driver code
using System ; class GFG { static int convert ( int m , int n ) { if ( m == n ) return 0 ; if ( m > n ) return m - n ; if ( m <= 0 && n > 0 ) return - 1 ; if ( n % 2 == 1 ) return 1 + convert ( m , n + 1 ) ; else return 1 + convert ( m , n / 2 ) ; } public static void Main ( ) { int m = 3 , n = 11 ; Console . Write ( " Minimum ▁ number ▁ of ▁ " + " operations ▁ : ▁ " + convert ( m , n ) ) ; } }
Seeds ( Or Seed Roots ) of a number | C # program to find Seed of a number ; Stores product of digits of x in prodDig [ x ] ; If x has single digit ; If digit product is already computed ; If digit product is not computed before . ; Prints all seeds of n ; Find all seeds using prodDig [ ] ; If there was no seed ; Print seeds ; Driver code
using System ; using System . Collections ; class GFG { static int MAX = 10000 ; static int [ ] prodDig = new int [ MAX ] ; static int getDigitProduct ( int x ) { if ( x < 10 ) return x ; if ( prodDig [ x ] != 0 ) return prodDig [ x ] ; int prod = ( x % 10 ) * getDigitProduct ( x / 10 ) ; return ( prodDig [ x ] = prod ) ; } static void findSeed ( int n ) { ArrayList res = new ArrayList ( ) ; for ( int i = 1 ; i <= n / 2 ; i ++ ) if ( i * getDigitProduct ( i ) == n ) res . Add ( i ) ; if ( res . Count == 0 ) { Console . WriteLine ( " NO ▁ seed ▁ exists " ) ; return ; } for ( int i = 0 ; i < res . Count ; i ++ ) Console . WriteLine ( res [ i ] + " ▁ " ) ; } static void Main ( ) { int n = 138 ; findSeed ( n ) ; } }
Number with maximum number of prime factors | C # program to find integer having maximum number of prime factor in first N natural numbers . ; Return smallest number having prime factors . ; Sieve of eratosthenes method to count number of prime factors . ; Finding number having maximum number of prime factor . ; Driver Code
using System ; class GFG { static int maxPrimefactorNum ( int N ) { int [ ] arr = new int [ N + 5 ] ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( arr [ i ] == 0 ) { for ( int j = 2 * i ; j <= N ; j += i ) { arr [ j ] ++ ; } } arr [ i ] = 1 ; } int maxval = 0 , maxint = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( arr [ i ] > maxval ) { maxval = arr [ i ] ; maxint = i ; } } return maxint ; } public static void Main ( ) { int N = 40 ; Console . WriteLine ( maxPrimefactorNum ( N ) ) ; } }
Sum of all Subarrays | Set 1 | Efficient C # program to compute sum of subarray elements ; function compute sum all sub - array ; computing sum of subarray using formula ; return all subarray sum ; Driver code
using System ; class GFG { public static long SubArraySum ( int [ ] arr , int n ) { long result = 0 ; for ( int i = 0 ; i < n ; i ++ ) result += ( arr [ i ] * ( i + 1 ) * ( n - i ) ) ; return result ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 } ; int n = arr . Length ; Console . WriteLine ( " Sum ▁ of ▁ SubArray : ▁ " + SubArraySum ( arr , n ) ) ; } }
Highest power of 2 less than or equal to given number | C # code to find highest power of 2 smaller than or equal to n . ; If i is a power of 2 ; Driver Code
using System ; class GFG { public static int highestPowerof2 ( int n ) { int res = 0 ; for ( int i = n ; i >= 1 ; i -- ) { if ( ( i & ( i - 1 ) ) == 0 ) { res = i ; break ; } } return res ; } static public void Main ( ) { int n = 10 ; Console . WriteLine ( highestPowerof2 ( n ) ) ; } }