codeparrot/xlcost-text-to-code huggingface dataset:
Count of numbers whose sum of increasing powers of digits is equal to the number itself | C # implementation of the approach ; Function to return the count of digits of n ; Function to return the sum of increasing powers of N ; To store the required answer ; Count of digits in n which will be the power of the last digit ; While there are digits left ; Get the last digit ; Add the last digit after raising it to the required power ; Decrement the power for the previous digit ; Remove the last digit ; Function to return the count of integers which satisfy the given conditions ; If current element satisfies the given condition ; Driver code
using System ; class GFG { static int countDigits ( int n ) { int cnt = 0 ; while ( n > 0 ) { cnt ++ ; n /= 10 ; } return cnt ; } static int digitPowSum ( int n ) { int sum = 0 ; int pw = countDigits ( n ) ; while ( n > 0 ) { int d = n % 10 ; sum += ( int ) Math . Pow ( d , pw ) ; pw -- ; n /= 10 ; } return sum ; } static int countNum ( int n ) { int count = 0 ; for ( int i = 0 ; i <= n ; i ++ ) { if ( i == digitPowSum ( i ) ) count ++ ; } return count ; } public static void Main ( String [ ] args ) { int n = 200 ; Console . WriteLine ( countNum ( n ) ) ; } }
Print all perfect squares from the given range | C # implementation of the approach ; Function to print all the perfect squares from the given range ; For every element from the range ; If current element is a perfect square ; Driver code
using System ; class GFG { static void perfectSquares ( int l , int r ) { for ( int i = l ; i <= r ; i ++ ) { if ( Math . Sqrt ( i ) == ( int ) Math . Sqrt ( i ) ) Console . Write ( i + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int l = 2 , r = 24 ; perfectSquares ( l , r ) ; } }
Print all perfect squares from the given range | C # implementation of the approach ; Function to print all the perfect squares from the given range ; Getting the very first number ; First number 's square ; Next number is at the difference of ; While the perfect squares are from the range ; Print the perfect square ; Get the next perfect square ; Next odd number to be added ; Driver code
using System ; class GFG { static void perfectSquares ( float l , float r ) { int number = ( int ) Math . Ceiling ( Math . Sqrt ( l ) ) ; int n2 = number * number ; number = ( number * 2 ) + 1 ; while ( ( n2 >= l && n2 <= r ) ) { Console . Write ( n2 + " ▁ " ) ; n2 = n2 + number ; number += 2 ; } } public static void Main ( String [ ] args ) { int l = 2 , r = 24 ; perfectSquares ( l , r ) ; } }
Find the value of N XOR 'ed to itself K times | C # implementation of the approach ; Function to return n ^ n ^ ... k times ; Find the result ; Driver code
using System ; class GFG { static int xorK ( int n , int k ) { int res = n ; for ( int i = 1 ; i < k ; i ++ ) res = ( res ^ n ) ; return n ; } static public void Main ( ) { int n = 123 , k = 3 ; Console . Write ( xorK ( n , k ) ) ; } }
Find all the possible remainders when N is divided by all positive integers from 1 to N + 1 | C # implementation of the approach ; Function to find all the distinct remainders when n is divided by all the elements from the range [ 1 , n + 1 ] ; Set will be used to store the remainders in order to eliminate duplicates ; Find the remainders ; Print the contents of the set ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void findRemainders ( long n ) { List < long > vc = new List < long > ( ) ; for ( long i = 1 ; i <= Math . Ceiling ( Math . Sqrt ( n ) ) ; i ++ ) vc . Add ( n / i ) ; for ( long i = ( long ) ( n / Math . Ceiling ( Math . Sqrt ( n ) ) - 1 ) ; i >= 0 ; i -- ) vc . Add ( i ) ; vc . Reverse ( ) ; foreach ( long it in vc ) Console . Write ( it + " ▁ " ) ; } public static void Main ( String [ ] args ) { long n = 5 ; findRemainders ( n ) ; } }
Count of primes below N which can be expressed as the sum of two primes | C # implementation of the approach ; Function for Sieve of Eratosthenes ; false here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to return the count of primes less than or equal to n which can be expressed as the sum of two primes ; To store the required count ; If the integer is prime and it can be expressed as the sum of 2 and a prime number ; Driver code
using System ; class GFG { static int MAX = 100005 ; static bool [ ] prime = new bool [ MAX ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < MAX ; i ++ ) prime [ i ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i < MAX ; i += p ) prime [ i ] = false ; } } } static int countPrimes ( int n ) { SieveOfEratosthenes ( ) ; int cnt = 0 ; for ( int i = 2 ; i < n ; i ++ ) { if ( prime [ i ] && prime [ i - 2 ] ) cnt ++ ; } return cnt ; } public static void Main ( String [ ] args ) { int n = 11 ; Console . Write ( countPrimes ( n ) ) ; } }
Print all palindrome dates between the given years | C # implementation of the approach ; Returns true if given year is valid ; Return true if year is a multiple pf 4 and not multiple of 100. OR year is multiple of 400. ; Returns true if given year is valid or not . ; If year , month and day are not in given range ; Handle February month with leap year ; Months of April , June , Sept and Nov must have number of days less than or equal to 30. ; Function to print the palindrome dates between the given years ; For every year ; Current year as a String ; To store the reverse of year ; Get the day and the month ; If the current palindrome date is valid ; Driver code
using System ; class GFG { static int MAX_VALID_YR = 9999 ; static int MIN_VALID_YR = 1800 ; static bool isLeap ( int year ) { return ( ( ( year % 4 == 0 ) && ( year % 100 != 0 ) ) || ( year % 400 == 0 ) ) ; } static bool isValidDate ( int d , int m , int y ) { if ( y > MAX_VALID_YR y < MIN_VALID_YR ) return false ; if ( m < 1 m > 12 ) return false ; if ( d < 1 d > 31 ) return false ; if ( m == 2 ) { if ( isLeap ( y ) ) return ( d <= 29 ) ; else return ( d <= 28 ) ; } if ( m == 4 m == 6 m == 9 m == 11 ) return ( d <= 30 ) ; return true ; } static void printPalindromeDates ( int y1 , int y2 ) { for ( int year = y1 ; year <= y2 ; year ++ ) { String str = String . Join ( " " , year ) ; String rev = str ; rev = reverse ( rev ) ; int day = Int32 . Parse ( rev . Substring ( 0 , 2 ) ) ; int month = Int32 . Parse ( rev . Substring ( 2 , 2 ) ) ; if ( isValidDate ( day , month , year ) ) { Console . Write ( rev + str + " STRNEWLINE " ) ; } } } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { int y1 = 2001 , y2 = 2005 ; printPalindromeDates ( y1 , y2 ) ; } }
Find an integer in the given range that satisfies the given conditions | C # implementation of the approach ; Function that returns true if x contains all distinct digits ; Last digit of x ; If current digit has appeared before ; Mark the current digit to present ; Remove the last digit ; Function to return the required value of k ; To store the maximum value for the given expression ; If i contains all distinct digits ; If the value of the expression is also maximum then update k and the expression ; Driver code
using System ; class GFG { static int MAX = 10 ; static bool distinctDigits ( int x ) { bool [ ] present = new bool [ MAX ] ; while ( x > 0 ) { int digit = x % 10 ; if ( present [ digit ] ) return false ; present [ digit ] = true ; x /= 10 ; } return true ; } static int findK ( int l , int r ) { int maxExp = int . MinValue ; int k = - 1 ; for ( int i = l ; i <= r ; i ++ ) { if ( distinctDigits ( i ) ) { int exp = ( l - i ) * ( i - r ) ; if ( exp >= maxExp ) { k = i ; maxExp = exp ; } } } return k ; } public static void Main ( String [ ] args ) { int l = 50 , r = 60 ; Console . Write ( findK ( l , r ) ) ; } }
Restore a permutation from the given helper array | C # implementation of the approach ; Function to find the required permutation ; Each element in P ' is like a  cumulative sum in Q ; minval is the minimum value in P ' ; To check if each entry in P is from the range [ 1 , n ] ; Invalid permutation ; If a valid permutation exists ; Print the permutation ; No valid permutation ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void findPerm ( int [ ] Q , int n ) { int minval = 0 , qsum = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { qsum += Q [ i ] ; if ( qsum < minval ) minval = qsum ; } int [ ] P = new int [ n ] ; P [ 0 ] = 1 - minval ; bool permFound = true ; for ( int i = 0 ; i < n - 1 ; i ++ ) { P [ i + 1 ] = P [ i ] + Q [ i ] ; if ( P [ i + 1 ] > n P [ i + 1 ] < 1 ) { permFound = false ; break ; } } if ( permFound ) { for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( P [ i ] + " ▁ " ) ; } } else { Console . Write ( - 1 ) ; } } public static void Main ( String [ ] args ) { int [ ] Q = { - 2 , 1 } ; int n = 1 + Q . Length ; findPerm ( Q , n ) ; } }
Find the player who will win the Coin game | C # program to find the player who wins the game ; Function to check the wining player ; As discussed in the above approach ; Driver Code
using System ; class GFG { static void findWinner ( int n ) { if ( ( n - 1 ) % 6 == 0 ) { Console . WriteLine ( " Second ▁ Player ▁ wins ▁ the ▁ game " ) ; } else { Console . WriteLine ( " First ▁ Player ▁ wins ▁ the ▁ game " ) ; } } public static void Main ( ) { int n = 7 ; findWinner ( n ) ; } }
Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls | C # implementation of the approach ; To store the factorial and the factorial mod inverse of a number ; Function to find ( a ^ m1 ) % mod ; Function to find factorial of all the numbers ; Function to find the factorial mod inverse of all the numbers ; Function to return nCr ; Function to find ways to arrange K green balls among N balls such that we need exactly i moves to collect all K green balls ; Driver code ; Function call
using System ; class GFG { static readonly int N = 100005 ; static readonly int mod = ( int ) ( 1e9 + 7 ) ; static long [ ] factorial = new long [ N ] ; static long [ ] modinverse = new long [ N ] ; static long power ( long a , int m1 ) { if ( m1 == 0 ) return 1 ; else if ( m1 == 1 ) return a ; else if ( m1 == 2 ) return ( 1L * a * a ) % mod ; else if ( m1 % 2 == 1 ) return ( 1L * a * power ( power ( a , m1 / 2 ) , 2 ) ) % mod ; else return power ( power ( a , m1 / 2 ) , 2 ) % mod ; } static void factorialfun ( ) { factorial [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) factorial [ i ] = ( 1L * factorial [ i - 1 ] * i ) % mod ; } static void modinversefun ( ) { modinverse [ N - 1 ] = ( int ) ( power ( factorial [ N - 1 ] , mod - 2 ) % mod ) ; for ( int i = N - 2 ; i >= 0 ; i -- ) modinverse [ i ] = ( 1 * modinverse [ i + 1 ] * ( i + 1 ) ) % mod ; } static long binomial ( int n , int r ) { if ( r > n ) return 0 ; long a = ( 1L * factorial [ n ] * modinverse [ n - r ] ) % mod ; a = ( 1 * a * modinverse [ r ] ) % mod ; return a ; } static void arrange_balls ( int n , int k ) { factorialfun ( ) ; modinversefun ( ) ; for ( int i = 1 ; i <= k ; i ++ ) Console . Write ( ( 1L * binomial ( n - k + 1 , i ) * binomial ( k - 1 , i - 1 ) ) % mod + " ▁ " ) ; } public static void Main ( String [ ] args ) { int n = 5 , k = 3 ; arrange_balls ( n , k ) ; } }
Minimum inversions required so that no two adjacent elements are same | C # implementation of the approach ; Function to return the minimum inversions required so that no two adjacent elements are same ; To store the inversions required to make the array { 1 , 0 , 1 , 0 , 1 , 0 , 1 , ... } and { 0 , 1 , 0 , 1 , 0 , 1 , 0 , ... } respectively ; Find all the changes required ; Return the required answer ; Driver code
using System ; class GFG { static int min_changes ( int [ ] a , int n ) { int ans_a = 0 , ans_b = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { if ( a [ i ] == 0 ) ans_a ++ ; else ans_b ++ ; } else { if ( a [ i ] == 0 ) ans_b ++ ; else ans_a ++ ; } } return Math . Min ( ans_a , ans_b ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 } ; int n = a . Length ; Console . WriteLine ( min_changes ( a , n ) ) ; } }
Sum of all the numbers present at given level in Modified Pascal ’ s triangle | C # program to calculate sum of all the numbers present at given level in an Modified Pascal 's triangle ; Function to calculate sum ; Driver Code
using System ; class GFG { static void ans ( int n ) { if ( n == 1 ) Console . WriteLine ( "1" ) ; else Console . WriteLine ( "0" ) ; } public static void Main ( String [ ] args ) { int n = 2 ; ans ( n ) ; } }
Find if there exists multiple ways to draw line through ( x , y ) to cut rectangle in equal halfs | C # implementation of the approach ; Function that returns true if multiple lines are possible passing through ( x , y ) that divide the given rectangle into two equal parts ; If the point ( x , y ) is the centre of the rectangle ; Driver code
using System ; class GFG { static bool isPossible ( int w , int h , int x , int y ) { if ( x * 2 == w && y * 2 == h ) return true ; return false ; } static public void Main ( ) { int w = 1 , h = 2 , x = 1 , y = 2 ; if ( isPossible ( w , h , x , y ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Minimize the number by changing at most K digits | C # implementation of the approach ; Function to return the minimized number ; Total digits in the number ; If the string is empty or there are no operations to perform ; "0" is a valid number ; If the first digit is not already 1 then update it to 1 and decrement k ; While there are operations left and the number can still be updated ; If the current digit is not already 0 then update it to 0 and decrement k ; Return the minimised number ; Driver code
using System ; class GFG { static String minNum ( char [ ] num , int k ) { int len = num . Length ; if ( len == 0 k == 0 ) { return String . Join ( " " , num ) ; } if ( len == 1 ) return "0" ; if ( num [ 0 ] != '1' ) { num [ 0 ] = '1' ; k -- ; } int i = 1 ; while ( k > 0 && i < len ) { if ( num [ i ] != '0' ) { num [ i ] = '0' ; k -- ; } i ++ ; } return String . Join ( " " , num ) ; } public static void Main ( String [ ] args ) { String num = "91945" ; int k = 3 ; Console . WriteLine ( minNum ( num . ToCharArray ( ) , k ) ) ; } }
Form N by adding 1 or 2 in minimum number of operations X where X is divisible by M | C # program to find minimum number of steps to cover distance x ; Function to calculate the minimum number of steps required total steps taken is divisible by m and only 1 or 2 steps can be taken at a time ; If m > n ans is - 1 ; else discussed above approach ; Driver code
using System ; class GFG { static int minsteps ( int n , int m ) { if ( m > n ) { return - 1 ; } else { return ( ( n + 1 ) / 2 + m - 1 ) / m * m ; } } public static void Main ( String [ ] args ) { int n = 17 , m = 4 ; int ans = minsteps ( n , m ) ; Console . WriteLine ( ans ) ; } }
Compare numbers represented by Linked Lists | C # implementation of the approach ; Structure for a linked list node ; A helper function to remove zeros from the start of the linked list ; A helper function to find the length of linked list ; Given a reference ( pointer to pointer ) to the head of a list and an int , push a new node on the front of the list . ; Allocate node ; Set the data ; Link the old list after the new node ; Set the head to point to the new node ; Function to compare the numbers represented as linked lists ; Remover leading zeroes from the linked lists ; Since the number represented by a has a greater length , it will be greater ; If the lengths of two numbers are equal we have to check their magnitudes ; If we reach here , then a and b are not null and their data is same , so move to next nodes in both lists ; If linked lists are identical , then we need to return zero ; Driver code ; The constructed linked lists are : a : 5 -> 6 -> 7 b : 2 -> 3 -> 3
using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node next ; } ; static Node removeLeadingZeros ( Node a ) { if ( a != null && a . data == 0 ) return removeLeadingZeros ( a . next ) ; else return a ; } static int getSize ( Node a ) { int sz = 0 ; while ( a != null ) { a = a . next ; sz ++ ; } return sz ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static int compare ( Node a , Node b ) { a = removeLeadingZeros ( a ) ; b = removeLeadingZeros ( b ) ; int lenA = getSize ( a ) ; int lenB = getSize ( b ) ; if ( lenA > lenB ) { return 1 ; } else if ( lenB > lenA ) { return - 1 ; } while ( a != null && b != null ) { if ( a . data > b . data ) return 1 ; else if ( a . data < b . data ) return - 1 ; a = a . next ; b = b . next ; } return 0 ; } public static void Main ( String [ ] args ) { Node a = null ; a = push ( a , 7 ) ; a = push ( a , 6 ) ; a = push ( a , 5 ) ; Node b = null ; b = push ( b , 3 ) ; b = push ( b , 3 ) ; b = push ( b , 2 ) ; Console . WriteLine ( compare ( a , b ) ) ; } }
Sum of the updated array after performing the given operation | C # implementation of the approach ; Utility function to return the sum of the array ; Function to return the sum of the modified array ; Find the sum of the subarray arr [ i + 1. . . n - 1 ] ; Subtract the subarray sum ; Return the sum of the modified array ; Driver code
using System ; class GFG { static int sumArr ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } static int sumModArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { int subSum = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { subSum += arr [ j ] ; } arr [ i ] -= subSum ; } return sumArr ( arr , n ) ; } public static void Main ( ) { int [ ] arr = { 40 , 25 , 12 , 10 } ; int n = arr . Length ; Console . WriteLine ( sumModArr ( arr , n ) ) ; } }
Find the minimum possible health of the winning player | C # implementation of the above approach ; Function to return the minimum possible health of the last player ; Find the GCD of the array elements ; Driver code
using System ; class GFG { static int minHealth ( int [ ] health , int n ) { int gcd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { gcd = __gcd ( gcd , health [ i ] ) ; } return gcd ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int [ ] health = { 5 , 6 , 1 , 2 , 3 , 4 } ; int n = health . Length ; Console . WriteLine ( minHealth ( health , n ) ) ; } }
Construct an array from its pair | C # implementation of the approach ; Utility function to print the array ; Function to generate the original array from the pair - product array ; First element of the resulting array ; Find all the other elements ; Print the elements of the generated array ; Driver code
using System ; class GFG { static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void constructArr ( int [ ] pair , int n ) { int size = ( 1 + ( int ) Math . Sqrt ( 1 + 8 * n ) ) / 2 ; int [ ] arr = new int [ size ] ; arr [ 0 ] = ( int ) Math . Sqrt ( ( pair [ 0 ] * pair [ 1 ] ) / pair [ size - 1 ] ) ; for ( int i = 1 ; i < size ; i ++ ) arr [ i ] = pair [ i - 1 ] / arr [ 0 ] ; printArr ( arr , size ) ; } public static void Main ( String [ ] args ) { int [ ] pair = { 48 , 18 , 24 , 24 , 32 , 12 } ; int n = pair . Length ; constructArr ( pair , n ) ; } }
Count of odd and even sum pairs in an array | C # implementation of the approach ; Function to find the count of pairs with odd sum and the count of pairs with even sum ; To store the count of even and odd number from the array ; If the current element is even ; If it is odd ; To store the count of pairs with even sum ; All the even elements will make pairs with each other and the sum of the pair will be even ; All the odd elements will make pairs with each other and the sum of the pair will be even ; To store the count of pairs with odd sum ; All the even elements will make pairs with all the odd element and the sum of the pair will be odd ; Driver code
using System ; class GFG { static void findPairs ( int [ ] arr , int n ) { int cntEven = 0 , cntOdd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) cntEven ++ ; else cntOdd ++ ; } int evenPairs = 0 ; evenPairs += ( ( cntEven * ( cntEven - 1 ) ) / 2 ) ; evenPairs += ( ( cntOdd * ( cntOdd - 1 ) ) / 2 ) ; int oddPairs = 0 ; oddPairs += ( cntEven * cntOdd ) ; Console . WriteLine ( " Odd ▁ pairs ▁ = ▁ " + oddPairs ) ; Console . WriteLine ( " Even ▁ pairs ▁ = ▁ " + evenPairs ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; findPairs ( arr , n ) ; } }
Number of ways to distribute N Paper Set among M students | C # implementation of the approach ; Function to return n ! % 1000000007 ; To store the factorial ; Find the factorial ; Function to return the count of possible ways ; Driver code
using System ; class GFG { static int MOD = 1000000007 ; static int factMod ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { fact *= ( i % MOD ) ; fact %= MOD ; } return fact ; } static int countWays ( int n , int m ) { return factMod ( m ) ; } public static void Main ( ) { int n = 2 , m = 2 ; Console . Write ( countWays ( n , m ) ) ; } }
Program for nth Fuss – Catalan Number | C # program for nth Fuss - Catalan Number ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth Fuss - Catalan number in O ( n ) time ; Calculate value of 3 nCn ; return 3 nCn / ( 2 n + 1 ) ; Driver code
using System ; class GFG { static int binomialCoeff ( int n , int k ) { int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static int Fuss_catalan ( int n ) { int c = binomialCoeff ( 3 * n , n ) ; return c / ( 2 * n + 1 ) ; } public static void Main ( String [ ] args ) { for ( int i = 0 ; i < 10 ; i ++ ) Console . Write ( Fuss_catalan ( i ) + " ▁ " ) ; } }
Check if a number is Euler Pseudoprime | C # implementation of the approach ; Function that returns true if n is composite ; Check if there is any divisor of n . we only need check divisor till sqrt ( n ) because if there is divisor which is greater than sqrt ( n ) then there must be a divisor which is less than sqrt ( n ) ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is greater than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Function that returns true if N is Euler Pseudoprime to the base A ; Invalid base ; N is not a composite odd number ; If A and N are not coprime ; All the conditions for Euler Pseudoprime are satisfied ; Driver code
using System ; class GFG { static bool isComposite ( int n ) { for ( int i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return true ; } return false ; } static int Power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) { res = ( res * x ) % p ; } x = ( x * x ) % p ; } return res ; } static bool isEulerPseudoprime ( int N , int A ) { if ( A <= 0 ) return false ; if ( N % 2 == 0 || ! isComposite ( N ) ) return false ; if ( __gcd ( A , N ) != 1 ) return false ; int mod = Power ( A , ( N - 1 ) / 2 , N ) ; if ( mod != 1 && mod != N - 1 ) return false ; return true ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int N = 121 , A = 3 ; if ( isEulerPseudoprime ( N , A ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }