text
stringlengths 17
3.65k
| code
stringlengths 60
5.26k
|
---|---|
Number of triangles after N moves | C # program to find middle of three distinct numbers to calculate the number of equilateral triangles ; function to calculate number of triangles in Nth step ; Driver code
|
using System ; class Triangle { public static double numberOfTriangles ( int n ) { double ans = 2 * ( Math . Pow ( 3 , n ) ) - 1 ; return ans ; } public static void Main ( ) { int n = 2 ; Console . WriteLine ( numberOfTriangles ( n ) ) ; } }
|
Motzkin number | C # Program to find Nth Motzkin Number . ; Return the nth Motzkin Number . ; Base case ; Finding i - th Motzkin number . ; driver code
|
using System ; class GFG { public static int motzkin ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = ( ( 2 * i + 1 ) * dp [ i - 1 ] + ( 3 * i - 3 ) * dp [ i - 2 ] ) / ( i + 2 ) ; return dp [ n ] ; } public static void Main ( ) { int n = 8 ; Console . WriteLine ( motzkin ( n ) ) ; } }
|
NicomachusΓΖΓ β Γ β Γ’ β¬β’ ΓΖΓ’ β¬ Γ Β’ Γ’ βΒ¬ Γ’ βΒ’Β’ ΓΖΓ β Γ β Γ’ β¬β’Β’ ΓΖΓ βΒ’ ΓΖ Β’ Γ Β’ Γ’ βΒ¬ Γ
‘¬ ΓΖΓ’ β¬Β¦Β‘Β¬ ΓΖΓ β Γ β Γ’ β¬β’Β’ ΓΖΓ βΒ’ ΓΖ Β’ Γ Β’ Γ’ βΒ¬ Γ
‘¬ ΓΖΓ’ β¬Β¦ΒΎΒ’ s Theorem ( Sum of k | Efficient C # code to find sum of k - th group of positive odd integers . ; Return the sum of kth group of positive odd integer . ; Driver program to test above methods
|
using System ; class GFG { public static int kthgroupsum ( int k ) { return k * k * k ; } public static void Main ( ) { int k = 3 ; Console . WriteLine ( kthgroupsum ( k ) ) ; } }
|
Find x , y , z that satisfy 2 / n = 1 / x + 1 / y + 1 / z | C # program to find x y z that satisfies 2 / n = 1 / x + 1 / y + 1 / z ... ; function to find x y and z that satisfy given equation . ; Driver program
|
using System ; class GFG { static void printXYZ ( int n ) { if ( n == 1 ) Console . WriteLine ( - 1 ) ; else { Console . WriteLine ( " x β is β " + n ) ; Console . WriteLine ( " y β is β " + ( n + 1 ) ) ; Console . WriteLine ( " z β is β " + ( n * ( n + 1 ) ) ) ; } } public static void Main ( ) { int n = 7 ; printXYZ ( n ) ; } }
|
Find n | C # program to find the n - th term in series 1 3 6 10 . . . ; Function to find nth term ; Driver Code
|
using System ; class GFG { static int term ( int n ) { return n * ( n + 1 ) / 2 ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( term ( n ) ) ; } }
|
Find Harmonic mean using Arithmetic mean and Geometric mean | C # implementation of compution of arithmetic mean , geometric mean and harmonic mean ; Function to calculate arithmetic mean , geometric mean and harmonic mean ; Driver function
|
using System ; class GeeksforGeeks { static double compute ( int a , int b ) { double AM , GM , HM ; AM = ( a + b ) / 2 ; GM = Math . Sqrt ( a * b ) ; HM = ( GM * GM ) / AM ; return HM ; } public static void Main ( ) { int a = 5 , b = 15 ; double HM = compute ( a , b ) ; Console . WriteLine ( " Harmonic β Mean β between β " + a + " β and β " + b + " β is β " + HM ) ; } }
|
Find n | C # program to find n - th element in the series 9 , 33 , 73 , 128. . ; Returns n - th element of the series ; driver function
|
using System ; class GFG { static int series ( int n ) { return ( 8 * n * n ) + 1 ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( series ( n ) ) ; } }
|
Check if a number is divisible by all prime divisors of another number | C # program to find if all prime factors of y divide x . ; Returns true if all prime factors of y divide x . ; Driver program to test above functions
|
using System ; class GFG { public static int gcd ( int a , int b ) { return b == 0 ? a : gcd ( b , a % b ) ; } static bool isDivisible ( int x , int y ) { if ( y == 1 ) return true ; int z = gcd ( x , y ) ; if ( z == 1 ) return false ; return isDivisible ( x , y / z ) ; } public static void Main ( ) { int x = 18 , y = 12 ; if ( isDivisible ( x , y ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
|
Program to find Sum of a Series a ^ 1 / 1 ! + a ^ 2 / 2 ! + a ^ 3 / 3 ! + a ^ 4 / 4 ! + à ’ Ò ⬦ à ’ Ò ⬦ . + a ^ n / n ! | C # program to print the sum of series ; Function to calculate sum of given series ; multiply ( a / i ) to previous term ; store result in res
|
using System ; class GFG { public static void Main ( ) { double n = 5 , a = 2 ; Console . WriteLine ( sumOfSeries ( a , n ) ) ; } static float sumOfSeries ( double a , double n ) { double res = 0 , prev = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { prev *= ( a / i ) ; res = res + prev ; } return ( float ) ( res ) ; } }
|
Program for Celsius To Fahrenheit conversion | C # program to convert Celsius scale to Fahrenheit scale ; function to convert Celsius scale to Fahrenheit scale ; Driver code
|
using System ; class GFG { static float Cel_To_Fah ( float n ) { return ( ( n * 9.0f / 5.0f ) + 32.0f ) ; } public static void Main ( ) { float n = 20.0f ; Console . Write ( Cel_To_Fah ( n ) ) ; } }
|
Series with largest GCD and sum equals to n | C # program to find the series with largest GCD and sum equals to n ; function to generate and print the sequence ; stores the maximum gcd that can be possible of sequence . ; if maximum gcd comes out to be zero then not possible ; the smallest gcd possible is 1 ; traverse the array to find out the max gcd possible ; checks if the number is divisible or not ; checks if x is smaller then the max gcd possible and x is greater then the resultant gcd till now , then r = x ; checks if n / x is smaller than the max gcd possible and n / x is greater then the resultant gcd till now , then r = x ; traverses and prints d , 2d , 3d , ... , ( k - 1 ) ; computes the last element of the sequence n - s . ; prints the last element ; Driver Code
|
using System ; class GFG { static void print_sequence ( int n , int k ) { int b = n / ( k * ( k + 1 ) / 2 ) ; if ( b == 0 ) { Console . Write ( " - 1" ) ; } else { int r = 1 ; for ( int x = 1 ; x * x <= n ; x ++ ) { if ( n % x != 0 ) continue ; if ( x <= b && x > r ) r = x ; if ( n / x <= b && n / x > r ) r = n / x ; } for ( int i = 1 ; i < k ; i ++ ) Console . Write ( r * i + " β " ) ; int res = n - ( r * ( k * ( k - 1 ) / 2 ) ) ; Console . WriteLine ( res ) ; } } public static void Main ( ) { int n = 24 ; int k = 4 ; print_sequence ( n , k ) ; n = 24 ; k = 5 ; print_sequence ( n , k ) ; n = 6 ; k = 4 ; print_sequence ( n , k ) ; } }
|
Number of compositions of a natural number | C # program to find the total number of compositions of a natural number ; Return 2 raised to power ( n - 1 ) ; Driver Code
|
using System ; class GFG { public static int countCompositions ( int n ) { return 1 << ( n - 1 ) ; } public static void Main ( ) { int n = 4 ; Console . Write ( countCompositions ( n ) ) ; } }
|
Program to count digits in an integer ( 4 Different Methods ) | C # Code to count number of digits in an integer ; Driver Code
|
using System ; class GFG { static int countDigit ( long n ) { if ( n / 10 == 0 ) return 1 ; return 1 + countDigit ( n / 10 ) ; } public static void Main ( ) { long n = 345289467 ; Console . WriteLine ( " Number β of β " + " digits β : β " + countDigit ( n ) ) ; } }
|
Tribonacci Numbers | A DP based C # program to print first n Tribonacci numbers . ; Driver code
|
using System ; class GFG { static void printTrib ( int n ) { int [ ] dp = new int [ n ] ; dp [ 0 ] = dp [ 1 ] = 0 ; dp [ 2 ] = 1 ; for ( int i = 3 ; i < n ; i ++ ) dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + dp [ i - 3 ] ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( dp [ i ] + " β " ) ; } public static void Main ( ) { int n = 10 ; printTrib ( n ) ; } }
|
Tribonacci Numbers | C # Program to print first n tribonacci numbers Matrix Multiplication function for 3 * 3 matrix ; Recursive function to raise the matrix T to the power n ; base condition . ; recursively call to square the matrix ; calculating square of the matrix T ; if n is odd multiply it one time with M ; base condition ; T [ 0 ] [ 0 ] contains the tribonacci number so return it ; Driver Code
|
using System ; class GFG { static void multiply ( int [ , ] T , int [ , ] M ) { int a , b , c , d , e , f , g , h , i ; a = T [ 0 , 0 ] * M [ 0 , 0 ] + T [ 0 , 1 ] * M [ 1 , 0 ] + T [ 0 , 2 ] * M [ 2 , 0 ] ; b = T [ 0 , 0 ] * M [ 0 , 1 ] + T [ 0 , 1 ] * M [ 1 , 1 ] + T [ 0 , 2 ] * M [ 2 , 1 ] ; c = T [ 0 , 0 ] * M [ 0 , 2 ] + T [ 0 , 1 ] * M [ 1 , 2 ] + T [ 0 , 2 ] * M [ 2 , 2 ] ; d = T [ 1 , 0 ] * M [ 0 , 0 ] + T [ 1 , 1 ] * M [ 1 , 0 ] + T [ 1 , 2 ] * M [ 2 , 0 ] ; e = T [ 1 , 0 ] * M [ 0 , 1 ] + T [ 1 , 1 ] * M [ 1 , 1 ] + T [ 1 , 2 ] * M [ 2 , 1 ] ; f = T [ 1 , 0 ] * M [ 0 , 2 ] + T [ 1 , 1 ] * M [ 1 , 2 ] + T [ 1 , 2 ] * M [ 2 , 2 ] ; g = T [ 2 , 0 ] * M [ 0 , 0 ] + T [ 2 , 1 ] * M [ 1 , 0 ] + T [ 2 , 2 ] * M [ 2 , 0 ] ; h = T [ 2 , 0 ] * M [ 0 , 1 ] + T [ 2 , 1 ] * M [ 1 , 1 ] + T [ 2 , 2 ] * M [ 2 , 1 ] ; i = T [ 2 , 0 ] * M [ 0 , 2 ] + T [ 2 , 1 ] * M [ 1 , 2 ] + T [ 2 , 2 ] * M [ 2 , 2 ] ; T [ 0 , 0 ] = a ; T [ 0 , 1 ] = b ; T [ 0 , 2 ] = c ; T [ 1 , 0 ] = d ; T [ 1 , 1 ] = e ; T [ 1 , 2 ] = f ; T [ 2 , 0 ] = g ; T [ 2 , 1 ] = h ; T [ 2 , 2 ] = i ; } static void power ( int [ , ] T , int n ) { if ( n == 0 n == 1 ) return ; int [ , ] M = { { 1 , 1 , 1 } , { 1 , 0 , 0 } , { 0 , 1 , 0 } } ; power ( T , n / 2 ) ; multiply ( T , T ) ; if ( n % 2 != 0 ) multiply ( T , M ) ; } static int tribonacci ( int n ) { int [ , ] T = { { 1 , 1 , 1 } , { 1 , 0 , 0 } , { 0 , 1 , 0 } } ; if ( n == 0 n == 1 ) return 0 ; else power ( T , n - 2 ) ; return T [ 0 , 0 ] ; } public static void Main ( ) { int n = 10 ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( tribonacci ( i ) + " β " ) ; Console . WriteLine ( ) ; } }
|
Geometric mean ( Two Methods ) | Program to calculate the geometric mean of the given array elements . ; function to calculate geometric mean and return float value . ; declare sum variable and initialize it to 1. ; Compute the sum of all the elements in the array . ; compute geometric mean through formula antilog ( ( ( log ( 1 ) + log ( 2 ) + . . . + log ( n ) ) / n ) and return the value to main function . ; Driver function ; function call
|
using System ; class GFG { static float geometricMean ( int [ ] arr , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( float ) Math . Log ( arr [ i ] ) ; sum = sum / n ; return ( float ) Math . Exp ( sum ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ; int n = arr . Length ; Console . WriteLine ( geometricMean ( arr , n ) ) ; } }
|
Smallest number k such that the product of digits of k is equal to n | C # implementation to find smallest number k such that the product of digits of k is equal to n ; function to find smallest number k such that the product of digits of k is equal to n ; if ' n ' is a single digit number , then it is the required number ; stack the store the digits ; repeatedly divide ' n ' by the numbers from 9 to 2 until all the numbers are used or ' n ' > 1 ; save the digit ' i ' that divides ' n ' onto the stack ; if true , then no number ' k ' can be formed ; pop digits from the stack ' digits ' and add them to ' k ' ; required smallest number ; Driver program to test above
|
using System ; using System . Collections . Generic ; public class GFG { static long smallestNumber ( int n ) { if ( n >= 0 && n <= 9 ) { return n ; } Stack < int > digits = new Stack < int > ( ) ; for ( int i = 9 ; i >= 2 && n > 1 ; i -- ) { while ( n % i == 0 ) { digits . Push ( i ) ; n = n / i ; } } if ( n != 1 ) { return - 1 ; } long k = 0 ; while ( digits . Count != 0 ) { k = k * 10 + digits . Peek ( ) ; digits . Pop ( ) ; } return k ; } static public void Main ( ) { int n = 100 ; Console . Write ( smallestNumber ( n ) ) ; } }
|
Check if a number is magic ( Recursive sum of digits is 1 ) | C # program to check if a number is Magic number . ; Note that the loop continues if n is 0 and sum is non - zero . It stops when n becomes 0 and sum becomes single digit . ; Return true if sum becomes 1. ; Driver code
|
using System ; class GFG { public static bool isMagic ( int n ) { int sum = 0 ; while ( n > 0 sum > 9 ) { if ( n == 0 ) { n = sum ; sum = 0 ; } sum += n % 10 ; n /= 10 ; } return ( sum == 1 ) ; } public static void Main ( ) { int n = 1234 ; if ( isMagic ( n ) ) Console . WriteLine ( " Magic β Number " ) ; else Console . WriteLine ( " Not β a β magic β Number " ) ; } }
|
Check if a number is magic ( Recursive sum of digits is 1 ) | C # program to check Whether the number is Magic or not . ; Accepting sample input ; Condition to check Magic number
|
using System ; using System . Collections . Generic ; class GFG { public static void Main ( String [ ] args ) { int x = 1234 ; if ( x % 9 == 1 ) Console . Write ( " Magic β Number " ) ; else Console . Write ( " Not β a β Magic β Number " ) ; } }
|
Sylvester 's sequence | C # Code for Sylvester sequence ; To store the product . ; To store the current number . ; Loop till n . ; Driver program
|
using System ; class GFG { public static void printSequence ( int n ) { int a = 1 ; int ans = 2 ; int N = 1000000007 ; for ( int i = 1 ; i <= n ; i ++ ) { Console . Write ( ans + " β " ) ; ans = ( ( a % N ) * ( ans % N ) ) % N ; a = ans ; ans = ( ans + 1 ) % N ; } } public static void Main ( ) { int n = 6 ; printSequence ( n ) ; } }
|
Program to find sum of first n 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 ) ) ; } }
|
Hailstone Numbers | C # program to generate hailstone numbers and calculate steps required to reduce them to 1 ; function to print hailstone numbers and to calculate the number of steps required ; N is initially 1. ; N is reduced to 1. ; If N is Even . ; N is Odd . ; Driver code ; Function to generate Hailstone Numbers ; Output : Number of Steps
|
using System ; class GFG { static int c ; static int HailstoneNumbers ( int N ) { Console . Write ( N + " β " ) ; if ( N == 1 && c == 0 ) { return c ; } else if ( N == 1 && c != 0 ) { c ++ ; return c ; } else if ( N % 2 == 0 ) { c ++ ; HailstoneNumbers ( N / 2 ) ; } else if ( N % 2 != 0 ) { c ++ ; HailstoneNumbers ( 3 * N + 1 ) ; } return c ; } public static void Main ( ) { int N = 7 ; int x ; x = HailstoneNumbers ( N ) ; Console . WriteLine ( ) ; Console . WriteLine ( " Number β of β Steps : β " + x ) ; } }
|
Find m | C # program to find m - th summation . ; Function to return mth summation ; base case ; Driver Code
|
using System ; class GFG { static int SUM ( int n , int m ) { if ( m == 1 ) return ( n * ( n + 1 ) / 2 ) ; int sum = SUM ( n , m - 1 ) ; return ( sum * ( sum + 1 ) / 2 ) ; } public static void Main ( ) { int n = 5 ; int m = 3 ; Console . Write ( " SUM ( " + n + " , β " + m + " ) : β " + SUM ( n , m ) ) ; } }
|
Count number of digits after decimal on dividing a number | C # program to count digits after dot when a number is divided by another . ; int ans = 0 ; Initialize result ; calculating remainder ; if this remainder appeared before then the numbers are irrational and would not converge to a solution the digits after decimal will be infinite ; Driver code
|
using System ; using System . Collections . Generic ; class GFG { static int count ( int x , int y ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; while ( x % y != 0 ) { x = x % y ; ans ++ ; if ( m . ContainsKey ( x ) ) return - 1 ; m . Add ( x , 1 ) ; x = x * 10 ; } return ans ; } public static void Main ( String [ ] args ) { int res = count ( 1 , 2 ) ; if ( ( res == - 1 ) ) Console . WriteLine ( " INF " ) ; else Console . WriteLine ( res ) ; res = count ( 5 , 3 ) ; if ( ( res == - 1 ) ) Console . WriteLine ( " INF " ) ; else Console . WriteLine ( res ) ; res = count ( 3 , 5 ) ; if ( ( res == - 1 ) ) Console . WriteLine ( " INF " ) ; else Console . WriteLine ( res ) ; } }
|
Find smallest number n such that n XOR n + 1 equals to given k . | C # to find n such that XOR of n and n + 1 is equals to given n ; function to return the required n ; if k is of form 2 ^ i - 1 ; Driver code
|
using System ; class GFG { static int xorCalc ( int k ) { if ( k == 1 ) return 2 ; if ( ( ( k + 1 ) & k ) == 0 ) return k / 2 ; return 1 ; } public static void Main ( ) { int k = 31 ; Console . WriteLine ( xorCalc ( k ) ) ; } }
|
Find n | C # program to find n - th number containing only 4 and 7. ; If n is odd , append 4 and move to parent ; If n is even , append 7 and move to parent ; Reverse res and return . ; Driver Code
|
using System ; class GFG { static string findNthNo ( int n ) { string res = " " ; while ( n >= 1 ) { if ( ( n & 1 ) == 1 ) { res = res + "4" ; n = ( n - 1 ) / 2 ; } else { res = res + "7" ; n = ( n - 2 ) / 2 ; } } char [ ] arr = res . ToCharArray ( ) ; Array . Reverse ( arr ) ; return new string ( arr ) ; } public static void Main ( ) { int n = 13 ; Console . Write ( findNthNo ( n ) ) ; } }
|
Total number of divisors for a given number | C # program for finding number of divisor ; program for finding no . of divisors ; sieve method for prime calculation ; Traversing through all prime numbers ; calculate number of divisor with formula total div = ( p1 + 1 ) * ( p2 + 1 ) * ... . . * ( pn + 1 ) where n = ( a1 ^ p1 ) * ( a2 ^ p2 ) . ... * ( an ^ pn ) ai being prime divisor for n and pi are their respective power in factorization ; Driver Code
|
using System ; class GFG { static int divCount ( int n ) { bool [ ] hash = new bool [ n + 1 ] ; for ( int p = 2 ; p * p < n ; p ++ ) if ( hash [ p ] == false ) for ( int i = p * 2 ; i < n ; i += p ) hash [ i ] = true ; int total = 1 ; for ( int p = 2 ; p <= n ; p ++ ) { if ( hash [ p ] == false ) { int count = 0 ; if ( n % p == 0 ) { while ( n % p == 0 ) { n = n / p ; count ++ ; } total = total * ( count + 1 ) ; } } } return total ; } public static void Main ( ) { int n = 24 ; Console . WriteLine ( divCount ( n ) ) ; } }
|
Maximum number of unique prime factors | C # program to find maximum number of prime factors for a number in range [ 1 , N ] ; Return smallest number having maximum prime factors . ; Sieve of eratosthenes method to count number of unique prime factors . ; Return maximum element in arr [ ] ; Driver Code
|
using System ; class GFG { static int getMax ( int [ ] Arr ) { int max = Arr [ 0 ] ; for ( int i = 1 ; i < Arr . Length ; i ++ ) if ( Arr [ i ] > max ) max = Arr [ i ] ; return max ; } static int maxPrimefactorNum ( int N ) { int [ ] arr = new int [ N + 1 ] ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( arr [ i ] == 0 ) for ( int j = 2 * i ; j <= N ; j += i ) arr [ j ] ++ ; arr [ i ] = 1 ; } return getMax ( arr ) ; } public static void Main ( ) { int N = 40 ; Console . WriteLine ( maxPrimefactorNum ( N ) ) ; } }
|
Decimal to binary conversion without using arithmetic operators | C # implementation of decimal to binary conversion without using arithmetic operators ; function for decimal to binary conversion without using arithmetic operators ; to store the binary equivalent of decimal ; to get the last binary digit of the number ' n ' and accumulate it at the beginning of ' bin ' ; right shift ' n ' by 1 ; required binary number ; Driver program to test above
|
using System ; class GFG { static String decToBin ( int n ) { if ( n == 0 ) return "0" ; String bin = " " ; while ( n > 0 ) { bin = ( ( n & 1 ) == 0 ? '0' : '1' ) + bin ; n >>= 1 ; } return bin ; } public static void Main ( ) { int n = 38 ; Console . WriteLine ( decToBin ( n ) ) ; } }
|
Sum of array elements that is first continuously increasing then decreasing | C # Code for Sum of array elements that is first continuously increasing then decreasing ; Driver program to test above function
|
using System ; class GFG { public static int arraySum ( int [ ] arr , int n ) { int x = ( n + 1 ) / 2 ; return ( arr [ 0 ] - 1 ) * n + x * x ; } public static void Main ( ) { int [ ] arr = { 10 , 11 , 12 , 13 , 12 , 11 , 10 } ; int n = arr . Length ; Console . WriteLine ( arraySum ( arr , n ) ) ; } }
|
Balance pans using given weights that are powers of a number | C # code to check whether scale can be balanced or not ; method returns true if balancing of scale is possible ; baseForm vector will store T 's representation on base a in reverse order ; convert T to representation on base a ; make first digit of representation as 0 ; loop over base representation of T ; if any digit is not 0 , 1 , ( a - 1 ) or a then balancing is not possible ; if digit is a or ( a - 1 ) then increase left index ' s β count / β ( case , β when β this β weight β is β transferred β to β T ' s side ) ; if representation is processed then balancing is possible ; Driver code
|
using System ; using System . Collections . Generic ; class GFG { static bool isBalancePossible ( int T , int a ) { List < int > baseForm = new List < int > ( ) ; int s = 0 ; while ( T > 0 ) { baseForm . Add ( T % a ) ; T /= a ; s ++ ; } baseForm . Add ( 0 ) ; for ( int i = 0 ; i < s ; i ++ ) { if ( baseForm [ i ] != 0 && baseForm [ i ] != 1 && baseForm [ i ] != ( a - 1 ) && baseForm [ i ] != a ) { return false ; } if ( baseForm [ i ] == a || baseForm [ i ] == ( a - 1 ) ) { baseForm . Insert ( i + 1 , baseForm [ i + 1 ] + 1 ) ; } } return true ; } public static void Main ( ) { int T = 11 ; int a = 4 ; bool balancePossible = isBalancePossible ( T , a ) ; if ( balancePossible ) { Console . WriteLine ( " Balance β is β possible " ) ; } else { Console . WriteLine ( " Balance β is β not β possible " ) ; } } }
|
Number of digits in the product of two numbers | C # Code for Number of digits in the product of two numbers ; function to count number of digits in the product of two numbers ; if either of the number is 0 , then product will be 0 ; required count of digits ; Driver code
|
using System ; class GFG { public static int countDigits ( int a , int b ) { if ( a == 0 b == 0 ) return 1 ; return ( int ) Math . Floor ( Math . Log10 ( Math . Abs ( a ) ) + Math . Log10 ( Math . Abs ( b ) ) ) + 1 ; } static void Main ( ) { int a = 33 ; int b = - 24 ; Console . Write ( countDigits ( a , b ) ) ; } }
|
Distributing M items in a circle of size N starting from K | C # program to find the position where last item is delivered . ; n == > Size of circle m == > Number of items k == > Initial position ; n - k + 1 is number of positions before we reach beginning of circle If m is less than this value , then we can simply return ( m - 1 ) th position ; Let us compute remaining items before we reach beginning . ; We compute m % n to skip all complete rounds . If we reach end , we return n else we return m % n ; Driver Program to test above function
|
using System ; class GFG { static int lastPosition ( int n , int m , int k ) { if ( m <= n - k + 1 ) return m + k - 1 ; m = m - ( n - k + 1 ) ; return ( m % n == 0 ) ? n : ( m % n ) ; } public static void Main ( ) { int n = 5 ; int m = 8 ; int k = 2 ; Console . WriteLine ( lastPosition ( n , m , k ) ) ; } }
|
An interesting solution to get all prime numbers smaller than n | C # program prints prime numbers smaller than n ; Compute factorials and apply Wilson 's theorem. ; Driver code
|
class GFG { static void primesInRange ( int n ) { int fact = 1 ; for ( int k = 2 ; k < n ; k ++ ) { fact = fact * ( k - 1 ) ; if ( ( fact + 1 ) % k == 0 ) System . Console . WriteLine ( k ) ; } } static void Main ( ) { int n = 15 ; primesInRange ( n ) ; } }
|
A product array puzzle | Set 2 ( O ( 1 ) Space ) | C # program for product array puzzle with O ( n ) time and O ( 1 ) space . ; epsilon value to maintain precision ; to hold sum of all values ; output product for each index anti log to find original product value ; Driver code
|
using System ; class GFG { static double EPS = 1e-9 ; static void productPuzzle ( int [ ] a , int n ) { double sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += Math . Log10 ( a [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( ( int ) ( EPS + Math . Pow ( 10.00 , sum - Math . Log10 ( a [ i ] ) ) ) + " β " ) ; } public static void Main ( ) { int [ ] a = { 10 , 3 , 5 , 6 , 2 } ; int n = a . Length ; Console . WriteLine ( " The β product β array β is : β " ) ; productPuzzle ( a , n ) ; } }
|
Change all even bits in a number to 0 | C # program to change even bits to 0. ; Returns modified number with all even bits 0. ; To store sum of bits at even positions . ; To store bits to shift ; One by one put all even bits to end ; If current last bit is set , add it to ans ; Next shift position ; Driver code
|
using System ; class GFG { static int changeEvenBits ( int n ) { int to_subtract = 0 ; int m = 0 ; for ( int x = n ; x > 0 ; x >>= 2 ) { if ( ( x & 1 ) > 0 ) to_subtract += ( 1 << m ) ; m += 2 ; } return n - to_subtract ; } public static void Main ( ) { int n = 30 ; Console . Write ( changeEvenBits ( n ) ) ; } }
|
Find the number closest to n and divisible by m | C # implementation to find the number closest to n and divisible by m ; function to find the number closest to n and divisible by m ; find the quotient ; 1 st possible closest number ; 2 nd possible closest number ; if true , then n1 is the required closest number ; else n2 is the required closest number ; Driver program to test above
|
using System ; class GFG { static int closestNumber ( int n , int m ) { int q = n / m ; int n1 = m * q ; int n2 = ( n * m ) > 0 ? ( m * ( q + 1 ) ) : ( m * ( q - 1 ) ) ; if ( Math . Abs ( n - n1 ) < Math . Abs ( n - n2 ) ) return n1 ; return n2 ; } public static void Main ( ) { int n = 13 , m = 4 ; Console . WriteLine ( closestNumber ( n , m ) ) ; n = - 15 ; m = 6 ; Console . WriteLine ( closestNumber ( n , m ) ) ; n = 0 ; m = 8 ; Console . WriteLine ( closestNumber ( n , m ) ) ; n = 18 ; m = - 7 ; Console . WriteLine ( closestNumber ( n , m ) ) ; } }
|
Check if a given number is Pronic | Java program to check and Print Pronic Number upto 200 ; function to check Pronic Number ; Checking Pronic Number by multiplying consecutive numbers ; Driver Code ; Printing Pronic Numbers upto 200
|
using System ; class GFG { static bool checkPronic ( int x ) { for ( int i = 0 ; i <= ( int ) ( Math . Sqrt ( x ) ) ; i ++ ) if ( x == i * ( i + 1 ) ) return true ; return false ; } public static void Main ( ) { for ( int i = 0 ; i <= 200 ; i ++ ) if ( checkPronic ( i ) ) Console . Write ( i + " β " ) ; } }
|
Find minimum sum of factors of number | C # program to find minimum sum of product of number ; To find minimum sum of product of number ; Find factors of number and add to the sum ; Return sum of numbers having minimum product ; Driver Code
|
using System ; public class GFG { static int findMinSum ( int num ) { int sum = 0 ; for ( int i = 2 ; i * i <= num ; i ++ ) { while ( num % i == 0 ) { sum += i ; num /= i ; } } sum += num ; return sum ; } public static void Main ( ) { int num = 12 ; Console . Write ( findMinSum ( num ) ) ; } }
|
Minimum number with digits as 4 and 7 only and given sum | C # program to find smallest number with given sum of digits . ; Prints minimum number with given digit sum and only allowed digits as 4 and 7. ; Cases where all remaining digits are 4 or 7 ( Remaining sum of digits should be multiple of 4 or 7 ) ; If both 4 s and 7 s are there in digit sum , we subtract a 4. ; Driver code
|
using System ; class GFG { static void findMin ( int sum ) { int a = 0 , b = 0 ; while ( sum > 0 ) { if ( sum % 7 == 0 ) { b ++ ; sum -= 7 ; } else if ( sum % 4 == 0 ) { a ++ ; sum -= 4 ; } else { a ++ ; sum -= 4 ; } } if ( sum < 0 ) { Console . Write ( " - 1n " ) ; return ; } for ( int i = 0 ; i < a ; i ++ ) Console . Write ( "4" ) ; for ( int i = 0 ; i < b ; i ++ ) Console . Write ( "7" ) ; Console . WriteLine ( ) ; } public static void Main ( ) { findMin ( 15 ) ; } }
|
Add minimum number to an array so that the sum becomes even | C # program to add minimum number so that the sum of array becomes even ; Function to find out minimum number ; Count odd number of terms in array ; Driver Code
|
using System ; class GFG { static int minNum ( int [ ] arr , int n ) { int odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 != 0 ) odd += 1 ; return ( ( odd % 2 ) != 0 ) ? 1 : 2 ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . Length ; Console . Write ( minNum ( arr , n ) ) ; } }
|
Find maximum number that can be formed using digits of a given number | C # program to print the maximum number from the set of digits of a given number ; Function to print the maximum number ; hashed array to store count of digits ; Converting given number to string ; Updating the count array ; result is to store the final number ; Traversing the count array to calculate the maximum number ; return the result ; Driver Code
|
using System ; class GFG { static int printMaxNum ( int num ) { int [ ] count = new int [ 10 ] ; String str = num . ToString ( ) ; for ( int i = 0 ; i < str . Length ; i ++ ) count [ str [ i ] - '0' ] ++ ; int result = 0 , multiplier = 1 ; for ( int i = 0 ; i <= 9 ; i ++ ) { while ( count [ i ] > 0 ) { result = result + ( i * multiplier ) ; count [ i ] -- ; multiplier = multiplier * 10 ; } } return result ; } public static void Main ( ) { int num = 38293367 ; Console . Write ( printMaxNum ( num ) ) ; } }
|
Form the largest number using at most one swap operation | C # implementation to form the largest number by applying atmost one swap operation ; function to form the largest number by applying atmost one swap operation ; for the rightmost digit , there will be no greater right digit ; index of the greatest right digit till the current index from the right direction ; traverse the array from second right element up to the left element ; if ' num . charAt ( i ) ' is less than the greatest digit encountered so far ; there is no greater right digit for ' num . charAt ( i ) ' ; update ' right ' index ; traverse the ' rightMax [ ] ' array from left to right ; if for the current digit , greater right digit exists then swap it with its greater right digit and break ; performing the required swap operation ; required largest number ; Utility method to swap two characters in a String ; Driver Function to test above Function
|
using System ; using System . Text ; public class GFG { static String largestNumber ( String num ) { int n = num . Length ; int right ; int [ ] rightMax = new int [ n ] ; rightMax [ n - 1 ] = - 1 ; right = n - 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( num [ i ] < num [ right ] ) rightMax [ i ] = right ; else { rightMax [ i ] = - 1 ; right = i ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( rightMax [ i ] != - 1 ) { num = swap ( num , i , rightMax [ i ] ) ; break ; } } return num ; } static String swap ( String num , int i , int j ) { StringBuilder sb = new StringBuilder ( num ) ; sb [ i ] = num [ j ] ; sb [ j ] = num [ i ] ; return sb . ToString ( ) ; } public static void Main ( ) { String num = "8725634" ; Console . WriteLine ( " Largest β Number β : β " + largestNumber ( num ) ) ; } }
|
Binomial Random Variables | C # program to compute Binomial Probability . ; function to calculate nCr i . e . , number of ways to choose r out of n objects ; Since nCr is same as nC ( n - r ) To decrease number of iterations ; function to calculate binomial r . v . probability ; Driver code
|
using System ; class GFG { static int nCr ( int n , int r ) { if ( r > n / 2 ) r = n - r ; int answer = 1 ; for ( int i = 1 ; i <= r ; i ++ ) { answer *= ( n - r + i ) ; answer /= i ; } return answer ; } static float binomialProbability ( int n , int k , float p ) { return nCr ( n , k ) * ( float ) Math . Pow ( p , k ) * ( float ) Math . Pow ( 1 - p , n - k ) ; } public static void Main ( ) { int n = 10 ; int k = 5 ; float p = ( float ) 1.0 / 3 ; float probability = binomialProbability ( n , k , p ) ; Console . Write ( " Probability β of β " + k ) ; Console . Write ( " β heads β when β a β coin β " + " is β tossed β " + n ) ; Console . Write ( " β times β where β " + " probability β of β each β head β is β " + p ) ; Console . Write ( " β is β = β " + probability ) ; } }
|
Find pair with maximum GCD in an array | C # Code for Find pair with maximum GCD in an array ; Function to find GCD of pair with max GCD in the array ; Computing highest element ; Array to store the count of divisors i . e . Potential GCDs ; Iterating over every element ; Calculating all the divisors ; Divisor found ; Incrementing count for divisor ; Element / divisor is also a divisor Checking if both divisors are not same ; Checking the highest potential GCD ; If this divisor can divide at least 2 numbers , it is a GCD of at least 1 pair ; Driver Code ; Array in which pair with max GCD is to be found ; Size of array
|
using System ; class GFG { public static int findMaxGCD ( int [ ] arr , int n ) { int high = 0 ; for ( int i = 0 ; i < n ; i ++ ) high = Math . Max ( high , arr [ i ] ) ; int [ ] divisors = new int [ high + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 1 ; j <= Math . Sqrt ( arr [ i ] ) ; j ++ ) { if ( arr [ i ] % j == 0 ) { divisors [ j ] ++ ; if ( j != arr [ i ] / j ) divisors [ arr [ i ] / j ] ++ ; } } } for ( int i = high ; i >= 1 ; i -- ) if ( divisors [ i ] > 1 ) return i ; return 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 8 , 8 , 12 } ; int n = arr . Length ; Console . WriteLine ( findMaxGCD ( arr , n ) ) ; } }
|
Find pair with maximum GCD in an array | C # Code to find pair with maximum GCD in an array ; function to find GCD of pair with max max GCD in the array ; Calculating Max in array ; Maintaining count array ; Variable to store the multiples of a number ; Iterating from MAX to 1 GCD is always between MAX and 1 The first GCD found will be the highest as we are decrementing the potential GCD ; Iterating from current potential GCD till it is less than MAX ; A multiple found ; Incrementing potential GCD by itself To check i , 2 i , 3 i ... . ; 2 multiples found , max GCD found ; Driver Code ; Array in which pair with max GCD is to be found ; Size of array
|
using System ; class GFG { public static int findMaxGCD ( int [ ] arr , int n ) { int high = 0 ; for ( int i = 0 ; i < n ; i ++ ) high = Math . Max ( high , arr [ i ] ) ; int [ ] count = new int [ high + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] ++ ; int counter = 0 ; for ( int i = high ; i >= 1 ; i -- ) { int j = i ; while ( j <= high ) { if ( count [ j ] > 0 ) counter += count [ j ] ; j += i ; if ( counter == 2 ) return i ; } counter = 0 ; } return 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 8 , 8 , 12 } ; int n = arr . Length ; Console . WriteLine ( findMaxGCD ( arr , n ) ) ; } }
|
Evil Number | C # program to check if a number is Evil number or Odious Number ; Returns number of 1 s from the binary number ; counting 1 s ; Check if number is evil or not ; converting n to binary form ; calculating remainder ; storing the remainders in binary form as a number ; Calling the count_one function to count and return number of 1 s in bin ; Driver Code
|
using System ; class GFG { static int count_one ( int n ) { int c_one = 0 ; while ( n != 0 ) { int rem = n % 10 ; if ( rem == 1 ) c_one = c_one + 1 ; n = n / 10 ; } return c_one ; } static int checkEvil ( int n ) { int i = 0 , bin = 0 , n_one = 0 ; while ( n != 0 ) { int r = n % 2 ; bin = bin + r * ( int ) ( Math . Pow ( 10 , i ) ) ; n = n / 2 ; } n_one = count_one ( bin ) ; if ( n_one % 2 == 0 ) return 1 ; else return 0 ; } public static void Main ( String [ ] args ) { int check , num ; num = 32 ; check = checkEvil ( num ) ; if ( check == 1 ) Console . WriteLine ( num + " β is β Evil β Number " ) ; else Console . WriteLine ( num + " β is β Odious β Number " ) ; } }
|
Count number of pairs ( A <= N , B <= N ) such that gcd ( A , B ) is B | C # implementation of counting pairs such that gcd ( a , b ) = b ; returns number of valid pairs ; initialize k ; loop till imin <= n ; Initialize result ; max i with given k floor ( n / k ) ; adding k * ( number of i with floor ( n / i ) = k to ans ; set imin = imax + 1 and k = n / imin ; Driver code
|
using System ; class GFG { static int CountPairs ( int n ) { int k = n ; int imin = 1 ; int ans = 0 ; while ( imin <= n ) { int imax = n / k ; ans += k * ( imax - imin + 1 ) ; imin = imax + 1 ; k = n / imin ; } return ans ; } public static void Main ( String [ ] args ) { Console . WriteLine ( CountPairs ( 1 ) ) ; Console . WriteLine ( CountPairs ( 2 ) ) ; Console . WriteLine ( CountPairs ( 3 ) ) ; } }
|
Find the last digit of given series | C # program to calculate to find last digit of above expression ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; 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 >> 1 ; y = y / 2 ; Returns modulo inverse of a with respect to m using extended Euclid Algorithm ; q is quotient ; m is remainder now , process same as Euclid 's algo ; Make x1 positive ; Function to calculate the above expression ; Initialize the result ; Compute first part of expression ; Compute second part of expression i . e . , ( ( 4 ^ ( n + 1 ) - 1 ) / 3 ) mod 10 Since division of 3 in modulo can ' t β β β be β performed β directly β therefore β we β β β need β to β find β it ' s modulo Inverse ; Driver code
|
class GFG { static long powermod ( long x , long y , long p ) { while ( y > 0 ) { if ( ( y & 1 ) > 0 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static long modInverse ( long a , long m ) { long m0 = m , t , q ; long x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = a / m ; t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } static long evaluteExpression ( long n ) { long firstsum = 0 , mod = 10 ; for ( int i = 2 , j = 0 ; ( 1 << j ) <= n ; i *= i , ++ j ) firstsum = ( firstsum + i ) % mod ; long secondsum = ( powermod ( 4L , n + 1 , mod ) - 1 ) * modInverse ( 3L , mod ) ; return ( firstsum * secondsum ) % mod ; } public static void Main ( ) { long n = 3 ; System . Console . WriteLine ( evaluteExpression ( n ) ) ; n = 10 ; System . Console . WriteLine ( evaluteExpression ( n ) ) ; } }
|
Sum of all proper divisors of natural numbers in an array | C # program to find sum of proper divisors for every element in an array . ; To store prime factors and their powers ; Fills factors such that factors [ i ] is a vector of pairs containing prime factors ( of i ) and their powers . Also sets values in isPrime [ ] ; To check if a number is prime ; If i is prime , then update its powers in all multiples of it . ; Returns sum of proper divisors of num using factors [ ] ; Applying above discussed formula for every array element ; Driver code
|
using System ; using System . Collections . Generic ; class GFG { static readonly int MAX = 100001 ; class pair { public int F , S ; public pair ( int f , int s ) { F = f ; S = s ; } } static List < pair > [ ] factors = new List < pair > [ MAX ] ; static void sieveOfEratothenese ( ) { bool [ ] isPrime = new bool [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) isPrime [ i ] = true ; isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i < MAX ; i ++ ) { if ( isPrime [ i ] ) { for ( int j = i ; j < MAX ; j += i ) { int k , l ; isPrime [ j ] = false ; for ( k = j , l = 0 ; k % i == 0 ; l ++ , k /= i ) ; factors [ j ] . Add ( new pair ( i , l ) ) ; } } } } static int sumOfProperDivisors ( int num ) { int mul = 1 ; for ( int i = 0 ; i < factors [ num ] . Count ; i ++ ) mul *= ( int ) ( ( Math . Pow ( factors [ num ] [ i ] . F , factors [ num ] [ i ] . S + 1 ) - 1 ) / ( factors [ num ] [ i ] . F - 1 ) ) ; return mul - num ; } public static void Main ( String [ ] args ) { for ( int i = 0 ; i < MAX ; i ++ ) factors [ i ] = new List < pair > ( ) ; sieveOfEratothenese ( ) ; int [ ] arr = { 8 , 13 , 24 , 36 , 59 , 75 , 91 } ; for ( int i = 0 ; i < arr . Length ; i ++ ) Console . Write ( sumOfProperDivisors ( arr [ i ] ) + " β " ) ; Console . WriteLine ( ) ; } }
|
Finding power of prime number p in n ! | C # implementation of finding power of p in n ! ; Method to calculate power of prime number p in n ! ; initializing answer ; initializing ; loop until temp <= n ; add number of numbers divisible by n ; each time multiply temp by p ; Driver Code
|
using System ; public class GFG { static int PowerOFPINnfactorial ( int n , int p ) { int ans = 0 ; int temp = p ; while ( temp <= n ) { ans += n / temp ; temp = temp * p ; } return ans ; } public static void Main ( String [ ] args ) { Console . WriteLine ( PowerOFPINnfactorial ( 4 , 2 ) ) ; } }
|
Program for Decimal to Binary Conversion | C # implementation of the approach ; Function to return the binary equivalent of decimal value N ; To store the binary number ; Count used to store exponent value ; Driver code
|
using System ; class GFG { static int decimalToBinary ( int N ) { int B_Number = 0 ; int cnt = 0 ; while ( N != 0 ) { int rem = N % 2 ; int c = ( int ) Math . Pow ( 10 , cnt ) ; B_Number += rem * c ; N /= 2 ; cnt ++ ; } return B_Number ; } static public void Main ( ) { int N = 17 ; Console . Write ( decimalToBinary ( N ) ) ; } }
|
Program for Binary To Decimal Conversion | Function to convert binary to decimal ; Initializing base1 value to 1 , i . e 2 ^ 0 ; Driver Code
|
class GFG { public static int binaryToDecimal ( int n ) { int num = n ; int dec_value = 0 ; int base1 = 1 ; int temp = num ; while ( temp > 0 ) { int last_digit = temp % 10 ; temp = temp / 10 ; dec_value += last_digit * base1 ; base1 = base1 * 2 ; } return dec_value ; } public static void Main ( ) { int num = 10101001 ; System . Console . Write ( binaryToDecimal ( num ) ) ; } }
|
Calculating Factorials using Stirling Approximation | C # program for calculating factorial of a number using Stirling Approximation ; function for calculating factorial ; evaluating factorial using stirling approximation ; Driver Code
|
class GFG { public static int stirlingFactorial ( double n ) { if ( n == 1 ) return 1 ; double z ; z = System . Math . Sqrt ( 2 * 3.14 * n ) * System . Math . Pow ( ( n / e ) , n ) ; return ( int ) ( z ) ; } public static void Main ( ) { System . Console . WriteLine ( stirlingFactorial ( 1 ) ) ; System . Console . WriteLine ( stirlingFactorial ( 2 ) ) ; System . Console . WriteLine ( stirlingFactorial ( 3 ) ) ; System . Console . WriteLine ( stirlingFactorial ( 4 ) ) ; System . Console . WriteLine ( stirlingFactorial ( 5 ) ) ; System . Console . WriteLine ( stirlingFactorial ( 6 ) ) ; System . Console . WriteLine ( stirlingFactorial ( 7 ) ) ; } }
|
Count pairs with Odd XOR | C # program to count pairs in array whose XOR is odd ; A function will return number of pair whose XOR is odd ; To store count of odd and even numbers ; Increase even if number is even otherwise increase odd ; Return number of pairs ; Driver program to test countXorPair ( )
|
using System ; public class CountXor { static int countXorPair ( int [ ] arr , int n ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } return odd * even ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 } ; Console . WriteLine ( countXorPair ( arr , arr . Length ) ) ; } }
|
Lychrel Number Implementation | C # Program to check whether the given number is Lychrel Number or not with given limit on number of iterations . ; Max Iterations ; Function to check whether number is Lychrel Number ; Function to check whether the number is Palindrome ; Function to reverse the number ; Driver program
|
using System ; class GFG { private static int MAX_ITERATIONS = 20 ; private static bool isLychrel ( long number ) { for ( int i = 0 ; i < MAX_ITERATIONS ; i ++ ) { number = number + reverse ( number ) ; if ( isPalindrome ( number ) ) return false ; } return true ; } private static bool isPalindrome ( long number ) { return number == reverse ( number ) ; } private static long reverse ( long number ) { long reverse = 0 ; while ( number > 0 ) { long remainder = number % 10 ; reverse = ( reverse * 10 ) + remainder ; number = number / 10 ; } return reverse ; } public static void Main ( ) { long number = 295 ; Console . Write ( number + " β is β lychrel ? β " + isLychrel ( number ) ) ; } }
|
Rectangular ( or Pronic ) Numbers | C # Program to find n - th rectangular number ; Returns n - th rectangular number ; Driver code
|
using System ; class GFG { static int findRectNum ( int n ) { return n * ( n + 1 ) ; } public static void Main ( ) { int n = 6 ; Console . Write ( findRectNum ( n ) ) ; } }
|
Program for Muller Method | C # Program to find root of a function , f ( x ) ; function to calculate f ( x ) ; Taking f ( x ) = x ^ 3 + 2 x ^ 2 + 10 x - 20 ; Calculating various constants required to calculate x3 ; Taking the root which is closer to x2 ; checking for resemblance of x3 with x2 till two decimal places ; Driver main function
|
using System ; class Muller1 { static int MAX_ITERATIONS = 10000 ; static double f ( double x ) { return 1 * Math . Pow ( x , 3 ) + 2 * x * x + 10 * x - 20 ; } static void Muller ( double a , double b , double c ) { int i ; double res ; for ( i = 0 ; ; ++ i ) { double f1 = f ( a ) ; double f2 = f ( b ) ; double f3 = f ( c ) ; double d1 = f1 - f3 ; double d2 = f2 - f3 ; double h1 = a - c ; double h2 = b - c ; double a0 = f3 ; double a1 = ( ( ( d2 * Math . Pow ( h1 , 2 ) ) - ( d1 * Math . Pow ( h2 , 2 ) ) ) / ( ( h1 * h2 ) * ( h1 - h2 ) ) ) ; double a2 = ( ( ( d1 * h2 ) - ( d2 * h1 ) ) / ( ( h1 * h2 ) * ( h1 - h2 ) ) ) ; double x = ( ( - 2 * a0 ) / ( a1 + Math . Abs ( Math . Sqrt ( a1 * a1 - 4 * a0 * a2 ) ) ) ) ; double y = ( ( - 2 * a0 ) / ( a1 - Math . Abs ( Math . Sqrt ( a1 * a1 - 4 * a0 * a2 ) ) ) ) ; if ( x >= y ) res = x + c ; else res = y + c ; double m = res * 100 ; double n = c * 100 ; m = Math . Floor ( m ) ; n = Math . Floor ( n ) ; if ( m == n ) break ; a = b ; b = c ; c = res ; if ( i > MAX_ITERATIONS ) { Console . WriteLine ( " Root β cannot β be β found β using " + " β Muller ' s β method " ) ; break ; } } if ( i <= MAX_ITERATIONS ) Console . WriteLine ( " The β value β of β the β root β is β " + Math . Round ( res , 4 ) ) ; } static void Main ( ) { double a = 0 , b = 1 , c = 2 ; Muller ( a , b , c ) ; } }
|
Optimized Euler Totient Function for Multiple Evaluations | C # program to efficiently compute values of euler totient function for multiple inputs . ; Stores prime numbers upto MAX - 1 values ; Finds prime numbers upto MAX - 1 and stores them in vector p ; if prime [ i ] is not marked before ; fill vector for every newly encountered prime ; run this loop till square root of MAX , mark the index i * j as not prime ; function to find totient of n ; this loop runs sqrt ( n / ln ( n ) ) times ; subtract multiples of p [ i ] from r ; Remove all occurrences of p [ i ] in n ; when n has prime factor greater than sqrt ( n ) ; Driver code ; preprocess all prime numbers upto 10 ^ 5
|
using System ; using System . Collections ; class GFG { static int MAX = 100001 ; static ArrayList p = new ArrayList ( ) ; static void sieve ( ) { int [ ] isPrime = new int [ MAX + 1 ] ; for ( int i = 2 ; i <= MAX ; i ++ ) { if ( isPrime [ i ] == 0 ) { p . Add ( i ) ; for ( int j = 2 ; i * j <= MAX ; j ++ ) isPrime [ i * j ] = 1 ; } } } static int phi ( int n ) { int res = n ; for ( int i = 0 ; ( int ) p [ i ] * ( int ) p [ i ] <= n ; i ++ ) { if ( n % ( int ) p [ i ] == 0 ) { res -= ( res / ( int ) p [ i ] ) ; while ( n % ( int ) p [ i ] == 0 ) n /= ( int ) p [ i ] ; } } if ( n > 1 ) res -= ( res / n ) ; return res ; } static void Main ( ) { sieve ( ) ; Console . WriteLine ( phi ( 11 ) ) ; Console . WriteLine ( phi ( 21 ) ) ; Console . WriteLine ( phi ( 31 ) ) ; Console . WriteLine ( phi ( 41 ) ) ; Console . WriteLine ( phi ( 51 ) ) ; Console . WriteLine ( phi ( 61 ) ) ; Console . WriteLine ( phi ( 91 ) ) ; Console . WriteLine ( phi ( 101 ) ) ; } }
|
Finding n | C # implementation for finding nth number made of prime digits only ; Prints n - th number where each digit is a prime number ; Finding the length of n - th number ; Count of numbers with len - 1 digits ; Count of numbers with i digits ; if i is the length of such number then n < 4 * ( 4 ^ ( i - 1 ) - 1 ) / 3 and n >= 4 * ( 4 ^ i - 1 ) / 3 if a valid i is found break the loop ; check for i + 1 ; Finding ith digit at ith place ; j = 1 means 2 j = 2 means ... j = 4 means 7 ; if prev_count + 4 ^ ( len - i ) is less than n , increase prev_count by 4 ^ ( x - i ) ; else print the ith digit and break ; Driver method
|
using System ; public class GFG { static void nthprimedigitsnumber ( long n ) { long len = 1 ; long prev_count = 0 ; while ( true ) { long curr_count = ( long ) ( prev_count + Math . Pow ( 4 , len ) ) ; if ( prev_count < n && curr_count >= n ) break ; len ++ ; prev_count = curr_count ; } for ( int i = 1 ; i <= len ; i ++ ) { for ( long j = 1 ; j <= 4 ; j ++ ) { if ( prev_count + Math . Pow ( 4 , len - i ) < n ) prev_count += ( long ) Math . Pow ( 4 , len - i ) ; else { if ( j == 1 ) Console . Write ( "2" ) ; else if ( j == 2 ) Console . Write ( "3" ) ; else if ( j == 3 ) Console . Write ( "5" ) ; else if ( j == 4 ) Console . Write ( "7" ) ; break ; } } } Console . WriteLine ( ) ; } public static void Main ( ) { nthprimedigitsnumber ( 10 ) ; nthprimedigitsnumber ( 21 ) ; } }
|
CassiniΓ’ β¬β’ s Identity | C # implementation to demonstrate working of CassiniaTMs Identity ; Returns ( - 1 ) ^ n ; Driver Code
|
using System ; class GFG { static int cassini ( int n ) { return ( n & 1 ) != 0 ? - 1 : 1 ; } public static void Main ( ) { int n = 5 ; Console . Write ( cassini ( n ) ) ; } }
|
Find if a number is divisible by every number in a list | C # program which check is a number divided with every element in list or no ; Function which check is a number divided with every element in list or not ; driver program
|
using System ; class GFG { static bool findNoIsDivisibleOrNot ( int [ ] a , int n ) { for ( int i = 0 ; i < a . Length ; i ++ ) { if ( a [ i ] % n != 0 ) return false ; } return true ; } public static void Main ( ) { int [ ] a = { 14 , 12 , 4 , 18 } ; int n = 2 ; if ( findNoIsDivisibleOrNot ( a , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
|
Find a range of composite numbers of given length | C # program to find a range of composite numbers of given length ; method to find factorial of given number ; to print range of length n having all composite integers ; Driver method
|
using System ; class GFG { static int factorial ( int n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; } static void printRange ( int n ) { int a = factorial ( n + 2 ) + 2 ; int b = a + n - 1 ; Console . WriteLine ( " [ " + a + " , β " + b + " ] " ) ; } public static void Main ( ) { int n = 3 ; printRange ( n ) ; } }
|
Find minimum value to assign all array elements so that array product becomes greater | C # program to find minimum value that can be assigned to along elements so that product becomes greater than current product . ; sort the array to apply Binary search ; using log property add every logarithmic value of element to val double val = 0 ; where double is long double ; set left and right extremities to find min value ; multiplying n to mid , to find the correct min value ; Driver code
|
using System ; public class GFG { static long findMinValue ( long [ ] arr , int n ) { Array . Sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { val += ( double ) ( Math . Log ( ( double ) ( arr [ i ] ) ) ) ; } long left = arr [ 0 ] , right = arr [ n - 1 ] ; long ans = 0 ; while ( left <= right ) { long mid = ( left + right ) / 2 ; double temp = ( double ) n * ( double ) ( Math . Log ( ( double ) ( mid ) ) ) ; if ( val < temp ) { ans = mid ; right = mid - 1 ; } else { left = mid + 1 ; } } return ans ; } static public void Main ( ) { long [ ] arr = { 4 , 2 , 1 , 10 , 6 } ; int n = arr . Length ; Console . WriteLine ( findMinValue ( arr , n ) ) ; } }
|
Find the sum of all the terms in the n | C # implementation to find the sum of all the terms in the nth row of the given series ; method to find the required sum ; sum = n * ( 2 * n ^ 2 + 1 ) ; Driver method
|
using System ; class Test { static int sumOfTermsInNthRow ( int n ) { int sum = ( int ) ( n * ( 2 * Math . Pow ( n , 2 ) + 1 ) ) ; return sum ; } public static void Main ( ) { int n = 4 ; Console . Write ( " Sum β of β all β the β terms β in β nth β row β = β " + sumOfTermsInNthRow ( n ) ) ; } }
|
First digit in product of an array of numbers | C # implementation to find first digit of a single number ; Keep dividing by 10 until it is greater than equal to 10 ; Driver method
|
using System ; public class GFG { static int firstDigit ( int x ) { while ( x >= 10 ) x = x / 10 ; return x ; } public static void Main ( ) { Console . WriteLine ( firstDigit ( 12345 ) ) ; Console . WriteLine ( firstDigit ( 5432 ) ) ; } }
|
Find the occurrences of digit d in the range [ 0. . n ] | C # program to count appearances of a digit ' d ' in range from [ 0. . n ] ; Initialize result ; Count appearances in numbers starting from d . ; When the last digit is equal to d ; When the first digit is equal to d then ; increment result as well as number ; In case of reverse of number such as 12 and 21 ; Driver code
|
using System ; public class GFG { static int getOccurence ( int n , int d ) { int result = 0 ; int itr = d ; while ( itr <= n ) { if ( itr % 10 == d ) result ++ ; if ( itr != 0 && itr / 10 == d ) { result ++ ; itr ++ ; } else if ( itr / 10 == d - 1 ) itr = itr + ( 10 - d ) ; else itr = itr + 10 ; } return result ; } public static void Main ( ) { int n = 11 , d = 1 ; Console . Write ( getOccurence ( n , d ) ) ; } }
|
Numbers having Unique ( or Distinct ) digits | C # code for the above approach ; Function to print unique numbers ; Iterate from l to r ; Convert the no . to String ; Convert String to set using stl ; Output if condition satisfies ; Driver Code ; Input of the lower and higher limits ; Function Call
|
using System ; using System . Collections . Generic ; class GFG { static void printUnique ( int l , int r ) { for ( int i = l ; i <= r ; i ++ ) { String s = String . Join ( " " , i ) ; HashSet < int > uniDigits = new HashSet < int > ( ) ; foreach ( int c in s . ToCharArray ( ) ) uniDigits . Add ( c ) ; if ( s . Length == uniDigits . Count ) { Console . Write ( i + " β " ) ; } } } public static void Main ( String [ ] args ) { int l = 1 , r = 20 ; printUnique ( l , r ) ; } }
|
Program to calculate the value of sin ( x ) and cos ( x ) using Expansion | C # code for implementing cos function ; Function for calculation ; Converting degrees to radian ; maps the sum along the series ; holds the actual value of sin ( n ) ; Main function
|
using System ; class GFG { static void cal_cos ( float n ) { float accuracy = ( float ) 0.0001 , x1 , denominator , cosx , cosval ; n = n * ( float ) ( 3.142 / 180.0 ) ; x1 = 1 ; cosx = x1 ; cosval = ( float ) Math . Cos ( n ) ; int i = 1 ; do { denominator = 2 * i * ( 2 * i - 1 ) ; x1 = - x1 * n * n / denominator ; cosx = cosx + x1 ; i = i + 1 ; } while ( accuracy <= cosval - cosx ) ; Console . WriteLine ( cosx ) ; } static void Main ( ) { float n = 30 ; cal_cos ( n ) ; } }
|
Find sum of digits in factorial of a number | C # program to find sum of digits in factorial of a number ; Function to multiply x with large number stored in vector v . Result is stored in v . ; Calculate res + prev carry ; updation at ith position ; Returns sum of digits in n ! ; One by one multiply i to current vector and update the vector . ; Find sum of digits in vector v [ ] ; Driver code
|
using System ; using System . Collections ; class GFG { static ArrayList v = new ArrayList ( ) ; static void multiply ( int x ) { int carry = 0 ; int size = v . Count ; for ( int i = 0 ; i < size ; i ++ ) { int res = carry + ( int ) v [ i ] * x ; v [ i ] = res % 10 ; carry = res / 10 ; } while ( carry != 0 ) { v . Add ( carry % 10 ) ; carry /= 10 ; } } static int findSumOfDigits ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) multiply ( i ) ; int sum = 0 ; int size = v . Count ; for ( int i = 0 ; i < size ; i ++ ) sum += ( int ) v [ i ] ; return sum ; } static void Main ( ) { int n = 1000 ; Console . WriteLine ( findSumOfDigits ( n ) ) ; } }
|
Find other two sides of a right angle triangle | C # program to print other two sides of right angle triangle given one side ; Finds two sides of a right angle triangle if it they exist . ; if n is odd ; case of n = 1 handled separately ; case of n = 2 handled separately ; Driver code
|
using System ; class GFG { static void printOtherSides ( int n ) { if ( n % 2 != 0 ) { if ( n == 1 ) Console . WriteLine ( " - 1" ) ; else { int b = ( n * n - 1 ) / 2 ; int c = ( n * n + 1 ) / 2 ; Console . Write ( " b β = β " + b + " , β c β = β " + c ) ; } } else { if ( n == 2 ) Console . Write ( " - 1" ) ; else { int b = n * n / 4 - 1 ; int c = n * n / 4 + 1 ; Console . Write ( " b β = β " + b + " , β c β = β " + c ) ; } } } public static void Main ( ) { int a = 3 ; printOtherSides ( a ) ; } }
|
Minimum positive integer to divide a number such that the result is an odd | C # program to make a number odd ; Return 1 if already odd ; Check on dividing with a number when the result becomes odd Return that number ; If n is divided by i and n / i is odd then return i ; Driver code
|
using System ; class GFG { static int makeOdd ( int n ) { if ( n % 2 != 0 ) return 1 ; int i ; for ( i = 2 ; i <= n ; i ++ ) if ( ( n % i == 0 ) && ( ( n / i ) % 2 == 1 ) ) break ; return i ; } public static void Main ( ) { int n = 36 ; int res = makeOdd ( n ) ; Console . Write ( res ) ; } }
|
XOR of all subarray XORs | Set 2 | C # program to get total xor of all subarray xors ; Returns XOR of all subarray xors ; if even number of terms are there , all numbers will appear even number of times . So result is 0. ; else initialize result by 0 as ( a xor 0 = a ) ; Driver Code
|
using System ; class GFG { static int getTotalXorOfSubarrayXors ( int [ ] arr , int N ) { if ( N % 2 == 0 ) return 0 ; int res = 0 ; for ( int i = 0 ; i < N ; i += 2 ) res ^= arr [ i ] ; return res ; } static void Main ( ) { int [ ] arr = { 3 , 5 , 2 , 4 , 6 } ; int N = arr . Length ; Console . Write ( getTotalXorOfSubarrayXors ( arr , N ) ) ; } }
|
Fill array with 1 's using minimum iterations of filling neighbors | C # program to find number of iterations to fill with all 1 s ; Returns count of iterations to fill arr [ ] with 1 s . ; Start traversing the array ; Traverse until a 0 is found ; Count contiguous 0 s ; Condition for Case 3 ; Condition to check if Case 1 satisfies : ; If count_zero is even ; If count_zero is odd ; Reset count_zero ; Case 2 ; Update res ; Driver code
|
using System ; class Test { static int countIterations ( int [ ] arr , int n ) { bool oneFound = false ; int res = 0 ; for ( int i = 0 ; i < n ; ) { if ( arr [ i ] == 1 ) oneFound = true ; while ( i < n && arr [ i ] == 1 ) i ++ ; int count_zero = 0 ; while ( i < n && arr [ i ] == 0 ) { count_zero ++ ; i ++ ; } if ( oneFound == false && i == n ) return - 1 ; int curr_count ; if ( i < n && oneFound == true ) { if ( ( count_zero & 1 ) == 0 ) curr_count = count_zero / 2 ; else curr_count = ( count_zero + 1 ) / 2 ; count_zero = 0 ; } else { curr_count = count_zero ; count_zero = 0 ; } res = Math . Max ( res , curr_count ) ; } return res ; } public static void Main ( ) { int [ ] arr = { 0 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 } ; Console . Write ( countIterations ( arr , arr . Length ) ) ; } }
|
Interesting facts about Fibonacci numbers | C # program to demonstrate that Fibonacci numbers that are divisible by their indexes have indexes as either power of 5 or multiple of 12. ; storing Fibonacci numbers
|
using System ; class GFG { static int MAX = 100 ; static void Main ( ) { long [ ] arr = new long [ MAX ] ; arr [ 0 ] = 0 ; arr [ 1 ] = 1 ; for ( int i = 2 ; i < MAX ; i ++ ) arr [ i ] = arr [ i - 1 ] + arr [ i - 2 ] ; Console . Write ( " Fibonacci β numbers β divisible β by β " + " their β indexes β are β : STRNEWLINE " ) ; for ( int i = 1 ; i < MAX ; i ++ ) if ( arr [ i ] % i == 0 ) System . Console . Write ( i + " β " ) ; } }
|
Express a number as sum of consecutive numbers | C # program to print a consecutive sequence to express N if possible . ; Print consecutive numbers from last to first ; Driver code
|
using System ; class GfG { static void printConsecutive ( int last , int first ) { Console . Write ( first ++ ) ; for ( int x = first ; x <= last ; x ++ ) Console . Write ( " β + β " + x ) ; } static void findConsecutive ( int N ) { for ( int last = 1 ; last < N ; last ++ ) { for ( int first = 0 ; first < last ; first ++ ) { if ( 2 * N == ( last - first ) * ( last + first + 1 ) ) { Console . Write ( N + " β = β " ) ; printConsecutive ( last , first + 1 ) ; return ; } } } Console . Write ( " - 1" ) ; } public static void Main ( ) { int n = 12 ; findConsecutive ( n ) ; } }
|
Find n | C # program to find n - th number in a series made of digits 4 and 7 ; Return n - th number in series made of 4 and 7 ; create an array of size ( n + 1 ) ; If i is odd ; Driver code
|
using System ; class GFG { static int printNthElement ( int n ) { int [ ] arr = new int [ n + 1 ] ; arr [ 1 ] = 4 ; arr [ 2 ] = 7 ; for ( int i = 3 ; i <= n ; i ++ ) { if ( i % 2 != 0 ) arr [ i ] = arr [ i / 2 ] * 10 + 4 ; else arr [ i ] = arr [ ( i / 2 ) - 1 ] * 10 + 7 ; } return arr [ n ] ; } public static void Main ( ) { int n = 6 ; Console . Write ( printNthElement ( n ) ) ; } }
|
Maximum sum of distinct numbers such that LCM of these numbers is N | C # program to find the max sum of numbers whose lcm is n ; Returns maximum sum of numbers with LCM as N ; Initialize result ; Finding a divisor of n and adding it to max_sum ; Driver Code
|
using System ; class MaxSum { static int maxSumLCM ( int n ) { int max_sum = 0 ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { max_sum += i ; if ( n / i != i ) max_sum += ( n / i ) ; } } return max_sum ; } public static void Main ( String [ ] args ) { int n = 2 ; Console . Write ( maxSumLCM ( n ) ) ; } }
|
Square root of a number using log | C # program to demonstrate finding square root of a number using sqrt ( )
|
using System ; class GFG { public static void Main ( ) { double n = 12 ; Console . Write ( Math . Sqrt ( n ) ) ; } }
|
Maximum value of an integer for which factorial can be calculated on a machine | C # program to find maximum value of an integer for which factorial can be calculated on your system ; when fact crosses its size , it gives negative value ; Driver Code
|
using System ; class GFG { public static int findMaxValue ( ) { int res = 2 ; long fact = 2 ; while ( true ) { if ( fact < 0 ) break ; res ++ ; fact = fact * res ; } return res - 1 ; } public static void Main ( ) { Console . Write ( " Maximum β value β of " + " β integer β " + findMaxValue ( ) ) ; } }
|
Check if LCM of array elements is divisible by a prime number or not | C # program to find LCM of array of number is divisible by a prime number k or not ; Function to check any number of array is divisible by k or not ; If any array element is divisible by k , then LCM of whole array should also be divisible . ; Driver code
|
using System ; class GFG { static bool func ( int [ ] a , int k ) { for ( int i = 0 ; i < a . Length ; i ++ ) if ( a [ i ] % k == 0 ) return true ; return false ; } public static void Main ( ) { int [ ] a = { 14 , 27 , 38 , 76 , 84 } ; int k = 19 ; bool res = func ( a , k ) ; Console . Write ( res ) ; } }
|
Find the closest and smaller tidy number | C # program to find closest tidy number smaller than the given number ; check whether string violates tidy property ; if string violates tidy property , then decrease the value stored at that index by 1 and replace all the value stored right to that index by 9 ; Driver code ; num will store closest tidy number
|
using System ; class GFG { static String tidyNum ( String str1 , int len ) { char [ ] str = str1 . ToCharArray ( ) ; for ( int i = len - 2 ; i >= 0 ; i -- ) { if ( str [ i ] > str [ i + 1 ] ) { str [ i ] -- ; for ( int j = i + 1 ; j < len ; j ++ ) str [ j ] = '9' ; } } string s = new string ( str ) ; return s ; } static void Main ( ) { String str = "11333445538" ; int len = str . Length ; Console . WriteLine ( tidyNum ( str , len ) ) ; } }
|
Count of m digit integers that are divisible by an integer n | C # program to count m digit numbers having n as divisor . ; Returns count of m digit numbers having n as divisor ; generating largest number of m digit ; generating largest number of m - 1 digit ; returning number of dividend ; main function
|
using System ; class GfG { static int findCount ( int m , int n ) { int num1 = 0 ; for ( int i = 0 ; i < m ; i ++ ) num1 = ( num1 * 10 ) + 9 ; int num2 = 0 ; for ( int i = 0 ; i < ( m - 1 ) ; i ++ ) num2 = ( num2 * 10 ) + 9 ; return ( ( num1 / n ) - ( num2 / n ) ) ; } public static void Main ( ) { int m = 2 , n = 6 ; Console . Write ( findCount ( m , n ) ) ; } }
|
Find the n | Simple C # program to find the n - th number made of even digits only ; function to calculate nth number made of even digits only ; variable to note how many such numbers have been found till now ; bool variable to check if 1 , 3 , 5 , 7 , 9 is there or not ; checking each digit of the number ; If 1 , 3 , 5 , 7 , 9 is found temp is changed to false ; temp is true it means that it does not have 1 , 3 , 5 , 7 , 9 ; If nth such number is found return it ; Driver code
|
using System ; class GFG { static int findNthEvenDigitNumber ( int n ) { int count = 0 ; for ( int i = 0 ; ; i ++ ) { int curr = i ; bool isCurrEvenDigit = true ; while ( curr != 0 ) { if ( curr % 10 == 1 curr % 10 == 3 curr % 10 == 5 curr % 10 == 7 curr % 10 == 9 ) isCurrEvenDigit = false ; curr = curr / 10 ; } if ( isCurrEvenDigit == true ) count ++ ; if ( count == n ) return i ; } } public static void Main ( ) { Console . WriteLine ( findNthEvenDigitNumber ( 2 ) ) ; Console . WriteLine ( findNthEvenDigitNumber ( 10 ) ) ; } }
|
Find the n | Efficient C # program to find n - th number made of even digits only ; function to find nth number made of even digits only ; If n = 1 return 0 ; vector to store the digits when converted into base 5 ; Reduce n to n - 1 to exclude 0 ; Reduce n to base 5 number and store digits ; pushing the digits into vector ; variable to represent the number after converting it to base 5. Since the digits are be in reverse order , we traverse vector from back ; return 2 * result ( to convert digits 0 , 1 , 2 , 3 , 4 to 0 , 2 , 4 , 6 , 8. ; Driver Code
|
using System ; using System . Collections ; class GFG { static int findNthEvenDigitNumber ( int n ) { if ( n == 1 ) { return 0 ; } ArrayList v = new ArrayList ( ) ; n = n - 1 ; while ( n > 0 ) { v . Add ( n % 5 ) ; n = n / 5 ; } int result = 0 ; for ( int i = v . Count - 1 ; i >= 0 ; i -- ) { result = result * 10 ; result = result + ( int ) v [ i ] ; } return 2 * result ; } public static void Main ( ) { Console . WriteLine ( findNthEvenDigitNumber ( 2 ) ) ; Console . WriteLine ( findNthEvenDigitNumber ( 10 ) ) ; } }
|
Check if a large number is divisible by 25 or not | C # program to find if a number is divisible by 25 or not ; Function to find that number divisible by 25 or not . ; If length of string is single digit then then it 's not divisible by 25 ; Driver Code
|
using System ; class IsDivisible { static bool isDivisibleBy25 ( String str ) { int n = str . Length ; if ( n == 1 ) return false ; return ( ( str [ n - 1 ] - '0' == 0 && str [ n - 2 ] - '0' == 0 ) || ( ( str [ n - 2 ] - '0' ) * 10 + ( str [ n - 1 ] - '0' ) ) % 25 == 0 ) ; } public static void Main ( ) { String str = "76955" ; if ( isDivisibleBy25 ( str ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
|
Check a large number is divisible by 16 or not | C # program to find if a number is divisible by 16 or not ; Function to find that number divisible by 16 or not ; Empty string ; If there is double digit ; If there is triple digit ; If number formed by last four digits is divisible by 16. ; Driver code
|
using System ; class GFG { static bool check ( String str ) { int n = str . Length ; if ( n == 0 && n == 1 ) return false ; if ( n == 2 ) return ( ( ( str [ n - 2 ] - '0' ) * 10 + ( str [ n - 1 ] - '0' ) ) % 16 == 0 ) ; if ( n == 3 ) return ( ( ( str [ n - 3 ] - '0' ) * 100 + ( str [ n - 2 ] - '0' ) * 10 + ( str [ n - 1 ] - '0' ) ) % 16 == 0 ) ; int last = str [ n - 1 ] - '0' ; int second_last = str [ n - 2 ] - '0' ; int third_last = str [ n - 3 ] - '0' ; int fourth_last = str [ n - 4 ] - '0' ; return ( ( fourth_last * 1000 + third_last * 100 + second_last * 10 + last ) % 16 == 0 ) ; } public static void Main ( ) { String str = "769528" ; if ( check ( str ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No β " ) ; } }
|
Find Index of given fibonacci number in constant time | A simple C # program to find index of given Fibonacci number . ; if Fibonacci number is less than 2 , its index will be same as number ; iterate until generated fibonacci number is less than given fibonacci number ; res keeps track of number of generated fibonacci number ; Driver Code
|
using System ; class GFG { static int findIndex ( int n ) { if ( n <= 1 ) return n ; int a = 0 , b = 1 , c = 1 ; int res = 1 ; while ( c < n ) { c = a + b ; res ++ ; a = b ; b = c ; } return res ; } public static void Main ( ) { int result = findIndex ( 21 ) ; Console . WriteLine ( result ) ; } }
|
Date after adding given number of days to the given date | C # program to find date after adding given number of days . ; Find values of day and month from offset of result year . ; Return if year is leap year or not . ; Given a date , returns number of days elapsed from the beginning of the current year ( 1 stjan ) . ; Given a year and days elapsed in it , finds date by storing results in d and m . ; Add x days to the given date . ; y2 is going to store result year and offset2 is going to store offset days in result year . ; x may store thousands of days . We find correct year and offset in the year . ; Driven Program
|
using System ; class GFG { static int m2 , d2 ; static bool isLeap ( int y ) { if ( y % 100 != 0 && y % 4 == 0 y % 400 == 0 ) return true ; return false ; } static int offsetDays ( int d , int m , int y ) { int offset = d ; if ( m - 1 == 11 ) offset += 335 ; if ( m - 1 == 10 ) offset += 304 ; if ( m - 1 == 9 ) offset += 273 ; if ( m - 1 == 8 ) offset += 243 ; if ( m - 1 == 7 ) offset += 212 ; if ( m - 1 == 6 ) offset += 181 ; if ( m - 1 == 5 ) offset += 151 ; if ( m - 1 == 4 ) offset += 120 ; if ( m - 1 == 3 ) offset += 90 ; if ( m - 1 == 2 ) offset += 59 ; if ( m - 1 == 1 ) offset += 31 ; if ( isLeap ( y ) && m > 2 ) offset += 1 ; return offset ; } static void revoffsetDays ( int offset , int y ) { int [ ] month = { 0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ; if ( isLeap ( y ) ) month [ 2 ] = 29 ; int i ; for ( i = 1 ; i <= 12 ; i ++ ) { if ( offset <= month [ i ] ) break ; offset = offset - month [ i ] ; } d2 = offset ; m2 = i ; } static void addDays ( int d1 , int m1 , int y1 , int x ) { int offset1 = offsetDays ( d1 , m1 , y1 ) ; int remDays = isLeap ( y1 ) ? ( 366 - offset1 ) : ( 365 - offset1 ) ; int y2 , offset2 = 0 ; if ( x <= remDays ) { y2 = y1 ; offset2 = offset1 + x ; } else { x -= remDays ; y2 = y1 + 1 ; int y2days = isLeap ( y2 ) ? 366 : 365 ; while ( x >= y2days ) { x -= y2days ; y2 ++ ; y2days = isLeap ( y2 ) ? 366 : 365 ; } offset2 = x ; } revoffsetDays ( offset2 , y2 ) ; Console . WriteLine ( " d2 β = β " + d2 + " , β m2 β = β " + m2 + " , β y2 β = β " + y2 ) ; } static void Main ( ) { int d = 14 , m = 3 , y = 2015 ; int x = 366 ; addDays ( d , m , y , x ) ; } }
|
Determine whether a given number is a Hyperperfect Number | C # program to check whether a given number is k - hyperperfect ; function to find the sum of all proper divisors ( excluding1 and N ) ; Iterate only until sqrt N as we are going to generate pairs to produce divisors ; As divisors occur in pairs , we can take the values i and N / i as long as i divides N ; Function to check whether the given number is prime ; base and corner cases ; Since integers can be represented as some 6 * k + y where y >= 0 , we can eliminate all integers that can be expressed in this form ; start from 5 as this is the next prime number ; Returns true if N is a K - Hyperperfect number Else returns false . ; Condition from the definition of hyperperfect ; Driver Code ; First two statements test against the condition N = 1 + K * ( sum ( proper divisors ) )
|
using System ; class GFG { static int divisorSum ( int N , int K ) { int sum = 0 ; for ( int i = 2 ; i <= Math . Ceiling ( Math . Sqrt ( N ) ) ; i ++ ) if ( N % i == 0 ) sum += ( i + N / i ) ; return sum ; } static bool isPrime ( int n ) { if ( n == 1 n == 0 ) 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 isHyperPerfect ( int N , int K ) { int sum = divisorSum ( N , K ) ; if ( ( 1 + K * ( sum ) ) == N ) return true ; else return false ; } static public void Main ( ) { int N1 = 1570153 , K1 = 12 ; int N2 = 321 , K2 = 3 ; if ( isHyperPerfect ( N1 , K1 ) ) Console . WriteLine ( N1 + " β is β " + K1 + " - HyperPerfect " ) ; else Console . WriteLine ( N1 + " β is β not β " + K1 + " - HyperPerfect " ) ; if ( isHyperPerfect ( N2 , K2 ) ) Console . WriteLine ( N2 + " β is β " + K2 + " - HyperPerfect " ) ; else Console . WriteLine ( N2 + " β is β not β " + K2 + " - HyperPerfect " ) ; } }
|
Given a number n , find the first k digits of n ^ n | C # program to find the first k digits of n ^ n ; function that manually calculates n ^ n and then removes digits until k digits remain ; take log10 of n ^ n . log10 ( n ^ n ) = n * log10 ( n ) ; We will now try to separate the decimal and integral part of the / product . The floor function returns the smallest integer less than or equal to the argument . So in this case , product - floor ( product ) will give us the decimal part of product ; we will now exponentiate this back by raising 10 to the power of decimal part ; We now try to find the power of 10 by which we will have to multiply the decimal part to obtain our final answer ; driver function
|
using System ; class GFG { public static long firstkdigits ( int n , int k ) { double product = n * Math . Log10 ( n ) ; double decimal_part = product - Math . Floor ( product ) ; decimal_part = Math . Pow ( 10 , decimal_part ) ; double digits = Math . Pow ( 10 , k - 1 ) ; return ( ( long ) ( decimal_part * digits ) ) ; } public static void Main ( ) { int n = 1450 ; int k = 6 ; Console . Write ( firstkdigits ( n , k ) ) ; } }
|
Generate k digit numbers with digits in strictly increasing order | C # program to generate well ordered numbers with k digits . ; number -- > Current value of number . x -- > Current digit to be considered k -- > Remaining number of digits ; Try all possible greater digits ; Generates all well ordered numbers of length k . ; Driver Code
|
using System ; class GFG { static void printWellOrdered ( int number , int x , int k ) { if ( k == 0 ) { Console . Write ( number + " β " ) ; return ; } for ( int i = ( x + 1 ) ; i < 10 ; i ++ ) printWellOrdered ( number * 10 + i , i , k - 1 ) ; } static void generateWellOrdered ( int k ) { printWellOrdered ( 0 , 0 , k ) ; } public static void Main ( ) { int k = 3 ; generateWellOrdered ( k ) ; } }
|
Multiply large integers under large modulo | C # program of finding modulo multiplication ; Returns ( a * b ) % mod ; Update a if it is more than or equal to mod ; If b is odd , add a with result ; Here we assume that doing 2 * a doesn 't cause overflow ; b >>= 1 ; b = b / 2 ; Driver code
|
using System ; class GFG { static long moduloMultiplication ( long a , long b , long mod ) { a %= mod ; while ( b > 0 ) { if ( ( b & 1 ) > 0 ) res = ( res + a ) % mod ; a = ( 2 * a ) % mod ; } return res ; } static void Main ( ) { long a = 10123465234878998 ; long b = 65746311545646431 ; long m = 10005412336548794 ; Console . WriteLine ( moduloMultiplication ( a , b , m ) ) ; } }
|
Number of occurrences of 2 as a digit in numbers from 0 to n | C # code for above implementation ; Driver code
|
using System ; class GFG { static int numberOf2sinRange ( int n ) { string s = " " ; for ( int i = 0 ; i < n + 1 ; i ++ ) s += i + " " ; int count = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '2' ) { count ++ ; } } return count ; } public static void Main ( string [ ] args ) { int n = 30 ; Console . Write ( numberOf2sinRange ( n ) ) ; } }
|
Number of occurrences of 2 as a digit in numbers from 0 to n | C # program to count 2 s from 0 to n ; Counts the number of 2 s in a number at d - th digit ; if the digit in spot digit is ; Counts the number of '2' digits between 0 and n ; Convert integer to String to find its length ; Traverse every digit and count for every digit ; Driver Code
|
using System ; class GFG { static int count2sinRangeAtDigit ( int number , int d ) { int powerOf10 = ( int ) Math . Pow ( 10 , d ) ; int nextPowerOf10 = powerOf10 * 10 ; int right = number % powerOf10 ; int roundDown = number - number % nextPowerOf10 ; int roundup = roundDown + nextPowerOf10 ; int digit = ( number / powerOf10 ) % 10 ; if ( digit < 2 ) { return roundDown / 10 ; } if ( digit == 2 ) { return roundDown / 10 + right + 1 ; } return roundup / 10 ; } static int numberOf2sinRange ( int number ) { string convert ; convert = number . ToString ( ) ; string s = convert ; int len = s . Length ; int count = 0 ; for ( int digit = 0 ; digit < len ; digit ++ ) { count += count2sinRangeAtDigit ( number , digit ) ; } return count ; } public static void Main ( ) { Console . WriteLine ( numberOf2sinRange ( 22 ) ) ; Console . WriteLine ( numberOf2sinRange ( 100 ) ) ; } }
|
Modulo 10 ^ 9 + 7 ( 1000000007 ) | ; f = f * i ; WRONG APPROACH as f may exceed ( 2 ^ 64 - 1 )
|
static long factorial ( int n ) { const long M = 1000000007 ; long f = 1 ; for ( int i = 1 ; i <= n ; i ++ ) return f % M ; }
|
Modulo 10 ^ 9 + 7 ( 1000000007 ) | ; f = ( f * i ) % M ; Now f never can exceed 10 ^ 9 + 7
|
static long factorial ( int n ) { long M = 1000000007 ; long f = 1 ; for ( int i = 1 ; i <= n ; i ++ ) return f ; }
|
Modulo 10 ^ 9 + 7 ( 1000000007 ) |
|
static int mod ( int a , int m ) { return ( a % m + m ) % m ; }
|
Program to find Star number | C # program to find star number ; Returns n - th star number ; Driver code
|
using System ; class GFG { static int findStarNum ( int n ) { return ( 6 * n * ( n - 1 ) + 1 ) ; } public static void Main ( ) { int n = 3 ; Console . Write ( findStarNum ( n ) ) ; } }
|
Check if a large number is divisible by 5 or not | C # program to find if a number is C # program to find if a number is divisible by 5 or not . ; Function to find that number divisible by 5 or not . The function assumes that string length is at least one . ; Driver Code
|
using System ; class IsDivisible { static bool isDivisibleBy5 ( String str ) { int n = str . Length ; return ( ( ( str [ n - 1 ] - '0' ) == 0 ) || ( ( str [ n - 1 ] - '0' ) == 5 ) ) ; } public static void Main ( ) { String str = "76955" ; if ( isDivisibleBy5 ( str ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.