text
stringlengths 17
3.65k
| code
stringlengths 60
5.26k
|
---|---|
Program to find the Interior and Exterior Angle of a Regular Polygon | C # program to find the interior and exterior angle of a given polygon ; function to find the interior and exterior angle ; formula to find the interior angle ; formula to find the exterior angle ; Displaying the output ; Driver code ; Function calling
|
using System ; class GFG { static void findAngle ( int n ) { int interiorAngle , exteriorAngle ; interiorAngle = ( n - 2 ) * 180 / n ; exteriorAngle = 360 / n ; Console . Write ( " Interior β angle : β " + interiorAngle + " STRNEWLINE " ) ; Console . Write ( " Exterior β angle : β " + exteriorAngle ) ; } public static void Main ( ) { int n = 10 ; findAngle ( n ) ; } }
|
Program to calculate distance between two points in 3 D | C # program to find distance between two points in 3 D . ; Function for distance ; Driver code ; function call for distance
|
using System ; class GFG { static void distance ( float x1 , float y1 , float z1 , float x2 , float y2 , float z2 ) { double d = Math . Pow ( ( Math . Pow ( x2 - x1 , 2 ) + Math . Pow ( y2 - y1 , 2 ) + Math . Pow ( z2 - z1 , 2 ) * 1.0 ) , 0.5 ) ; Console . WriteLine ( " Distance β is β STRNEWLINE " + d ) ; return ; } public static void Main ( ) { float x1 = 2 ; float y1 = - 5 ; float z1 = 7 ; float x2 = 3 ; float y2 = 4 ; float z2 = 5 ; distance ( x1 , y1 , z1 , x2 , y2 , z2 ) ; } }
|
Check if the large number formed is divisible by 41 or not | C # program to check a large number divisible by 41 or not ; Check if a number is divisible by 41 or not ; array to store all the digits ; base values ; calculate remaining digits ; calculate answer ; check for divisibility ; Driver Code
|
using System ; class GFG { static bool DivisibleBy41 ( int first , int second , int c , int n ) { int [ ] digit = new int [ n ] ; digit [ 0 ] = first ; digit [ 1 ] = second ; for ( int i = 2 ; i < n ; i ++ ) digit [ i ] = ( digit [ i - 1 ] * c + digit [ i - 2 ] ) % 10 ; int ans = digit [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) ans = ( ans * 10 + digit [ i ] ) % 41 ; if ( ans % 41 == 0 ) return true ; else return false ; } public static void Main ( ) { int first = 1 , second = 2 , c = 1 , n = 3 ; if ( DivisibleBy41 ( first , second , c , n ) ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } }
|
Program to print pentatope numbers upto Nth term | C # program to generate Pentatope Number series ; Function to generate nth tetrahedral number ; Function to print pentatope number series upto nth term . ; Initialize prev as 0. It store the sum of all previously generated pentatope numbers ; Loop to print pentatope series ; Find ith tetrahedral number ; Add ith tetrahedral number to sum of all previously generated tetrahedral number to get ith pentatope number ; Update sum of all previously generated tetrahedral number ; Driver code ; Function call to print pentatope number series
|
using System ; public class GFG { static int findTetrahedralNumber ( int n ) { return ( ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ) ; } static void printSeries ( int n ) { int prev = 0 ; int curr ; for ( int i = 1 ; i <= n ; i ++ ) { curr = findTetrahedralNumber ( i ) ; curr = curr + prev ; Console . Write ( curr + " β " ) ; prev = curr ; } } static public void Main ( ) { int n = 10 ; printSeries ( n ) ; } }
|
Program to print pentatope numbers upto Nth term | C # program to print Pentatope number series . ; Function to print pentatope series up to nth term ; Loop to print pentatope number series ; calculate and print ith pentatope number ; Driver code ; Function call to print pentatope number series
|
using System ; public class GFG { static void printSeries ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { int num = ( i * ( i + 1 ) * ( i + 2 ) * ( i + 3 ) / 24 ) ; Console . Write ( num + " β " ) ; } } static public void Main ( ) { int n = 10 ; printSeries ( n ) ; } }
|
Find unique pairs such that each element is less than or equal to N | C # program for finding the required pairs ; Finding the number of unique pairs ; Using the derived formula ; Printing the unique pairs ; Driver code
|
using System ; class GFG { static int No_Of_Pairs ( int N ) { int i = 1 ; while ( ( i * i * i ) + ( 2 * i * i ) + i <= N ) i ++ ; return ( i - 1 ) ; } static void print_pairs ( int pairs ) { int i = 1 , mul ; for ( i = 1 ; i <= pairs ; i ++ ) { mul = i * ( i + 1 ) ; Console . WriteLine ( " Pair β no . β " + i + " β - - > β ( " + ( mul * i ) + " , β " + mul * ( i + 1 ) + " ) " ) ; } } static void Main ( ) { int N = 500 , pairs ; pairs = No_Of_Pairs ( N ) ; Console . WriteLine ( " No . β of β pairs β = β " + pairs ) ; print_pairs ( pairs ) ; } }
|
Program to print tetrahedral numbers upto Nth term | C # program to generate tetrahedral number series ; function to generate nth triangular number ; function to print tetrahedral number series up to n ; Initialize prev as 0. It store the sum of all previously generated triangular number ; Loop to print series ; Find ithh triangular number ; Add ith triangular number to sum of all previously generated triangular number to get ith tetrahedral number ; Update sum of all previously generated triangular number ; Driver code ; function call to print series
|
using System ; public class GFG { static long findTriangularNumber ( int n ) { return ( n * ( n + 1 ) ) / 2 ; } static void printSeries ( int n ) { long prev = 0 ; long curr ; for ( int i = 1 ; i <= n ; i ++ ) { curr = findTriangularNumber ( i ) ; curr = curr + prev ; Console . Write ( curr + " β " ) ; prev = curr ; } } static public void Main ( ) { int n = 10 ; printSeries ( n ) ; } }
|
Program to print tetrahedral numbers upto Nth term | C # program to generate series of tetrahedral numbers ; function to print tetrahedral number series up to n ; loop to print series ; Calculate and print ith tetrahedral number ; Driver code ; function call to print series
|
using System ; public class GFG { static void printSeries ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { int num = i * ( i + 1 ) * ( i + 2 ) / 6 ; Console . Write ( num + " β " ) ; } } static public void Main ( ) { int n = 10 ; printSeries ( n ) ; } }
|
Number of odd and even results for every value of x in range [ min , max ] after performing N steps | C # program to print Number of odd / even results for every value of x in range [ min , end ] after performing N steps ; Function that prints the number of odd and even results ; If constant at layer i is even , beven is true , otherwise false . If the coefficient of x at layer i is even , aeven is true , otherwise false . ; If any of the coefficients at any layer is found to be even , then the product of all the coefficients will always be even . ; Checking whether the constant added after all layers is even or odd . ; Assuming input x is even . ; Assuming input x is odd . ; Displaying the counts . ; Driver Code
|
using System ; class GFG { static void count_even_odd ( int min , int max , int [ , ] steps ) { int a , b , even , odd ; bool beven = true , aeven = false ; int n = 2 ; for ( int i = 0 ; i < n ; i ++ ) { a = steps [ i , 0 ] ; b = steps [ i , 1 ] ; if ( ! ( aeven || ( a & 1 ) > 0 ) ) aeven = true ; if ( beven ) { if ( ( b & 1 ) > 0 ) beven = false ; } else if ( ! ( ( a & 1 ) > 0 ) ) { if ( ! ( ( b & 1 ) > 0 ) ) beven = true ; } else { if ( ( b & 1 ) > 0 ) beven = true ; } } if ( beven ) { even = ( int ) max / 2 - ( int ) ( min - 1 ) / 2 ; odd = 0 ; } else { even = ( int ) max / 2 - ( int ) ( min - 1 ) / 2 ; odd = 0 ; } if ( ! ( beven ^ aeven ) ) even += max - min + 1 - ( int ) max / 2 + ( int ) ( min - 1 ) / 2 ; else odd += max - min + 1 - ( int ) max / 2 + ( int ) ( min - 1 ) / 2 ; Console . Write ( " even β = β " + even + " , β odd β = β " + odd ) ; } public static void Main ( ) { int min = 1 , max = 4 ; int [ , ] steps = { { 1 , 2 } , { 3 , 4 } } ; count_even_odd ( min , max , steps ) ; } }
|
Maximum number of ones in a N * N matrix with given constraints | C # program to get Maximum Number of ones in a matrix with given constraints ; Function that returns the maximum number of ones ; Minimum number of zeroes ; Totol cells = square of the size of the matrices ; Initialising the answer ; Driver code ; Initialising the variables
|
using System ; class GFG { static int getMaxOnes ( int n , int x ) { int zeroes = ( n / x ) ; zeroes = zeroes * zeroes ; int total = n * n ; int ans = total - zeroes ; return ans ; } static public void Main ( ) { int n = 5 ; int x = 2 ; Console . WriteLine ( getMaxOnes ( n , x ) ) ; } }
|
Minimum operations required to make all the elements distinct in an array | C # program to find Minimum number of changes to make array distinct ; Function that returns minimum number of changes ; Hash - table to store frequency ; Increase the frequency of elements ; Traverse in the map to sum up the ( occurrences - 1 ) of duplicate elements ; Driver Code
|
using System ; using System . Collections . Generic ; class geeks { public static int minimumOperations ( int [ ] a , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( a [ i ] ) ) { var val = mp [ a [ i ] ] ; mp . Remove ( a [ i ] ) ; mp . Add ( a [ i ] , val + 1 ) ; } else { mp . Add ( a [ i ] , 1 ) ; } } int count = 0 ; foreach ( KeyValuePair < int , int > entry in mp ) { if ( entry . Value > 1 ) { count += ( entry . Value - 1 ) ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 1 , 2 , 3 , 3 , 4 , 3 } ; int n = a . Length ; Console . WriteLine ( minimumOperations ( a , n ) ) ; } }
|
Check if a M | C # program to check if M - th fibonacci divides N - th fibonacci ; exceptional case for F ( 2 ) ; if none of the above cases , hence not divisible ; Driver Code
|
using System ; class GFG { static void check ( int n , int m ) { if ( n == 2 m == 2 n % m == 0 ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } public static void Main ( ) { int m = 3 , n = 9 ; check ( n , m ) ; } }
|
Surface Area and Volume of Hexagonal Prism | C # program to find the Surface Area and Volume of Hexagonal Prism . ; Function to calculate Surface area ; Formula to calculate surface area ; Display surface area ; Function to calculate Volume ; formula to calculate Volume ; Display Volume ; Driver code ; surface area function call ; volume function call
|
using System ; class GFG { static void findSurfaceArea ( float a , float h ) { float Area ; Area = 6 * a * h + 3 * ( float ) ( Math . Sqrt ( 3 ) ) * a * a ; Console . WriteLine ( " Surface β Area : β " + Area ) ; } static void findVolume ( float a , float h ) { float Volume ; Volume = 3 * ( float ) ( Math . Sqrt ( 3 ) ) * a * a * h / 2 ; Console . WriteLine ( " Volume : β " + Volume ) ; } public static void Main ( ) { float a = 5 , h = 10 ; findSurfaceArea ( a , h ) ; findVolume ( a , h ) ; } }
|
Minimum number of mails required to distribute all the questions | C # code to find the minimum number of mails ; Function returns the min no of mails required ; Using the formula derived above ; Driver Code ; no of questions ; no of students ; maximum no of questions a mail can hold ; Calling function
|
using System ; class GFG { static double MinimumMail ( int n , int k , int x ) { double m = ( n - 1 ) + Math . Ceiling ( ( n - 1 ) * 1.0 / x ) * ( n - 1 ) + Math . Ceiling ( n * 1.0 / x ) * ( k - n ) ; return m ; } public static void Main ( ) { int N = 4 ; int K = 9 ; int X = 2 ; Console . WriteLine ( ( int ) MinimumMail ( N , K , X ) + " STRNEWLINE " ) ; } }
|
Program to find the Area of an Ellipse | C # program to find area of an Ellipse . ; Function to find area of an ellipse . ; formula to find the area of an Ellipse . ; Display the result ; Driver code
|
using System ; class GFG { static void findArea ( float a , float b ) { float Area ; Area = ( float ) 3.142 * a * b ; Console . WriteLine ( " Area : β " + Area ) ; } public static void Main ( ) { float a = 5 , b = 4 ; findArea ( a , b ) ; } }
|
Compute power of power k times % m | C # program for computing x ^ x ^ x ^ x . . % m ; Function to compute the given value ; compute power k times ; Driver Code ; Calling function
|
using System ; class GFG { static int calculate ( int x , int k , int m ) { int result = x ; k -- ; while ( k -- > 0 ) { result = ( int ) Math . Pow ( result , x ) ; if ( result > m ) result %= m ; } return result ; } static public void Main ( ) { int x = 5 , k = 2 , m = 3 ; Console . WriteLine ( calculate ( x , k , m ) ) ; } }
|
Recursive program to check if number is palindrome or not | Recursive C # program to check if the number is palindrome or not ; recursive function that returns the reverse of digits ; base case ; stores the reverse of a number ; Driver Code
|
using System ; class GFG { static int rev ( int n , int temp ) { if ( n == 0 ) return temp ; temp = ( temp * 10 ) + ( n % 10 ) ; return rev ( n / 10 , temp ) ; } public static void Main ( ) { int n = 121 ; int temp = rev ( n , 0 ) ; if ( temp == n ) Console . WriteLine ( " yes " ) ; else Console . WriteLine ( " no " ) ; } }
|
Program to find greater value between a ^ n and b ^ n | C # code for finding greater between the a ^ n and b ^ n ; Function to find the greater value ; If n is even ; Driver code
|
using System ; class GFG { static void findGreater ( int a , int b , int n ) { if ( ! ( ( n & 1 ) > 0 ) ) { a = Math . Abs ( a ) ; b = Math . Abs ( b ) ; } if ( a == b ) Console . WriteLine ( " a ^ n β is β " + " equal β to β b ^ n " ) ; else if ( a > b ) Console . WriteLine ( " a ^ n β is β greater β " + " than β b ^ n " ) ; else Console . WriteLine ( " b ^ n β is β greater β " + " than β a ^ n " ) ; } public static void Main ( ) { int a = 12 , b = 24 , n = 5 ; findGreater ( a , b , n ) ; } }
|
Print first n Fibonacci Numbers using direct formula | C # code to print fibonacci numbers till n using direct formula . \ ; Function to calculate fibonacci using recurrence relation formula ; Using direct formula ; Driver code
|
using System ; public class GFG { static void fibonacci ( double n ) { double fib ; for ( double i = 0 ; i < n ; i ++ ) { fib = ( Math . Pow ( ( 1 + Math . Sqrt ( 5 ) ) , i ) - Math . Pow ( ( 1 - Math . Sqrt ( 5 ) ) , i ) ) / ( Math . Pow ( 2 , i ) * Math . Sqrt ( 5 ) ) ; Console . Write ( ( int ) fib + " β " ) ; } } static public void Main ( ) { double n = 8 ; fibonacci ( n ) ; } }
|
Centered Hexadecagonal Number | C # Program to find nth centered hexadecagonal number ; centered hexadecagonal function ; Formula to calculate nth centered hexadecagonal number ; Driver Code
|
using System ; class GFG { static int center_hexadecagonal_num ( int n ) { return 8 * n * n - 8 * n + 1 ; } static public void Main ( ) { int n = 2 ; Console . Write ( n + " th β centered β " + " hexadecagonal β number : β " ) ; Console . WriteLine ( center_hexadecagonal_num ( n ) ) ; n = 12 ; Console . Write ( n + " th β centered β " + " hexadecagonal β number : β " ) ; Console . WriteLine ( center_hexadecagonal_num ( n ) ) ; } }
|
Check if the n | C # Program to check if the nth is odd or even in a sequence where each term is sum of previous two term ; Return if the nth term is even or odd . ; Return true if odd ; Driver Code
|
using System ; class GFG { public static int findNature ( int a , int b , int n ) { int [ ] seq = new int [ 100 ] ; seq [ 0 ] = a ; seq [ 1 ] = b ; for ( int i = 2 ; i <= n ; i ++ ) seq [ i ] = seq [ i - 1 ] + seq [ i - 2 ] ; if ( ( seq [ n ] & 1 ) != 0 ) return 1 ; else return 0 ; } public static void Main ( ) { int a = 2 , b = 4 ; int n = 3 ; if ( findNature ( a , b , n ) == 1 ) Console . Write ( " Odd β " ) ; else Console . Write ( " Even β " ) ; } }
|
Program to compare m ^ n and n ^ m | C # program to compare which is greater m ^ n or n ^ m ; function to compare m ^ n and n ^ m ; m ^ n ; n ^ m ; Driver Code ; function call to compare m ^ n and n ^ m
|
using System ; class GFG { static void check ( ulong m , ulong n ) { double RHS = m * ( double ) Math . Log ( n ) ; double LHS = n * ( double ) Math . Log ( m ) ; if ( LHS > RHS ) Console . Write ( " m ^ n β > β n ^ m " ) ; else if ( LHS < RHS ) Console . Write ( " m ^ n β < β n ^ m " ) ; else Console . Write ( " m ^ n β = β n ^ m " ) ; } static public void Main ( ) { ulong m = 987654321 , n = 123456987 ; check ( m , n ) ; } }
|
Hilbert Matrix | C # program for Hilbert Matrix ; Function that generates a Hilbert matrix ; using the formula to generate hilbert matrix ; Driver code
|
using System ; class GFG { static void printMatrix ( int n ) { float [ , ] H = new float [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { H [ i , j ] = ( float ) 1.0 / ( ( i + 1 ) + ( j + 1 ) - ( float ) 1.0 ) ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) Console . Write ( H [ i , j ] + " β " ) ; Console . WriteLine ( " " ) ; } } public static void Main ( ) { int n = 3 ; printMatrix ( n ) ; } }
|
Find the GCD that lies in given range | C # Program to find the Greatest Common divisor of two number which is in given range ; Return the greatest common divisor of two numbers ; Return the gretest common divisor of a and b which lie in the given range . ; Loop from 1 to sqrt ( GCD ( a , b ) . ; if i divides the GCD ( a , b ) , then find maximum of three numbers res , i and g / i ; Driven Program
|
using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int maxDivisorRange ( int a , int b , int l , int h ) { int g = gcd ( a , b ) ; int res = - 1 ; for ( int i = l ; i * i <= g && i <= h ; i ++ ) if ( g % i == 0 ) res = Math . Max ( res , Math . Max ( i , g / i ) ) ; return res ; } public static void Main ( ) { int a = 3 , b = 27 , l = 1 , h = 5 ; Console . WriteLine ( maxDivisorRange ( a , b , l , h ) ) ; } }
|
Number expressed as sum of five consecutive integers | C # Program to check if a number can be expressed as sum of five consecutive integers . ; function to check if a number can be expressed as sum of five consecutive integers . ; if n is 0 ; if n is positive , increment loop by 1. ; if n is negative , decrement loop by 1. ; Running loop from 0 to n - 4 ; check if sum of five consecutive integer is equal to n . ; Driver Program
|
using System ; class GFG { static void checksum ( int n ) { if ( n == 0 ) { Console . Write ( " - 2 β - 1 β 0 β 1 β 2" ) ; return ; } int inc ; if ( n > 0 ) inc = 1 ; else inc = - 1 ; for ( int i = 0 ; i <= n - 4 ; i += inc ) { if ( i + i + 1 + i + 2 + i + 3 + i + 4 == n ) { Console . Write ( ( i ) + " β " + ( i + 1 ) + " β " + ( i + 2 ) + " β " + ( i + 3 ) + " β " + ( i + 4 ) ) ; return ; } } Console . WriteLine ( " - 1" ) ; } public static void Main ( ) { int n = 15 ; checksum ( n ) ; } }
|
Number expressed as sum of five consecutive integers | C # Program to check if a number can be expressed as sum of five consecutive integer . ; function to check if a number can be expressed as sum of five consecutive integers . ; if n is multiple of 5 ; else print " - 1" . ; Driver Program
|
using System ; class GFG { static void checksum ( int n ) { if ( n % 5 == 0 ) Console . WriteLine ( ( n / 5 - 2 ) + " β " + ( n / 5 - 1 ) + " β " + ( n / 5 ) + " β " + ( n / 5 + 1 ) + " β " + ( n / 5 + 2 ) ) ; else Console . WriteLine ( " - 1" ) ; } public static void Main ( ) { int n = 15 ; checksum ( n ) ; } }
|
Number of Transpositions in a Permutation | C # Program to find the number of transpositions in a permutation ; This array stores which element goes to which position ; This function returns the size of a component cycle ; If it is already visited ; This functio returns the number of transpositions in the permutation ; Initializing visited [ ] array ; building the goesTo [ ] array ; Driver Code
|
using System ; class GFG { static int N = 1000001 ; static int [ ] visited = new int [ N ] ; static int [ ] goesTo = new int [ N ] ; static int dfs ( int i ) { if ( visited [ i ] == 1 ) return 0 ; visited [ i ] = 1 ; int x = dfs ( goesTo [ i ] ) ; return ( x + 1 ) ; } static int noOfTranspositions ( int [ ] P , int n ) { for ( int i = 1 ; i <= n ; i ++ ) visited [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) goesTo [ P [ i ] ] = i + 1 ; int transpositions = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( visited [ i ] == 0 ) { int ans = dfs ( i ) ; transpositions += ans - 1 ; } } return transpositions ; } public static void Main ( ) { int [ ] permutation = { 5 , 1 , 4 , 3 , 2 } ; int n = permutation . Length ; Console . WriteLine ( noOfTranspositions ( permutation , n ) ) ; } }
|
n | C # program to find n - th term of series ; Function to find the nth term of series ; Loop to add 4 th powers ; Driver code
|
using System ; class GFG { static int sumOfSeries ( int n ) { int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) ans += i * i * i * i ; return ans ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( sumOfSeries ( n ) ) ; } }
|
Number of unmarked integers in a special sieve | C # Program to determine the number of unmarked integers in a special sieve ; Driver Code
|
using System ; class GFG { static int countUnmarked ( int N ) { if ( N % 2 == 0 ) return N / 2 ; else return N / 2 + 1 ; } public static void Main ( ) { int N = 4 ; Console . WriteLine ( " Number β of β unmarked β " + " elements : β " + countUnmarked ( N ) ) ; } }
|
Sum of series 1 * 1 ! + 2 * 2 ! + β¦β¦ . . + n * n ! | C # program to find sum of the series . ; Function to calculate required series ; Driver code
|
using System ; class GFG { static int factorial ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int calculateSeries ( int n ) { return factorial ( n + 1 ) - 1 ; } static public void Main ( ) { int n = 3 ; Console . WriteLine ( calculateSeries ( n ) ) ; } }
|
Sum of series 1 * 1 * 2 ! + 2 * 2 * 3 ! + β¦β¦ . . + n * n * ( n + 1 ) ! | C # program to find sum of the series . ; Function to calculate required series ; Driver code
|
using System ; class GFG { static int factorial ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int calculateSeries ( int n ) { return 2 + ( n * n + n - 2 ) * factorial ( n + 1 ) ; } public static void Main ( ) { int n = 3 ; Console . WriteLine ( calculateSeries ( n ) ) ; } }
|
Aspiring Number | C # implementation to check whether a number is aspiring or not ; Function to calculate sum of all proper divisors ; Note that this loop runs till square root of n ; If divisors are equal , take only one of them ; else Otherwise take both ; calculate sum of all proper divisors only ; Function to get last number of Aliquot Sequence . ; Calculate next term from previous term ; Returns true if n is perfect ; To store sum of divisors ; Find all divisors and add them ; If sum of divisors is equal to n , then n is a perfect number ; Returns true if n is aspiring else returns false ; checking condition for aspiring ; Driver code
|
using System ; using System . Collections . Generic ; class GFG { static int getSum ( int n ) { for ( int i = 1 ; i <= ( int ) Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { sum = sum + i ; } { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum - n ; } static int getAliquot ( int n ) { HashSet < int > s = new HashSet < int > ( ) ; s . Add ( n ) ; while ( n > 0 ) { n = getSum ( n ) ; if ( s . Contains ( n ) ) { return n ; } s . Add ( n ) ; } return 0 ; } static bool isPerfect ( int n ) { int sum = 1 ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { sum = sum + i + n / i ; } } if ( sum == n && n != 1 ) { return true ; } return false ; } static bool isAspiring ( int n ) { int alq = getAliquot ( n ) ; if ( isPerfect ( alq ) && ! isPerfect ( n ) ) { return true ; } else { return false ; } } public static void Main ( String [ ] args ) { int n = 25 ; if ( isAspiring ( n ) ) { Console . WriteLine ( " Aspiring " ) ; } else { Console . WriteLine ( " Not β Aspiring " ) ; } } }
|
Forming smallest array with given constraints | C # program to find the length of smallest array begin with x , having y , ends with z and having absolute difference between adjacent elements <= 1. ; Return the size of smallest array with given constraint . ; Driver Code
|
using System ; class GFG { static int minimumLength ( int x , int y , int z ) { return 1 + Math . Abs ( x - y ) + Math . Abs ( y - z ) ; } public static void Main ( ) { int x = 3 , y = 1 , z = 2 ; Console . WriteLine ( minimumLength ( x , y , z ) ) ; } }
|
Find the other | C # program to find the other - end point of diameter ; function to find the other - end point of diameter ; find end point for x coordinates ; find end point for y coordinates ; Driver Code
|
using System ; class GFG { static void endPointOfDiameterofCircle ( int x1 , int y1 , int c1 , int c2 ) { Console . Write ( " x2 β = β " + ( 2 * c1 - x1 ) + " β " ) ; Console . Write ( " y2 β = β " + ( 2 * c2 - y1 ) ) ; } public static void Main ( ) { int x1 = - 4 , y1 = - 1 ; int c1 = 3 , c2 = 5 ; endPointOfDiameterofCircle ( x1 , y1 , c1 , c2 ) ; } }
|
Newton 's Divided Difference Interpolation Formula | C # program for implementing Newton divided difference formula ; Function to find the product term ; Function for calculating divided difference table ; Function for applying Newton 's divided difference formula ; Function for displaying divided difference table ; Driver Function ; number of inputs given ; y [ ] [ ] is used for divided difference table where y [ ] [ 0 ] is used for input ; calculating divided difference table ; displaying divided difference table ; value to be interpolated ; printing the value
|
using System ; class GFG { static float proterm ( int i , float value , float [ ] x ) { float pro = 1 ; for ( int j = 0 ; j < i ; j ++ ) { pro = pro * ( value - x [ j ] ) ; } return pro ; } static void dividedDiffTable ( float [ ] x , float [ , ] y , int n ) { for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < n - i ; j ++ ) { y [ j , i ] = ( y [ j , i - 1 ] - y [ j + 1 , i - 1 ] ) / ( x [ j ] - x [ i + j ] ) ; } } } static float applyFormula ( float value , float [ ] x , float [ , ] y , int n ) { float sum = y [ 0 , 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { sum = sum + ( proterm ( i , value , x ) * y [ 0 , i ] ) ; } return sum ; } static void printDiffTable ( float [ , ] y , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n - i ; j ++ ) { Console . Write ( Math . Round ( y [ i , j ] , 4 ) + " TABSYMBOL β " ) ; } Console . WriteLine ( " " ) ; } } public static void Main ( ) { int n = 4 ; float value ; float [ , ] y = new float [ 10 , 10 ] ; float [ ] x = { 5 , 6 , 9 , 11 } ; y [ 0 , 0 ] = 12 ; y [ 1 , 0 ] = 13 ; y [ 2 , 0 ] = 14 ; y [ 3 , 0 ] = 16 ; dividedDiffTable ( x , y , n ) ; printDiffTable ( y , n ) ; value = 7 ; Console . WriteLine ( " STRNEWLINE Value β at β " + ( value ) + " β is β " + Math . Round ( applyFormula ( value , x , y , n ) , 2 ) ) ; } }
|
Centered heptagonal number | C # program to find n - th Centered heptagonal number ; Function to find Centered heptagonal number ; Formula to calculate nth Centered heptagonal number ; Driver Code
|
using System ; class GFG { static long centered_heptagonal_num ( long n ) { return ( 7 * n * n - 7 * n + 2 ) / 2 ; } public static void Main ( ) { long n = 5 ; Console . WriteLine ( n + " th β Centered β " + " heptagonal β number β : β " + centered_heptagonal_num ( n ) ) ; } }
|
Sum of square | C # Program to find the sum of sum of squares of first n natural number ; Function to find sum of sum of square of first n natural number ; Driver Program
|
using System ; public class GFG { static int findSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += ( ( i * ( i + 1 ) * ( 2 * i + 1 ) ) / 6 ) ; return sum ; } static public void Main ( ) { int n = 3 ; Console . WriteLine ( findSum ( n ) ) ; } }
|
Check if a given matrix is Hankel or not | C # Program to check if given matrix is Hankel Matrix or not . ; Function to check if given matrix is Hankel Matrix or not . ; for each row ; for each column ; checking if i + j is less than n ; checking if the element is equal to the corresponding diagonal constant ; checking if the element is equal to the corresponding diagonal constant ; Drivers code
|
using System ; class GFG { static bool checkHankelMatrix ( int n , int [ , ] m ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i + j < n ) { if ( m [ i , j ] != m [ i + j , 0 ] ) return false ; } else { if ( m [ i , j ] != m [ i + j - n + 1 , n - 1 ] ) return false ; } } } return true ; } public static void Main ( ) { int n = 4 ; int [ , ] m = { { 1 , 2 , 3 , 5 } , { 2 , 3 , 5 , 8 } , { 3 , 5 , 8 , 0 } , { 5 , 8 , 0 , 9 } } ; if ( checkHankelMatrix ( n , m ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
|
Check if a number can be expressed as power | Set 2 ( Using Log ) | C # program to find if a number can be expressed as x raised to power y . ; Find Log n in different bases and check if the value is an integer ; Driver Code
|
using System ; class GFG { static bool isPower ( int n ) { for ( int x = 2 ; x <= ( int ) Math . Sqrt ( n ) ; x ++ ) { float f = ( float ) Math . Log ( n ) / ( float ) Math . Log ( x ) ; if ( ( f - ( int ) f ) == 0.0 ) return true ; } return false ; } public static void Main ( ) { for ( int i = 2 ; i < 100 ; i ++ ) if ( isPower ( i ) ) Console . Write ( i + " β " ) ; } }
|
Queries on sum of odd number digit sums of all the factors of a number | C # Program to answer queries on sum of sum of odd number digits of all the factors of a number ; finding sum of odd digit number in each integer . ; for each number ; using previous number sum , finding the current number num of odd digit also , adding last digit if it is odd . ; finding sum of sum of odd digit of all the factors of a number . ; for each possible factor ; adding the contribution . ; Wrapper function ; Driver code
|
using System ; class GFG { static int N = 1000005 ; static void sumOddDigit ( int [ ] digitSum ) { for ( int i = 1 ; i < N ; i ++ ) { digitSum [ i ] = digitSum [ i / 10 ] + ( i & 1 ) * ( i % 10 ) ; } } static void sumFactor ( int [ ] digitSum , int [ ] factorDigitSum ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = i ; j < N ; j += i ) { factorDigitSum [ j ] += digitSum [ i ] ; } } } static void wrapper ( int q , int [ ] n ) { int [ ] digitSum = new int [ N ] ; int [ ] factorDigitSum = new int [ N ] ; sumOddDigit ( digitSum ) ; sumFactor ( digitSum , factorDigitSum ) ; for ( int i = 0 ; i < q ; i ++ ) Console . Write ( factorDigitSum [ n [ i ] ] + " β " ) ; } public static void Main ( ) { int q = 2 ; int [ ] n = new int [ ] { 10 , 36 } ; wrapper ( q , n ) ; } }
|
Program for Gauss | C # Implementation for Gauss - Jordan Elimination Method ; Function to print the matrix ; function to reduce matrix to reduced row echelon form . ; Performing elementary operations ; Excluding all i == j ; Converting Matrix to reduced row echelon form ( diagonal matrix ) ; Function to print the desired result if unique solutions exists , otherwise prints no solution or infinite solutions depending upon the input given . ; Printing the solution by dividing constants by their respective diagonal elements ; To check whether infinite solutions exists or no solution exists ; flag == 2 for infinite solution flag == 3 for No solution ; Driver code ; Order of Matrix ( n ) ; Performing Matrix transformation ; Printing Final Matrix ; Printing Solutions ( if exist )
|
using System ; using System . Collections . Generic ; class GFG { static int M = 10 ; static void PrintMatrix ( float [ , ] a , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) Console . Write ( a [ i , j ] + " β " ) ; Console . WriteLine ( ) ; } } static int PerformOperation ( float [ , ] a , int n ) { int i , j , k = 0 , c , flag = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i , i ] == 0 ) { c = 1 ; while ( ( i + c ) < n && a [ i + c , i ] == 0 ) c ++ ; if ( ( i + c ) == n ) { flag = 1 ; break ; } for ( j = i , k = 0 ; k <= n ; k ++ ) { float temp = a [ j , k ] ; a [ j , k ] = a [ j + c , k ] ; a [ j + c , k ] = temp ; } } for ( j = 0 ; j < n ; j ++ ) { if ( i != j ) { float p = a [ j , i ] / a [ i , i ] ; for ( k = 0 ; k <= n ; k ++ ) a [ j , k ] = a [ j , k ] - ( a [ i , k ] ) * p ; } } } return flag ; } static void PrintResult ( float [ , ] a , int n , int flag ) { Console . Write ( " Result β is β : β " ) ; if ( flag == 2 ) Console . WriteLine ( " Infinite β Solutions β Exists " ) ; else if ( flag == 3 ) Console . WriteLine ( " No β Solution β Exists " ) ; else { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i , n ] / a [ i , i ] + " β " ) ; } } static int CheckConsistency ( float [ , ] a , int n , int flag ) { int i , j ; float sum ; flag = 3 ; for ( i = 0 ; i < n ; i ++ ) { sum = 0 ; for ( j = 0 ; j < n ; j ++ ) sum = sum + a [ i , j ] ; if ( sum == a [ i , j ] ) flag = 2 ; } return flag ; } public static void Main ( String [ ] args ) { float [ , ] a = { { 0 , 2 , 1 , 4 } , { 1 , 1 , 2 , 6 } , { 2 , 1 , 1 , 7 } } ; int n = 3 , flag = 0 ; flag = PerformOperation ( a , n ) ; if ( flag == 1 ) flag = CheckConsistency ( a , n , flag ) ; Console . WriteLine ( " Final β Augumented β Matrix β is β : β " ) ; PrintMatrix ( a , n ) ; Console . WriteLine ( " " ) ; PrintResult ( a , n , flag ) ; } }
|
Number of digits in the nth number made of given four digits | C # program to count number of digits in n - th number made of given four digits . ; Efficient function to calculate number of digits in the nth number constructed by using 6 , 1 , 4 and 9 as digits in the ascending order . ; Number of digits increase after every i - th number where i increases in powers of 4. ; Driver Code
|
using System ; public class GFG { static int number_of_digits ( int n ) { int i ; int res ; int sum = 0 ; for ( i = 4 , res = 1 ; ; i *= 4 , res ++ ) { sum += i ; if ( sum >= n ) break ; } return res ; } static public void Main ( ) { int n = 21 ; Console . WriteLine ( number_of_digits ( n ) ) ; } }
|
Print prime numbers from 1 to N in reverse order | C # program to print all primes between 1 to N in reverse order using Sieve of Eratosthenes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Print all prime numbers ; Driver code ; static input ; To display ; calling the function
|
using System ; class GFG { static void reverseorder ( int n ) { bool [ ] prime = new bool [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int i = n ; i >= 2 ; i -- ) if ( prime [ i ] == true ) Console . Write ( i + " β " ) ; } public static void Main ( ) { int N = 25 ; Console . WriteLine ( " Prime β number β in " + " β reverse β order " ) ; if ( N == 1 ) Console . WriteLine ( " No β prime β no " + " β exist β in β this β range " ) ; else reverseorder ( N ) ; } }
|
Vantieghems Theorem for Primality Test | C # code to verify Vantieghem 's Theorem ; Check if above condition is satisfied ; product of previous powers of 2 ; Driver code
|
using System ; class GFG { static void checkVantieghemsTheorem ( int limit ) { long prod = 1 ; for ( long n = 2 ; n < limit ; n ++ ) { if ( ( ( prod - n < 0 ? 0 : prod - n ) % ( ( 1 << ( int ) n ) - 1 ) ) == 0 ) Console . Write ( n + " β is β prime STRNEWLINE " ) ; prod *= ( ( 1 << ( int ) n ) - 1 ) ; } } public static void Main ( ) { checkVantieghemsTheorem ( 10 ) ; } }
|
Count numbers formed by given two digit with sum having given digits | C # program to count the number of numbers formed by digits a and b exactly of a length N such that the sum of the digits of the number thus formed is of digits a and b . ; function to check if sum of digits is made of a and b ; sum of digits is 0 ; if any of digits in sum is other than a and b ; calculate the modInverse V / of a number in O ( log n ) ; q is quotient ; m is remainder now , process same as Euclid 's algo ; Update y and x ; Make x positive ; function to pregenerate factorials ; function to pre calculate the modInverse of factorials ; calculates the modInverse of the last factorial ; precalculates the modInverse of all factorials by formulae ; function that returns the value of nCi ; function that returns the count of numbers ; function call to pre - calculate the factorials and modInverse of factorials ; if a and b are same ; Driver Code
|
using System ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int N = 1000005 ; static int [ ] fact = new int [ N ] ; static int [ ] invfact = new int [ N ] ; static int check ( int x , int a , int b ) { if ( x == 0 ) { return 0 ; } while ( x > 0 ) { if ( x % 10 != a & x % 10 != b ) { return 0 ; } x /= 10 ; } return 1 ; } static int modInverse ( int a , int m ) { int m0 = m ; int y = 0 , x = 1 ; if ( m == 1 ) { return 0 ; } while ( a > 1 ) { int q = a / m ; int t = m ; m = a % m ; a = t ; t = y ; y = x - q * y ; x = t ; } if ( x < 0 ) { x += m0 ; } return x ; } static void pregenFact ( ) { fact [ 0 ] = fact [ 1 ] = 1 ; for ( int i = 1 ; i <= 1000000 ; ++ i ) { fact [ i ] = ( int ) ( ( long ) fact [ i - 1 ] * i % mod ) ; } } static void pregenInverse ( ) { invfact [ 0 ] = invfact [ 1 ] = 1 ; invfact [ 1000000 ] = modInverse ( fact [ 1000000 ] , mod ) ; for ( int i = 999999 ; i > 1 ; -- i ) { invfact [ i ] = ( int ) ( ( ( long ) invfact [ i + 1 ] * ( long ) ( i + 1 ) ) % mod ) ; } } static int comb ( int big , int small ) { return ( int ) ( ( long ) fact [ big ] * invfact [ small ] % mod * invfact [ big - small ] % mod ) ; } static int count ( int a , int b , int n ) { pregenFact ( ) ; pregenInverse ( ) ; if ( a == b ) { return ( check ( a * n , a , b ) ) ; } int ans = 0 ; for ( int i = 0 ; i <= n ; ++ i ) { if ( check ( i * a + ( n - i ) * b , a , b ) == 1 ) { ans = ( ans + comb ( n , i ) ) % mod ; } } return ans ; } public static void Main ( String [ ] args ) { int a = 3 , b = 4 , n = 11028 ; Console . WriteLine ( count ( a , b , n ) ) ; } }
|
Finding n | C # Program to Finding n - th term of series 3 , 13 , 42 , 108 , 235 . . . ; Function to generate a fixed number ; Driver Code
|
using System ; class GFG { public static int magicOfSequence ( int N ) { int sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) sum += ( i * i * i + i * 2 ) ; return sum ; } static public void Main ( ) { int N = 4 ; Console . WriteLine ( magicOfSequence ( N ) ) ; } }
|
Expressing a number as sum of consecutive | Set 2 ( Using odd factors ) | C # program to count number of ways to express N as sum of consecutive numbers . ; returns the number of odd factors ; If i is an odd factor and n is a perfect square ; If n is not perfect square ; Driver Code ; N as sum of consecutive numbers
|
using System ; class GFG { static int countOddFactors ( long n ) { int odd_factors = 0 ; for ( int i = 1 ; 1 * i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( 1 * i * i == n ) { if ( ( i & 1 ) == 1 ) odd_factors ++ ; } else { if ( ( i & 1 ) == 1 ) odd_factors ++ ; int factor = ( int ) n / i ; if ( ( factor & 1 ) == 1 ) odd_factors ++ ; } } } return odd_factors - 1 ; } static void Main ( ) { long N = 15 ; Console . WriteLine ( countOddFactors ( N ) ) ; N = 10 ; Console . WriteLine ( countOddFactors ( N ) ) ; } }
|
Making zero array by decrementing pairs of adjacent | C # program to find if it is possible to make all array elements 0 by decrement operations . ; used for storing the sum of even and odd position element in array . ; if position is odd , store sum value of odd position in odd ; if position is even , store sum value of even position in even ; Driver Code
|
using System ; class GFG { static bool isPossibleToZero ( int [ ] a , int n ) { int even = 0 , odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( i & 1 ) == 0 ) odd += a [ i ] ; else even += a [ i ] ; } return ( odd == even ) ; } static public void Main ( ) { int [ ] arr = { 0 , 1 , 1 , 0 } ; int n = arr . Length ; if ( isPossibleToZero ( arr , n ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
|
Program for sum of cos ( x ) series | C # program to find the sum of cos ( x ) series ; here x is in degree . we have to convert it to radian for using it with series formula , as in series expansion angle is in radian ; Driver Code
|
using System ; class GFG { static double PI = 3.142 ; static double cosXSertiesSum ( double x , int n ) { x = x * ( PI / 180.0 ) ; double res = 1 ; double sign = 1 , fact = 1 , pow = 1 ; for ( int i = 1 ; i < 5 ; i ++ ) { sign = sign * - 1 ; fact = fact * ( 2 * i - 1 ) * ( 2 * i ) ; pow = pow * x * x ; res = res + sign * pow / fact ; } return res ; } public static void Main ( ) { float x = 50 ; int n = 5 ; Console . Write ( ( float ) ( cosXSertiesSum ( x , n ) * 1000000 ) / 1000000.00 ) ; } }
|
Sum of digits written in different bases from 2 to n | Java program to find the sum of digits of n in different base1s from 2 to n - 1. ; function to calculate sum of digit for a given base1 ; Sum of digits ; Calculating the number ( n ) by taking mod with the base1 and adding remainder to the result and parallelly reducing the num value . ; returning the result ; function calling for multiple base1s ; Driver Code
|
using System ; class GFG { static int solve ( int n , int base1 ) { int result = 0 ; while ( n > 0 ) { int remainder = n % base1 ; result = result + remainder ; n = n / base1 ; } return result ; } static void printSumsOfDigits ( int n ) { for ( int base1 = 2 ; base1 < n ; ++ base1 ) Console . Write ( solve ( n , base1 ) + " β " ) ; } public static void Main ( ) { int n = 8 ; printSumsOfDigits ( n ) ; } }
|
Possible two sets from first N natural numbers difference of sums as D | C # program for implementing above approach ; Function returns true if it is possible to split into two sets otherwise returns false ; Driver code
|
using System ; class GFG { static bool check ( int N , int D ) { int temp = ( N * ( N + 1 ) ) / 2 + D ; return ( temp % 2 == 0 ) ; } static public void Main ( ) { int N = 5 ; int M = 7 ; if ( check ( N , M ) ) Console . Write ( " yes " ) ; else Console . Write ( " no " ) ; } }
|
Minimum digits to remove to make a number Perfect Square | C # program to find required minimum digits need to remove to make a number perfect square ; function to check minimum number of digits should be removed to make this number a perfect square ; size of the string ; our final answer ; to store string which is perfect square . ; We make all possible subsequences ; to check jth bit is set or not . ; we do not consider a number with leading zeros ; convert our temporary string into integer ; checking temp is perfect square or not . ; taking maximum sized string ; print PerfectSquare ; Driver code
|
using System ; class GFG { static int perfectSquare ( string s ) { int n = s . Length ; int ans = - 1 ; string num = " " ; for ( int i = 1 ; i < ( 1 << n ) ; i ++ ) { string str = " " ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( ( i >> j ) & 1 ) == 1 ) { str += s [ j ] ; } } if ( str [ 0 ] != '0' ) { int temp = 0 ; for ( int j = 0 ; j < str . Length ; j ++ ) temp = temp * 10 + ( int ) ( str [ j ] - '0' ) ; int k = ( int ) Math . Sqrt ( temp ) ; if ( k * k == temp ) { if ( ans < ( int ) str . Length ) { ans = ( int ) str . Length ; num = str ; } } } } if ( ans == - 1 ) return ans ; else { Console . Write ( num + " β " ) ; return n - ans ; } } public static void Main ( ) { Console . WriteLine ( perfectSquare ( "8314" ) ) ; Console . WriteLine ( perfectSquare ( "753" ) ) ; } }
|
Lagrange 's four square theorem | C # program for Lagrange 's four square identity ; Prints all the possible combinations 4 numbers whose sum of squares is equal to the given no . ; loops checking the sum of squares ; if sum of four squares equals the given no . ; printing the numbers ; Driver code ; 74 = 0 * 0 + 0 * 0 + 5 * 5 + 7 * 7 74 = 0 * 0 + 1 * 1 + 3 * 3 + 8 * 8 74 = 0 * 0 + 3 * 3 + 4 * 4 + 7 * 7 74 = 1 * 1 + 1 * 1 + 6 * 6 + 6 * 6 74 = 2 * 2 + 3 * 3 + 5 * 5 + 6 * 6
|
using System ; class GFG { static void printFourSquares ( int a ) { for ( int i = 0 ; i * i <= a ; i ++ ) { for ( int j = i ; j * j <= a ; j ++ ) { for ( int k = j ; k * k <= a ; k ++ ) { for ( int l = k ; l * l <= a ; l ++ ) { if ( i * i + j * j + k * k + l * l == a ) { Console . Write ( a + " β = β " + i + " * " + i + " β + β " + j + " * " + j + " β + β " ) ; Console . Write ( k + " * " + k + " β + β " + l + " * " + l + " STRNEWLINE " ) ; } } } } } } public static void Main ( ) { int a = 74 ; printFourSquares ( a ) ; } }
|
Hardy | C # program to count all prime factors ; A function to count prime factors of a given number n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; This condition is to handle the case when n is a prime number greater than 2 ; Driver function
|
using System ; class GFG { static int exactPrimeFactorCount ( int n ) { int count = 0 ; if ( n % 2 == 0 ) { count ++ ; while ( n % 2 == 0 ) n = n / 2 ; } for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { count ++ ; while ( n % i == 0 ) n = n / i ; } } if ( n > 2 ) count ++ ; return count ; } public static void Main ( ) { int n = 51242183 ; Console . WriteLine ( " The β number β of " + " β distinct β prime β factors β is / are β " + exactPrimeFactorCount ( n ) ) ; Console . WriteLine ( " The β value β of β " + " log ( log ( n ) ) β is β " + Math . Log ( Math . Log ( n ) ) ) ; } }
|
Number of Digits in a ^ b | C # Program to calculate no . of digits in a ^ b ; function to calculate number of digits in a ^ b ; driver program
|
using System ; class GFG { static int no_of_digit ( int a , int b ) { return ( ( int ) ( b * Math . Log10 ( a ) ) + 1 ) ; } public static void Main ( ) { int a = 2 , b = 100 ; Console . Write ( " no . β of β digits β = β " + no_of_digit ( a , b ) ) ; } }
|
Check whether a number is Emirpimes or not | C # code to check whether a number is Emirpimes or not ; Checking whether a number is semi - prime or not ; Increment count of prime numbers ; If number is still greater than 1 , after exiting the for loop add it to the count variable as it indicates the number is a prime number ; Return '1' if count is equal to '2' else return '0' ; Checking whether a number is emirpimes or not ; Number itself is not semiprime . ; Finding reverse of n . ; The definition of emirpimes excludes palindromes , hence we do not check further , if the number entered is a palindrome ; Checking whether the reverse of the semi prime number entered is also a semi prime number or not ; Driver Code
|
using System ; class GFG { static bool checkSemiprime ( int num ) { int cnt = 0 ; for ( int i = 2 ; cnt < 2 && i * i <= num ; ++ i ) { while ( num % i == 0 ) { num /= i ; ++ cnt ; } } if ( num > 1 ) ++ cnt ; return cnt == 2 ; } static bool isEmirpimes ( int n ) { if ( checkSemiprime ( n ) == false ) return false ; int r = 0 ; for ( int t = n ; t != 0 ; t = t / n ) r = r * 10 + t % 10 ; if ( r == n ) return false ; return ( checkSemiprime ( r ) ) ; } public static void Main ( ) { int n = 15 ; if ( isEmirpimes ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
|
Booth β s Multiplication Algorithm | C # code to implement booth 's algorithm ; function to perform adding in the accumulator ; updating accumulator with A = A + BR ; function to find the number 's complement ; function to perform right shift ; function to display operations ; accumulator content ; multiplier content ; Function to implement booth 's algo ; SECOND CONDITION ; subtract BR from accumulator ; THIRD CONDITION ; add BR to accumulator ; FIRST CONDITION ; decrement counter ; Driver code ; Number of multiplicand bit ; multiplicand ; copy multiplier to temp array mt [ ] ; No . of multiplier bit ; sequence counter ; multiplier
|
using System ; class GFG { static void add ( int [ ] ac , int [ ] x , int qrn ) { int i , c = 0 ; for ( i = 0 ; i < qrn ; i ++ ) { ac [ i ] = ac [ i ] + x [ i ] + c ; if ( ac [ i ] > 1 ) { ac [ i ] = ac [ i ] % 2 ; c = 1 ; } else c = 0 ; } } static void complement ( int [ ] a , int n ) { int i ; int [ ] x = new int [ 8 ] ; Array . Clear ( x , 0 , 8 ) ; x [ 0 ] = 1 ; for ( i = 0 ; i < n ; i ++ ) { a [ i ] = ( a [ i ] + 1 ) % 2 ; } add ( a , x , n ) ; } static void rightShift ( int [ ] ac , int [ ] qr , ref int qn , int qrn ) { int temp , i ; temp = ac [ 0 ] ; qn = qr [ 0 ] ; Console . Write ( " TABSYMBOL TABSYMBOL rightShift TABSYMBOL " ) ; for ( i = 0 ; i < qrn - 1 ; i ++ ) { ac [ i ] = ac [ i + 1 ] ; qr [ i ] = qr [ i + 1 ] ; } qr [ qrn - 1 ] = temp ; } static void display ( int [ ] ac , int [ ] qr , int qrn ) { int i ; for ( i = qrn - 1 ; i >= 0 ; i -- ) Console . Write ( ac [ i ] ) ; Console . Write ( " TABSYMBOL " ) ; for ( i = qrn - 1 ; i >= 0 ; i -- ) Console . Write ( qr [ i ] ) ; } static void boothAlgorithm ( int [ ] br , int [ ] qr , int [ ] mt , int qrn , int sc ) { int qn = 0 ; int [ ] ac = new int [ 10 ] ; Array . Clear ( ac , 0 , 10 ) ; int temp = 0 ; Console . Write ( " qn TABSYMBOL q [ n β + β 1 ] TABSYMBOL BR TABSYMBOL " + " TABSYMBOL AC TABSYMBOL QR TABSYMBOL TABSYMBOL sc STRNEWLINE " ) ; Console . Write ( " TABSYMBOL TABSYMBOL TABSYMBOL initial TABSYMBOL TABSYMBOL " ) ; display ( ac , qr , qrn ) ; Console . Write ( " TABSYMBOL TABSYMBOL " + sc + " STRNEWLINE " ) ; while ( sc != 0 ) { Console . Write ( qr [ 0 ] + " TABSYMBOL " + qn ) ; if ( ( qn + qr [ 0 ] ) == 1 ) { if ( temp == 0 ) { add ( ac , mt , qrn ) ; Console . Write ( " TABSYMBOL TABSYMBOL A β = β A β - β BR TABSYMBOL " ) ; for ( int i = qrn - 1 ; i >= 0 ; i -- ) Console . Write ( ac [ i ] ) ; temp = 1 ; } else if ( temp == 1 ) { add ( ac , br , qrn ) ; Console . Write ( " TABSYMBOL TABSYMBOL A β = β A β + β BR TABSYMBOL " ) ; for ( int i = qrn - 1 ; i >= 0 ; i -- ) Console . Write ( ac [ i ] ) ; temp = 0 ; } Console . Write ( " STRNEWLINE TABSYMBOL " ) ; rightShift ( ac , qr , ref qn , qrn ) ; } else if ( qn - qr [ 0 ] == 0 ) rightShift ( ac , qr , ref qn , qrn ) ; display ( ac , qr , qrn ) ; Console . Write ( " TABSYMBOL " ) ; sc -- ; Console . Write ( " TABSYMBOL " + sc + " STRNEWLINE " ) ; } } static void Main ( ) { int [ ] mt = new int [ 10 ] ; int sc , brn , qrn ; brn = 4 ; int [ ] br = new int [ ] { 0 , 1 , 1 , 0 } ; for ( int i = brn - 1 ; i >= 0 ; i -- ) mt [ i ] = br [ i ] ; Array . Reverse ( br ) ; complement ( mt , brn ) ; qrn = 4 ; sc = qrn ; int [ ] qr = new int [ ] { 1 , 0 , 1 , 0 } ; Array . Reverse ( qr ) ; boothAlgorithm ( br , qr , mt , qrn , sc ) ; Console . WriteLine ( ) ; Console . Write ( " Result β = β " ) ; for ( int i = qrn - 1 ; i >= 0 ; i -- ) Console . Write ( qr [ i ] ) ; } }
|
Connell Sequence | C # code to generate first ' n ' terms of Connell Sequence ; Function to generate a fixed number of even or odd terms . The size of r decides whether numbers to be generated even or odd . ; Generating the first ' n ' terms of Connell Sequence ; A dummy 0 is inserted at the beginning for consistency ; Calling function gen ( ) to generate ' k ' number of terms ; Checking if ' n ' terms are already generated ; Removing the previously inserted dummy 0 ; Driver Code
|
using System ; using System . Collections . Generic ; class GFG { static List < long > gen ( long n , List < long > r ) { long a = r [ r . Count - 1 ] ; a ++ ; for ( int i = 1 ; i <= n ; a += 2 , i ++ ) r . Add ( a ) ; return r ; } static List < long > conell ( long n ) { List < long > res = new List < long > ( ) ; long k = 1 ; res . Add ( 0 ) ; while ( true ) { res = gen ( k , res ) ; k ++ ; int j = res . Count - 1 ; while ( j != n && j + k > n ) k -- ; if ( j >= n ) break ; } res . RemoveAt ( 0 ) ; return res ; } static void Main ( ) { long n = 10 ; Console . WriteLine ( " The β first β " + n + " β terms β are " ) ; List < long > res = conell ( n ) ; for ( int i = 0 ; i < res . Count ; i ++ ) Console . Write ( res [ i ] + " β " ) ; Console . WriteLine ( ) ; } }
|
Generate a list of n consecutive composite numbers ( An interesting method ) | C # program to print n consecutive composite numbers ; function to find factorial of given number ; Prints n consecutive numbers . ; Driver program to test above function
|
using System ; public class Program { static long factorial ( int n ) { long res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { res *= i ; } return res ; } static void printNComposite ( int n ) { long fact = factorial ( n + 1 ) ; for ( int i = 2 ; i <= n + 1 ; ++ i ) { Console . Write ( fact + i + " β " ) ; } } public static void Main ( ) { int n = 4 ; printNComposite ( n ) ; } }
|
Frugal Number | Program to check for Frugal number ; Finding primes upto entered number ; Finding primes by Sieve of Eratosthenes method ; If prime [ i ] is not changed , then it is prime ; Update all multiples of p ; Forming array of the prime numbers found ; Returns number of digits in n ; Checking whether a number is Frugal or not ; Finding number of digits in prime factorization of the number ; Exponent for current factor ; Counting number of times this prime factor divides ( Finding exponent ) ; Finding number of digits in the exponent Avoiding exponents of value 1 ; Checking condition for frugal number ; Driver Code
|
using System ; using System . Collections . Generic ; class GFG { static List < long > primes ( long n ) { bool [ ] prime = new bool [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) prime [ i ] = true ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( prime [ i ] == true ) { for ( int j = i * 2 ; j <= n ; j += i ) prime [ j ] = false ; } } List < long > arr = new List < long > ( ) ; for ( int i = 2 ; i < n ; i ++ ) if ( prime [ i ] ) arr . Add ( i ) ; return arr ; } static int countDigits ( long n ) { long temp = n ; int c = 0 ; while ( temp != 0 ) { temp = temp / 10 ; c ++ ; } return c ; } static bool frugal ( long n ) { List < long > r = primes ( n ) ; long t = n ; long s = 0 ; for ( int i = 0 ; i < r . Count ; i ++ ) { if ( t % r [ i ] == 0 ) { long k = 0 ; while ( t % r [ i ] == 0 ) { t = t / r [ i ] ; k ++ ; } if ( k == 1 ) s = s + countDigits ( r [ i ] ) ; else if ( k != 1 ) s = s + countDigits ( r [ i ] ) + countDigits ( k ) ; } } return ( countDigits ( n ) > s && s != 0 ) ; } static void Main ( ) { long n = 343 ; if ( frugal ( n ) ) Console . Write ( " A β Frugal β number STRNEWLINE " ) ; else Console . Write ( " Not β a β frugal β number STRNEWLINE " ) ; } }
|
N | C # program to find n - th number which is both square and cube . ; Driver code
|
using System ; class GFG { static int nthSquareCube ( int n ) { return n * n * n * n * n * n ; } static public void Main ( ) { int n = 5 ; Console . WriteLine ( nthSquareCube ( n ) ) ; } }
|
Squared triangular number ( Sum of cubes ) | C # program to check if a given number is sum of cubes of natural numbers . ; Function to find if the given number is sum of the cubes of first n natural numbers ; Start adding cubes of the numbers from 1 ; If sum becomes equal to s return n ; Driver code
|
using System ; class GFG { public static int findS ( int s ) { int sum = 0 ; for ( int n = 1 ; sum < s ; n ++ ) { sum += n * n * n ; if ( sum == s ) return n ; } return - 1 ; } static public void Main ( string [ ] args ) { int s = 9 ; int n = findS ( s ) ; if ( n == - 1 ) Console . WriteLine ( " - 1" ) ; else Console . WriteLine ( n ) ; } }
|
Number with even sum of digits | C # program to find n - th Good number . ; Function to find kth good number ; Find the last digit of n . ; If last digit is between 0 to 4 then return 2 * n . ; If last digit is between 5 to 9 then return 2 * n + 1. ; Driver code
|
using System ; class GFG { public static int findKthGoodNo ( int n ) { int lastDig = n % 10 ; if ( lastDig >= 0 && lastDig <= 4 ) return n << 1 ; else return ( n << 1 ) + 1 ; } static public void Main ( string [ ] args ) { int n = 10 ; Console . WriteLine ( findKthGoodNo ( n ) ) ; } }
|
Nicomachu 's Theorem | C # program to verify Nicomachu 's Theorem ; Compute sum of cubes ; Check if sum is equal to given formula . ; Driver Code
|
using System ; class GFG { static void NicomachuTheorum_sum ( int n ) { int sum = 0 ; for ( int k = 1 ; k <= n ; k ++ ) sum += k * k * k ; int triNo = n * ( n + 1 ) / 2 ; if ( sum == triNo * triNo ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( ) { int n = 5 ; NicomachuTheorum_sum ( n ) ; } }
|
Largest even digit number not greater than N | C # program to print the largest integer not greater than N with all even digits ; function to check if all digits are even of a given number ; iterate for all digits ; if digit is odd ; all digits are even ; function to return the largest number with all digits even ; iterate till we find a number with all digits even ; Driver Code
|
using System ; public class GFG { static int checkDigits ( int n ) { while ( n > 0 ) { if ( ( ( n % 10 ) % 2 ) > 0 ) return 0 ; n /= 10 ; } return 1 ; } static int largestNumber ( int n ) { for ( int i = n ; ; i -- ) if ( checkDigits ( i ) > 0 ) return i ; } static public void Main ( ) { int N = 23 ; Console . WriteLine ( largestNumber ( N ) ) ; } }
|
Largest even digit number not greater than N | C # program to print the largest integer not greater than N with all even digits ; function to return the largest number with all digits even ; convert the number to a string for easy operations ; find first odd digit ; if no digit , then N is the answer ; till first odd digit , add all even numbers ; decrease 1 from the odd digit ; add 0 in the rest of the digits ; Driver Code
|
using System ; class GFG { static int largestNumber ( int n ) { string s = " " ; int duplicate = n ; while ( n > 0 ) { s = ( char ) ( n % 10 + 48 ) + s ; n /= 10 ; } int index = - 1 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( ( ( ( int ) ( s [ i ] - '0' ) % 2 ) & 1 ) > 0 ) { index = i ; break ; } } if ( index == - 1 ) return duplicate ; int num = 0 ; for ( int i = 0 ; i < index ; i ++ ) num = num * 10 + ( int ) ( s [ i ] - '0' ) ; num = num * 10 + ( ( int ) s [ index ] - ( int ) ( '0' ) - 1 ) ; for ( int i = index + 1 ; i < s . Length ; i ++ ) num = num * 10 + 8 ; return num ; } static void Main ( ) { int N = 24578 ; Console . WriteLine ( largestNumber ( N ) ) ; } }
|
Number of digits in 2 raised to power n | C # program to find number of digits in 2 ^ n ; Function to find number of digits in 2 ^ n ; Driver code
|
using System ; class GFG { static int countDigits ( int n ) { return ( int ) ( n * Math . Log10 ( 2 ) + 1 ) ; } static void Main ( ) { int n = 5 ; Console . Write ( countDigits ( n ) ) ; } }
|
Smallest even digits number not less than N | C # program to print the smallest integer not less than N with all even digits ; function to check if all digits are even of a given number ; iterate for all digits ; if digit is odd ; all digits are even ; function to return the smallest number with all digits even ; iterate till we find a number with all digits even ; Driver Code
|
using System ; class GFG { static int check_digits ( int n ) { while ( n != 0 ) { if ( ( n % 10 ) % 2 != 0 ) return 0 ; n /= 10 ; } return 1 ; } static int smallest_number ( int n ) { for ( int i = n ; ; i ++ ) if ( check_digits ( i ) != 0 ) return i ; } public static void Main ( ) { int N = 2397 ; Console . WriteLine ( smallest_number ( N ) ) ; } }
|
Smallest triangular number larger than p | C # code to find the bucket to choose for picking flowers out of it ; Driver code
|
using System ; class GFG { static int findBucketNo ( int p ) { return ( int ) Math . Ceiling ( ( Math . Sqrt ( 8 * p + 1 ) - 1 ) / 2 ) ; } static public void Main ( ) { int p = 10 ; Console . WriteLine ( findBucketNo ( p ) ) ; } }
|
LCM of factorial and its neighbors | Program to calculate the LCM of N ! and its neighbor ( N - 1 ) ! and ( N + 1 ) ! ; function to calculate the factorial ; returning the factorial of the largest number in the given three consecutive numbers ; Driver code
|
using System ; class GFG { static int factorial ( int n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; } static int LCMOfNeighbourFact ( int n ) { return factorial ( n + 1 ) ; } public static void Main ( ) { int N = 5 ; Console . WriteLine ( LCMOfNeighbourFact ( N ) ) ; } }
|
Expressing factorial n as sum of consecutive numbers | C # program to count number of ways we can express a factorial as sum of consecutive numbers ; sieve of Eratosthenes to compute the prime numbers ; Store all prime numbers ; function to calculate the largest power of a prime in a number ; Modular multiplication to avoid the overflow of multiplication Please see below for details https : www . geeksforgeeks . org / how - to - avoid - overflow - in - modular - multiplication / ; Returns count of ways to express n ! as sum of consecutives . ; We skip 2 ( First prime ) as we need to consider only odd primes ; compute the largest power of prime ; if the power of current prime number is zero in N ! , power of primes greater than current prime number will also be zero , so break out from the loop ; multiply the result at every step ; subtract 1 to exclude the case of 1 being an odd divisor ; Driver function
|
using System ; using System . Collections ; class GFG { static int MAX = 50002 ; static ArrayList primes = new ArrayList ( ) ; public static void sieve ( ) { bool [ ] isPrime = new bool [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i < MAX ; i += p ) isPrime [ i ] = false ; } } for ( int p = 2 ; p < MAX ; p ++ ) if ( isPrime [ p ] == true ) primes . Add ( p ) ; } public static int power_prime ( int x , int y ) { int count = 0 ; int z = y ; while ( x >= z ) { count += ( x / z ) ; z *= y ; } return count ; } public static int modMult ( int a , int b , int mod ) { int res = 0 ; a = a % mod ; while ( b > 0 ) { if ( b % 2 == 1 ) res = ( res + a ) % mod ; a = ( a * 2 ) % mod ; b /= 2 ; } return res % mod ; } public static int countWays ( int n , int m ) { int ans = 1 ; for ( int i = 1 ; i < primes . Count ; i ++ ) { int powers = power_prime ( n , Convert . ToInt32 ( primes [ i ] ) ) ; if ( powers == 0 ) break ; ans = modMult ( ans , powers + 1 , m ) % m ; } if ( ( ( ans - 1 ) % m ) < 0 ) return ( ans - 1 + m ) % m ; else return ( ans - 1 ) % m ; } public static void Main ( ) { sieve ( ) ; int n = 4 , m = 7 ; Console . WriteLine ( countWays ( n , m ) ) ; } }
|
Check if the given two numbers are friendly pair or not | C # code to check if the given two number are friendly pair or not ; Returns sum of all factors of n . ; Traversing through all prime factors . ; THE BELOW STATEMENT MAKES IT BETTER THAN ABOVE METHOD AS WE REDUCE VALUE OF n . ; This condition is to handle the case when n is a prime number greater than 2. ; Function to return gcd of a and b ; Function to check if the given two number are friendly pair or not ; Finding the sum of factors of n and m ; finding gcd of n and sum of its factors . ; finding gcd of m and sum of its factors . ; checking is numerator and denominator of abundancy index of both number are equal or not ; Driver code
|
using System ; class GFG { 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 int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static bool checkFriendly ( int n , int m ) { int sumFactors_n = sumofFactors ( n ) ; int sumFactors_m = sumofFactors ( m ) ; int gcd_n = gcd ( n , sumFactors_n ) ; int gcd_m = gcd ( m , sumFactors_m ) ; if ( n / gcd_n == m / gcd_m && sumFactors_n / gcd_n == sumFactors_m / gcd_m ) return true ; else return false ; } public static void Main ( String [ ] args ) { int n = 6 , m = 28 ; if ( checkFriendly ( n , m ) ) Console . Write ( " Yes STRNEWLINE " ) ; else Console . Write ( " No STRNEWLINE " ) ; } }
|
Find n | C # program to find n - th Fortunate number ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to Find primorial of order n ( product of first n prime numbers ) . ; Function to find next prime number greater than n ; Note that difference ( or m ) should be greater than 1. ; loop continuously until isPrime returns true for a number above n ; Ignoring the prime number that is 1 greater than n ; Returns n - th Fortunate number ; Driver Code
|
using System ; class GFG { public 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 ; } public static int primorial ( int n ) { int p = 2 ; n -- ; for ( int i = 3 ; n != 0 ; i ++ ) { if ( isPrime ( i ) == true ) { p = p * i ; n -- ; } i ++ ; } return p ; } public static int findNextPrime ( int n ) { int nextPrime = n + 2 ; while ( true ) { if ( isPrime ( nextPrime ) == true ) break ; nextPrime ++ ; } return nextPrime ; } public static int fortunateNumber ( int n ) { int p = primorial ( n ) ; return findNextPrime ( p ) - p ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( fortunateNumber ( n ) ) ; } }
|
Probability for three randomly chosen numbers to be in AP | C # program to find probability that 3 randomly chosen numbers form AP . ; function to calculate probability ; Driver code
|
using System ; class GFG { static double procal ( int n ) { return ( 3.0 * n ) / ( 4.0 * ( n * n ) - 1 ) ; } public static void Main ( ) { int [ ] a = { 1 , 2 , 3 , 4 , 5 } ; int n = a . Length ; Console . Write ( Math . Round ( procal ( n ) * 1000000.0 ) / 1000000.0 ) ; } }
|
Fermat 's Last Theorem | C # program to verify fermat 's last theorem for a given range and n. ; Check if there exists a triplet such that a ^ n + b ^ n = c ^ n ; Driver code
|
using System ; class GFG { static void testSomeNumbers ( int limit , int n ) { if ( n < 3 ) return ; for ( int a = 1 ; a <= limit ; a ++ ) for ( int b = a ; b <= limit ; b ++ ) { int pow_sum = ( int ) ( Math . Pow ( a , n ) + Math . Pow ( b , n ) ) ; double c = Math . Pow ( pow_sum , 1.0 / n ) ; int c_pow = ( int ) Math . Pow ( ( int ) c , n ) ; if ( c_pow == pow_sum ) { Console . WriteLine ( " Count β example β found " ) ; return ; } } Console . WriteLine ( " No β counter β example β within " + " β given β range β and β data " ) ; } public static void Main ( ) { testSomeNumbers ( 12 , 3 ) ; } }
|
Product of given N fractions in reduced form | C # program to find product of N fractions in reduced form . ; Function to return gcd of a and b ; Print the Product of N fraction in Reduced Form . ; finding the product of all N numerators and denominators . ; Finding GCD of new numerator and denominator ; Converting into reduced form . ; Driven Program
|
using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void productReduce ( int n , int [ ] num , int [ ] den ) { int new_num = 1 , new_den = 1 ; for ( int i = 0 ; i < n ; i ++ ) { new_num *= num [ i ] ; new_den *= den [ i ] ; } int GCD = gcd ( new_num , new_den ) ; new_num /= GCD ; new_den /= GCD ; Console . WriteLine ( new_num + " / " + new_den ) ; } public static void Main ( ) { int n = 3 ; int [ ] num = { 1 , 2 , 5 } ; int [ ] den = { 2 , 1 , 6 } ; productReduce ( n , num , den ) ; } }
|
Find value of ( n ^ 1 + n ^ 2 + n ^ 3 + n ^ 4 ) mod 5 for given n | Code for finding the value of f ( n ) mod 5 for given n . ; function for f ( n ) mod 5 ; if n % 5 == 1 return 4 ; else return 0 ; Driver program
|
using System ; class GFG { static int fnMod ( int n ) { if ( n % 5 == 1 ) return 4 ; else return 0 ; } public static void Main ( ) { int n = 10 ; Console . WriteLine ( fnMod ( n ) ) ; n = 11 ; Console . WriteLine ( fnMod ( n ) ) ; } }
|
Recursive sum of digits of a number formed by repeated appends | C # program to find Sum of digits of a number formed by repeating a number X number of times until sum becomes single digit . ; return single digit sum of a number . ; Returns recursive sum of digits of a number formed by repeating a number X number of times until sum become single digit . ; driver program
|
using System ; public class GFG { static int digSum ( int n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } static int repeatedNumberSum ( int n , int x ) { int sum = x * digSum ( n ) ; return digSum ( sum ) ; } public static void Main ( ) { int n = 24 , x = 3 ; Console . Write ( repeatedNumberSum ( n , x ) ) ; } }
|
Sum of n digit numbers divisible by a given number | Simple C # program to sum of n digit divisible numbers . ; Returns sum of n digit numbers divisible by ' number ' ; compute the first and last term ; sum of number which having n digit and divisible by number ; Driver code
|
using System ; class GFG { static int totalSumDivisibleByNum ( int n , int number ) { int firstnum = ( int ) Math . Pow ( 10 , n - 1 ) ; int lastnum = ( int ) Math . Pow ( 10 , n ) ; int sum = 0 ; for ( int i = firstnum ; i < lastnum ; i ++ ) if ( i % number == 0 ) sum += i ; return sum ; } public static void Main ( ) { int n = 3 , num = 7 ; Console . WriteLine ( totalSumDivisibleByNum ( n , num ) ) ; } }
|
Extended Midy 's theorem | C # program to demonstrate extended Midy 's theorem ; Returns repeating sequence of a fraction . If repeating sequence doesn 't exits, then returns -1 ; Create a map to store already seen remainders remainder is used as key and its position in result is stored as value . Note that we need position for cases like 1 / 6. In this case , the recurring sequence doesn 't start from first remainder. ; Find first remainder ; Keep finding remainder until either remainder becomes 0 or repeats ; Store this remainder ; Multiply remainder with 10 ; Append rem / denr to result ; Update remainder ; Checks whether a number is prime or not ; If all conditions are met , it proves Extended Midy 's theorem ; Dividing repeated part into m parts ; Computing sum of parts . ; Checking for Extended Midy ; Driver code
|
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static string fractionToDecimal ( int numerator , int denominator ) { string res = " " ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int rem = numerator % denominator ; while ( ( rem != 0 ) && ! mp . ContainsKey ( rem ) ) { mp [ rem ] = res . Length ; rem = rem * 10 ; int res_part = rem / denominator ; res += res_part + " " ; rem = rem % denominator ; } return ( rem == 0 ) ? " - 1" : res . Substring ( mp [ rem ] ) ; } static bool isPrime ( int n ) { for ( int i = 2 ; i <= n / 2 ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static void ExtendedMidys ( string str , int n , int m ) { if ( ! isPrime ( n ) ) { Console . Write ( " Denominator β is β not β prime , β " + " thus β Extended β Midy ' s β theorem β " + " is β not β applicable " ) ; return ; } int l = str . Length ; if ( l % 2 == 0 && l % m == 0 ) { int [ ] part = new int [ m ] ; int sum = 0 , res = 0 ; for ( int i = 0 ; i < l ; i ++ ) { int var = i / m ; part [ var ] = part [ var ] * 10 + ( str [ i ] - '0' ) ; } for ( int i = 0 ; i < m ; i ++ ) { sum = sum + part [ i ] ; Console . Write ( part [ i ] + " β " ) ; } Console . WriteLine ( ) ; res = ( int ) Math . Pow ( 10 , m ) - 1 ; if ( sum % res == 0 ) Console . Write ( " Extended β Midy ' s β " + " theorem β holds ! " ) ; else Console . Write ( " Extended β Midy ' s β " + " theorem β doesn ' t β hold ! " ) ; } else if ( l % 2 != 0 ) { Console . Write ( " The β repeating β decimal β is β of β " + " odd β length β thus β Extended β Midy ' s β " + " theorem β is β not β applicable " ) ; } else if ( l % m != 0 ) { Console . Write ( " The β repeating β decimal β can β " + " not β be β divided β into β m β digits " ) ; } } public static void Main ( string [ ] args ) { int numr = 1 , denr = 17 , m = 4 ; string res = fractionToDecimal ( numr , denr ) ; if ( res == " - 1" ) Console . Write ( " The β fraction β does β not β " + " have β repeating β decimal " ) ; else { Console . WriteLine ( " Repeating β decimal β = β " + res ) ; ExtendedMidys ( res , denr , m ) ; } } }
|
Count n digit numbers divisible by given number | Simple C # program to count n digit divisible numbers . ; Returns count of n digit numbers divisible by ' number ' ; compute the first and last term ; count total number of which having n digit and divisible by number ; Driver code
|
using System ; class GFG { static int numberofterm ( int n , int number ) { int firstnum = ( int ) Math . Pow ( 10 , n - 1 ) ; int lastnum = ( int ) Math . Pow ( 10 , n ) ; int count = 0 ; for ( int i = firstnum ; i < lastnum ; i ++ ) if ( i % number == 0 ) count ++ ; return count ; } public static void Main ( ) { int n = 3 , num = 7 ; Console . Write ( numberofterm ( n , num ) ) ; } }
|
N | C # program to find N - th term in George Cantor set of rational number ; let i = numerator ; let j = denominator ; to keep the check of no . of terms ; loop till k is not equal to n ; check if k is already equal to N then the first term is the required rational number ; loop for traversing from right to left downwards diagonally ; loop for traversing from left to right upwards diagonally ; Driver code
|
using System ; class GFG { static void georgeCantor ( int n ) { int i = 1 ; int j = 1 ; int k = 1 ; while ( k < n ) { j ++ ; k ++ ; if ( k == n ) break ; while ( j > 1 && k < n ) { i ++ ; j -- ; k ++ ; } if ( k == n ) break ; i ++ ; k ++ ; if ( k == n ) break ; while ( i > 1 && k < n ) { i -- ; j ++ ; k ++ ; } } Console . WriteLine ( " N - th β term β : β " + i + " / " + j ) ; } public static void Main ( ) { int n = 15 ; georgeCantor ( n ) ; } }
|
Number is divisible by 29 or not | C # program to demonstrate above method to check divisibility by 29. ; Returns true if n is divisible by 29 else returns false . ; add the lastdigit * 3 to renaming number until number comes only 2 digit ; return true if number is divisible by 29 another ; Driver code
|
using System ; class GFG { static bool isDivisible ( long n ) { while ( n / 100 > 0 ) { int last_digit = ( int ) n % 10 ; n /= 10 ; n += last_digit * 3 ; } return ( n % 29 == 0 ) ; } public static void Main ( ) { long n = 348 ; if ( isDivisible ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
|
Solve the Linear Equation of Single Variable | C # program to solve the given equation ; Function to solve the given equation ; Traverse the equation ; For cases such as : x , - x , + x ; Flip sign once ' = ' is seen ; There may be a number left in the end ; For infinite solutions ; For no solution ; x = total sum / coeff of x ' - ' sign indicates moving numeric value to right hand side ; Driver code
|
using System ; class GFG { static string solveEquation ( string equation ) { int n = equation . Length , sign = 1 , coeff = 0 ; int total = 0 , i = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( equation [ j ] == ' + ' equation [ j ] == ' - ' ) { if ( j > i ) total += sign * Int32 . Parse ( equation . Substring ( i , j - i ) ) ; i = j ; } else if ( equation [ j ] == ' x ' ) { if ( ( i == j ) equation [ j - 1 ] == ' + ' ) coeff += sign ; else if ( equation [ j - 1 ] == ' - ' ) coeff -= sign ; else coeff += sign * Int32 . Parse ( equation . Substring ( i , j - i ) ) ; i = j + 1 ; } else if ( equation [ j ] == ' = ' ) { if ( j > i ) total += sign * Int32 . Parse ( equation . Substring ( i , j - i ) ) ; sign = - 1 ; i = j + 1 ; } } if ( i < n ) total += sign * Int32 . Parse ( equation . Substring ( i ) ) ; if ( coeff == 0 && total == 0 ) return " Infinite β solutions " ; if ( coeff == 0 && total != 0 ) return " No β solution " ; int ans = - total / coeff ; return " x β = β " + ans . ToString ( ) ; } static void Main ( ) { string equation = " x + 5-3 + x = 6 + x - 2" ; Console . Write ( solveEquation ( equation ) ) ; } }
|
Check if a given number is Pronic | Efficient Approach | C # program to check if a number is pronic or not ; Function to check Pronic Number ; Checking Pronic Number by multiplying consecutive numbers ; Driver Code
|
using System ; class GFG { static bool pronic_check ( int n ) { int x = ( int ) ( Math . Sqrt ( n ) ) ; if ( x * ( x + 1 ) == n ) return true ; else return false ; } public static void Main ( ) { int n = 56 ; if ( pronic_check ( n ) == true ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } }
|
Check if given number is perfect square | C # program for the above approach ; If ceil and floor are equal the number is a perfect square ; Driver Code
|
using System ; class GFG { static void checkperfectsquare ( int n ) { if ( Math . Ceiling ( ( double ) Math . Sqrt ( n ) ) == Math . Floor ( ( double ) Math . Sqrt ( n ) ) ) { Console . Write ( " perfect β square " ) ; } else { Console . Write ( " not β a β perfect β square " ) ; } } public static void Main ( ) { int n = 49 ; checkperfectsquare ( n ) ; } }
|
Writing power function for large numbers | C # program to compute factorial of big numbers ; Maximum number of digits in output ; This function multiplies x with the number represented by res [ ] . res_size is size of res [ ] or number of digits in the number represented by res [ ] . This function uses simple school mathematics for multiplication . This function may value of res_size and returns the new value of res_size ; Initialize carry ; One by one multiply n with individual digits of res [ ] ; Store last digit of ' prod ' in res [ ] ; Put rest in carry ; Put carry in res and increase result size ; This function finds power of a number x ; printing value "1" for power = 0 ; Initialize result ; Multiply x n times ( x ^ n = x * x * x ... . n times ) ; Driver code
|
using System ; class GFG { static int MAX = 100000 ; static int multiply ( int x , int [ ] res , int res_size ) { int carry = 0 ; for ( int i = 0 ; i < res_size ; i ++ ) { int prod = res [ i ] * x + carry ; res [ i ] = prod % 10 ; carry = prod / 10 ; } while ( carry > 0 ) { res [ res_size ] = carry % 10 ; carry = carry / 10 ; res_size ++ ; } return res_size ; } static void power ( int x , int n ) { if ( n == 0 ) { Console . Write ( "1" ) ; return ; } int [ ] res = new int [ MAX ] ; int res_size = 0 ; int temp = x ; while ( temp != 0 ) { res [ res_size ++ ] = temp % 10 ; temp = temp / 10 ; } for ( int i = 2 ; i <= n ; i ++ ) res_size = multiply ( x , res , res_size ) ; Console . Write ( x + " ^ " + n + " β = β " ) ; for ( int i = res_size - 1 ; i >= 0 ; i -- ) Console . Write ( res [ i ] ) ; } public static void Main ( ) { int exponent = 100 ; int b_ase = 2 ; power ( b_ase , exponent ) ; } }
|
P | C # program to check if a number is a p - smooth number or not ; function to check if number n is a P - smooth number or not ; prime factorise it by 2 ; if the number is divisible by 2 ; check for all the possible numbers that can divide it ; prime factorize it by i ; stores the maximum if maximum and i , if i divides the number ; if n at the end is a prime number , then it a divisor itself ; Driver program to test above function
|
using System ; class GFG { static bool check ( int n , int p ) { int maximum = - 1 ; while ( ( n % 2 ) == 0 ) { maximum = Math . Max ( maximum , 2 ) ; n = n / 2 ; } for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { maximum = Math . Max ( maximum , i ) ; n = n / i ; } } if ( n > 2 ) maximum = Math . Max ( maximum , n ) ; return ( maximum <= p ) ; } public static void Main ( ) { int n = 24 , p = 7 ; if ( check ( n , p ) ) Console . Write ( " yes " ) ; else Console . Write ( " no " ) ; } }
|
Program to Change RGB color model to HSV color model | C # program change RGB Color Model to HSV Color Model ; R , G , B values are divided by 255 to change the range from 0. .255 to 0. .1 ; h , s , v = hue , saturation , value double cmax = Math . Max ( r , Math . Max ( g , b ) ) ; maximum of r , g , b double cmin = Math . Min ( r , Math . Min ( g , b ) ) ; minimum of r , g , b double diff = cmax - cmin ; diff of cmax and cmin . ; if cmax and cmax are equal then h = 0 ; if cmax equal r then compute h ; if cmax equal g then compute h ; if cmax equal b then compute h ; if cmax equal zero ; compute v ; Driver Code ; rgb_to_hsv ( 45 , 215 , 0 ) ; rgb_to_hsv ( 31 , 52 , 29 ) ;
|
using System ; class GFG { static void rgb_to_hsv ( double r , double g , double b ) { r = r / 255.0 ; g = g / 255.0 ; b = b / 255.0 ; double h = - 1 , s = - 1 ; if ( cmax == cmin ) h = 0 ; else if ( cmax == r ) h = ( 60 * ( ( g - b ) / diff ) + 360 ) % 360 ; else if ( cmax == g ) h = ( 60 * ( ( b - r ) / diff ) + 120 ) % 360 ; else if ( cmax == b ) h = ( 60 * ( ( r - g ) / diff ) + 240 ) % 360 ; if ( cmax == 0 ) s = 0 ; else s = ( diff / cmax ) * 100 ; double v = cmax * 100 ; Console . WriteLine ( " ( " + h + " β " + s + " β " + v + " ) " ) ; } public static void Main ( String [ ] args ) { rgb_to_hsv ( 129 , 88 , 47 ) ; } }
|
Time when minute hand and hour hand coincide | C # code to find the minute at which the minute hand and hour hand coincide ; function to find the minute ; finding the angle between minute hand and the first hour hand ; Driver Code
|
using System ; class GFG { static void find_time ( int h1 ) { int theta = 30 * h1 ; Console . WriteLine ( " ( " + theta * 2 + " / " + " β 11 β ) minutes " ) ; } public static void Main ( ) { int h1 = 3 ; find_time ( h1 ) ; } }
|
Sum of Series ( n ^ 2 | C # program to finding the sum of the nth series ; Function that calculate the sum of the nth series ; Using formula of the nth term ; Driver Code
|
using System ; class GFG { static int sum_series ( int n ) { int nSquare = n * n ; return nSquare * ( nSquare - 1 ) / 4 ; } public static void Main ( ) { int n = 2 ; Console . Write ( sum_series ( n ) ) ; } }
|
Check if a number is sandwiched between primes | C # Program to check whether a number is sandwiched between two primes or not ; returns true if number n is prime ; 0 and 1 both are non - primes ; finding square root of n ; checking if n has any factor upto square root of n if yes its not prime ; Driver Code
|
using System ; class GFG { static bool isPrime ( int n ) { if ( n == 0 n == 1 ) return false ; int root = ( int ) Math . Sqrt ( n ) ; for ( int i = 2 ; i <= root ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static bool isSandwitched ( int n ) { return ( isPrime ( n - 1 ) && isPrime ( n + 1 ) ) ; } public static void Main ( ) { int n = 642 ; Console . Write ( n + " β : β " ) ; if ( isSandwitched ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . Write ( " No " ) ; n = 9 ; Console . Write ( n + " β : β " ) ; if ( isSandwitched ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
|
Tomohiko Sakamoto 's Algorithm | A C # program to implement the Tomohiko Sakamoto Algorithm ; function to implement tomohiko sakamoto algorithm ; array with leading number of days values ; if month is less than 3 reduce year by 1 ; Driver Code
|
using System ; class GFG { public static int day_of_the_week ( int y , int m , int d ) { int [ ] t = { 0 , 3 , 2 , 5 , 0 , 3 , 5 , 1 , 4 , 6 , 2 , 4 } ; if ( m < 3 ) y -= 1 ; return ( y + y / 4 - y / 100 + y / 400 + t [ m - 1 ] + d ) % 7 ; } public static void Main ( ) { int day = 13 , month = 7 , year = 2017 ; Console . WriteLine ( day_of_the_week ( year , month , day ) ) ; } }
|
Recursive program for prime number | C # Program to find whether a Number is Prime or Not using Recursion ; Returns true if n is prime , else return false . i is current divisor to check . ; Base cases ; Check for next divisor ; Driver code
|
using System ; class GFG { static bool isPrime ( int n , int i ) { if ( n <= 2 ) return ( n == 2 ) ? true : false ; if ( n % i == 0 ) return false ; if ( i * i > n ) return true ; return isPrime ( n , i + 1 ) ; } static void Main ( ) { int n = 15 ; if ( isPrime ( n , 2 ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
|
Square Free Number | C # Program to print all prime factors ; Returns true if n is a square free number , else returns false . ; If 2 again divides n , then n is not a square free number . ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; Check if i is a prime factor ; If i again divides , then n is not square free ; Driver program
|
using System ; class GFG { static bool isSquareFree ( int n ) { if ( n % 2 == 0 ) n = n / 2 ; if ( n % 2 == 0 ) return false ; for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { n = n / i ; if ( n % i == 0 ) return false ; } } return true ; } public static void Main ( ) { int n = 10 ; if ( isSquareFree ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
|
Area of a square from diagonal length | C # Program to find the area of square when its diagonal is given . ; Returns area of square from given diagonal ; Driver code
|
using System ; class GFG { static double findArea ( double d ) { return ( d * d ) / 2 ; } public static void Main ( ) { double d = 10 ; Console . WriteLine ( findArea ( d ) ) ; } }
|
Sum of series 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + . . . + ( 2 * n | C # Program to find sum of series 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + . . . + ( 2 * n - 1 ) ^ 2. ; Function to find sum of series . ; Driver code
|
using System ; class GFG { static int sumOfSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum = sum + ( 2 * i - 1 ) * ( 2 * i - 1 ) ; return sum ; } public static void Main ( ) { int n = 10 ; Console . Write ( sumOfSeries ( n ) ) ; } }
|
Sum of series 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + . . . + ( 2 * n | C # Program to find sum of series 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + . . . + ( 2 * n - 1 ) ^ 2. ; Function to find sum of series . ; Formula to find sum of series . ; Driver function
|
using System ; class GFG { static int sumOfSeries ( int n ) { return ( n * ( 2 * n - 1 ) * ( 2 * n + 1 ) ) / 3 ; } public static void Main ( ) { int n = 10 ; Console . Write ( sumOfSeries ( n ) ) ; } }
|
Program to implement standard error of mean | C # Program to implement standard error of mean . ; Function to find sample mean . ; loop to calculate sum of array elements . ; Function to calculate sample standard deviation . ; Function to calculate sample error . ; Formula to find sample error . ; Driver code
|
using System ; class GFG { static float mean ( float [ ] arr , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + arr [ i ] ; return sum / n ; } static float SSD ( float [ ] arr , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( arr [ i ] - mean ( arr , n ) ) * ( arr [ i ] - mean ( arr , n ) ) ; return ( float ) Math . Sqrt ( sum / ( n - 1 ) ) ; } static float sampleError ( float [ ] arr , int n ) { return SSD ( arr , n ) / ( float ) Math . Sqrt ( n ) ; } public static void Main ( ) { float [ ] arr = { 78.53f , 79.62f , 80.25f , 81.05f , 83.21f , 83.46f } ; int n = arr . Length ; Console . Write ( sampleError ( arr , n ) ) ; } }
|
Minimum moves to reach target on a infinite line | Set 2 | C # code to find minimum moves to reach target ; Function to find minimum steps to reach target ; Handling negatives by symmetry ; Keep moving while sum is smaller i . e calculating n ; case 1 : d is even ; d is odd ; Driver code ; Function call
|
using System ; class GFG { static int StepstoReachTarget ( int target ) { target = Math . Abs ( target ) ; int n = ( int ) Math . Ceiling ( ( - 1.0 + ( int ) Math . Sqrt ( 1 + 8.0 * target ) ) / 2 ) ; int sum = n * ( n + 1 ) / 2 ; if ( sum == target ) return n ; int d = sum - target ; if ( ( d & 1 ) == 0 ) return n ; else return n + ( ( n & 1 ) != 0 ? 2 : 1 ) ; } public static void Main ( ) { int target = 5 ; Console . Write ( StepstoReachTarget ( target ) ) ; } }
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.