text
stringlengths 17
3.65k
| code
stringlengths 60
5.26k
|
---|---|
Ways to express a number as product of two different factors | C # program to find number of ways in which number expressed as product of two different factors ; To count number of ways in which number expressed as product of two different numbers ; To store count of such pairs ; Counting number of pairs upto sqrt ( n ) - 1 ; To return count of pairs ; Driver program to test countWays ( )
|
using System ; public class main { static int countWays ( int n ) { int count = 0 ; for ( int i = 1 ; i * i < n ; i ++ ) if ( n % i == 0 ) count ++ ; return count ; } public static void Main ( ) { int n = 12 ; Console . WriteLine ( countWays ( n ) ) ; } }
|
Count divisors of n that have at | Program to count divisors of n that have at least one digit common with n ; function to return true if any digit of m is present in hash [ ] . ; check till last digit ; if number is also present in the original number then return true ; if no number matches then return 1 ; Count the no of divisors that have at least 1 digits same ; Store digits present in n in a hash [ ] ; marks that the number is present ; last digit removed ; loop to traverse from 1 to sqrt ( n ) to count divisors ; if i is the factor ; call the function to check if any digits match or not ; if n / i != i then a different number , then check it also ; return the answer ; Driver code
|
using System ; class GFG { static bool isDigitPresent ( int m , bool [ ] hash ) { while ( m > 0 ) { if ( hash [ m % 10 ] ) return true ; m = m / 10 ; } return false ; } static int countDivisibles ( int n ) { bool [ ] hash = new bool [ 10 ] ; int m = n ; while ( m > 0 ) { hash [ m % 10 ] = true ; m = m / 10 ; } int ans = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( isDigitPresent ( i , hash ) ) ans ++ ; if ( n / i != i ) { if ( isDigitPresent ( n / i , hash ) ) ans ++ ; } } } return ans ; } public static void Main ( ) { int n = 15 ; Console . Write ( countDivisibles ( n ) ) ; } }
|
Doolittle Algorithm : LU Decomposition | C # Program to decompose a matrix into lower and upper triangular matrix ; Decomposing matrix into Upper and Lower triangular matrix ; Upper Triangular ; Summation of L ( i , j ) * U ( j , k ) ; Evaluating U ( i , k ) ; Lower Triangular ; lower [ i , i ] = 1 ; Diagonal as 1 ; Summation of L ( k , j ) * U ( j , i ) ; Evaluating L ( k , i ) ; setw is for displaying nicely ; Displaying the result : ; Lower ; Upper ; Driver code
|
using System ; class GFG { static int MAX = 100 ; static String s = " " ; static void luDecomposition ( int [ , ] mat , int n ) { int [ , ] lower = new int [ n , n ] ; int [ , ] upper = new int [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int k = i ; k < n ; k ++ ) { int sum = 0 ; for ( int j = 0 ; j < i ; j ++ ) sum += ( lower [ i , j ] * upper [ j , k ] ) ; upper [ i , k ] = mat [ i , k ] - sum ; } for ( int k = i ; k < n ; k ++ ) { if ( i == k ) else { int sum = 0 ; for ( int j = 0 ; j < i ; j ++ ) sum += ( lower [ k , j ] * upper [ j , i ] ) ; lower [ k , i ] = ( mat [ k , i ] - sum ) / upper [ i , i ] ; } } } Console . WriteLine ( setw ( 2 ) + " TABSYMBOL Lower ▁ Triangular " + setw ( 10 ) + " Upper ▁ Triangular " ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) Console . Write ( setw ( 4 ) + lower [ i , j ] + " TABSYMBOL " ) ; Console . Write ( " TABSYMBOL " ) ; for ( int j = 0 ; j < n ; j ++ ) Console . Write ( setw ( 4 ) + upper [ i , j ] + " TABSYMBOL " ) ; Console . Write ( " STRNEWLINE " ) ; } } static String setw ( int noOfSpace ) { s = " " ; for ( int i = 0 ; i < noOfSpace ; i ++ ) s += " ▁ " ; return s ; } public static void Main ( String [ ] arr ) { int [ , ] mat = { { 2 , - 1 , - 2 } , { - 4 , 6 , 3 } , { - 4 , - 2 , 8 } } ; luDecomposition ( mat , 3 ) ; } }
|
Divide number into two parts divisible by given numbers | C # program to break the number string into two divisible parts by given numbers ; method prints divisible parts if possible , otherwise prints ' Not ▁ possible ' ; creating arrays to store reminder ; looping over all suffix and storing reminder with f ; getting suffix reminder from previous suffix reminder ; looping over all prefix and storing reminder with s ; getting prefix reminder from next prefix reminder ; updating base1 value ; now looping over all reminders to check partition condition ; if both reminders are 0 and digit itself is not 0 , then print result and return ; if we reach here , then string can ' be partitioned under constraints ; Driver Code
|
using System ; class GFG { static void printTwoDivisibleParts ( String num , int f , int s ) { int N = num . Length ; int [ ] prefixReminder = new int [ N + 1 ] ; int [ ] suffixReminder = new int [ N + 1 ] ; suffixReminder [ 0 ] = 0 ; for ( int i = 1 ; i < N ; i ++ ) suffixReminder [ i ] = ( suffixReminder [ i - 1 ] * 10 + ( num [ i - 1 ] - '0' ) ) % f ; prefixReminder [ N ] = 0 ; int base1 = 1 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { prefixReminder [ i ] = ( prefixReminder [ i + 1 ] + ( num [ i ] - '0' ) * base1 ) % s ; base1 = ( base1 * 10 ) % s ; } for ( int i = 0 ; i < N ; i ++ ) { if ( prefixReminder [ i ] == 0 && suffixReminder [ i ] == 0 && num [ i ] != '0' ) { Console . WriteLine ( num . Substring ( 0 , i ) + " ▁ " + num . Substring ( i ) ) ; return ; } } Console . WriteLine ( " Not ▁ Possible " ) ; } public static void Main ( ) { String num = "246904096" ; int f = 12345 ; int s = 1024 ; printTwoDivisibleParts ( num , f , s ) ; } }
|
Number of subarrays whose minimum and maximum are same | Program to count number of subarrays having same minimum and maximum . ; calculate the no of contiguous subarrays which has the same minimum and maximum ; stores the answer ; loop to traverse from 0 - n ; start checking subarray from next element ; traverse for finding subarrays ; if the elements are same then we check further and keep a count of same numbers in ' r ' ; the no of elements in between r and i with same elements . ; the no . of subarrays that can be formed between i and r ; again start checking from the next index ; returns answer ; Driver program
|
using System ; class Subarray { static int calculate ( int [ ] a , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int r = i + 1 ; for ( int j = r ; j < n ; j ++ ) { if ( a [ i ] == a [ j ] ) r += 1 ; else break ; } int d = r - i ; ans += ( d * ( d + 1 ) / 2 ) ; i = r - 1 ; } return ans ; } public static void Main ( ) { int [ ] a = { 2 , 4 , 5 , 3 , 3 , 3 } ; Console . WriteLine ( calculate ( a , a . Length ) ) ; } }
|
Count of numbers satisfying m + sum ( m ) + sum ( sum ( m ) ) = N | C # program to count numbers satisfying equation . ; function that returns sum of digits in a number ; initially sum of digits is 0 ; loop runs till all digits have been extracted ; last digit from backside ; sums up the digits ; the number is reduced to the number removing the last digit ; returns the sum of digits in a number ; function to calculate the count of such occurrences ; counter to calculate the occurrences ; loop to traverse from n - 97 to n ; calls the function to calculate the sum of digits of i ; calls the function to calculate the sum of digits of a ; if the summation is equal to n then increase counter by 1 ; returns the count ; Driver Code ; calling the function
|
using System ; class GFG { static int sum ( int n ) { int rem = 0 ; int sum_of_digits = 0 ; while ( n > 0 ) { rem = n % 10 ; sum_of_digits += rem ; n = n / 10 ; } return sum_of_digits ; } static int count ( int n ) { int c = 0 ; for ( int i = n - 97 ; i <= n ; i ++ ) { int a = sum ( i ) ; int b = sum ( a ) ; if ( ( i + a + b ) == n ) { c += 1 ; } } return c ; } public static void Main ( ) { int n = 9939 ; Console . Write ( count ( n ) ) ; } }
|
Check if a number is power of k using base changing method | C # program to check if a number can be raised to k ; loop to change base n to base = k ; Find current digit in base k ; If digit is neither 0 nor 1 ; Make sure that only one 1 is present . ; Driver code
|
using System ; class GFG { static bool isPowerOfK ( int n , int k ) { bool oneSeen = false ; while ( n > 0 ) { int digit = n % k ; if ( digit > 1 ) return false ; if ( digit == 1 ) { if ( oneSeen ) return false ; oneSeen = true ; } n /= k ; } return true ; } public static void Main ( ) { int n = 64 , k = 4 ; if ( isPowerOfK ( n , k ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
|
Check if number is palindrome or not in Octal | C # program to check if octal representation of a number is prime ; Function to Check no is in octal or not ; Function To check no is palindrome or not ; If number is already in octal , we traverse digits using repeated division with 10. Else we traverse digits using repeated division with 8 ; To store individual digits ; Traversing all digits ; checking if octal no is palindrome ; Driver Code
|
using System ; class GFG { static long MAX_DIGITS = 20 ; static long isOctal ( long n ) { while ( n > 0 ) { if ( ( n % 10 ) >= 8 ) return 0 ; else n = n / 10 ; } return 1 ; } static long isPalindrome ( long n ) { long divide = ( isOctal ( n ) == 0 ) ? 8 : 10 ; long [ ] octal = new long [ MAX_DIGITS ] ; long i = 0 ; while ( n != 0 ) { octal [ i ++ ] = n % divide ; n = n / divide ; } for ( long j = i - 1 , k = 0 ; k <= j ; j -- , k ++ ) if ( octal [ j ] != octal [ k ] ) return 0 ; return 1 ; } static int Main ( ) { long n = 97 ; if ( isPalindrome ( n ) > 0 ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; return 0 ; } }
|
Find all factorial numbers less than or equal to n | C # program to find all factorial numbers smaller than or equal to n . ; Compute next factorial using previous ; Driver code
|
using System ; class GFG { static void printFactorialNums ( int n ) { int fact = 1 ; int x = 2 ; while ( fact <= n ) { Console . Write ( fact + " ▁ " ) ; fact = fact * x ; x ++ ; } } public static void Main ( ) { int n = 100 ; printFactorialNums ( n ) ; } }
|
Happy Numbers | C # program to check if a number is happy number ; Returns sum of squares of digits of a number n . For example for n = 12 it returns 1 + 4 = 5 ; Returns true if n is Happy number else returns false . ; A set to store numbers during repeated square sum process ; Keep replacing n with sum of squares of digits until we either reach 1 or we endup in a cycle ; Number is Happy if we reach 1 ; Replace n with sum of squares of digits ; If n is already visited , a cycle is formed , means not Happy ; Mark n as visited ; Driver code
|
using System ; using System . Collections . Generic ; class GFG { static int sumDigitSquare ( int n ) { int sq = 0 ; while ( n > 0 ) { int digit = n % 10 ; sq += digit * digit ; n = n / 10 ; } return sq ; } static bool isHappy ( int n ) { HashSet < int > s = new HashSet < int > ( ) ; s . Add ( n ) ; while ( true ) { if ( n == 1 ) return true ; n = sumDigitSquare ( n ) ; if ( s . Contains ( n ) ) return false ; s . Add ( n ) ; } } public static void Main ( ) { int n = 23 ; if ( isHappy ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
|
Check whether a number has exactly three distinct factors or not | C # program to check whether number has exactly three distinct factors or not ; Utility function to check whether a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check whether given number has three distinct factors or not ; Find square root of number ; Check whether number is perfect square or not ; If number is perfect square , check whether square root is prime or not ; Driver program
|
using System ; public class GFG { static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static bool isThreeDisctFactors ( long n ) { int sq = ( int ) Math . Sqrt ( n ) ; if ( 1L L * sq * sq != n ) return false ; return isPrime ( sq ) ? true : false ; } static public void Main ( ) { long num = 9 ; if ( isThreeDisctFactors ( num ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; num = 15 ; if ( isThreeDisctFactors ( num ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; num = 12397923568441 ; if ( isThreeDisctFactors ( num ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
|
Find the last digit when factorial of A divides factorial of B | C # program to find last digit of a number obtained by dividing factorial of a number with factorial of another number . ; Function which computes the last digit of resultant of B ! / A ! ; If A = B , B ! = A ! and B ! / A ! = 1 ; If difference ( B - A ) >= 5 , answer = 0 ; If non of the conditions are true , we iterate from A + 1 to B and multiply them . We are only concerned for the last digit , thus we take modulus of 10 ; Driver Code
|
using System ; class GFG { static int computeLastDigit ( long A , long B ) { int variable = 1 ; if ( A == B ) return 1 ; else if ( ( B - A ) >= 5 ) return 0 ; else { for ( long i = A + 1 ; i <= B ; i ++ ) variable = ( int ) ( variable * ( i % 10 ) ) % 10 ; return variable % 10 ; } } public static void Main ( ) { Console . WriteLine ( computeLastDigit ( 2632 , 2634 ) ) ; } }
|
Program for sum of arithmetic series | C # Program to find the sum of arithmetic series . ; Function to find sum of series . ; Driver function
|
using System ; class GFG { static float sumOfAP ( float a , float d , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + a ; a = a + d ; } return sum ; } public static void Main ( ) { int n = 20 ; float a = 2.5f , d = 1.5f ; Console . Write ( sumOfAP ( a , d , n ) ) ; } }
|
Product of factors of number | C # program to calculate product of factors of number ; function to product the factors ; If factors are equal , multiply only once ; Otherwise multiply both ; Driver Code
|
using System ; class GFG { public static long M = 1000000007 ; static long multiplyFactors ( int n ) { long prod = 1 ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) prod = ( prod * i ) % M ; else { prod = ( prod * i ) % M ; prod = ( prod * n / i ) % M ; } } } return prod ; } public static void Main ( ) { int n = 12 ; Console . Write ( multiplyFactors ( n ) ) ; } }
|
Product of factors of number | C # program to calculate product of factors of number ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; function to count the factors ; If factors are equal , count only once ; Otherwise count both ; Calculate product of factors ; If numFactor is odd return power ( n , numFactor / 2 ) * sqrt ( n ) ; Driver Code
|
using System ; class GFG { public static long M = 1000000007 ; static long power ( long x , long y ) { long res = 1 ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % M ; y = ( y >> 1 ) % M ; x = ( x * x ) % M ; } return res ; } static int countFactors ( int n ) { int count = 0 ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) count ++ ; else count += 2 ; } } return count ; } static long multiplyFactors ( int n ) { int numFactor = countFactors ( n ) ; long product = power ( n , numFactor / 2 ) ; if ( numFactor % 2 == 1 ) product = ( product * ( int ) Math . Sqrt ( n ) ) % M ; return product ; } public static void Main ( ) { int n = 12 ; Console . Write ( multiplyFactors ( n ) ) ; } }
|
Decimal representation of given binary string is divisible by 10 or not | C # implementation to check whether decimal representation of given binary number is divisible by 10 or not ; function to check whether decimal representation of given binary number is divisible by 10 or not ; if last digit is '1' , then number is not divisible by 10 ; to accumulate the sum of last digits in perfect powers of 2 ; traverse from the 2 nd last up to 1 st digit in ' bin ' ; if digit in '1' ; calculate digit 's position from the right ; according to the digit 's position, obtain the last digit of the applicable perfect power of 2 ; if last digit is 0 , then divisible by 10 ; not divisible by 10 ; Driver program to test above function
|
using System ; class GFG { static bool isDivisibleBy10 ( String bin ) { int n = bin . Length ; if ( bin [ n - 1 ] == '1' ) return false ; int sum = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( bin [ i ] == '1' ) { int posFromRight = n - i - 1 ; if ( posFromRight % 4 == 1 ) sum = sum + 2 ; else if ( posFromRight % 4 == 2 ) sum = sum + 4 ; else if ( posFromRight % 4 == 3 ) sum = sum + 8 ; else if ( posFromRight % 4 == 0 ) sum = sum + 6 ; } } if ( sum % 10 == 0 ) return true ; return false ; } public static void Main ( ) { String bin = "11000111001110" ; if ( isDivisibleBy10 ( bin ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
|
Tribonacci Numbers | A space optimized based C # program to print first n Tribinocci numbers . ; Initialize first three numbers ; Loop to add previous three numbers for each number starting from 3 and then assign first , second , third to second , third , and curr to third respectively ; Driver code
|
using System ; class GFG { static void printTrib ( int n ) { if ( n < 1 ) return ; int first = 0 , second = 0 ; int third = 1 ; Console . Write ( first + " ▁ " ) ; if ( n > 1 ) Console . Write ( second + " ▁ " ) ; if ( n > 2 ) Console . Write ( second + " ▁ " ) ; for ( int i = 3 ; i < n ; i ++ ) { int curr = first + second + third ; first = second ; second = third ; third = curr ; Console . Write ( curr + " ▁ " ) ; } } public static void Main ( ) { int n = 10 ; printTrib ( n ) ; } }
|
Prime Number of Set Bits in Binary Representation | Set 2 | C # code to find count of numbers having prime number of set bits in their binary representation in the range [ L , R ] ; Function to create an array of prime numbers upto number ' n ' ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as false . A value in prime [ i ] will finally be true if i is Not a prime , else false . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; append all the prime numbers to the list ; Utility function to count the number of set bits ; Driver program ; Here prime numbers are checked till the maximum number of bits possible because that the maximum bit sum possible is the number of bits itself .
|
using System ; using System . Linq ; using System . Collections ; class GFG { static ArrayList SieveOfEratosthenes ( 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 + 1 ; i += p ) prime [ i ] = true ; } ArrayList lis = new ArrayList ( ) ; for ( int p = 2 ; p <= n ; p ++ ) if ( prime [ p ] == false ) lis . Add ( p ) ; return lis ; } static int setBits ( int n ) { return ( int ) Convert . ToString ( n , 2 ) . Count ( c => c == '1' ) ; } public static void Main ( ) { int x = 4 , y = 8 ; int count = 0 ; ArrayList primeArr = new ArrayList ( ) ; primeArr = SieveOfEratosthenes ( Convert . ToInt32 ( Math . Ceiling ( Math . Log ( y , 2.0 ) ) ) ) ; for ( int i = x ; i < y + 1 ; i ++ ) { int temp = setBits ( i ) ; if ( primeArr . Contains ( temp ) ) count += 1 ; } Console . WriteLine ( count ) ; } }
|
Count trailing zeroes present in binary representation of a given number using XOR | C # implementation of the above approach ; Function to print count of trailing zeroes present in binary representation of N ; Stores XOR of N and ( N - 1 ) ; Return count of set bits in res ; Driver Code ; Function call to print the count of trailing zeroes in the binary representation of N
|
using System ; public class GFG { public static int countTrailingZeroes ( int N ) { int res = ( int ) Math . Log ( N ^ ( N - 1 ) , 2.0 ) ; if ( res >= 0 ) return res ; else return 0 ; } static public void Main ( ) { int N = 12 ; Console . WriteLine ( countTrailingZeroes ( N ) ) ; } }
|
Maximum sum of Bitwise XOR of elements with their respective positions in a permutation of size N | C # program for the above approach ; Function to calculate the score ; Stores the possible score for the current permutation ; Traverse the permutation array ; Return the readonly score ; Function to generate all the possible permutation and get the max score ; If [ ] arr length is equal to N process the permutation ; Generating the permutations ; If the current element is chosen ; Mark the current element as true ; Recursively call for next possible permutation ; Backtracking ; Return the ans ; Driver Code ; Stores the permutation ; To display the result
|
using System ; using System . Collections . Generic ; public class GFG { static int calcScr ( List < int > arr ) { int ans = 0 ; for ( int i = 0 ; i < arr . Count ; i ++ ) ans += ( i ^ arr [ i ] ) ; return ans ; } static int getMax ( List < int > arr , int ans , List < Boolean > chosen , int N ) { if ( arr . Count == N ) { ans = Math . Max ( ans , calcScr ( arr ) ) ; return ans ; } for ( int i = 0 ; i < N ; i ++ ) { if ( chosen [ i ] ) continue ; chosen [ i ] = true ; arr . Add ( i ) ; ans = getMax ( arr , ans , chosen , N ) ; chosen [ i ] = false ; arr . Remove ( arr . Count - 1 ) ; } return ans ; } public static void Main ( String [ ] args ) { int N = 2 ; List < int > arr = new List < int > ( ) ; int ans = - 1 ; List < bool > chosen = new List < bool > ( N ) ; for ( int i = 0 ; i < N ; i ++ ) chosen . Add ( false ) ; ans = getMax ( arr , ans , chosen , N ) ; Console . Write ( ans + " STRNEWLINE " ) ; } }
|
Additive Congruence method for generating Pseudo Random Numbers | C # implementation of the above approach ; Function to generate random numbers ; Initialize the seed state ; Traverse to generate required numbers of random numbers ; Follow the additive congruential method ; Driver Code ; Seed value ; Modulus parameter ; Increment term ; Number of Random numbers to be generated ; To store random numbers ; Function call ; Print the generated random numbers
|
using System ; class GFG { static void additiveCongruentialMethod ( int Xo , int m , int c , int [ ] randomNums , int noOfRandomNums ) { randomNums [ 0 ] = Xo ; for ( int i = 1 ; i < noOfRandomNums ; i ++ ) { randomNums [ i ] = ( randomNums [ i - 1 ] + c ) % m ; } } public static void Main ( String [ ] args ) { int Xo = 3 ; int m = 15 ; int c = 2 ; int noOfRandomNums = 20 ; int [ ] randomNums = new int [ noOfRandomNums ] ; additiveCongruentialMethod ( Xo , m , c , randomNums , noOfRandomNums ) ; for ( int i = 0 ; i < noOfRandomNums ; i ++ ) { Console . Write ( randomNums [ i ] + " ▁ " ) ; } } }
|
Second decagonal numbers | C # program for the above approach ; Function to find N - th term in the series ; Driver code
|
using System ; class GFG { static void findNthTerm ( int n ) { Console . WriteLine ( n * ( 4 * n + 3 ) ) ; } public static void Main ( String [ ] args ) { int N = 4 ; findNthTerm ( N ) ; } }
|
65537 | C # program for above approach ; Function to find the nth 65537 - gon Number ; Driver code
|
using System ; class GFG { static int gonNum65537 ( int n ) { return ( 65535 * n * n - 65533 * n ) / 2 ; } public static void Main ( String [ ] args ) { int n = 3 ; Console . Write ( gonNum65537 ( n ) ) ; } }
|
Hexacontatetragon numbers | C # program to find N - th Hexacontatetragon number ; Function to find the nth Hexacontatetragon number ; Driver code
|
using System ; class GFG { static int HexacontatetragonNum ( int n ) { return ( 62 * n * n - 60 * n ) / 2 ; } public static void Main ( ) { int n = 3 ; Console . Write ( HexacontatetragonNum ( n ) ) ; } }
|
Icosikaipentagon Number | C # program for the above approach ; Finding the nth chiliagon number ; Driver code
|
using System ; class GFG { static int Icosikaipentagon ( int n ) { return ( 23 * n * n - 21 * n ) / 2 ; } public static void Main ( ) { int n = 3 ; Console . Write ( "3rd ▁ Icosikaipentagon ▁ Number ▁ is ▁ = ▁ " + Icosikaipentagon ( n ) ) ; } }
|
Chiliagon Number | C # program for the above approach ; Finding the nth chiliagon number ; Driver code
|
using System ; class GFG { static int chiliagonNum ( int n ) { return ( 998 * n * n - 996 * n ) / 2 ; } public static void Main ( ) { int n = 3 ; Console . Write ( "3rd ▁ chiliagon ▁ Number ▁ is ▁ = ▁ " + chiliagonNum ( n ) ) ; } }
|
Pentacontagon number | C # program for above approach ; Finding the nth pentacontagon number ; Driver code
|
using System ; class GFG { static int pentacontagonNum ( int n ) { return ( 48 * n * n - 46 * n ) / 2 ; } public static void Main ( string [ ] args ) { int n = 3 ; Console . Write ( "3rd ▁ pentacontagon ▁ Number ▁ is ▁ = ▁ " + pentacontagonNum ( n ) ) ; } }
|
Array value by repeatedly replacing max 2 elements with their absolute difference | C # program to find the array value by repeatedly replacing max 2 elements with their absolute difference ; Function that return last value of array ; Build a binary max_heap ; For max 2 elements ; Iterate until queue is not empty ; If only 1 element is left ; Return the last remaining value ; Check that difference is non zero ; Finally return 0 ; Driver Code
|
using System ; using System . Collections . Generic ; class GFG { static int lastElement ( int [ ] arr ) { Queue < int > pq = new Queue < int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) pq . Enqueue ( arr [ i ] ) ; int m1 , m2 ; while ( pq . Contains ( 0 ) ) { if ( pq . Count == 1 ) { return pq . Peek ( ) ; } m1 = pq . Dequeue ( ) ; m2 = pq . Peek ( ) ; if ( m1 != m2 ) pq . Enqueue ( m1 - m2 ) ; } return 0 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 4 , 1 , 8 , 1 , 1 } ; Console . WriteLine ( lastElement ( arr ) ) ; } }
|
Number formed by adding product of its max and min digit K times | C # program for the above approach ; Function to find the formed number ; K -- ; M ( 1 ) = N ; Check if minimum digit is 0 ; Function that returns the product of maximum and minimum digit of N number . ; Finds the last digit . ; Moves to next digit ; Driver code
|
using System ; class GFG { public static int formed_no ( int N , int K ) { if ( K == 1 ) { return N ; } int answer = N ; while ( K != 0 ) { int a_current = prod_of_max_min ( answer ) ; if ( a_current == 0 ) break ; answer += a_current ; } return answer ; } static int prod_of_max_min ( int n ) { int largest = 0 ; int smallest = 10 ; while ( n != 0 ) { int r = n % 10 ; largest = Math . Max ( r , largest ) ; smallest = Math . Min ( r , smallest ) ; n = n / 10 ; } return largest * smallest ; } public static void Main ( String [ ] args ) { int N = 487 , K = 100000000 ; Console . WriteLine ( formed_no ( N , K ) ) ; } }
|
Logarithm tricks for Competitive Programming | C # implementation count the number of digits in a number ; Function to count the number of digits in a number ; Driver Code
|
using System ; class GFG { static int countDigit ( double n ) { return ( ( int ) Math . Floor ( Math . Log10 ( n ) + 1 ) ) ; } public static void Main ( ) { double N = 80 ; Console . Write ( countDigit ( N ) ) ; } }
|
Program to find the sum of the series 1 + x + x ^ 2 + x ^ 3 + . . + x ^ n | C # implementation to find the sum of series 1 + x ^ 2 + x ^ 3 + ... . + x ^ n ; C # code to print the sum of the given series ; First Term ; Loop to print the N terms of the series and compute their sum ; Driver Code
|
using System ; class GFG { static double sum ( int x , int n ) { double i , total = 1.0 , multi = x ; Console . Write ( "1 ▁ " ) ; for ( i = 1 ; i < n ; i ++ ) { total = total + multi ; Console . Write ( multi ) ; Console . Write ( " ▁ " ) ; multi = multi * x ; } Console . WriteLine ( ) ; return total ; } public static void Main ( String [ ] args ) { int x = 2 ; int n = 5 ; Console . Write ( " { 0 : F2 } " , sum ( x , n ) ) ; } }
|
Find the remainder when N is divided by 4 using Bitwise AND operator | C # implementation to find N modulo 4 using Bitwise AND operator ; Driver code ; Function to find the remainder ; Bitwise AND with 3 ; return x
|
using System ; class GFG { public static void Main ( ) { int N = 43 ; int ans = findRemainder ( N ) ; Console . Write ( ans ) ; } public static int findRemainder ( int n ) { int x = n & 3 ; return x ; } } is by
|
Find all Autobiographical Numbers with given number of digits | C # implementation to find Autobiographical numbers with length N ; Converting the integer number to string ; Extracting each character from each index one by one and converting into an integer ; initialize count as 0 ; Check if it is equal to the index i if true then increment the count ; It is an Autobiographical number ; Return false if the count and the index number are not equal ; Function to print autobiographical number with given number of digits ; both the boundaries are taken double , so as to satisfy Math . Pow ( ) function 's signature ; Left boundary of interval ; Right boundary of interval ; Flag = 0 implies that the number is not an autobiographical no . ; Driver Code
|
using System ; class autobio { public static bool isAutoBio ( int num ) { String autoStr ; int index , number , i , j , cnt ; autoStr = num . ToString ( ) ; for ( i = 0 ; i < autoStr . Length ; i ++ ) { index = Int32 . Parse ( autoStr [ i ] + " " ) ; cnt = 0 ; for ( j = 0 ; j < autoStr . Length ; j ++ ) { number = Int32 . Parse ( autoStr [ j ] + " " ) ; if ( number == i ) cnt ++ ; } if ( cnt != index ) return false ; } return true ; } public static void findAutoBios ( double n ) { double high , low ; int i , flag = 0 ; low = Math . Pow ( 10.0 , n - 1 ) ; high = Math . Pow ( 10.0 , n ) - 1.0 ; for ( i = ( int ) low ; i <= ( int ) high ; i ++ ) if ( isAutoBio ( i ) ) { flag = 1 ; Console . Write ( i + " , ▁ " ) ; } if ( flag == 0 ) Console . WriteLine ( " There ▁ is ▁ no ▁ Autobiographical ▁ Number " + " with ▁ " + ( int ) n + " ▁ digits " ) ; } public static void Main ( String [ ] args ) { double N = 0 ; findAutoBios ( N ) ; N = 4 ; findAutoBios ( N ) ; } }
|
Check whether the number can be made palindromic after adding K | C # program to check whether the number can be made palindrome number after adding K ; Function to check whether a number is a palindrome or not ; Convert num to string ; Comparing kth character from the beginning and N - kth character from the end . If all the characters match , then the number is a palindrome ; If all the above conditions satisfy , it means that the number is a palindrome ; Driver code
|
using System ; class GFG { static void checkPalindrome ( int num ) { String str = num . ToString ( ) ; int l = 0 , r = str . Length - 1 ; while ( l < r ) { if ( str [ l ] != str [ r ] ) { Console . Write ( " No " ) ; return ; } l ++ ; r -- ; } Console . Write ( " Yes " ) ; return ; } public static void Main ( String [ ] args ) { int n = 19 , k = 3 ; checkPalindrome ( n + k ) ; } }
|
Count of subsets with sum equal to X using Recursion | C # program to print the count of subsets with sum equal to the given value X ; Recursive function to return the count of subsets with sum equal to the given value ; The recursion is stopped at N - th level where all the subsets of the given array have been checked ; Incrementing the count if sum is equal to 0 and returning the count ; Recursively calling the function for two cases Either the element can be counted in the subset If the element is counted , then the remaining sum to be checked is sum - the selected element If the element is not included , then the remaining sum to be checked is the total sum ; Driver code
|
using System ; class GFG { static int subsetSum ( int [ ] arr , int n , int i , int sum , int count ) { if ( i == n ) { if ( sum == 0 ) { count ++ ; } return count ; } count = subsetSum ( arr , n , i + 1 , sum - arr [ i ] , count ) ; count = subsetSum ( arr , n , i + 1 , sum , count ) ; return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int sum = 10 ; int n = arr . Length ; Console . Write ( subsetSum ( arr , n , 0 , sum , 0 ) ) ; } }
|
Distinct Prime Factors of an Array | C # implementation of the above approach ; Function to return an array of prime numbers upto n using Sieve of Eratosthenes ; Function to return distinct prime factors from the given array ; Creating an empty array to store distinct prime factors ; Iterating through all the prime numbers and check if any of the prime numbers is a factor of the given input array ; Driver code ; Finding prime numbers upto 10000 using Sieve of Eratosthenes
|
using System ; using System . Collections . Generic ; class GFG { static List < int > sieve ( int n ) { List < int > prime = new List < int > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) prime . Add ( 0 ) ; int p = 2 ; while ( p * p <= n ) { if ( prime [ p ] == 0 ) { for ( int i = 2 * p ; i < n + 1 ; i += p ) prime [ i ] = 1 ; } p += 1 ; } List < int > allPrimes = new List < int > ( ) ; for ( int i = 2 ; i < n ; i ++ ) { if ( prime [ i ] == 0 ) allPrimes . Add ( i ) ; } return allPrimes ; } static List < int > distPrime ( List < int > arr , List < int > allPrimes ) { List < int > list1 = new List < int > ( ) ; for ( int i = 0 ; i < allPrimes . Count ; i ++ ) { for ( int j = 0 ; j < arr . Count ; j ++ ) { if ( arr [ j ] % allPrimes [ i ] == 0 ) { list1 . Add ( allPrimes [ i ] ) ; break ; } } } return list1 ; } public static void Main ( string [ ] args ) { List < int > allPrimes = new List < int > ( sieve ( 10000 ) ) ; List < int > arr = new List < int > ( ) ; arr . Add ( 15 ) ; arr . Add ( 30 ) ; arr . Add ( 60 ) ; List < int > ans = new List < int > ( distPrime ( arr , allPrimes ) ) ; Console . Write ( " [ " ) ; for ( int i = 0 ; i < ans . Count ; i ++ ) Console . Write ( ans [ i ] + " ▁ " ) ; Console . Write ( " ] " ) ; } }
|
Sum of the count of number of adjacent squares in an M X N grid | C # implementation of the above approach ; function to calculate the sum of all cells adjacent value ; Driver Code
|
using System ; class GFG { static int sum ( int m , int n ) { return 8 * m * n - 6 * m - 6 * n + 4 ; } public static void Main ( String [ ] args ) { int m = 3 , n = 2 ; Console . WriteLine ( sum ( m , n ) ) ; } }
|
Count pairs in an array such that the absolute difference between them is ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € š ¬ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ à ¢ à ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’¢ à ¢ à ¢ â € š ¬ Ã …¾¢¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € š ¬ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ à ¢ à ¢ â ‚¬ Å ¾¢¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ Ã ¢ â € ž ¢¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢¢ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ Ã⠀¦¡¬ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € 𠬦¡¬°¥ K | C # implementation of the approach ; Function to return the count of required pairs ; Sort the given array ; To store the required count ; Update j such that it is always > i ; Find the first element arr [ j ] such that ( arr [ j ] - arr [ i ] ) >= K This is because after this element , all the elements will have absolute difference with arr [ i ] >= k and the count of valid pairs will be ( n - j ) ; Update the count of valid pairs ; Get to the next element to repeat the steps ; Return the count ; Driver code
|
using System ; class GFG { static int count ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int cnt = 0 ; int i = 0 , j = 1 ; while ( i < n && j < n ) { j = ( j <= i ) ? ( i + 1 ) : j ; while ( j < n && ( arr [ j ] - arr [ i ] ) < k ) j ++ ; cnt += ( n - j ) ; i ++ ; } return cnt ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; int k = 2 ; Console . Write ( count ( arr , n , k ) ) ; } }
|
Find the volume of rectangular right wedge | C # implementation of the approach ; Function to return the volume of the rectangular right wedge ; Driver code
|
using System ; class GFG { static double volumeRec ( double a , double b , double e , double h ) { return ( ( ( b * h ) / 6 ) * ( 2 * a + e ) ) ; } public static void Main ( ) { double a = 2 , b = 5 , e = 5 , h = 6 ; Console . WriteLine ( " Volume ▁ = ▁ " + volumeRec ( a , b , e , h ) ) ; } }
|
Count squares with odd side length in Chessboard | C # implementation of the approach ; Function to return the count of odd length squares possible ; To store the required count ; For all odd values of i ; Add the count of possible squares of length i ; Return the required count ; Driver code
|
using System ; class GFG { static int count_square ( int n ) { int count = 0 ; for ( int i = 1 ; i <= n ; i = i + 2 ) { int k = n - i + 1 ; count += ( k * k ) ; } return count ; } public static void Main ( ) { int N = 8 ; Console . WriteLine ( count_square ( N ) ) ; } }
|
Count of elements whose absolute difference with the sum of all the other elements is greater than k | C # implementation of the approach ; Function to return the number of anomalies ; To store the count of anomalies ; To store the sum of the array elements ; Find the sum of the array elements ; Count the anomalies ; Driver code
|
using System ; class GFG { static int countAnomalies ( int [ ] arr , int n , int k ) { int cnt = 0 ; int i , sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( i = 0 ; i < n ; i ++ ) if ( Math . Abs ( arr [ i ] - ( sum - arr [ i ] ) ) > k ) cnt ++ ; return cnt ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 5 } ; int n = arr . Length ; int k = 1 ; Console . WriteLine ( countAnomalies ( arr , n , k ) ) ; } }
|
Find the number of integers x in range ( 1 , N ) for which x and x + 1 have same number of divisors | C # implementation of the approach ; To store number of divisors and Prefix sum of such numbers ; Function to find the number of integers 1 < x < N for which x and x + 1 have the same number of positive divisors ; Count the number of divisors ; Run a loop upto sqrt ( i ) ; If j is divisor of i ; If it is perfect square ; x and x + 1 have same number of positive divisors ; Driver code ; Function call ; Required answer
|
using System ; class GFG { static int N = 100005 ; static int [ ] d = new int [ N ] ; static int [ ] pre = new int [ N ] ; static void Positive_Divisors ( ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { if ( j * j == i ) d [ i ] ++ ; else d [ i ] += 2 ; } } } int ans = 0 ; for ( int i = 2 ; i < N ; i ++ ) { if ( d [ i ] == d [ i - 1 ] ) ans ++ ; pre [ i ] = ans ; } } public static void Main ( String [ ] args ) { Positive_Divisors ( ) ; int n = 15 ; Console . WriteLine ( pre [ n ] ) ; } }
|
Length of the smallest number which is divisible by K and formed by using 1 's only | C # implementation of the approach ; Function to return length of the resultant number ; If K is a multiple of 2 or 5 ; Generate all possible numbers 1 , 11 , 111 , 111 , ... , K 1 's ; If number is divisible by k then return the length ; Driver code
|
using System ; class GFG { static int numLen ( int K ) { if ( K % 2 == 0 K % 5 == 0 ) return - 1 ; int number = 0 ; int len = 1 ; for ( len = 1 ; len <= K ; len ++ ) { number = number * 10 + 1 ; if ( ( number % K == 0 ) ) return len ; } return - 1 ; } static void Main ( ) { int K = 7 ; Console . WriteLine ( numLen ( K ) ) ; } }
|
Find if the given number is present in the infinite sequence or not | C # implementation of the approach ; Function that returns true if the sequence will contain B ; Driver code
|
using System ; class GFG { static bool doesContainB ( int a , int b , int c ) { if ( a == b ) { return true ; } if ( ( b - a ) * c > 0 && ( b - a ) % c == 0 ) { return true ; } return false ; } public static void Main ( ) { int a = 1 , b = 7 , c = 3 ; if ( doesContainB ( a , b , c ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
|
Find a permutation of 2 N numbers such that the result of given expression is exactly 2 K | C # program to find the required permutation of first 2 * N natural numbers ; Function to find the required permutation of first 2 * N natural numbers ; Iterate in blocks of 2 ; We need more increments , so print in reverse order ; We have enough increments , so print in same order ; Driver code
|
using System ; class GFG { static void printPermutation ( int n , int k ) { for ( int i = 1 ; i <= n ; i ++ ) { int x = 2 * i - 1 ; int y = 2 * i ; if ( i <= k ) Console . Write ( y + " ▁ " + x + " ▁ " ) ; else Console . Write ( x + " ▁ " + y + " ▁ " ) ; } } public static void Main ( ) { int n = 2 , k = 1 ; printPermutation ( n , k ) ; } }
|
Maximize the sum of products of the degrees between any two vertices of the tree | C # implementation of above approach ; Function to return the maximum possible sum ; Initialize degree for node u to 2 ; If u is the leaf node or the root node ; Initialize degree for node v to 2 ; If v is the leaf node or the root node ; Update the sum ; Driver code
|
using System ; class GFG { static int maxSum ( int N ) { int ans = 0 ; for ( int u = 1 ; u <= N ; u ++ ) { for ( int v = 1 ; v <= N ; v ++ ) { if ( u == v ) continue ; int degreeU = 2 ; if ( u == 1 u == N ) degreeU = 1 ; int degreeV = 2 ; if ( v == 1 v == N ) degreeV = 1 ; ans += ( degreeU * degreeV ) ; } } return ans ; } static void Main ( ) { int N = 6 ; Console . WriteLine ( maxSum ( N ) ) ; } }
|
Find integers that divides maximum number of elements of the array | C # implementation of the approach ; Function to print the integers that divide the maximum number of elements from the array ; Initialize two lists to store rank and factors ; Start from 2 till the maximum element in arr ; Initialize a variable to count the number of elements it is a factor of ; Maximum rank in the rank list ; Print all the elements with rank m ; Driver code
|
using System ; using System . Collections ; using System . Linq ; class GFG { static void maximumFactor ( int [ ] arr ) { int [ ] rank = new int [ arr . Max ( ) + 1 ] ; int [ ] factors = new int [ arr . Max ( ) + 1 ] ; int g = 0 ; for ( int i = 2 ; i <= arr . Max ( ) ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < arr . Length ; j ++ ) if ( arr [ j ] % i == 0 ) count += 1 ; rank [ g ] = count ; factors [ g ] = i ; g ++ ; } int m = rank . Max ( ) ; for ( int i = 0 ; i < rank . Length ; i ++ ) { if ( ( int ) rank [ i ] == m ) Console . Write ( factors [ i ] + " ▁ " ) ; } } static void Main ( ) { int [ ] arr = { 120 , 15 , 24 , 63 , 18 } ; maximumFactor ( arr ) ; } }
|
Natural Numbers | C # program to find sum of first n natural numbers . ; Returns sum of first n natural numbers ; Driver code
|
using System ; class GFG { static int findSum ( int n ) { int sum = 0 ; for ( int x = 1 ; x <= n ; x ++ ) sum = sum + x ; return sum ; } public static void Main ( ) { int n = 5 ; Console . Write ( findSum ( n ) ) ; } }
|
Median | C # program to find median ; Function for calculating median ; First we sort the array ; check for even case ; Driver Code
|
using System ; class GFG { public static double findMedian ( int [ ] a , int n ) { Array . Sort ( a ) ; if ( n % 2 != 0 ) return ( double ) a [ n / 2 ] ; return ( double ) ( a [ ( n - 1 ) / 2 ] + a [ n / 2 ] ) / 2.0 ; } public static void Main ( ) { int [ ] a = { 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 } ; int n = a . Length ; Console . Write ( " Median ▁ = ▁ " + findMedian ( a , n ) + " STRNEWLINE " ) ; } }
|
Mean | C # program to find mean ; Function for calculating mean ; Driver Code
|
using System ; class GFG { public static double findMean ( int [ ] a , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] ; return ( double ) sum / ( double ) n ; } public static void Main ( ) { int [ ] a = { 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 } ; int n = a . Length ; Console . Write ( " Mean ▁ = ▁ " + findMean ( a , n ) + " STRNEWLINE " ) ; } }
|
Check if the array has an element which is equal to product of remaining elements | C # implementation of above approach ; Function to Check if the array has an element which is equal to product of all the remaining elements ; Storing frequency in map ; Calculate the product of all the elements ; If the prod is a perfect square then check if its square root ; exist in the array or not ; Driver code
|
using System ; using System . Collections ; class GFG { static bool CheckArray ( int [ ] arr , int n ) { int prod = 1 ; ArrayList freq = new ArrayList ( ) ; for ( int i = 0 ; i < n ; ++ i ) { freq . Add ( arr [ i ] ) ; prod *= arr [ i ] ; } int root = ( int ) Math . Sqrt ( prod ) ; if ( root * root == prod ) { if ( freq . Contains ( root ) & freq . LastIndexOf ( root ) != ( freq . Count ) ) { return true ; } } return false ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 12 , 3 , 2 } ; int n = arr . Length ; if ( CheckArray ( arr , n ) ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } } }
|
Check whether a number has consecutive 0 's in the given base or not | C # implementation of the above approach ; Function to convert N into base K ; Weight of each digit ; Function to check for consecutive 0 ; Flag to check if there are consecutive zero or not ; If there are two consecutive zero then returning False ; We first convert to given base , then check if the converted number has two consecutive 0 s or not ; Driver code
|
using System ; class GFG { static int toK ( int N , int K ) { int w = 1 ; int s = 0 ; while ( N != 0 ) { int r = N % K ; N = N / K ; s = r * w + s ; w *= 10 ; } return s ; } static Boolean check ( int N ) { Boolean fl = false ; while ( N != 0 ) { int r = N % 10 ; N = N / 10 ; if ( fl == true && r == 0 ) return false ; if ( r > 0 ) { fl = false ; continue ; } fl = true ; } return true ; } static void hasConsecutiveZeroes ( int N , int K ) { int z = toK ( N , K ) ; if ( check ( z ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( String [ ] args ) { int N = 15 ; int K = 8 ; hasConsecutiveZeroes ( N , K ) ; } }
|
Sum of every Kâ €™ th prime number in an array | C # implementation of the approach ; 0 and 1 are not prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; compute the answer ; count of primes ; sum of the primes ; traverse the array ; if the number is a prime ; increase the count ; if it is the K 'th prime ; Driver code ; create the sieve
|
class GFG { static int MAX = 1000000 ; static bool [ ] prime = new bool [ MAX + 1 ] ; static void SieveOfEratosthenes ( ) { prime [ 1 ] = false ; prime [ 0 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == false ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = true ; } } } static void SumOfKthPrimes ( int [ ] arr , int n , int k ) { int c = 0 ; long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! prime [ arr [ i ] ] ) { c ++ ; if ( c % k == 0 ) { sum += arr [ i ] ; c = 0 ; } } } System . Console . WriteLine ( sum ) ; } static void Main ( ) { SieveOfEratosthenes ( ) ; int [ ] arr = new int [ ] { 2 , 3 , 5 , 7 , 11 } ; int n = arr . Length ; int k = 2 ; SumOfKthPrimes ( arr , n , k ) ; } }
|
Find the super power of a given Number | C # for finding super power of n ; global hash for prime ; sieve method for storing a list of prime ; function to return super power ; find the super power ; Driver Code
|
class GFG { static int MAX = 100000 ; static bool [ ] prime = new bool [ 100002 ] ; static void SieveOfEratosthenes ( ) { for ( int p = 2 ; p * p <= MAX ; p ++ ) if ( prime [ p ] == false ) for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = true ; } static int superpower ( int n ) { SieveOfEratosthenes ( ) ; int superPower = 0 , factor = 0 ; int i = 2 ; while ( n > 1 && i <= MAX ) { if ( ! prime [ i ] ) { factor = 0 ; while ( n % i == 0 && n > 1 ) { factor ++ ; n = n / i ; } if ( superPower < factor ) superPower = factor ; } i ++ ; } return superPower ; } static void Main ( ) { int n = 256 ; System . Console . WriteLine ( superpower ( n ) ) ; } }
|
Count of Prime Nodes of a Singly Linked List | C # implementation to find count of prime numbers in the singly linked list ; Node of the singly linked list ; Function to insert a node at the beginning of the singly Linked List ; Function to check if a number is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to find count of prime nodes in a linked list ; If current node is prime ; Update count ; Driver code ; start with the empty list ; create the linked list 15 . 5 . 6 . 10 . 17 ; Function call to print require answer
|
using System ; class GFG { public class Node { public int data ; public Node next ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static int countPrime ( Node head_ref ) { int count = 0 ; Node ptr = head_ref ; while ( ptr != null ) { if ( isPrime ( ptr . data ) ) { count ++ ; } ptr = ptr . next ; } return count ; } public static void Main ( String [ ] args ) { Node head = null ; head = push ( head , 17 ) ; head = push ( head , 10 ) ; head = push ( head , 6 ) ; head = push ( head , 5 ) ; head = push ( head , 15 ) ; Console . Write ( " Count ▁ of ▁ prime ▁ nodes ▁ = ▁ " + countPrime ( head ) ) ; } }
|
Smallest prime divisor of a number | C # program to count the number of subarrays that having 1 ; Function to find the smallest divisor ; if divisible by 2 ; iterate from 3 to sqrt ( n ) ; Driver Code
|
using System ; class GFG { static int smallestDivisor ( int n ) { if ( n % 2 == 0 ) return 2 ; for ( int i = 3 ; i * i <= n ; i += 2 ) { if ( n % i == 0 ) return i ; } return n ; } static public void Main ( ) { int n = 31 ; Console . WriteLine ( smallestDivisor ( n ) ) ; } }
|
Count Number of animals in a zoo from given number of head and legs | C # implementation of above approach ; Function that calculates Rabbits ; Driver code
|
using System ; class GFG { static int countRabbits ( int Heads , int Legs ) { int count = 0 ; count = ( Legs ) - 2 * ( Heads ) ; count = count / 2 ; return count ; } public static void Main ( ) { int Heads = 100 , Legs = 300 ; int Rabbits = countRabbits ( Heads , Legs ) ; Console . WriteLine ( " Rabbits ▁ = ▁ " + Rabbits ) ; Console . WriteLine ( " Pigeons ▁ = ▁ " + ( Heads - Rabbits ) ) ; } }
|
Program to evaluate the expression ( ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € š ¬ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ à ¢ à ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’¢ à ¢ à ¢ â € š ¬ Ã …¾¢¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € š ¬ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ à ¢ à ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢¢ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ Ã⠀¦¡¬¹ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ Ã ¢ â € ž ¢¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢¢ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’ à ¢ â ‚¬¦¡¬ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € š ¬ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ à ¢ à ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢¢ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ Ã⠀¦¡¬¦¡ X + 1 ) ^ 6 + ( ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € š ¬ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ à ¢ à ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’¢ à ¢ à ¢ â € š ¬ Ã …¾¢¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € š ¬ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ à ¢ à ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢¢ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ Ã⠀¦¡¬¹ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ Ã ¢ â € ž ¢¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢¢ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’ à ¢ â ‚¬¦¡¬ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € š ¬ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ à ¢ à ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢¢ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ Ã⠀¦¡¬¦¡ X | C # program to evaluate the given expression ; Function to find the sum ; Driver Code
|
using System ; class gfg { public static double calculateSum ( double n ) { return 2 * ( Math . Pow ( n , 6 ) + 15 * Math . Pow ( n , 4 ) + 15 * Math . Pow ( n , 2 ) + 1 ) ; } public static int Main ( ) { double n = 1.4142 ; Console . WriteLine ( Math . Ceiling ( calculateSum ( n ) ) ) ; return 0 ; } }
|
Find the sum of series 3 , | calculate sum upto N term of series ; Driver code
|
class GFG { static int Sum_upto_nth_Term ( int n ) { return ( 1 - ( int ) System . Math . Pow ( - 2 , n ) ) ; } public static void Main ( ) { int N = 5 ; System . Console . WriteLine ( Sum_upto_nth_Term ( N ) ) ; } }
|
Count numbers whose XOR with N is equal to OR with N | C # program to find the XOR equals OR count ; Function to calculate count of numbers with XOR equals OR ; variable to store count of unset bits ; Driver code
|
using System ; class GFG { static int xorEqualsOrCount ( int N ) { int count = 0 ; int bit ; while ( N > 0 ) { bit = N % 2 ; if ( bit == 0 ) count ++ ; N = N / 2 ; } return ( int ) Math . Pow ( 2 , count ) ; } public static void Main ( ) { int N = 7 ; Console . WriteLine ( xorEqualsOrCount ( N ) ) ; } }
|
Program to find sum of 1 + x / 2 ! + x ^ 2 / 3 ! + ... + x ^ n / ( n + 1 ) ! | C # Program to compute sum of 1 + x / 2 ! + x ^ 2 / 3 ! + ... + x ^ n / ( n + 1 ) ! ; Method to find factorial of a number ; Method to compute the sum ; Iterate the loop till n and compute the formula ; Driver Code ; Get x and n ; Find and print the sum
|
using System ; class SumOfSeries { static int fact ( int n ) { if ( n == 1 ) return 1 ; return n * fact ( n - 1 ) ; } static double sum ( int x , int n ) { double total = 1.0 ; for ( int i = 1 ; i <= n ; i ++ ) { total = total + ( Math . Pow ( x , i ) / fact ( i + 1 ) ) ; } return total ; } public static void Main ( ) { int x = 5 , n = 4 ; Console . WriteLine ( " Sum ▁ is : ▁ " + sum ( x , n ) ) ; } }
|
Find Sum of Series 1 ^ 2 | C # Program to find sum of series 1 ^ 2 - 2 ^ 2 + 3 ^ 3 - 4 ^ 4 + ... ; Function to find sum of series ; If i is even ; If i is odd ; return the result ; Driver Code ; Get n ; Find the sum ; Get n ; Find the sum
|
using System ; class GFG { static int sum_of_series ( int n ) { int result = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) result = result - ( int ) Math . Pow ( i , 2 ) ; else result = result + ( int ) Math . Pow ( i , 2 ) ; } return result ; } public static void Main ( ) { int n = 3 ; Console . WriteLine ( sum_of_series ( n ) ) ; n = 10 ; Console . WriteLine ( sum_of_series ( n ) ) ; } }
|
Program to find the sum of the series 23 + 45 + 75 + ... . . upto N terms | C # program to find sum upto N - th term of the series : 23 , 45 , 75 , 113. . . ; return the final sum ; Driver Code ; Get the value of N ; Get the sum of the series
|
using System ; class GFG { static int findSum ( int N ) { return ( 2 * N * ( N + 1 ) * ( 4 * N + 17 ) + 54 * N ) / 6 ; } static void Main ( ) { int N = 4 ; Console . Write ( findSum ( N ) ) ; } }
|
Program to find the value of sin ( nÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬¦½ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € 𠬹 ÃƒÆ ’ à ¢ â ‚¬¦ à ¢ à ¢ â € š ¬ Ã … â € œ ) | C # Program to find the value of sin ( n ? ) ; This function use to calculate the binomial coefficient upto 15 ; use simple DP to find coefficient ; Function to find the value of cos ( n - theta ) ; find cosTheta from sinTheta ; store required answer ; use to toggle sign in sequence . ; Driver code
|
using System ; class GFG { private static int MAX = 16 ; static long [ , ] nCr = new long [ MAX , MAX ] ; static void binomial ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) nCr [ i , j ] = 1 ; else nCr [ i , j ] = nCr [ i - 1 , j ] + nCr [ i - 1 , j - 1 ] ; } } } static double findCosNTheta ( double sinTheta , int n ) { double cosTheta = Math . Sqrt ( 1 - sinTheta * sinTheta ) ; double ans = 0 ; long toggle = 1 ; for ( int i = 1 ; i <= n ; i += 2 ) { ans = ans + nCr [ n , i ] * Math . Pow ( cosTheta , n - i ) * Math . Pow ( sinTheta , i ) * toggle ; toggle = toggle * - 1 ; } return ans ; } public static void Main ( ) { binomial ( ) ; double sinTheta = 0.5 ; int n = 10 ; Console . Write ( findCosNTheta ( sinTheta , n ) ) ; } }
|
Program to find Nth term of series 9 , 23 , 45 , 75 , 113. . . | C # program to find N - th term of the series : 9 , 23 , 45 , 75 , 113. . . ; calculate Nth term of series ; Driver code ; Get the value of N ; Find the Nth term and print it
|
using System ; class GFG { static int nthTerm ( int N ) { return ( 2 * N + 3 ) * ( 2 * N + 3 ) - 2 * N ; } public static void Main ( ) { int N = 4 ; Console . WriteLine ( nthTerm ( N ) ) ; } }
|
Program to Find the value of cos ( nÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬¦½ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € 𠬹 ÃƒÆ ’ à ¢ â ‚¬¦ à ¢ à ¢ â € š ¬ Ã … â € œ ) | C # program to find the value of cos ( n - theta ) ; Function to calculate the binomial coefficient upto 15 ; use simple DP to find coefficient ; Function to find the value of cos ( n - theta ) ; find sinTheta from cosTheta ; to store required answer ; use to toggle sign in sequence . ; Driver code
|
using System ; public class GFG { static int MAX = 16 ; static int [ , ] nCr = new int [ MAX , MAX ] ; static void binomial ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) nCr [ i , j ] = 1 ; else nCr [ i , j ] = nCr [ i - 1 , j ] + nCr [ i - 1 , j - 1 ] ; } } } static double findCosnTheta ( double cosTheta , int n ) { double sinTheta = Math . Sqrt ( 1 - cosTheta * cosTheta ) ; double ans = 0 ; int toggle = 1 ; for ( int i = 0 ; i <= n ; i += 2 ) { ans = ans + nCr [ n , i ] * Math . Pow ( cosTheta , n - i ) * Math . Pow ( sinTheta , i ) * toggle ; toggle = toggle * - 1 ; } return ans ; } public static void Main ( ) { binomial ( ) ; double cosTheta = 0.5 ; int n = 10 ; Console . WriteLine ( findCosnTheta ( cosTheta , n ) ) ; } }
|
Find sum of the series 1 + 22 + 333 + 4444 + ... ... upto n terms | C # Program to find Sum of first n terms ; Returning the final sum ; Driver code ; no . of terms to find the sum
|
using System ; class solution { static int calculateSum ( int n ) { return ( ( int ) Math . Pow ( 10 , n + 1 ) * ( 9 * n - 1 ) + 10 ) / ( int ) Math . Pow ( 9 , 3 ) - n * ( n + 1 ) / 18 ; } public static void Main ( ) { int n = 3 ; Console . WriteLine ( " Sum = ▁ " + calculateSum ( n ) ) ; } }
|
Find sum of the series 1 | C # program to find sum of first n terms of the given series ; when n is odd ; when n is not odd ; Driver code ; no . of terms to find the sum
|
using System ; class GFG { static int calculateSum ( int n ) { if ( n % 2 == 1 ) return ( n + 1 ) / 2 ; return - n / 2 ; } public static void Main ( ) { int n = 8 ; Console . WriteLine ( calculateSum ( n ) ) ; } }
|
Check if a number can be expressed as a ^ b | Set 2 | C # program to check if a number can be expressed as a ^ b . ; Driver code
|
using System ; class GFG { public static bool isPower ( int a ) { if ( a == 1 ) { return true ; } for ( int i = 2 ; i * i <= a ; i ++ ) { double val = Math . Log ( a ) / Math . Log ( i ) ; if ( ( val - ( int ) val ) < 0.00000001 ) { return true ; } } return false ; } public static void Main ( string [ ] args ) { int n = 16 ; Console . WriteLine ( isPower ( n ) ? " Yes " : " No " ) ; } }
|
Program to calculate Root Mean Square | C # program to calculate Root Mean Square ; Function that Calculate Root Mean Square ; Calculate square . ; Calculate Mean . ; Calculate Root . ; Driver Code
|
using System ; class GFG { static float rmsValue ( int [ ] arr , int n ) { int square = 0 ; float mean = 0 ; float root = 0 ; for ( int i = 0 ; i < n ; i ++ ) { square += ( int ) Math . Pow ( arr [ i ] , 2 ) ; } mean = ( square / ( float ) ( n ) ) ; root = ( float ) Math . Sqrt ( mean ) ; return root ; } public static void Main ( ) { int [ ] arr = { 10 , 4 , 6 , 8 } ; int n = arr . Length ; Console . Write ( rmsValue ( arr , n ) ) ; } }
|
Program to find the quantity after mixture replacement | C # code using above Formula . ; Function to calculate the Remaining amount . ; calculate Right hand Side ( RHS ) . ; calculate Amount left by multiply it with original value . ; Driver Code
|
using System ; class GFG { static double Mixture ( int X , int Y , int Z ) { double result1 = 0.0 , result = 0.0 ; result1 = ( ( X - Y ) / ( float ) X ) ; result = Math . Pow ( result1 , Z ) ; result = result * X ; return result ; } public static void Main ( ) { int X = 10 , Y = 2 , Z = 2 ; Console . WriteLine ( ( float ) Mixture ( X , Y , Z ) + " ▁ litres " ) ; } }
|
Sum of sum of all subsets of a set formed by first N natural numbers | C # program to find Sum of all subsets of a set formed by first N natural numbers | Set - 2 ; Iterative 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 the result ; y must be even now y = y / 2 ; function to find ff ( n ) ; modulo value ; In formula n is starting from zero ; calculate answer using formula 2 ^ n * ( n ^ 2 + n + 2 ) - 1 ; whenever answer is greater than or equals to mod then modulo it . ; adding modulo while subtraction is very necessary otherwise it will cause wrong answer ; Driver Code ; function call
|
using System ; class GFG { static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y != 0 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static int check ( int n ) { int mod = ( int ) ( 1e9 + 7 ) ; n -- ; int ans = n * n ; if ( ans >= mod ) ans %= mod ; ans += n + 2 ; if ( ans >= mod ) ans %= mod ; ans = ( power ( 2 , n , mod ) % mod * ans % mod ) % mod ; ans = ( ans - 1 + mod ) % mod ; return ans ; } public static void Main ( String [ ] args ) { int n = 4 ; Console . WriteLine ( check ( n ) ) ; } }
|
Program to find LCM of 2 numbers without using GCD | C # program to find LCM of 2 numbers without using GCD ; Function to return LCM of two numbers ; Driver Code
|
using System ; class GfG { public static int findLCM ( int a , int b ) { int lar = Math . Max ( a , b ) ; int small = Math . Min ( a , b ) ; for ( int i = lar ; ; i += lar ) { if ( i % small == 0 ) return i ; } } public static void Main ( ) { int a = 5 , b = 7 ; Console . WriteLine ( " LCM ▁ of ▁ " + a + " ▁ and ▁ " + b + " ▁ is ▁ " + findLCM ( a , b ) ) ; } }
|
Smarandache | C # program to print the first ' n ' terms of the Smarandache - Wellin Sequence ; Function to collect first ' n ' prime numbers ; List to store first ' n ' primes ; Function to generate Smarandache - Wellin Sequence ; Storing the first ' n ' prime numbers in a list ; Driver Code
|
class GFG { static void primes ( int n ) { int i = 2 ; int j = 0 ; int [ ] result = new int [ n ] ; int z = 0 ; while ( j < n ) { bool flag = true ; for ( int item = 2 ; item <= ( int ) ( i * 1 / 2 ) ; item ++ ) if ( i % item == 0 && i != item ) { flag = false ; break ; } if ( flag ) { result [ z ++ ] = i ; j += 1 ; } i += 1 ; } for ( i = 0 ; i < result . Length ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) System . Console . Write ( result [ j ] ) ; System . Console . Write ( " ▁ " ) ; } } static void smar_wln ( int n ) { primes ( n ) ; } static void Main ( ) { int n = 5 ; System . Console . WriteLine ( " First ▁ " + n + " ▁ terms ▁ of ▁ the ▁ Sequence ▁ are " ) ; smar_wln ( n ) ; } }
|
Pentatope number | C # Program to find the nth Pentatope number ; function for Pentatope number ; formula for find Pentatope nth term ; Driver Code
|
using System ; class GFG { static int Pentatope_number ( int n ) { return n * ( n + 1 ) * ( n + 2 ) * ( n + 3 ) / 24 ; } public static void Main ( ) { int n = 7 ; Console . WriteLine ( n + " th ▁ " + " Pentatope ▁ number ▁ : " + Pentatope_number ( n ) ) ; n = 12 ; Console . WriteLine ( n + " th ▁ " + " Pentatope ▁ number ▁ : " + Pentatope_number ( n ) ) ; } }
|
Program for Centered Icosahedral Number | C # Program to find nth Centered icosahedral number Java Program to find nth Centered icosahedral number ; Function to find Centered icosahedral number ; Formula to calculate nth Centered icosahedral number and return it into main function . ; Driver Code
|
using System ; class GFG { static int centeredIcosahedralNum ( int n ) { return ( 2 * n + 1 ) * ( 5 * n * n + 5 * n + 3 ) / 3 ; } public static void Main ( ) { int n = 10 ; Console . WriteLine ( centeredIcosahedralNum ( n ) ) ; n = 12 ; Console . WriteLine ( centeredIcosahedralNum ( n ) ) ; } }
|
Centered Square Number | C # program to find nth Centered square number . ; Function to calculate Centered square number function ; Formula to calculate nth Centered square number ; Driver Code
|
using System ; public class GFG { static int centered_square_num ( int n ) { return n * n + ( ( n - 1 ) * ( n - 1 ) ) ; } static public void Main ( ) { int n = 7 ; Console . WriteLine ( n + " th ▁ Centered " + " ▁ square ▁ number : ▁ " + centered_square_num ( n ) ) ; } }
|
Sum of first n natural numbers | C # program to find sum series 1 , 3 , 6 , 10 , 15 , 21. . . and then find its sum ; Function to find the sum of series ; Driver code
|
using System ; class GFG { static int seriesSum ( int n ) { return ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( seriesSum ( n ) ) ; } }
|
Dodecagonal number | C # program to find the nth Dodecagonal number ; function for Dodecagonal number ; formula for find Dodecagonal nth term ; Driver Code
|
using System ; class GFG { static int Dodecagonal_number ( int n ) { return 5 * n * n - 4 * n ; } static void Main ( ) { int n = 7 ; Console . WriteLine ( Dodecagonal_number ( n ) ) ; n = 12 ; Console . WriteLine ( Dodecagonal_number ( n ) ) ; } }
|
Arithmetic Number | C # program to check if a number is arithmetic number or not ; Sieve Of Eratosthenes ; 1 is not a prime number ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Storing primes in an array ; Update value in primesquare [ p * p ] , if p is prime . ; Function to count divisors ; If number is 1 , then it will have only 1 as a factor . So , total factors will be 1. ; Calling SieveOfEratosthenes to store prime factors of n and to store square of prime factors of n ; ans will contain total number of distinct divisors ; Loop for counting factors of n ; a [ i ] is not less than cube root n ; Calculating power of a [ i ] in n . cnt is power of prime a [ i ] in n . ; if a [ i ] is a factor of n ; cnt = cnt + 1 ; incrementing power ; Calculating number of divisors If n = a ^ p * b ^ q then total divisors of n are ( p + 1 ) * ( q + 1 ) ; First case ; Second case ; Third casse ; return ans ; Total divisors ; Returns sum of all factors of n . ; Traversing through all prime factors . ; This condition is to handle the case when n is a prime number greater than 2. ; Check if number is Arithmetic Number or not . ; Driver code
|
using System ; class GFG { static void SieveOfEratosthenes ( int n , bool [ ] prime , bool [ ] primesquare , int [ ] a ) { for ( int i = 2 ; i <= n ; i ++ ) prime [ i ] = true ; for ( int i = 0 ; i <= ( n * n ) ; i ++ ) primesquare [ i ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } int j = 0 ; for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) { a [ j ] = p ; primesquare [ p * p ] = true ; j ++ ; } } } static int countDivisors ( int n ) { if ( n == 1 ) return 1 ; bool [ ] prime = new bool [ n + 1 ] ; bool [ ] primesquare = new bool [ n * n + 1 ] ; SieveOfEratosthenes ( n , prime , primesquare , a ) ; int ans = 1 ; for ( int i = 0 ; ; i ++ ) { if ( a [ i ] * a [ i ] * a [ i ] > n ) break ; int cnt = 1 ; while ( n % a [ i ] == 0 ) { n = n / a [ i ] ; } ans = ans * cnt ; } if ( prime [ n ] ) ans = ans * 2 ; else if ( primesquare [ n ] ) ans = ans * 3 ; else if ( n != 1 ) ans = ans * 4 ; } static int sumofFactors ( int n ) { int res = 1 ; for ( int i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } static bool checkArithmetic ( int n ) { int count = countDivisors ( n ) ; int sum = sumofFactors ( n ) ; return ( sum % count == 0 ) ; } public static void Main ( String [ ] args ) { int n = 6 ; if ( checkArithmetic ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
|
Finding n | A formula based C # program to find sum of series with cubes of first n natural numbers ; Driver Function
|
using System ; class GFG { static int magicOfSequence ( int N ) { return ( N * ( N + 1 ) / 2 ) + 2 * N ; } public static void Main ( ) { int N = 6 ; Console . Write ( magicOfSequence ( N ) ) ; } }
|
Form a number using corner digits of powers | C # program to find number formed by corner digits of powers . ; Find next power by multiplying N with current power ; Store digits of Power one by one . ; Calculate carry . ; Store carry in Power array . ; Prints number formed by corner digits of powers of N . ; Storing N raised to power 0 ; Initializing empty result ; One by one compute next powers and add their corner digits . ; Call Function that store power in Power array . ; Store unit and last digits of power in res . ; Driver Code
|
using System ; using System . Collections . Generic ; using System . Linq ; using System . Collections ; class GFG { static void nextPower ( int N , ref List < int > power ) { int carry = 0 ; for ( int i = 0 ; i < power . Count ; i ++ ) { int prod = ( power [ i ] * N ) + carry ; power [ i ] = prod % 10 ; carry = prod / 10 ; } while ( carry >= 1 ) { power . Add ( carry % 10 ) ; carry = carry / 10 ; } } static void printPowerNumber ( int X , int N ) { List < int > power = new List < int > ( ) ; power . Add ( 1 ) ; List < int > res = new List < int > ( ) ; for ( int i = 1 ; i <= X ; i ++ ) { nextPower ( N , ref power ) ; res . Add ( power . Last ( ) ) ; res . Add ( power . First ( ) ) ; } for ( int i = 0 ; i < res . Count ; i ++ ) Console . Write ( res [ i ] ) ; } public static void Main ( ) { int N = 19 , X = 4 ; printPowerNumber ( X , N ) ; } }
|
First digit in factorial of a number | A C # program for finding the First digit of the large factorial number ; Removing trailing 0 s as this does not change first digit . ; loop for divide the fact until it become the single digit and return the fact ; driver function
|
using System ; class GFG { static int firstDigit ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { fact = fact * i ; while ( fact % 10 == 0 ) fact = fact / 10 ; } while ( fact >= 10 ) fact = fact / 10 ; return fact ; } public static void Main ( ) { int n = 5 ; Console . Write ( firstDigit ( n ) ) ; } }
|
Sum of the series 1.2 . 3 + 2.3 . 4 + ... + n ( n + 1 ) ( n + 2 ) | Java program to find sum of the series 1.2 . 3 + 2.3 . 4 + 3.4 . 5 + ... ; Driver Code
|
using System ; class GFG { static int sumofseries ( int n ) { int res = 0 ; for ( int i = 1 ; i <= n ; i ++ ) res += ( i ) * ( i + 1 ) * ( i + 2 ) ; return res ; } public static void Main ( ) { Console . WriteLine ( sumofseries ( 3 ) ) ; } }
|
Find N Geometric Means between A and B | C # program to illustrate n geometric mean between A and B ; insert function for calculating the means ; Finding the value of R Common ration ; for finding N the Geometric mean between A and B ; Driver code
|
using System ; public class GFG { static void printGMeans ( int A , int B , int N ) { float R = ( float ) Math . Pow ( ( float ) ( B / A ) , 1.0 / ( float ) ( N + 1 ) ) ; for ( int i = 1 ; i <= N ; i ++ ) Console . Write ( A * Math . Pow ( R , i ) + " ▁ " ) ; } public static void Main ( ) { int A = 3 , B = 81 , N = 2 ; printGMeans ( A , B , N ) ; } }
|
Numbers having difference with digit sum more than s | C # Program to find number of integer such that integer - digSum > s ; function for digit sum ; function to calculate count of integer s . t . integer - digSum > s ; if n < s no integer possible ; iterate for s range and then calculate total count of such integer if starting integer is found ; if no integer found return 0 ; Driver program
|
using System ; class GFG { static long digitSum ( long n ) { long digSum = 0 ; while ( n > 0 ) { digSum += n % 10 ; n /= 10 ; } return digSum ; } public static long countInteger ( long n , long s ) { if ( n < s ) return 0 ; for ( long i = s ; i <= Math . Min ( n , s + 163 ) ; i ++ ) if ( ( i - digitSum ( i ) ) > s ) return ( n - i + 1 ) ; return 0 ; } public static void Main ( ) { long n = 1000 , s = 100 ; Console . WriteLine ( countInteger ( n , s ) ) ; } }
|
Division without using ' / ' operator | C # program to divide a number by other without using / operator ; Function to find division without using ' / ' operator ; Handling negative numbers ; if num1 is greater than equal to num2 subtract num2 from num1 and increase quotient by one . ; checking if neg equals to 1 then making quotient negative ; Driver Code
|
using System ; class GFG { static int division ( int num1 , int num2 ) { if ( num1 == 0 ) return 0 ; if ( num2 == 0 ) return int . MaxValue ; bool negResult = false ; if ( num1 < 0 ) { num1 = - num1 ; if ( num2 < 0 ) num2 = - num2 ; else negResult = true ; } else if ( num2 < 0 ) { num2 = - num2 ; negResult = true ; } int quotient = 0 ; while ( num1 >= num2 ) { num1 = num1 - num2 ; quotient ++ ; } if ( negResult ) quotient = - quotient ; return quotient ; } public static void Main ( ) { int num1 = 13 , num2 = 2 ; Console . Write ( division ( num1 , num2 ) ) ; } }
|
Nonagonal number | C # Program find first n nonagonal number . ; Function to find nonagonal number series . ; Driver Code
|
using System ; class GFG { static void Nonagonal ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { Console . Write ( i * ( 7 * i - 5 ) / 2 ) ; Console . Write ( " ▁ " ) ; } } public static void Main ( ) { int n = 10 ; Nonagonal ( n ) ; } }
|
Find n | C # program to calculate nth term of a series ; Function for calualtion ; For summation of square of first n - natural nos . ; summation of first n natural nos . ; return result ; Driver Code
|
using System ; class GFG { static int seriesFunc ( int n ) { int sumSquare = ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; int sumNatural = ( n * ( n + 1 ) / 2 ) ; return ( sumSquare + sumNatural + 1 ) ; } public static void Main ( ) { int n = 8 ; Console . WriteLine ( seriesFunc ( n ) ) ; n = 13 ; Console . WriteLine ( seriesFunc ( 13 ) ) ; } }
|
Program to check Plus Perfect Number | C # implementation to check if the number is plus perfect or not ; function to check plus perfect number ; calculating number of digits ; calculating plus perfect number ; checking whether number is plus perfect or not ; Driver program
|
using System ; class GFG { static bool checkplusperfect ( int x ) { int temp = x ; int n = 0 ; while ( x != 0 ) { x /= 10 ; n ++ ; } x = temp ; int sum = 0 ; while ( x != 0 ) { sum += ( int ) Math . Pow ( x % 10 , n ) ; x /= 10 ; } return ( sum == temp ) ; } public static void Main ( ) { int x = 9474 ; if ( checkplusperfect ( x ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
|
Number of distinct subsets of a set | C # program to count number of distinct subsets in an array of distinct numbers ; Returns 2 ^ n ; Driver program
|
using System ; class GFG { static int subsetCount ( int [ ] arr , int n ) { return 1 << n ; } public static void Main ( ) { int [ ] A = { 1 , 2 , 3 } ; int n = A . Length ; Console . WriteLine ( subsetCount ( A , n ) ) ; } }
|
Program to calculate GST from original and net prices | C # Program to compute GST from original and net prices . ; return value after calculate GST % ; Driver code
|
using System ; class GFG { static float Calculate_GST ( float org_cost , float N_price ) { return ( ( ( N_price - org_cost ) * 100 ) / org_cost ) ; } public static void Main ( ) { float org_cost = 100 ; float N_price = 120 ; Console . Write ( " ▁ GST ▁ = ▁ " + Calculate_GST ( org_cost , N_price ) + " % " ) ; } }
|
Centered hexagonal number | C # Program to find nth centered hexadecimal number ; Function to find centered hexadecimal number ; Formula to calculate nth centered hexadecimal number and return it into main function ; Driver Code
|
using System ; class GFG { static int centeredHexagonalNumber ( int n ) { return 3 * n * ( n - 1 ) + 1 ; } public static void Main ( ) { int n = 10 ; Console . Write ( n + " th ▁ centered ▁ " + " hexagonal ▁ number : ▁ " ) ; Console . Write ( centeredHexagonalNumber ( n ) ) ; } }
|
Find the distance covered to collect items at equal distances | C # program to calculate the distance for given problem ; function to calculate the distance ; Driver program
|
using System ; class GFG { public static int find_distance ( int n ) { return n * ( ( 3 * n ) + 7 ) ; } public static void Main ( ) { int n = 5 ; Console . Write ( find_distance ( n ) ) ; } }
|
Twin Prime Numbers | C # Code for Twin Prime Numbers ; Please refer below post for details of this function https : goo . gl / Wv3fGv ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Returns true if n1 and n2 are twin primes ; Driver program
|
using System ; class GFG { static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static bool twinPrime ( int n1 , int n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && Math . Abs ( n1 - n2 ) == 2 ) ; } public static void Main ( ) { int n1 = 11 , n2 = 13 ; if ( twinPrime ( n1 , n2 ) ) Console . WriteLine ( " Twin ▁ Prime " ) ; else Console . WriteLine ( " Not ▁ Twin ▁ Prime " ) ; } }
|
Sum of the sequence 2 , 22 , 222 , ... ... ... | C # Code for Sum of the sequence 2 , 22 , 222 , ... ; Function which return the the sum of series ; Driver Code
|
using System ; class GFG { static double sumOfSeries ( int n ) { return 0.0246 * ( Math . Pow ( 10 , n ) - 1 - ( 9 * n ) ) ; } public static void Main ( ) { int n = 3 ; Console . Write ( sumOfSeries ( n ) ) ; } }
|
Find sum of even index binomial coefficients | C # Program to find sum even indexed Binomial Coefficient . ; Returns value of even indexed Binomial Coefficient Sum which is 2 raised to power n - 1. ; Driver Code
|
using System ; class GFG { static int evenbinomialCoeffSum ( int n ) { return ( 1 << ( n - 1 ) ) ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( evenbinomialCoeffSum ( n ) ) ; } }
|
Program to print triangular number series till n | C # Program to print triangular number series till n ; Function to find triangular number ; For each iteration increase j by 1 and add it into k ; Increasing j by 1 ; Add value of j into k and update k ; Driver Code
|
using System ; class GFG { static void triangular_series ( int n ) { int i , j = 1 , k = 1 ; for ( i = 1 ; i <= n ; i ++ ) { Console . Write ( k + " ▁ " ) ; j += 1 ; k += j ; } } public static void Main ( ) { int n = 5 ; triangular_series ( n ) ; } }
|
Sum of the series 1 + ( 1 + 3 ) + ( 1 + 3 + 5 ) + ( 1 + 3 + 5 + 7 ) + à ¢ â ‚¬¦ à ¢ â ‚¬¦ + ( 1 + 3 + 5 + 7 + à ¢ â ‚¬¦ + ( 2 n | C # implementation to find the sum of the given series ; function to find the sum of the given series ; required sum ; Driver program to test above
|
using System ; class GfG { static int sumOfTheSeries ( int n ) { return ( n * ( n + 1 ) / 2 ) * ( 2 * n + 1 ) / 3 ; } public static void Main ( ) { int n = 5 ; Console . Write ( " Sum ▁ = ▁ " + sumOfTheSeries ( n ) ) ; } }
|
Sum of the series 1 + ( 1 + 2 ) + ( 1 + 2 + 3 ) + ( 1 + 2 + 3 + 4 ) + ... ... + ( 1 + 2 + 3 + 4 + ... + n ) | C # Code For Sum of the series ; Function to find sum of given series ; Driver program to test above function
|
using System ; class GFG { static int sumOfSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= i ; j ++ ) sum += j ; return sum ; } public static void Main ( ) { int n = 10 ; Console . Write ( sumOfSeries ( n ) ) ; } }
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.