codeparrot/xlcost-text-to-code huggingface dataset:
Count digits in given number N which divide N | Utility function to check divisibility by digit ; ( N [ i ] - '0' ) gives the digit value and form the number ; Function to count digits which appears in N and divide N divide [ 10 ] -- > array which tells that particular digit divides N or not count [ 10 ] -- > counts frequency of digits which divide N ; We initialize all digits of N as not divisible by N . ; start checking divisibility of N by digits 2 to 9 ; if digit divides N then mark it as true ; Now traverse the number string to find and increment result whenever a digit divides N . ; Driver Code
< ? php function divisible ( $ N , $ digit ) { $ ans = 0 ; for ( $ i = 0 ; $ i < strlen ( $ N ) ; $ i ++ ) { $ ans = ( $ ans * 10 + ( int ) ( $ N [ $ i ] - '0' ) ) ; $ ans %= $ digit ; } return ( $ ans == 0 ) ; } function allDigits ( $ N ) { $ divide = array_fill ( 0 , 10 , false ) ; for ( $ digit = 2 ; $ digit <= 9 ; $ digit ++ ) { if ( divisible ( $ N , $ digit ) ) $ divide [ $ digit ] = true ; } $ result = 0 ; for ( $ i = 0 ; $ i < strlen ( $ N ) ; $ i ++ ) { if ( $ divide [ ( int ) ( $ N [ $ i ] - '0' ) ] == true ) $ result ++ ; } return $ result ; } $ N = "122324" ; echo allDigits ( $ N ) ; ? >
Count numbers which can be constructed using two numbers | Returns count of numbers from 1 to n that can be formed using x and y . ; Create an auxiliary array and initialize it as false . An entry arr [ i ] = true is going to mean that i can be formed using x and y ; x and y can be formed using x and y . ; Initialize result ; Traverse all numbers and increment result if a number can be formed using x and y . ; If i can be formed using x and y ; Then i + x and i + y can also be formed using x and y . ; Increment result ; Driver code
< ? php function countNums ( $ n , $ x , $ y ) { $ arr = array_fill ( 0 , $ n + 1 , false ) ; if ( $ x <= $ n ) $ arr [ $ x ] = true ; if ( $ y <= $ n ) $ arr [ $ y ] = true ; $ result = 0 ; for ( $ i = min ( $ x , $ y ) ; $ i <= $ n ; $ i ++ ) { if ( $ arr [ $ i ] ) { if ( $ i + $ x <= $ n ) $ arr [ $ i + $ x ] = true ; if ( $ i + $ y <= $ n ) $ arr [ $ i + $ y ] = true ; $ result ++ ; } } return $ result ; } $ n = 15 ; $ x = 5 ; $ y = 7 ; echo countNums ( $ n , $ x , $ y ) ; ? >
Emirp numbers | Function to find reverse of any number ; Sieve method used for generating emirp number ( use of sieve ofEratosthenes ) ; 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 ; Traverse all prime numbers ; Find reverse a number ; A number is emrip if it is not a palindrome number and its reverse is also prime . ; Mark reverse prime as false so that it 's  not printed again ; Driver Code
< ? php function reverse ( $ x ) { $ rev = 0 ; while ( $ x > 0 ) { $ rev = ( $ rev * 10 ) + $ x % 10 ; $ x = ( int ) ( $ x / 10 ) ; } return $ rev ; } function printEmirp ( $ n ) { $ prime = array_fill ( 0 , ( $ n + 1 ) , 1 ) ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] == 1 ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = 0 ; } } for ( $ p = 2 ; $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] == 1 ) { $ rev = reverse ( $ p ) ; if ( $ p != $ rev && $ rev <= $ n && $ prime [ $ rev ] == 1 ) { echo $ p . " " ▁ . ▁ $ rev ▁ . ▁ " " $ prime [ $ rev ] = 0 ; } } } } $ n = 100 ; printEmirp ( $ n ) ; ? >
Abundant Number | Function to calculate sum of divisors ; Note that this loop runs till square root of n ; If divisors are equal , take only one of them ; Otherwise take both ; calculate sum of all proper divisors only ; Function to check Abundant Number ; Return true if sum of divisors is greater than n . ; Driver Code
< ? php function getSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) $ sum = $ sum + $ i ; else { $ sum = $ sum + $ i ; $ sum = $ sum + ( $ n / $ i ) ; } } } $ sum = $ sum - $ n ; return $ sum ; } function checkAbundant ( $ n ) { return ( getSum ( $ n ) > $ n ) ; } $ k = checkAbundant ( 12 ) ? " YES STRNEWLINE " : " NO STRNEWLINE " ; echo ( $ k ) ; $ k = checkAbundant ( 15 ) ? " YES STRNEWLINE " : " NO STRNEWLINE " ; echo ( $ k ) ; ? >
Powerful Number | function to check if the number is powerful ; First divide the number repeatedly by 2 ; If only 2 ^ 1 divides n ( not higher powers ) , then return false ; if n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n ( not higher powers ) , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Driver Code
< ? php function isPowerful ( $ n ) { while ( $ n % 2 == 0 ) { $ power = 0 ; while ( $ n % 2 == 0 ) { $ n /= 2 ; $ power ++ ; } if ( $ power == 1 ) return false ; } for ( $ factor = 3 ; $ factor <= sqrt ( $ n ) ; $ factor += 2 ) { $ power = 0 ; while ( $ n % $ factor == 0 ) { $ n = $ n / $ factor ; $ power ++ ; } if ( $ power == 1 ) return false ; } return ( $ n == 1 ) ; } $ d = isPowerful ( 20 ) ? " YES STRNEWLINE " : " NO STRNEWLINE " ; echo $ d ; $ d = isPowerful ( 27 ) ? " YES STRNEWLINE " : " NO STRNEWLINE " ; echo $ d ; ? >
Deficient Number | Function to calculate sum of divisors ; Initialize sum of prime factors ; Note that this loop runs till square root of n ; If divisors are equal , take only one of them ; Otherwise take both ; Function to check Deficient Number ; Check if sum ( n ) < 2 * n ; Driver Code
< ? php function divisorsSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) { $ sum = $ sum + $ i ; } else { $ sum = $ sum + $ i ; $ sum = $ sum + ( $ n / $ i ) ; } } } return $ sum ; } function isDeficient ( $ n ) { return ( divisorsSum ( $ n ) < ( 2 * $ n ) ) ; } $ ds = isDeficient ( 12 ) ? " YES STRNEWLINE " : " NO STRNEWLINE " ; echo ( $ ds ) ; $ ds = isDeficient ( 15 ) ? " YES STRNEWLINE " : " NO STRNEWLINE " ; echo ( $ ds ) ; ? >
Smith Number | PHP program to to check whether a number is Smith Number or not . ; array to store all prime less than and equal to 10 ^ 6 ; utility function for sieve of sundaram ; In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x . Since we want primes smaller than MAX , we reduce MAX to half . This array is used to separate numbers of the form i + j + 2 ij from others where 1 <= i <= j ; Main logic of Sundaram . Mark all numbers which do not generate prime number by doing 2 * i + 1 ; Since 2 is a prime number ; Print other primes . Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; Returns true if n is a Smith number , else false . ; Find sum the digits of prime factors of n ; If primes [ i ] is a prime factor , add its digits to pDigitSum . ; If n != 1 then one prime factor still to be summed up ; ; All prime factors digits summed up Now sum the original number digits ; If sum of digits in prime factors and sum of digits in original number are same , then return true . Else return false . ; Finding all prime numbers before limit . These numbers are used to find prime factors .
< ? php $ MAX = 10000 ; $ primes = array ( ) ; function sieveSundaram ( ) { global $ MAX , $ primes ; $ marked = array_fill ( 0 , ( $ MAX / 2 + 100 ) , false ) ; for ( $ i = 1 ; $ i <= ( sqrt ( $ MAX ) - 1 ) / 2 ; $ i ++ ) for ( $ j = ( $ i * ( $ i + 1 ) ) << 1 ; $ j <= $ MAX / 2 ; $ j = $ j + 2 * $ i + 1 ) $ marked [ $ j ] = true ; array_push ( $ primes , 2 ) ; for ( $ i = 1 ; $ i <= $ MAX / 2 ; $ i ++ ) if ( $ marked [ $ i ] == false ) array_push ( $ primes , 2 * $ i + 1 ) ; } function isSmith ( $ n ) { global $ MAX , $ primes ; $ original_no = $ n ; $ pDigitSum = 0 ; for ( $ i = 0 ; $ primes [ $ i ] <= $ n / 2 ; $ i ++ ) { while ( $ n % $ primes [ $ i ] == 0 ) { $ p = $ primes [ $ i ] ; $ n = $ n / $ p ; while ( $ p > 0 ) { $ pDigitSum += ( $ p % 10 ) ; $ p = $ p / 10 ; } } } if ( $ n != 1 && $ n != $ original_no ) { while ( $ n > 0 ) { $ pDigitSum = $ pDigitSum + $ n % 10 ; $ n = $ n / 10 ; } } $ sumDigits = 0 ; while ( $ original_no > 0 ) { $ sumDigits = $ sumDigits + $ original_no % 10 ; $ original_no = $ original_no / 10 ; } return ( $ pDigitSum == $ sumDigits ) ; } sieveSundaram ( ) ; echo " Printing ▁ first ▁ few ▁ Smith ▁ Numbers " . " ▁ using ▁ isSmith ( ) STRNEWLINE " ; for ( $ i = 1 ; $ i < 500 ; $ i ++ ) if ( isSmith ( $ i ) ) echo $ i . " " ; ? >
Kaprekar Number | Returns true if n is a Kaprekar number , else false ; Count number of digits in square ; Split the square at different points and see if sum of any pair of splitted numbers is equal to n . ; To avoid numbers like 10 , 100 , 1000 ( These are not Karprekar numbers ; Find sum of current parts and compare with n ; compare with original number ; Driver code
< ? php function iskaprekar ( $ n ) { if ( $ n == 1 ) return true ; $ sq_n = $ n * $ n ; $ count_digits = 0 ; while ( $ sq_n ) { $ count_digits ++ ; $ sq_n = ( int ) ( $ sq_n / 10 ) ; } for ( $ r_digits = 1 ; $ r_digits < $ count_digits ; $ r_digits ++ ) { $ eq_parts = pow ( 10 , $ r_digits ) ; if ( $ eq_parts == $ n ) continue ; $ sum = ( int ) ( $ sq_n1 / $ eq_parts ) + $ sq_n1 % $ eq_parts ; if ( $ sum == $ n ) return true ; } return false ; } echo " Printing ▁ first ▁ few ▁ Kaprekar ▁ " . " Numbers ▁ using ▁ iskaprekar ( ) STRNEWLINE " ; for ( $ i = 1 ; $ i < 10000 ; $ i ++ ) if ( iskaprekar ( $ i ) ) echo $ i . " " ; ? >
Keith Number | Returns true if x is Keith , else false . ; Store all digits of x in a vector " terms " Also find number of digits and store in " n " . ; $n = 0 ; n is number of digits in x ; To get digits in right order ( from MSB to LSB ) ; Keep finding next trms of a sequence generated using digits of x until we either reach x or a number greate than x ; Next term is sum of previous n terms ; When the control comes out of the while loop , either the next_term is equal to the number or greater than it . If next_term is equal to x , then x is a Keith number , else not ; Driver Code
< ? php function isKeith ( $ x ) { $ terms = array ( ) ; $ temp = $ x ; while ( $ temp > 0 ) { array_push ( $ terms , $ temp % 10 ) ; $ temp = ( int ) ( $ temp / 10 ) ; $ n ++ ; } $ terms = array_reverse ( $ terms ) ; $ next_term = 0 ; $ i = $ n ; while ( $ next_term < $ x ) { $ next_term = 0 ; for ( $ j = 1 ; $ j <= $ n ; $ j ++ ) $ next_term += $ terms [ $ i - $ j ] ; array_push ( $ terms , $ next_term ) ; $ i ++ ; } return ( $ next_term == $ x ) ; } isKeith ( 14 ) ? print ( " Yes STRNEWLINE " ) : print ( " No STRNEWLINE " ) ; isKeith ( 12 ) ? print ( " Yes STRNEWLINE " ) : print ( " No STRNEWLINE " ) ; isKeith ( 197 ) ? print ( " Yes STRNEWLINE " ) : print ( " No STRNEWLINE " ) ; ? >
Check if a number can be expressed as a sum of consecutive numbers | This function returns true if n can be expressed sum of consecutive . ; We basically return true if n is a power of two ; Driver code
< ? php function canBeSumofConsec ( $ n ) { return ( ( $ n & ( $ n - 1 ) ) && $ n ) ; } $ n = 15 ; if ( canBeSumofConsec ( $ n ) ) echo " true " ; else echo " false " ; ? >
Even Fibonacci Numbers Sum | Returns sum of even Fibonacci numbers which are less than or equal to given limit . ; Initialize first two even prime numbers and their sum ; calculating sum of even Fibonacci value ; get next even value of Fibonacci sequence ; If we go beyond limit , we break loop ; Move to next even number and update sum ; Driver code
< ? php function evenFibSum ( $ limit ) { if ( $ limit < 2 ) return 0 ; $ ef1 = 0 ; $ ef2 = 2 ; $ sum = $ ef1 + $ ef2 ; while ( $ ef2 <= $ limit ) { $ ef3 = 4 * $ ef2 + $ ef1 ; if ( $ ef3 > $ limit ) break ; $ ef1 = $ ef2 ; $ ef2 = $ ef3 ; $ sum += $ ef2 ; } return $ sum ; } $ limit = 400 ; echo ( evenFibSum ( $ limit ) ) ; ? >
Find numbers with K odd divisors in a given range | function to check if number is perfect square or not ; Function to return count of divisors of a number ; Note that this loop runs till square root ; If divisors are equal , count it only once ; Otherwise print both ; Function to calculate all divisors having exactly k divisors between a and b ; Initialize result ; calculate only for perfect square numbers ; check if number is perfect square or not ; total divisors of number equals to k or not ; Driver Code
< ? php function isPerfect ( $ n ) { $ s = sqrt ( $ n ) ; return ( $ s * $ s == $ n ) ; } function divisorsCount ( $ n ) { $ count = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ n ) + 1 ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) $ count += 1 ; else $ count += 2 ; } } return $ count ; } function kDivisors ( $ a , $ b , $ k ) { $ count = 0 ; for ( $ i = $ a ; $ i <= $ b ; $ i ++ ) { if ( isPerfect ( $ i ) ) if ( divisorsCount ( $ i ) == $ k ) $ count ++ ; } return $ count ; } $ a = 2 ; $ b = 49 ; $ k = 3 ; echo kDivisors ( $ a , $ b , $ k ) ; ? >
Nth Even Fibonacci Number | Function which return nth even fibonnaci number ; calculation of Fn = 4 * ( Fn - 1 ) + Fn - 2 ; Driver Code
< ? php function evenFib ( $ n ) { if ( $ n < 1 ) return $ n ; if ( $ n == 1 ) return 2 ; return ( ( 4 * evenFib ( $ n - 1 ) ) + evenFib ( $ n - 2 ) ) ; } $ n = 7 ; echo ( evenFib ( $ n ) ) ; ? >
N 'th Smart Number | Limit on result ; Function to calculate n 'th smart number  ; Initialize all numbers as not prime ; iterate to mark all primes and smart number ; Traverse all numbers till maximum limit ; ' i ' is maked as prime number because it is not multiple of any other prime ; mark all multiples of ' i ' as non prime ; If i is the third prime factor of j then add it to result as it has at least three prime factors . ; Sort all smart numbers ; return n 'th smart number  ; Driver program to run the case
< ? php $ MAX = 3000 ; function smartNumber ( $ n ) { global $ MAX ; $ primes = array_fill ( 0 , $ MAX , 0 ) ; $ result = array ( ) ; for ( $ i = 2 ; $ i < $ MAX ; $ i ++ ) { if ( $ primes [ $ i ] == 0 ) { $ primes [ $ i ] = 1 ; for ( $ j = $ i * 2 ; $ j < $ MAX ; $ j = $ j + $ i ) { $ primes [ $ j ] -= 1 ; if ( ( $ primes [ $ j ] + 3 ) == 0 ) array_push ( $ result , $ j ) ; } } } sort ( $ result ) ; return $ result [ $ n - 1 ] ; } $ n = 50 ; echo smartNumber ( $ n ) ; ? >
Repeated subtraction among two numbers | Returns count of steps before one of the numbers become 0 after repeated subtractions . ; If y divides x , then simply return x / y . ; Else recur . Note that this function works even if x is smaller than y because in that case first recursive call exchanges roles of x and y . ; Driver code
< ? php function countSteps ( $ x , $ y ) { if ( $ x % $ y == 0 ) return floor ( ( ( int ) $ x / $ y ) ) ; return floor ( ( ( int ) $ x / $ y ) + countSteps ( $ y , $ x % $ y ) ) ; } $ x = 100 ; $ y = 19 ; echo countSteps ( $ x , $ y ) ; ? >
Count number of solutions of x ^ 2 = 1 ( mod p ) in given range | Program to count number of values that satisfy x ^ 2 = 1 mod p where x lies in range [ 1 , n ] ; Initialize result ; Traverse all numbers smaller than given number p . Note that we don 't traverse from  1 to n, but 1 to p ; If x is a solution , then count all numbers of the form x + i * p such that x + i * p is in range [ 1 , n ] ; The largest number in the form of x + p * i in range [ 1 , n ] ; Add count of numbers of the form x + p * i . 1 is added for x itself . ; Driver code
< ? php function findCountOfSolutions ( $ n , $ p ) { $ ans = 0 ; for ( $ x = 1 ; $ x < $ p ; $ x ++ ) { if ( ( $ x * $ x ) % $ p == 1 ) { $ last = $ x + $ p * ( $ n / $ p ) ; if ( $ last > $ n ) $ last -= $ p ; $ ans += ( ( $ last - $ x ) / $ p + 1 ) ; } } return $ ans ; } $ n = 10 ; $ p = 5 ; echo findCountOfSolutions ( $ n , $ p ) ; ? >
Kaprekar Constant | This function checks validity of kaprekar ' s ▁ constant . ▁ It ▁ returns ▁ kaprekar ' s constant for any four digit number " n " such that all digits of n are not same . ; Store current n as previous number ; Get four digits of given number ; Sort all four dgits in ascending order And giet in the form of number " asc " ; Get all four dgits in descending order in the form of number " desc " ; Get the difference of two numbers ; If difference is same as previous , we have reached kaprekar 's constant ; Else recur ; A wrapper over kaprekarRec ( ) ; Trying few four digit numbers , we always get 6174
< ? php function kaprekarRec ( $ n , $ prev ) { if ( $ n == 0 ) return 0 ; $ prev = $ n ; $ digits = array_fill ( 0 , 4 , 0 ) ; for ( $ i = 0 ; $ i < 4 ; $ i ++ ) { $ digits [ $ i ] = $ n % 10 ; $ n = ( int ) ( $ n / 10 ) ; } sort ( $ digits ) ; $ asc = 0 ; for ( $ i = 0 ; $ i < 4 ; $ i ++ ) $ asc = $ asc * 10 + $ digits [ $ i ] ; rsort ( $ digits ) ; $ desc = 0 ; for ( $ i = 0 ; $ i < 4 ; $ i ++ ) $ desc = $ desc * 10 + $ digits [ $ i ] ; $ diff = abs ( $ asc - $ desc ) ; if ( $ diff == $ prev ) return $ diff ; return kaprekarRec ( $ diff , $ prev ) ; } function kaprekar ( $ n ) { $ rev = 0 ; return kaprekarRec ( $ n , $ rev ) ; } echo kaprekar ( 1000 ) . " STRNEWLINE " ; echo kaprekar ( 1112 ) . " STRNEWLINE " ; echo kaprekar ( 9812 ) . " STRNEWLINE " ; ? >
Bakhshali Approximation for computing square roots | This PHP program gives result approximated to 5 decimal places . ; This will be the nearest perfect square to s ; This is the sqrt of pSq ; Find the nearest perfect square to s ; calculate d ; calculate P ; calculate A ; calculate sqrt ( S ) . ; Driver Code
< ? php function sqroot ( $ s ) { $ pSq = 0 ; $ N = 0 ; for ( $ i = intval ( $ s ) ; $ i > 0 ; $ i -- ) { for ( $ j = 1 ; $ j < $ i ; $ j ++ ) { if ( $ j * $ j == $ i ) { $ pSq = $ i ; $ N = $ j ; break ; } } if ( $ pSq > 0 ) break ; } $ d = $ s - $ pSq ; $ P = $ d / ( 2.0 * $ N ) ; $ A = $ N + $ P ; $ sqrt_of_s = $ A - ( ( $ P * $ P ) / ( 2.0 * $ A ) ) ; return $ sqrt_of_s ; } $ num = 9.2345 ; $ sqroot_of_num = sqroot ( $ num ) ; echo " Square ▁ root ▁ of ▁ " . $ num . " ▁ = ▁ " . round ( ( $ sqroot_of_num * 100000.0 ) / 100000.0 , 5 ) ; ? >
Breaking an Integer to get Maximum Product | 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
< ? php function power ( $ x , $ a ) { $ res = 1 ; while ( $ a ) { if ( $ a & 1 ) $ res = $ res * $ x ; $ x = $ x * $ x ; $ a >>= 1 ; } return $ res ; } function breakInteger ( $ N ) { if ( $ N == 2 ) return 1 ; if ( $ N == 3 ) return 2 ; $ maxProduct = 0 ; 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 ; } $ maxProduct = breakInteger ( 10 ) ; echo $ maxProduct ; ? >
Finding sum of digits of a number until sum becomes single digit | PHP 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
< ? php function digSum ( $ n ) { $ sum = 0 ; while ( $ n > 0 $ sum > 9 ) { if ( $ n == 0 ) { $ n = $ sum ; $ sum = 0 ; } $ sum += $ n % 10 ; $ n = ( int ) $ n / 10 ; } return $ sum ; } $ n = 1234 ; echo digSum ( $ n ) ; ? >
Finding sum of digits of a number until sum becomes single digit | Driver program to test the above function
< ? php function digSum ( $ n ) { if ( $ n == 0 ) return 0 ; return ( $ n % 9 == 0 ) ? 9 : ( $ n % 9 ) ; } $ n = 9999 ; echo digSum ( $ n ) ; ? >
Multiples of 3 or 7 | Returns count of all numbers smaller than or equal to n and multiples of 3 or 7 or both ; Driver code
< ? php function countMultiples ( $ n ) { return floor ( $ n / 3 + $ n / 7 - $ n / 21 ) ; } echo " Count = " ? >
Find Last Digit of a ^ b for Large Numbers | Function to find b % a ; Initialize result ; calculating mod of b with a to make b like 0 <= b < a ; return $mod ; 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
< ? php function Modulo ( $ a , $ b ) { $ mod = 0 ; for ( $ i = 0 ; $ i < strlen ( $ b ) ; $ i ++ ) $ mod = ( $ mod * 10 + $ b [ $ i ] - '0' ) % $ a ; } function LastDigit ( $ a , $ b ) { $ len_a = strlen ( $ a ) ; $ len_b = strlen ( $ b ) ; 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 ; $ exp = ( Modulo ( 4 , $ b ) == 0 ) ? 4 : Modulo ( 4 , $ b ) ; $ res = pow ( $ a [ $ len_a - 1 ] - '0' , $ exp ) ; return $ res % 10 ; } $ a = "117" ; $ b = "3" ; echo LastDigit ( $ a , $ b ) ; ? >
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
< ? php function reversDigits ( $ num ) { $ rev_num = 0 ; while ( $ num > 0 ) { $ rev_num = $ rev_num * 10 + $ num % 10 ; $ num = ( int ) ( $ num / 10 ) ; } return $ rev_num ; } function isPalindrome ( $ num ) { return ( reversDigits ( $ num ) == $ num ) ; } function ReverseandAdd ( $ num ) { $ rev_num = 0 ; while ( $ num <= 4294967295 ) { $ rev_num = reversDigits ( $ num ) ; $ num = $ num + $ rev_num ; if ( isPalindrome ( $ num ) ) { print ( $ num . " " ) ; break ; } else if ( $ num > 4294967295 ) { print ( " No ▁ palindrome ▁ exist " ) ; } } } ReverseandAdd ( 195 ) ; ReverseandAdd ( 265 ) ; ? >
Stein 's Algorithm for finding GCD | Function to implement Stein 's Algorithm ; GCD ( 0 , b ) == b ; GCD ( a , 0 ) == a , GCD ( 0 , 0 ) == 0 ; look for factors of 2 if ( ~ $a & 1 ) a is even ; if ( $b & 1 ) b is odd ; else both a and b are even ; if ( ~ $b & 1 ) a is odd , b is even ; reduce larger number ; Driver code
< ? php function gcd ( $ a , $ b ) { if ( $ a == $ b ) return $ a ; if ( $ a == 0 ) return $ b ; if ( $ b == 0 ) return $ a ; { return gcd ( $ a >> 1 , $ b ) ; return gcd ( $ a >> 1 , $ b >> 1 ) << 1 ; } return gcd ( $ a , $ b >> 1 ) ; if ( $ a > $ b ) return gcd ( ( $ a - $ b ) >> 1 , $ b ) ; return gcd ( ( $ b - $ a ) >> 1 , $ a ) ; } $ a = 34 ; $ b = 17 ; echo " Gcd ▁ of ▁ given ▁ numbers ▁ is : ▁ " , gcd ( $ a , $ b ) ; ? >
Print all n | 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 Code
< ? php function findNDigitNumsUtil ( $ n , $ sum , $ out , $ index ) { if ( $ index > $ n $ sum < 0 ) return ; if ( $ index == $ n ) { if ( $ sum == 0 ) { $ out [ $ index ] = ' ' ; foreach ( $ out as & $ value ) print ( $ value ) ; print ( " ▁ " ) ; } return ; } for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { $ out [ $ index ] = chr ( $ i + ord ( '0' ) ) ; findNDigitNumsUtil ( $ n , $ sum - $ i , $ out , $ index + 1 ) ; } } function findNDigitNums ( $ n , $ sum ) { $ out = array_fill ( 0 , $ n + 1 , false ) ; for ( $ i = 1 ; $ i <= 9 ; $ i ++ ) { $ out [ 0 ] = chr ( $ i + ord ( '0' ) ) ; findNDigitNumsUtil ( $ n , $ sum - $ i , $ out , 1 ) ; } } $ n = 2 ; $ sum = 3 ; findNDigitNums ( $ n , $ sum ) ; ? >