codeparrot/xlcost-text-to-code huggingface dataset:
Find out the prime numbers in the form of A + nB or B + nA | C # implementation of the approach ; Utility function to check whether two numbers is co - prime or not ; Utility function to check whether a number is prime or not ; Corner case ; Check from 2 to sqrt ( n ) ; finding the Prime numbers ; Checking whether given numbers are co - prime or not ; To store the N primes ; If ' possible ' is true ; Printing n numbers of prime ; checking the form of a + nb ; Checking the form of b + na ; If ' possible ' is false return - 1 ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static bool coprime ( int a , int b ) { if ( __gcd ( a , b ) == 1 ) return true ; else return false ; } static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n == 2 n == 3 ) return true ; for ( int i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static void findNumbers ( int a , int b , int n ) { bool possible = true ; if ( ! coprime ( a , b ) ) possible = false ; int c1 = 1 ; int c2 = 1 ; int num1 , num2 ; HashSet < int > st = new HashSet < int > ( ) ; if ( possible ) { while ( st . Count != n ) { num1 = a + ( c1 * b ) ; if ( isPrime ( num1 ) ) { st . Add ( num1 ) ; } c1 ++ ; num2 = b + ( c2 * a ) ; if ( isPrime ( num2 ) ) { st . Add ( num2 ) ; } c2 ++ ; } foreach ( int i in st ) Console . Write ( i + " ▁ " ) ; } else Console . Write ( " - 1" ) ; } public static void Main ( String [ ] args ) { int a = 3 ; int b = 5 ; int n = 4 ; findNumbers ( a , b , n ) ; } }
Find the integers that doesnot ends with T1 or T2 when squared and added X | C # program to find the integers that ends with either T1 or T2 when squared and added X ; Function to print the elements Not ending with T1 or T2 ; Flag to check if none of the elements Do not end with t1 or t2 ; Traverse through all the elements ; Temporary variable to store the value ; If the last digit is neither t1 nor t2 then ; Print the number ; Set the flag as False ; If none of the elements meets the specification ; Driver Code ; Test case 1 ; Call the function ; Test case 2 ; Call the function
using System ; class GFG { static void findIntegers ( int n , int [ ] a , int x , int t1 , int t2 ) { bool flag = true ; for ( int i = 0 ; i < n ; i ++ ) { int temp = ( int ) Math . Pow ( a [ i ] , 2 ) + x ; if ( temp % 10 != t1 && temp % 10 != t2 ) { Console . Write ( temp + " ▁ " ) ; flag = false ; } } if ( flag ) { Console . WriteLine ( ) ; Console . Write ( " - 1" ) ; } } public static void Main ( String [ ] args ) { int N = 4 ; int X = 10 ; int T1 = 5 ; int T2 = 6 ; int [ ] a = { 3 , 1 , 4 , 7 } ; findIntegers ( N , a , X , T1 , T2 ) ; N = 4 ; X = 2 ; T1 = 5 ; T2 = 6 ; int [ ] b = { 2 , 18 , 22 , 8 } ; findIntegers ( N , b , X , T1 , T2 ) ; } }
First N terms whose sum of digits is a multiple of 10 | C # Program to Find the Unique elements in linked lists ; Function to return the sum of digits of n ; Add last digit to the sum ; Remove last digit ; Function to return the nth term of the required series ; If sum of digit is already a multiple of 10 then append 0 ; To store the minimum digit that must be appended ; Return n after appending the required digit ; Function to print the first n terms of the required series ; Driver code
using System ; class GFG { readonly static int TEN = 10 ; static int digitSum ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += n % TEN ; n /= TEN ; } return sum ; } static int getNthTerm ( int n ) { int sum = digitSum ( n ) ; if ( sum % TEN == 0 ) return ( n * TEN ) ; int extra = TEN - ( sum % TEN ) ; return ( ( n * TEN ) + extra ) ; } static void firstNTerms ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) Console . Write ( getNthTerm ( i ) + " ▁ " ) ; } public static void Main ( String [ ] args ) { int n = 10 ; firstNTerms ( n ) ; } }
Nearest greater number by interchanging the digits | C # program to find nearest greater value ; Find all the possible permutation of Value A . ; Convert into Integer ; Find the minimum value of A by interchanging the digit of A and store min1 . ; Swap two string character ; Driver code ; Convert integer value into string to find all the permutation of the number ; count = 1 means number greater than B exists
using System ; class GFG { static int min1 = int . MaxValue ; static int count = 0 ; public int permutation ( String str1 , int i , int n , int p ) { if ( i == n ) { int q = int . Parse ( str1 ) ; if ( q - p > 0 && q < min1 ) { min1 = q ; count = 1 ; } } else { for ( int j = i ; j <= n ; j ++ ) { str1 = swap ( str1 , i , j ) ; permutation ( str1 , i + 1 , n , p ) ; str1 = swap ( str1 , i , j ) ; } } return min1 ; } public String swap ( String str , int i , int j ) { char [ ] ch = str . ToCharArray ( ) ; char temp = ch [ i ] ; ch [ i ] = ch [ j ] ; ch [ j ] = temp ; return String . Join ( " " , ch ) ; } public static void Main ( String [ ] args ) { int A = 213 ; int B = 111 ; GFG gfg = new GFG ( ) ; String str1 = A . ToString ( ) ; int len = str1 . Length ; int h = gfg . permutation ( str1 , 0 , len - 1 , B ) ; if ( count == 1 ) Console . WriteLine ( h ) ; else Console . WriteLine ( - 1 ) ; } }
Alcuin 's Sequence | C # program for Alcuin 's Sequence ; find the nth term of Alcuin 's sequence ; return the ans ; print first n terms of Alcuin number ; display the number ; Driver code
using System ; class GFG { static int Alcuin ( int n ) { double _n = n , ans ; ans = Math . Round ( ( _n * _n ) / 12 ) - Math . Floor ( _n / 4 ) * Math . Floor ( ( _n + 2 ) / 4 ) ; return ( int ) ans ; } static void solve ( int n ) { int i = 0 ; for ( i = 1 ; i <= n ; i ++ ) { Console . Write ( Alcuin ( i ) + " , ▁ " ) ; } } public static void Main ( String [ ] args ) { int n = 15 ; solve ( n ) ; } }
Find the final sequence of the array after performing given operations | C # implementation of the approach ; Driver Code
using System ; class GFG { static int [ ] solve ( int [ ] arr , int n ) { int [ ] b = new int [ n ] ; int p = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { b [ p ] = arr [ i -- ] ; if ( i >= 0 ) b [ n - 1 - p ] = arr [ i ] ; p ++ ; } return b ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; int [ ] b = solve ( arr , n ) ; Console . WriteLine ( " [ " + String . Join ( " , " , b ) + " ] " ) ; } }
Check if the product of every contiguous subsequence is different or not in a number | C # implementation of the approach ; Function that returns true if the product of every digit of a contiguous subsequence is distinct ; To store the given number as a string ; Append all the digits starting from the end ; Reverse the string to get the original number ; Store size of the string ; Set to store product of each contiguous subsequence ; Find product of every contiguous subsequence ; If current product already exists in the set ; Utility reverse function ; Driver code
using System ; using System . Collections . Generic ; class GFG { static Boolean productsDistinct ( int N ) { String s = " " ; while ( N > 0 ) { s += ( char ) ( N % 10 + '0' ) ; N /= 10 ; } s = reverse ( s ) ; int sz = s . Length ; HashSet < int > se = new HashSet < int > ( ) ; for ( int i = 0 ; i < sz ; i ++ ) { int product = 1 ; for ( int j = i ; j < sz ; j ++ ) { product *= ( int ) ( s [ j ] - '0' ) ; if ( se . Contains ( product ) ) return false ; else se . Add ( product ) ; } } return true ; } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r ; r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { int N = 2345 ; if ( productsDistinct ( N ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Number of ways to arrange 2 * N persons on the two sides of a table with X and Y persons on opposite sides | C # implementation for the above approach ; Function to returns factorial of n ; Function to find nCr ; Function to find the number of ways to arrange 2 * N persons ; Driver code ; Function call
using System ; class GFG { static int factorial ( int n ) { if ( n <= 1 ) return 1 ; return n * factorial ( n - 1 ) ; } static int nCr ( int n , int r ) { return factorial ( n ) / ( factorial ( n - r ) * factorial ( r ) ) ; } static int NumberOfWays ( int n , int x , int y ) { return nCr ( 2 * n - x - y , n - x ) * factorial ( n ) * factorial ( n ) ; } public static void Main ( String [ ] args ) { int n = 5 , x = 4 , y = 2 ; Console . WriteLine ( NumberOfWays ( n , x , y ) ) ; } }
Find smallest positive number Y such that Bitwise AND of X and Y is Zero | C # program to find smallest number Y for a given value of X such that X AND Y is zero ; Method to find smallest number Y for a given value of X such that X AND Y is zero ; Convert the number into its binary form ; Case 1 : If all bits are ones , then return the next number ; Case 2 : find the first 0 - bit index and return the Y ; To store the count of set bits ; Driver code
using System ; class GFG { static long findSmallestNonZeroY ( long A_num ) { String A_binary = Convert . ToString ( A_num , 2 ) ; long B = 1 ; int len = A_binary . Length ; int no_ones = bitCount ( A_num ) ; if ( len == no_ones ) { return A_num + 1 ; } for ( int i = 0 ; i < len ; i ++ ) { char ch = A_binary [ len - i - 1 ] ; if ( ch == '0' ) { B = ( long ) Math . Pow ( 2.0 , ( double ) i ) ; break ; } } return B ; } static int bitCount ( long x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } public static void Main ( String [ ] args ) { long X = findSmallestNonZeroY ( 10 ) ; Console . WriteLine ( X ) ; } }
Maximum element in an array such that its previous and next element product is maximum | C # implementation of the approach ; Function to return the largest element such that its previous and next element product is maximum ; Calculate the product of the previous and the next element for the current element ; Update the maximum product ; If current product is equal to the current maximum product then choose the maximum element ; Driver code
using System ; class GFG { static int maxElement ( int [ ] a , int n ) { if ( n < 3 ) return - 1 ; int maxElement = a [ 0 ] ; int maxProd = a [ n - 1 ] * a [ 1 ] ; for ( int i = 1 ; i < n ; i ++ ) { int currProd = a [ i - 1 ] * a [ ( i + 1 ) % n ] ; if ( currProd > maxProd ) { maxProd = currProd ; maxElement = a [ i ] ; } else if ( currProd == maxProd ) { maxElement = Math . Max ( maxElement , a [ i ] ) ; } } return maxElement ; } public static void Main ( ) { int [ ] a = { 5 , 6 , 4 , 3 , 2 } ; int n = a . Length ; Console . WriteLine ( maxElement ( a , n ) ) ; } }
Total ways of choosing X men and Y women from a total of M men and W women | C # implementation of the approach ; Function to return the value of ncr effectively ; Initialize the answer ; Divide simultaneously by i to avoid overflow ; Function to return the count of required ways ; Driver code
using System ; class GFG { static int ncr ( int n , int r ) { int ans = 1 ; for ( int i = 1 ; i <= r ; i += 1 ) { ans *= ( n - r + i ) ; ans /= i ; } return ans ; } static int totalWays ( int X , int Y , int M , int W ) { return ( ncr ( M , X ) * ncr ( W , Y ) ) ; } static public void Main ( ) { int X = 4 , Y = 3 , M = 6 , W = 5 ; Console . WriteLine ( totalWays ( X , Y , M , W ) ) ; } }
Generate elements of the array following given conditions | C # implementation of the approach ; Function to generate the required array ; Initialize cnt variable for assigning unique value to prime and its multiples ; When we get a prime for the first time then assign a unique smallest value to it and all of its multiples ; Print the generated array ; Driver code
using System ; class GFG { static void specialSieve ( int n ) { int cnt = 0 ; int [ ] prime = new int [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) prime [ i ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( ! ( prime [ i ] > 0 ) ) { cnt ++ ; for ( int j = i ; j <= n ; j += i ) prime [ j ] = cnt ; } } for ( int i = 2 ; i <= n ; i ++ ) Console . Write ( prime [ i ] + " ▁ " ) ; } public static void Main ( ) { int n = 6 ; specialSieve ( n ) ; } }
Find the node whose sum with X has maximum set bits | C # implementation of the approach ; number of set bits ; Function to perform dfs to find the maximum value ; If current set bits value is greater than the current maximum ; If count is equal to the maximum then choose the node with minimum value ; Driver code ; Weights of the node ; Edges of the tree
using System ; using System . Collections . Generic ; class GFG { static int maximum = int . MinValue , x , ans = int . MaxValue ; static List < List < int > > graph = new List < List < int > > ( ) ; static List < int > weight = new List < int > ( ) ; static int __builtin_popcount ( int x ) { int c = 0 ; for ( int i = 0 ; i < 60 ; i ++ ) if ( ( ( x >> i ) & 1 ) != 0 ) c ++ ; return c ; } static void dfs ( int node , int parent ) { int a = __builtin_popcount ( weight [ node ] + x ) ; if ( maximum < a ) { maximum = a ; ans = node ; } else if ( maximum == a ) ans = Math . Min ( ans , node ) ; for ( int i = 0 ; i < graph [ node ] . Count ; i ++ ) { if ( graph [ node ] [ i ] == parent ) continue ; dfs ( graph [ node ] [ i ] , node ) ; } } public static void Main ( ) { x = 15 ; weight . Add ( 0 ) ; weight . Add ( 5 ) ; weight . Add ( 10 ) ; weight . Add ( 11 ) ; ; weight . Add ( 8 ) ; weight . Add ( 6 ) ; for ( int i = 0 ; i < 100 ; i ++ ) graph . Add ( new List < int > ( ) ) ; graph [ 1 ] . Add ( 2 ) ; graph [ 2 ] . Add ( 3 ) ; graph [ 2 ] . Add ( 4 ) ; graph [ 1 ] . Add ( 5 ) ; dfs ( 1 , 1 ) ; Console . Write ( ans ) ; } }
Count Pairs from two arrays with even sum | C # implementation of the approach ; Function to return count of required pairs ; Count of odd and even numbers from both the arrays ; Find the count of odd and even elements in a [ ] ; Find the count of odd and even elements in b [ ] ; Count the number of pairs ; Return the number of pairs ; Driver code
using System ; class GFG { static int count_pairs ( int [ ] a , int [ ] b , int n , int m ) { int odd1 = 0 , even1 = 0 ; int odd2 = 0 , even2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) odd1 ++ ; else even1 ++ ; } for ( int i = 0 ; i < m ; i ++ ) { if ( b [ i ] % 2 == 1 ) odd2 ++ ; else even2 ++ ; } int pairs = Math . Min ( odd1 , odd2 ) + Math . Min ( even1 , even2 ) ; return pairs ; } public static void Main ( ) { int [ ] a = { 9 , 14 , 6 , 2 , 11 } ; int [ ] b = { 8 , 4 , 7 , 20 } ; int n = a . Length ; int m = b . Length ; Console . WriteLine ( count_pairs ( a , b , n , m ) ) ; } }
Find an index such that difference between product of elements before and after it is minimum |  ; Function to find index ; Array to store log values of elements ; Prefix Array to Maintain Sum of log values till index i ; Answer Index ; Find minimum absolute value ; Driver code
using System ; using System . Collections ; class GFG { public static void solve ( int [ ] Array , int N ) { double [ ] Arraynew = new double [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { Arraynew [ i ] = Math . Log ( Array [ i ] ) ; } double [ ] prefixsum = new double [ N ] ; prefixsum [ 0 ] = Arraynew [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { prefixsum [ i ] = prefixsum [ i - 1 ] + Arraynew [ i ] ; } int answer = 0 ; double minabs = Math . Abs ( prefixsum [ N - 1 ] - 2 * prefixsum [ 0 ] ) ; for ( int i = 1 ; i < N - 1 ; i ++ ) { double ans1 = Math . Abs ( prefixsum [ N - 1 ] - 2 * prefixsum [ i ] ) ; if ( ans1 < minabs ) { minabs = ans1 ; answer = i ; } } Console . WriteLine ( " Index ▁ is : ▁ " + answer ) ; } public static void Main ( string [ ] args ) { int [ ] Array = { 1 , 4 , 12 , 2 , 6 } ; int N = 5 ; solve ( Array , N ) ; } }
Find the value of N when F ( N ) = f ( a ) + f ( b ) where a + b is the minimum possible and a * b = N | C # implementation of the approach ; Function to return the value of F ( N ) ; Base cases ; Count the number of times a number if divisible by 2 ; Return the summation ; Driver code
using System ; class GFG { static int getValueOfF ( int n ) { if ( n == 1 ) return 0 ; if ( n == 2 ) return 1 ; int cnt = 0 ; while ( n % 2 == 0 ) { cnt += 1 ; n /= 2 ; } return 2 * cnt ; } static public void Main ( ) { int n = 20 ; Console . WriteLine ( getValueOfF ( n ) ) ; } }
Find the sum of the number of divisors | C # code for above given approach ; To store the number of divisors ; Function to find the number of divisors of all numbers in the range 1 to n ; For every number 1 to n ; Increase divisors count for every number ; Function to find the sum of divisors ; To store sum ; Count the divisors ; Driver code ; Function call
using System ; class GFG { static int N = 100005 ; static int mod = 1000000007 ; static int [ ] cnt = new int [ N ] ; static void Divisors ( ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j * i < N ; j ++ ) { cnt [ i * j ] ++ ; } } } static int Sumofdivisors ( int A , int B , int C ) { int sum = 0 ; Divisors ( ) ; for ( int i = 1 ; i <= A ; i ++ ) { for ( int j = 1 ; j <= B ; j ++ ) { for ( int k = 1 ; k <= C ; k ++ ) { int x = i * j * k ; sum += cnt [ x ] ; if ( sum >= mod ) { sum -= mod ; } } } } return sum ; } public static void Main ( String [ ] args ) { int A = 5 , B = 6 , C = 7 ; Console . WriteLine ( Sumofdivisors ( A , B , C ) ) ; } }
Find ( 1 ^ n + 2 ^ n + 3 ^ n + 4 ^ n ) mod 5 | Set 2 | C # implementation of the approach ; Function to return A mod B ; length of the string ; to store required answer ; Function to return ( 1 ^ n + 2 ^ n + 3 ^ n + 4 ^ n ) % 5 ; Calculate and return ans ; Driver code
using System ; class GFG { static int A_mod_B ( string N , int a ) { int len = N . Length ; int ans = 0 ; for ( int i = 0 ; i < len ; i ++ ) ans = ( ans * 10 + ( int ) N [ i ] - '0' ) % a ; return ans % a ; } static int findMod ( string N ) { int mod = A_mod_B ( N , 4 ) ; int ans = ( 1 + ( int ) Math . Pow ( 2 , mod ) + ( int ) Math . Pow ( 3 , mod ) + ( int ) Math . Pow ( 4 , mod ) ) ; return ( ans % 5 ) ; } public static void Main ( ) { string N = "4" ; Console . WriteLine ( findMod ( N ) ) ; } }
Elements greater than the previous and next element in an Array | c # program to print elements greater than the previous and next element in an Array ; Function to print elements greater than the previous and next element in an Array ; Traverse array from index 1 to n - 2 and check for the given condition ; Driver Code
using System ; public class GfG { public static void printElements ( int [ ] arr , int n ) { for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] && arr [ i ] > arr [ i + 1 ] ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 2 , 3 , 1 , 5 , 4 , 9 , 8 , 7 , 5 } ; int n = arr . Length ; printElements ( arr , n ) ; } }
Sum of the series Kn + ( K ( n | C # program of above approach ; Recursive C program to compute modular power ; Base cases ; If B is even ; If B is odd ; Function to return sum ; Driver code
using System ; class GFG { static int exponent ( int A , int B ) { if ( A == 0 ) return 0 ; if ( B == 0 ) return 1 ; int y ; if ( B % 2 == 0 ) { y = exponent ( A , B / 2 ) ; y = ( y * y ) ; } else { y = A ; y = ( y * exponent ( A , B - 1 ) ) ; } return y ; } static int sum ( int k , int n ) { int sum = exponent ( k , n + 1 ) - exponent ( k - 1 , n + 1 ) ; return sum ; } public static void Main ( ) { int n = 3 ; int K = 3 ; Console . WriteLine ( sum ( K , n ) ) ; } }
Minimize the cost to split a number | C # implementation of the approach ; check if a number is prime or not ; run a loop upto square root of x ; Function to return the minimized cost ; If n is prime ; If n is odd and can be split into ( prime + 2 ) then cost will be 1 + 1 = 2 ; Every non - prime even number can be expressed as the sum of two primes ; n is odd so n can be split into ( 3 + even ) further even part can be split into ( prime + prime ) ( 3 + prime + prime ) will cost 3 ; Driver code
using System ; public class GFG { static bool isPrime ( int x ) { for ( int i = 2 ; i * i <= x ; i ++ ) { if ( x % i == 0 ) return false ; } return true ; } static int minimumCost ( int n ) { if ( isPrime ( n ) ) return 1 ; if ( n % 2 == 1 && isPrime ( n - 2 ) ) return 2 ; if ( n % 2 == 0 ) return 2 ; return 3 ; } public static void Main ( String [ ] args ) { int n = 6 ; Console . WriteLine ( minimumCost ( n ) ) ; } }
Find amount of water wasted after filling the tank | C # program to find the volume of water wasted ; Function to calculate amount of wasted water ; filled amount of water in one minute ; total time taken to fill the tank because of leakage ; wasted amount of water ; Driver code
using System ; class GFG { static double wastedWater ( double V , double M , double N ) { double wasted_amt , amt_per_min , time_to_fill ; amt_per_min = M - N ; time_to_fill = V / amt_per_min ; wasted_amt = N * time_to_fill ; return wasted_amt ; } public static void Main ( ) { double V , M , N ; V = 700 ; M = 10 ; N = 3 ; Console . WriteLine ( wastedWater ( V , M , N ) ) ; V = 1000 ; M = 100 ; N = 50 ; Console . WriteLine ( wastedWater ( V , M , N ) ) ; } }
Smallest and Largest N | C # implementation of the approach ; Function to print the largest and the smallest n - digit perfect cube ; Smallest n - digit perfect cube ; Largest n - digit perfect cube ; Driver code
using System ; class GFG { static void nDigitPerfectCubes ( int n ) { int smallest = ( int ) Math . Pow ( Math . Ceiling ( MathF . Cbrt ( ( float ) Math . Pow ( 10 , ( n - 1 ) ) ) ) , 3 ) ; Console . Write ( smallest + " ▁ " ) ; int largest = ( int ) Math . Pow ( Math . Ceiling ( MathF . Cbrt ( ( float ) Math . Pow ( 10 , ( n ) ) ) ) - 1 , 3 ) ; Console . Write ( largest ) ; } static void Main ( ) { int n = 3 ; nDigitPerfectCubes ( n ) ; } }
Count numbers which are divisible by all the numbers from 2 to 10 | C # implementation of the approach ; Function to return the count of numbers from 1 to n which are divisible by all the numbers from 2 to 10 ; Driver code
using System ; class GFG { static int countNumbers ( int n ) { return ( n / 2520 ) ; } public static void Main ( String [ ] args ) { int n = 3000 ; Console . WriteLine ( countNumbers ( n ) ) ; } }
Sum of all odd factors of numbers in the range [ l , r ] | C # implementation of the approach ; Function to calculate the prefix sum of all the odd factors ; Add i to all the multiples of i ; Update the prefix sum ; Function to return the sum of all the odd factors of the numbers in the given range ; Driver code
using System ; class GFG { public static int MAX = 100001 ; public static int [ ] prefix = new int [ MAX ] ; public static void sieve_modified ( ) { for ( int i = 1 ; i < MAX ; i += 2 ) { for ( int j = i ; j < MAX ; j += i ) { prefix [ j ] += i ; } } for ( int i = 1 ; i < MAX ; i ++ ) { prefix [ i ] += prefix [ i - 1 ] ; } } public static int sumOddFactors ( int L , int R ) { return ( prefix [ R ] - prefix [ L - 1 ] ) ; } public static void Main ( string [ ] args ) { sieve_modified ( ) ; int l = 6 , r = 10 ; Console . WriteLine ( sumOddFactors ( l , r ) ) ; } }
Number of ways in which an item returns back to its initial position in N swaps in array of size K | C # program to find the number of ways in which an item returns back to its initial position in N swaps in an array of size K ; Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Function to return the number of ways ; Base case ; Recursive case F ( n ) = ( k - 1 ) ^ ( n - 1 ) - F ( n - 1 ) . ; Driver code ; Function calling
using System ; class GFG { static int mod = 1000000007 ; public static int power ( int x , int y ) { int p = mod ; int res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } public static int solve ( int n , int k ) { if ( n == 1 ) return 0 ; return ( power ( ( k - 1 ) , n - 1 ) % mod - solve ( n - 1 , k ) + mod ) % mod ; } public static void Main ( string [ ] args ) { int n = 4 , k = 5 ; Console . Write ( solve ( n , k ) ) ; } }