codeparrot/xlcost-text-to-code huggingface dataset:
Breaking an Integer to get Maximum Product | C # program to find maximum product by breaking the Integer ; method return x ^ a in log ( a ) time ; Method returns maximum product obtained by breaking N ; base case 2 = 1 + 1 ; base case 3 = 2 + 1 ; breaking based on mod with 3 ; If divides evenly , then break into all 3 ; If division gives mod as 1 , then break as 4 + power of 3 for remaining part ; If division gives mod as 2 , then break as 2 + power of 3 for remaining part ; Driver code to test above methods
class GFG { static int power ( int x , int a ) { int res = 1 ; while ( a > 0 ) { if ( ( a & 1 ) > 0 ) res = res * x ; x = x * x ; a >>= 1 ; } return res ; } static int breakInteger ( int N ) { if ( N == 2 ) return 1 ; if ( N == 3 ) return 2 ; int maxProduct = - 1 ; switch ( N % 3 ) { case 0 : maxProduct = power ( 3 , N / 3 ) ; break ; case 1 : maxProduct = 2 * 2 * power ( 3 , ( N / 3 ) - 1 ) ; break ; case 2 : maxProduct = 2 * power ( 3 , N / 3 ) ; break ; } return maxProduct ; } public static void Main ( ) { int maxProduct = breakInteger ( 10 ) ; System . Console . WriteLine ( maxProduct ) ; } }
Finding sum of digits of a number until sum becomes single digit | C # program to find sum of digits of a number until sum becomes single digit . ; Loop to do sum while sum is not less than or equal to 9 ; Driver code
using System ; class GFG { static int digSum ( int n ) { int sum = 0 ; while ( n > 0 sum > 9 ) { if ( n == 0 ) { n = sum ; sum = 0 ; } sum += n % 10 ; n /= 10 ; } return sum ; } public static void Main ( ) { int n = 1234 ; Console . Write ( digSum ( n ) ) ; } }
Finding sum of digits of a number until sum becomes single digit | Driver Code
using System ; class GFG { static int digSum ( int n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } public static void Main ( ) { int n = 9999 ; Console . Write ( digSum ( n ) ) ; } }
Multiples of 3 or 7 | A better Java program to find count of all numbers that multiples ; Returns count of all numbers smaller than or equal to n and multiples of 3 or 7 or both ; Driver Code
using System ; class GFG { static int countMultiples ( int n ) { return n / 3 + n / 7 - n / 21 ; } static public void Main ( ) { Console . WriteLine ( " Count ▁ = ▁ " + countMultiples ( 25 ) ) ; } }
Find Last Digit of a ^ b for Large Numbers | C # code to find last digit of a ^ b . ; Function to find b % a ; Initialize result ; calculating mod of b with a to make b like 0 <= b < a ; return modulo ; Function to find last digit of a ^ b ; if a and b both are 0 ; if exponent is 0 ; if base is 0 ; if exponent is divisible by 4 that means last digit will be pow ( a , 4 ) % 10. if exponent is not divisible by 4 that means last digit will be pow ( a , b % 4 ) % 10 ; Find last digit in ' a ' and compute its exponent ; Return last digit of result ; Driver program to run test case
using System ; class GFG { static int Modulo ( int a , char [ ] b ) { int mod = 0 ; for ( int i = 0 ; i < b . Length ; i ++ ) mod = ( mod * 10 + b [ i ] - '0' ) % a ; return mod ; } static int LastDigit ( char [ ] a , char [ ] b ) { int len_a = a . Length , len_b = b . Length ; if ( len_a == 1 && len_b == 1 && b [ 0 ] == '0' && a [ 0 ] == '0' ) return 1 ; if ( len_b == 1 && b [ 0 ] == '0' ) return 1 ; if ( len_a == 1 && a [ 0 ] == '0' ) return 0 ; int exp = ( Modulo ( 4 , b ) == 0 ) ? 4 : Modulo ( 4 , b ) ; int res = ( int ) ( Math . Pow ( a [ len_a - 1 ] - '0' , exp ) ) ; return res % 10 ; } public static void Main ( ) { char [ ] a = "117" . ToCharArray ( ) , b = { '3' } ; Console . Write ( LastDigit ( a , b ) ) ; } }
Reverse and Add Function | C # Program to implement reverse and add function ; Iterative function to reverse digits of num ; Function to check whether he number is palindrome or not ; Reverse and Add Function ; Reversing the digits of the number ; Adding the reversed number with the original ; Checking whether the number is palindrome or not ; Driver code
using System ; class GFG { static long reversDigits ( long num ) { long rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } static bool isPalindrome ( long num ) { return ( reversDigits ( num ) == num ) ; } static void ReverseandAdd ( long num ) { long rev_num = 0 ; while ( num <= 4294967295 ) { rev_num = reversDigits ( num ) ; num = num + rev_num ; if ( isPalindrome ( num ) ) { Console . WriteLine ( num ) ; break ; } else if ( num > 4294967295 ) { Console . WriteLine ( " No ▁ palindrome ▁ exist " ) ; } } } public static void Main ( ) { ReverseandAdd ( 195 ) ; ReverseandAdd ( 265 ) ; } }
Stein 's Algorithm for finding GCD | Recursive C # program to implement Stein 's Algorithm ; Function to implement Stein 's Algorithm ; GCD ( 0 , b ) == b ; GCD ( a , 0 ) == a , GCD ( 0 , 0 ) == 0 ; look for factors of 2 a is even ; b is odd ; both a and b are even ; a is odd , b is even ; reduce larger number ; Driver code
using System ; class GFG { static int gcd ( int a , int b ) { if ( a == b ) return a ; if ( a == 0 ) return b ; if ( b == 0 ) return a ; if ( ( ~ a & 1 ) == 1 ) { if ( ( b & 1 ) == 1 ) return gcd ( a >> 1 , b ) ; else return gcd ( a >> 1 , b >> 1 ) << 1 ; } if ( ( ~ b & 1 ) == 1 ) return gcd ( a , b >> 1 ) ; if ( a > b ) return gcd ( ( a - b ) >> 1 , b ) ; return gcd ( ( b - a ) >> 1 , a ) ; } public static void Main ( ) { int a = 34 , b = 17 ; Console . Write ( " Gcd ▁ of ▁ given " + " numbers ▁ is ▁ " + gcd ( a , b ) ) ; } }
Sub | C # program to answer multiple queries of divisibility by 3 in substrings of a number ; Array to store the sum of digits ; Utility function to evaluate a character 's  integer value ; This function receives the string representation of the number and precomputes the sum array ; This function receives l and r representing the indices and prints the required output ; Driver code
using System ; class GFG { static int [ ] sum = new int [ 1000005 ] ; static int toInt ( char x ) { return x - '0' ; } static void prepareSum ( String s ) { sum [ 0 ] = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { sum [ i + 1 ] = sum [ i ] + toInt ( s [ i ] ) ; } } static void query ( int l , int r ) { if ( ( sum [ r + 1 ] - sum [ l ] ) % 3 == 0 ) { Console . WriteLine ( " Divisible ▁ by ▁ 3" ) ; } else { Console . WriteLine ( " Not ▁ divisible ▁ by ▁ 3" ) ; } } public static void Main ( ) { String n = "12468236544" ; prepareSum ( n ) ; query ( 0 , 1 ) ; query ( 1 , 2 ) ; query ( 3 , 6 ) ; query ( 0 , 10 ) ; } }
Print all n | C # recursive program to print all n - digit numbers whose sum of digits equals to given sum ; n , sum -- > value of inputs out -- > output array index -- > index of next digit to be filled in output array ; Base case ; If number becomes N - digit ; if sum of its digits is equal to given sum , print it ; Traverse through every digit . Note that here we ' re ▁ considering ▁ leading ▁ ▁ 0' s as digits ; append current digit to number ; recurse for next digit with reduced sum ; This is mainly a wrapper over findNDigitNumsUtil . It explicitly handles leading digit ; output array to store N - digit numbers ; fill 1 st position by every digit from 1 to 9 and calls findNDigitNumsUtil ( ) for remaining positions ; driver program to test above function
using System ; class GFG { static void findNDigitNumsUtil ( int n , int sum , char [ ] ou , int index ) { if ( index > n sum < 0 ) return ; if ( index == n ) { if ( sum == 0 ) { ou [ index ] = ' \0' ; Console . Write ( ou ) ; Console . Write ( " ▁ " ) ; } return ; } for ( int i = 0 ; i <= 9 ; i ++ ) { ou [ index ] = ( char ) ( i + '0' ) ; findNDigitNumsUtil ( n , sum - i , ou , index + 1 ) ; } } static void findNDigitNums ( int n , int sum ) { char [ ] ou = new char [ n + 1 ] ; for ( int i = 1 ; i <= 9 ; i ++ ) { ou [ 0 ] = ( char ) ( i + '0' ) ; findNDigitNumsUtil ( n , sum - i , ou , 1 ) ; } } public static void Main ( ) { int n = 2 , sum = 3 ; findNDigitNums ( n , sum ) ; } }
Program for Armstrong Numbers |
using System ; public class armstrongNumber { public void isArmstrong ( String n ) { char [ ] s = n . ToCharArray ( ) ; int size = s . Length ; int sum = 0 ; foreach ( char num in s ) { int temp = 1 ; int i = Int32 . Parse ( char . ToString ( num ) ) ; for ( int j = 0 ; j <= size - 1 ; j ++ ) { temp *= i ; } sum += temp ; } if ( sum == Int32 . Parse ( n ) ) { Console . WriteLine ( " True " ) ; } else { Console . WriteLine ( " False " ) ; } } public static void Main ( String [ ] args ) { armstrongNumber am = new armstrongNumber ( ) ; am . isArmstrong ( "153" ) ; am . isArmstrong ( "231" ) ; } }
Palindromic Primes | C # Program to print all palindromic primes smaller than or equal to n . ; A function that returns true only if num contains one digit ; comparison operation is faster than division operation . So using following instead of " return ▁ num ▁ / ▁ 10 ▁ = = ▁ 0 ; " ; A recursive function to find out whether num is palindrome or not . Initially , dupNum contains address of a copy of num . ; Base case ( needed for recursion termination ) : This statement / mainly compares the first digit with the last digit ; This is the key line in this method . Note that all recursive / calls have a separate copy of num , but they all share same copy of dupNum . We divide num while moving up the recursion tree ; The following statements are executed when we move up the recursion call tree ; At this point , if num % 10 contains ith digit from beginning , then ( dupNum ) % 10 contains ith digit from end ; The main function that uses recursive function isPalUtil ( ) to find out whether num is palindrome or not ; If num is negative , make it positive ; Create a separate copy of num , so that modifications made to address dupNum don 't  change the input number.  int dupNum = num; dupNum = num ; Function to generate all primes and checking whether number is palindromic or not ; 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 palindromic prime numbers ; checking whether the given number is prime palindromic or not ; Driver function
using System ; class GFG { static bool oneDigit ( int num ) { return ( num >= 0 && num < 10 ) ; } static bool isPalUtil ( int num , int dupNum ) { if ( oneDigit ( num ) ) return ( num == ( dupNum ) % 10 ) ; if ( ! isPalUtil ( num / 10 , dupNum ) ) return false ; dupNum /= 10 ; return ( num % 10 == ( dupNum ) % 10 ) ; } static bool isPal ( int num ) { if ( num < 0 ) num = - num ; return isPalUtil ( num , dupNum ) ; } static void printPalPrimesLessThanN ( int n ) { bool [ ] prime = new bool [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= n ; i += p ) { prime [ i ] = false ; } } } for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] && isPal ( p ) ) { Console . Write ( p + " ▁ " ) ; } } } public static void Main ( ) { int n = 100 ; Console . Write ( " Palindromic ▁ primes ▁ smaller ▁ than ▁ or ▁ " + " equal ▁ to ▁ are ▁ : STRNEWLINE " , n ) ; printPalPrimesLessThanN ( n ) ; } }
Almost Prime Numbers | C # Program to print first n numbers that are k - primes ; A function to count all prime factors of a given number ; Count the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , count i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; A function to print the first n numbers that are k - almost primes . ; Print this number if it is k - prime ; Increment count of k - primes printed so far ; Driver code
using System ; class GFG { static int countPrimeFactors ( int n ) { int count = 0 ; while ( n % 2 == 0 ) { n = n / 2 ; count ++ ; } for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { n = n / i ; count ++ ; } } if ( n > 2 ) count ++ ; return ( count ) ; } static void printKAlmostPrimes ( int k , int n ) { for ( int i = 1 , num = 2 ; i <= n ; num ++ ) { if ( countPrimeFactors ( num ) == k ) { Console . Write ( num + " ▁ " ) ; i ++ ; } } return ; } public static void Main ( ) { int n = 10 , k = 2 ; Console . WriteLine ( " First ▁ " + n + " ▁ " + k + " - almost ▁ prime ▁ numbers ▁ : ▁ " ) ; printKAlmostPrimes ( k , n ) ; } }
Program to add two fractions | C # program to add 2 fractions ; Function to return gcd of a and b ; Function to convert the obtained fraction into it 's simplest form ; Finding gcd of both terms ; Converting both terms into simpler terms by dividing them by common factor ; Function to add two fractions ; Finding gcd of den1 and den2 ; Denominator of final fraction obtained finding LCM of den1 and den2 LCM * GCD = a * b ; Changing the fractions to have same denominator Numerator of the final fraction obtained ; Calling function to convert final fraction into it 's simplest form ; Driver program
class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void lowest ( int den3 , int num3 ) { int common_factor = gcd ( num3 , den3 ) ; den3 = den3 / common_factor ; num3 = num3 / common_factor ; System . Console . WriteLine ( num3 + " / " + den3 ) ; } static void addFraction ( int num1 , int den1 , int num2 , int den2 ) { int den3 = gcd ( den1 , den2 ) ; den3 = ( den1 * den2 ) / den3 ; int num3 = ( num1 ) * ( den3 / den1 ) + ( num2 ) * ( den3 / den2 ) ; lowest ( den3 , num3 ) ; } public static void Main ( ) { int num1 = 1 , den1 = 500 , num2 = 2 , den2 = 1500 ; System . Console . Write ( num1 + " / " + den1 + " ▁ + ▁ " + num2 + " / " + den2 + " ▁ is ▁ equal ▁ to ▁ " ) ; addFraction ( num1 , den1 , num2 , den2 ) ; } }
The Lazy Caterer 's Problem | C # program to find the solution to The Lazy Caterer 's Problem ; This function returns the maximum number of pieces that can be made form pancake using n cuts ; Use the formula ; Driver code
using System ; class GFG { static int findPieces ( int n ) { return ( n * ( n + 1 ) ) / 2 + 1 ; } public static void Main ( ) { Console . WriteLine ( findPieces ( 1 ) ) ; Console . WriteLine ( findPieces ( 2 ) ) ; Console . WriteLine ( findPieces ( 3 ) ) ; Console . Write ( findPieces ( 50 ) ) ; } }
Count digits in a factorial | Set 2 | An optimised C # program to find the number of digits in a factorial . ; Function returns the number of digits present in n ! since the result can be large , long long is used as return type ; factorial of - ve number doesn 't exists ; base case ; Use Kamenetsky formula to calculate the number of digits ; Driver Code
using System ; class GFG { public static double M_E = 2.71828182845904523536 ; public static double M_PI = 3.141592654 ; static long findDigits ( int n ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; double x = ( n * Math . Log10 ( n / M_E ) + Math . Log10 ( 2 * M_PI * n ) / 2.0 ) ; return ( long ) Math . Floor ( x ) + 1 ; } public static void Main ( ) { Console . WriteLine ( findDigits ( 1 ) ) ; Console . WriteLine ( findDigits ( 50000000 ) ) ; Console . WriteLine ( findDigits ( 1000000000 ) ) ; Console . Write ( findDigits ( 120 ) ) ; } }
Count digits in a factorial | Set 1 | A C ++ program to find the number of digits in a factorial ; This function receives an integer n , and returns the number of digits present in n ! ; factorial exists only for n >= 0 ; base case ; else iterate through n and calculate the value ; Driver code
using System ; class GFG { static int findDigits ( int n ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; double digits = 0 ; for ( int i = 2 ; i <= n ; i ++ ) digits += Math . Log10 ( i ) ; return ( int ) Math . Floor ( digits ) + 1 ; } public static void Main ( ) { Console . Write ( findDigits ( 1 ) + " STRNEWLINE " ) ; Console . Write ( findDigits ( 5 ) + " STRNEWLINE " ) ; Console . Write ( findDigits ( 10 ) + " STRNEWLINE " ) ; Console . Write ( findDigits ( 120 ) + " STRNEWLINE " ) ; } }
Find number of subarrays with even sum | C # program to count number of sub - arrays whose sum is even using brute force Time Complexity - O ( N ^ 2 ) Space Complexity - O ( 1 ) ; Find sum of all subarrays and increment result if sum is even ; Driver code
using System ; class GFG { static int countEvenSum ( int [ ] arr , int n ) { int result = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { int sum = 0 ; for ( int j = i ; j <= n - 1 ; j ++ ) { sum = sum + arr [ j ] ; if ( sum % 2 == 0 ) result ++ ; } } return ( result ) ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 2 , 3 , 4 , 1 } ; int n = arr . Length ; Console . Write ( " The ▁ Number ▁ of ▁ Subarrays " + " ▁ with ▁ even ▁ sum ▁ is ▁ " ) ; Console . WriteLine ( countEvenSum ( arr , n ) ) ; } }
Find number of subarrays with even sum | C # program to count number of sub - arrays with even sum using an efficient algorithm Time Complexity - O ( N ) Space Complexity - O ( 1 ) ; A temporary array of size 2. temp [ 0 ] is going to store count of even subarrays and temp [ 1 ] count of odd . temp [ 0 ] is initialized as 1 because there a single even element is also counted as a subarray ; Initialize count . sum is sum of elements under modulo 2 and ending with arr [ i ] . ; i ' th ▁ iteration ▁ computes ▁ sum ▁ ▁ of ▁ arr [ 0 . . i ] ▁ under ▁ modulo ▁ 2 ▁ ▁ and ▁ increments ▁ even / odd ▁ count ▁ ▁ according ▁ to ▁ sum ' s value ; 2 is added to handle negative numbers ; Increment even or odd count ; Use handshake lemma to count even subarrays ( Note that an even cam be formed by two even or two odd ) ; Driver code
using System ; class GFG { static int countEvenSum ( int [ ] arr , int n ) { int [ ] temp = { 1 , 0 } ; int result = 0 , sum = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { sum = ( ( sum + arr [ i ] ) % 2 + 2 ) % 2 ; temp [ sum ] ++ ; } result = result + ( temp [ 0 ] * ( temp [ 0 ] - 1 ) / 2 ) ; result = result + ( temp [ 1 ] * ( temp [ 1 ] - 1 ) / 2 ) ; return ( result ) ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 2 , 3 , 4 , 1 } ; int n = arr . Length ; Console . WriteLine ( " The ▁ Number ▁ of ▁ Subarrays " + " ▁ with ▁ even ▁ sum ▁ is ▁ " + countEvenSum ( arr , n ) ) ; } }
Sum of Fibonacci Numbers | C # Program to find sum of Fibonacci numbers ; Computes value of first fibonacci numbers ; Initialize result ; Add remaining terms ; Driver Code
using System ; class GFG { static int calculateSum ( int n ) { if ( n <= 0 ) return 0 ; int [ ] fibo = new int [ n + 1 ] ; fibo [ 0 ] = 0 ; fibo [ 1 ] = 1 ; int sum = fibo [ 0 ] + fibo [ 1 ] ; for ( int i = 2 ; i <= n ; i ++ ) { fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ; sum += fibo [ i ] ; } return sum ; } static void Main ( ) { int n = 4 ; Console . WriteLine ( " Sum ▁ of ▁ Fibonacci " + " ▁ numbers ▁ is ▁ : ▁ " + calculateSum ( n ) ) ; } }
Sum of Fibonacci Numbers | C # Program to find sum of Fibonacci numbers in O ( Log n ) time . ; Create an array for memoization ; Returns n 'th Fibonacci  number using table f[] ; Arrays . fill ( f , 0 ) ; Base cases ; If fib ( n ) is already computed ; Applying above formula [ Note value n & 1 is 1 if n is odd , else 0 ] . ; Computes value of first Fibonacci numbers ; Driver Code
using System ; class GFG { static int MAX = 1000 ; static int [ ] f = new int [ MAX ] ; static int fib ( int n ) { for ( int i = 0 ; i < MAX ; i ++ ) f [ i ] = 0 ; if ( n == 0 ) return 0 ; if ( n == 1 n == 2 ) return ( f [ n ] = 1 ) ; if ( f [ n ] == 1 ) return f [ n ] ; int k ; if ( ( n & 1 ) == 1 ) k = ( n + 1 ) / 2 ; else k = n / 2 ; if ( ( n & 1 ) == 1 ) f [ n ] = ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) ; else f [ n ] = ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ; return f [ n ] ; } static int calculateSum ( int n ) { return fib ( n + 2 ) - 1 ; } public static void Main ( ) { int n = 4 ; Console . Write ( " Sum ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : ▁ " + calculateSum ( n ) ) ; } }
Find all combinations that add upto given number | C # program to find out all combinations of positive numbers that add upto given number ; arr - array to store the combination index - next location in array num - given number reducedNum - reduced number ; Base condition ; If combination is found , print it ; Find the previous number stored in arr [ ] . It helps in maintaining increasing order ; note loop starts from previous number i . e . at array location index - 1 ; next element of array is k ; call recursively with reduced number ; Function to find out all combinations of positive numbers that add upto given number . It uses findCombinationsUtil ( ) ; array to store the combinations It can contain max n elements ; find all combinations ; Driver code
using System ; class GFG { static void findCombinationsUtil ( int [ ] arr , int index , int num , int reducedNum ) { if ( reducedNum < 0 ) return ; if ( reducedNum == 0 ) { for ( int i = 0 ; i < index ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; return ; } int prev = ( index == 0 ) ? 1 : arr [ index - 1 ] ; for ( int k = prev ; k <= num ; k ++ ) { arr [ index ] = k ; findCombinationsUtil ( arr , index + 1 , num , reducedNum - k ) ; } } static void findCombinations ( int n ) { int [ ] arr = new int [ n ] ; findCombinationsUtil ( arr , 0 , n , n ) ; } static public void Main ( ) { int n = 5 ; findCombinations ( n ) ; } }
Find Square Root under Modulo p | Set 2 ( Shanks Tonelli algorithm ) | C # program to implement Shanks Tonelli algorithm for finding Modular Square Roots ; utility function to find pow ( base , exponent ) % modulus ; utility function to find gcd ; Returns k such that b ^ k = 1 ( mod p ) ; Initializing k with first odd prime number ; function return p - 1 ( = x argument ) as x * 2 ^ e , where x will be odd sending e as reference because updation is needed in actual e ; Main function for finding the modular square root ; a and p should be coprime for finding the modular square root ; If below expression return ( p - 1 ) then modular square root is not possible ; expressing p - 1 , in terms of s * 2 ^ e , where s is odd number ; finding smallest q such that q ^ ( ( p - 1 ) / 2 ) ( mod p ) = p - 1 ; q - 1 is in place of ( - 1 % p ) ; Initializing variable x , b and g ; keep looping until b become 1 or m becomes 0 ; finding m such that b ^ ( 2 ^ m ) = 1 ; updating value of x , g and b according to algorithm ; Driver code ; p should be prime
using System ; class GFG { static int z = 0 ; static int pow1 ( int base1 , int exponent , int modulus ) { int result = 1 ; base1 = base1 % modulus ; while ( exponent > 0 ) { if ( exponent % 2 == 1 ) result = ( result * base1 ) % modulus ; exponent = exponent >> 1 ; base1 = ( base1 * base1 ) % modulus ; } return result ; } static int gcd ( int a , int b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } static int order ( int p , int b ) { if ( gcd ( p , b ) != 1 ) { Console . WriteLine ( " p ▁ and ▁ b ▁ are " + " not ▁ co - prime . " ) ; return - 1 ; } int k = 3 ; while ( true ) { if ( pow1 ( b , k , p ) == 1 ) return k ; k ++ ; } } static int convertx2e ( int x ) { z = 0 ; while ( x % 2 == 0 ) { x /= 2 ; z ++ ; } return x ; } static int STonelli ( int n , int p ) { if ( gcd ( n , p ) != 1 ) { Console . WriteLine ( " a ▁ and ▁ p ▁ are ▁ not ▁ coprime " ) ; return - 1 ; } if ( pow1 ( n , ( p - 1 ) / 2 , p ) == ( p - 1 ) ) { Console . WriteLine ( " no ▁ sqrt ▁ possible " ) ; return - 1 ; } int s , e ; s = convertx2e ( p - 1 ) ; e = z ; int q ; for ( q = 2 ; ; q ++ ) { if ( pow1 ( q , ( p - 1 ) / 2 , p ) == ( p - 1 ) ) break ; } int x = pow1 ( n , ( s + 1 ) / 2 , p ) ; int b = pow1 ( n , s , p ) ; int g = pow1 ( q , s , p ) ; int r = e ; while ( true ) { int m ; for ( m = 0 ; m < r ; m ++ ) { if ( order ( p , b ) == - 1 ) return - 1 ; if ( order ( p , b ) == Math . Pow ( 2 , m ) ) break ; } if ( m == 0 ) return x ; x = ( x * pow1 ( g , ( int ) Math . Pow ( 2 , r - m - 1 ) , p ) ) % p ; g = pow1 ( g , ( int ) Math . Pow ( 2 , r - m ) , p ) ; b = ( b * g ) % p ; if ( b == 1 ) return x ; r = m ; } } static void Main ( ) { int n = 2 ; int p = 113 ; int x = STonelli ( n , p ) ; if ( x == - 1 ) Console . WriteLine ( " Modular ▁ square ▁ root " + " is ▁ not ▁ exist STRNEWLINE " ) ; else Console . WriteLine ( " Modular ▁ square ▁ root ▁ of " + " { 0 } ▁ and ▁ { 1 } ▁ is ▁ { 2 } STRNEWLINE " , n , p , x ) ; } }