codeparrot/xlcost-text-to-code huggingface dataset:
Largest palindromic number in an array | Function to check if n is palindrome ; Find the appropriate divisor to extract the leading digit ; If first and last digits are not same then return false ; Removing the leading and trailing digits from the number ; Reducing divisor by a factor of 2 as 2 digits are dropped ; Function to find the largest palindromic number ; If a palindrome larger than the currentMax is found ; Return the largest palindromic number from the array ; Driver Code ; print required answer
< ? php function isPalindrome ( $ n ) { $ divisor = 1 ; while ( ( int ) ( $ n / $ divisor ) >= 10 ) $ divisor *= 10 ; while ( $ n != 0 ) { $ leading = ( int ) ( $ n / $ divisor ) ; $ trailing = $ n % 10 ; if ( $ leading != $ trailing ) return false ; $ n = ( $ n % $ divisor ) / 10 ; $ divisor = $ divisor / 100 ; } return true ; } function largestPalindrome ( $ A , $ n ) { $ currentMax = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ A [ $ i ] > $ currentMax && isPalindrome ( $ A [ $ i ] ) ) $ currentMax = $ A [ $ i ] ; } return $ currentMax ; } $ A = array ( 1 , 232 , 54545 , 999991 ) ; $ n = sizeof ( $ A ) ; echo ( largestPalindrome ( $ A , $ n ) ) ; ? >
Reduce the array to a single element with the given operation | Function to return the final element ; Driver code
< ? php function getFinalElement ( $ n ) { $ finalNum = 0 ; for ( $ finalNum = 2 ; ( $ finalNum * 2 ) <= $ n ; $ finalNum *= 2 ) ; return $ finalNum ; } $ N = 12 ; echo getFinalElement ( $ N ) ; ? >
Sum of all odd length palindromic numbers within the range [ L , R ] | Function that returns true if the given number is a palindrome ; Here we are generating a new number ( reverse_num ) by reversing the digits of original input number ; If the original input number ( num ) is equal to its reverse ( reverse_num ) then its palindrome else it is not . ; Function that returns true if the given number is of odd length ; Function to return the sum of all odd length palindromic numbers within the given range ; if number is palindrome and of odd length ; Driver code
< ? php function isPalindrome ( $ num ) { $ reverse_num = 0 ; $ remainder ; $ temp ; $ temp = $ num ; while ( $ temp != 0 ) { $ remainder = $ temp % 10 ; $ reverse_num = $ reverse_num * 10 + $ remainder ; $ temp = ( int ) ( $ temp / 10 ) ; } if ( $ reverse_num == $ num ) { return true ; } return false ; } function isOddLength ( $ num ) { $ count = 0 ; while ( $ num > 0 ) { $ num = ( int ) ( $ num / 10 ) ; $ count ++ ; } if ( $ count % 2 != 0 ) { return true ; } return false ; } function sumOfAllPalindrome ( $ L , $ R ) { $ sum = 0 ; if ( $ L <= $ R ) for ( $ i = $ L ; $ i <= $ R ; $ i ++ ) { if ( isPalindrome ( $ i ) && isOddLength ( $ i ) ) { $ sum += $ i ; } } return $ sum ; } $ L = 110 ; $ R = 1130 ; echo sumOfAllPalindrome ( $ L , $ R ) ; ? >
Sum of Fibonacci Numbers with alternate negatives | Computes value of first fibonacci numbers and stores their alternate sum ; Initialize result ; Add remaining terms ; For even terms ; For odd terms ; Return the alternating sum ; Get n ; Find the alternating sum
< ? php function calculateAlternateSum ( $ n ) { if ( $ n <= 0 ) return 0 ; $ fibo = array ( ) ; $ fibo [ 0 ] = 0 ; $ fibo [ 1 ] = 1 ; $ sum = pow ( $ fibo [ 0 ] , 2 ) + pow ( $ fibo [ 1 ] , 2 ) ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ fibo [ $ i ] = $ fibo [ $ i - 1 ] + $ fibo [ $ i - 2 ] ; if ( $ i % 2 == 0 ) $ sum -= $ fibo [ $ i ] ; else $ sum += $ fibo [ $ i ] ; } return $ sum ; } $ n = 8 ; echo ( " Alternating ▁ Fibonacci ▁ Sum ▁ upto ▁ " ) ; echo $ n ; echo " ▁ terms : ▁ " ; echo ( calculateAlternateSum ( $ n ) ) ; ? >
Find nth Term of the Series 1 2 2 4 4 4 4 8 8 8 8 8 8 8 8 ... | Function that will return nth term ; Get n ; Get the value ; Get n ; Get the value
< ? php function getValue ( $ n ) { $ i = 0 ; $ k = 1 ; while ( $ i < $ n ) { $ i = $ i + $ k ; $ k = $ k * 2 ; } return ( int ) $ k / 2 ; } $ n = 9 ; echo getValue ( $ n ) , " STRNEWLINE " ; $ n = 1025 ; echo getValue ( $ n ) , " STRNEWLINE " ; ? >
Construct a frequency array of digits of the values obtained from x ^ 1 , x ^ 2 , ... ... . . , x ^ n | Function that traverses digits in a number and modifies frequency count array ; Array to keep count of digits ; Traversing through x ^ 1 to x ^ n ; For power function , both its parameters are to be in double ; calling countDigits function on x ^ i ; Printing count of digits 0 - 9 ; Driver code
< ? php function countDigits ( $ val , & $ arr ) { while ( $ val > 0 ) { $ digit = $ val % 10 ; $ arr [ ( int ) ( $ digit ) ] += 1 ; $ val = ( int ) ( $ val / 10 ) ; } return ; } function countFrequency ( $ x , $ n ) { $ freq_count = array_fill ( 0 , 10 , 0 ) ; for ( $ i = 1 ; $ i < $ n + 1 ; $ i ++ ) { $ val = pow ( $ x , $ i ) ; countDigits ( $ val , $ freq_count ) ; } for ( $ i = 0 ; $ i < 10 ; $ i ++ ) { echo $ freq_count [ $ i ] . " " ; } } $ x = 15 ; $ n = 3 ; countFrequency ( $ x , $ n ) ? >
Number of values of b such that a = b + ( a ^ b ) | function to return the number of solutions ; check for every possible value ; Driver Code
< ? php function countSolutions ( $ a ) { $ count = 0 ; for ( $ i = 0 ; $ i <= $ a ; $ i ++ ) { if ( $ a == ( $ i + ( $ a ^ $ i ) ) ) $ count ++ ; } return $ count ; } $ a = 3 ; echo countSolutions ( $ a ) ; ? >
Number of values of b such that a = b + ( a ^ b ) | function to return the number of solutions ; Driver Code
< ? php function countSolutions ( $ a ) { $ count = bitCount ( $ a ) ; $ count = ( int ) pow ( 2 , $ count ) ; return $ count ; } function bitCount ( $ n ) { $ count = 0 ; while ( $ n != 0 ) { $ count ++ ; $ n &= ( $ n - 1 ) ; } return $ count ; } $ a = 3 ; echo ( countSolutions ( $ a ) ) ; ? >
Sum of Area of all possible square inside a rectangle | Function to calculate the sum of area of all possible squares that comes inside the rectangle ; Square with max size possible ; calculate total square of a given size ; calculate area of squares of a particular size ; total area ; increment size ; Driver Code
< ? php function calculateAreaSum ( $ l , $ b ) { $ size = 1 ; $ maxSize = min ( $ l , $ b ) ; $ totalArea = 0 ; for ( $ i = 1 ; $ i <= $ maxSize ; $ i ++ ) { $ totalSquares = ( $ l - $ size + 1 ) * ( $ b - $ size + 1 ) ; $ area = $ totalSquares * $ size * $ size ; $ totalArea += $ area ; $ size ++ ; } return $ totalArea ; } $ l = 4 ; $ b = 3 ; echo calculateAreaSum ( $ l , $ b ) ; ? >
Hyperfactorial of a number | function to calculate the value of hyperfactorial ; initialise the val to 1 ; returns the hyperfactorial of a number ; Driver code
< ? php function boost_hyperfactorial ( $ num ) { $ val = 1 ; for ( $ i = 1 ; $ i <= $ num ; $ i ++ ) { $ val = $ val * pow ( $ i , $ i ) ; } return $ val ; } $ num = 5 ; echo boost_hyperfactorial ( $ num ) ; ? >
Hyperfactorial of a number | function to calculate the value of hyperfactorial ; initialise the val to 1 ; 1 ^ 1 * 2 ^ 2 * 3 ^ 3. . . . ; returns the hyperfactorial of a number ; Driver code
< ? php function boost_hyperfactorial ( $ num ) { $ val = 1 ; for ( $ i = 1 ; $ i <= $ num ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ i ; $ j ++ ) { $ val *= $ i ; } } return $ val ; } $ num = 5 ; echo boost_hyperfactorial ( $ num ) ; ? >
Subtract 1 without arithmetic operators | PHP code to subtract one from a given number ; Flip all the set bits until we find a 1 ; flip the rightmost 1 bit ; Driver Code
< ? php function subtractOne ( $ x ) { $ m = 1 ; while ( ! ( $ x & $ m ) ) { $ x = $ x ^ $ m ; $ m <<= 1 ; } $ x = $ x ^ $ m ; return $ x ; } echo subtractOne ( 13 ) ; ? >
Find the mean vector of a Matrix | PHP program to find mean vector of given matrix ; Function to find mean vector ; loop to traverse each column ; to calculate mean of each row ; to store sum of elements of a column ; Driver Code
< ? php $ rows = 3 ; $ cols = 3 ; function meanVector ( $ mat ) { global $ rows , $ cols ; echo " [ ▁ " ; for ( $ i = 0 ; $ i < $ rows ; $ i ++ ) { $ mean = 0.00 ; $ sum = 0 ; for ( $ j = 0 ; $ j < $ cols ; $ j ++ ) $ sum += $ mat [ $ j ] [ $ i ] ; $ mean = $ sum / $ rows ; echo $ mean , " " ; ▁ } ▁ echo ▁ " ] " } $ mat = array ( array ( 1 , 2 , 3 ) , array ( 4 , 5 , 6 ) , array ( 7 , 8 , 9 ) ) ; meanVector ( $ mat ) ; ? >
Hoax Number | Function to find distinct prime factors of given number n ; n is odd at this point , since it is no longer divisible by 2. So we can test only for the odd numbers , whether they are factors of n ; Check if i is prime factor ; This condition is to handle the case when n is a prime number greater than 2 ; Function to calculate sum of digits of distinct prime factors of given number n and sum of digits of number n and compare the sums obtained ; Distinct prime factors of n are being stored in vector pf ; If n is a prime number , it cannot be a hoax number ; Finding sum of digits of distinct prime factors of the number n ; Finding sum of digits in current prime factor pf [ i ] . ; Finding sum of digits of number n ; Comparing the two calculated sums ; Driver Code
< ? php function primeFactors ( $ n ) { $ res = array ( ) ; if ( $ n % 2 == 0 ) { while ( $ n % 2 == 0 ) $ n = ( int ) $ n / 2 ; array_push ( $ res , 2 ) ; } for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i = $ i + 2 ) { if ( $ n % $ i == 0 ) { while ( $ n % $ i == 0 ) $ n = ( int ) $ n / $ i ; array_push ( $ res , $ i ) ; } } if ( $ n > 2 ) array_push ( $ res , $ n ) ; return $ res ; } function isHoax ( $ n ) { $ pf = primeFactors ( $ n ) ; if ( $ pf [ 0 ] == $ n ) return false ; $ all_pf_sum = 0 ; for ( $ i = 0 ; $ i < count ( $ pf ) ; $ i ++ ) { $ pf_sum ; for ( $ pf_sum = 0 ; $ pf [ $ i ] > 0 ; $ pf_sum += $ pf [ $ i ] % 10 , $ pf [ $ i ] /= 10 ) ; $ all_pf_sum += $ pf_sum ; } for ( $ sum_n = 0 ; $ n > 0 ; $ sum_n += $ n % 10 , $ n /= 10 ) ; return $ sum_n == $ all_pf_sum ; } $ n = 84 ; if ( isHoax ( $ n ) ) echo ( " A ▁ Hoax ▁ Number STRNEWLINE " ) ; else echo ( " Not ▁ a ▁ Hoax ▁ Number STRNEWLINE " ) ; ? >
Modular multiplicative inverse from 1 to n | A naive method to find modular multiplicative inverse of ' a ' under modulo ' prime ' ; Driver Program
< ? php function modInverse ( int $ a , int $ prime ) { $ a = $ a % $ prime ; for ( $ x = 1 ; $ x < $ prime ; $ x ++ ) if ( ( $ a * $ x ) % $ prime == 1 ) return $ x ; return -1 ; } function printModIverses ( $ n , $ prime ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) echo modInverse ( $ i , $ prime ) , " ▁ " ; } $ n = 10 ; $ prime = 17 ; printModIverses ( $ n , $ prime ) ; ? >
Convert to number with digits as 3 and 8 only | function for minimum operation ; remainder and operations count ; count digits not equal to 3 or 8 ; Driver Code
< ? php function minOp ( $ num ) { $ count = 0 ; while ( $ num ) { $ rem = intval ( $ num % 10 ) ; if ( ! ( $ rem == 3 $ rem == 8 ) ) $ count ++ ; $ num = intval ( $ num / 10 ) ; } return $ count ; } $ num = 234198 ; echo " Minimum ▁ Operations ▁ = ▁ " . minOp ( $ num ) ; ? >
Biggest integer which has maximum digit sum in range from 1 to n | function to calculate the sum of digits of a number . ; Returns the maximum number with maximum sum of digits . ; initializing b as 1 and initial max sum to be of n ; iterates from right to left in a digit ; while iterating this is the number from from right to left ; calls the function to check if sum of cur is more then of ans ; reduces the number to one unit less ; Driver Code
< ? php function sumOfDigits ( $ a ) { $ sum = 0 ; while ( $ a ) { $ sum += $ a % 10 ; $ a = ( int ) $ a / 10 ; } return $ sum ; } function findMax ( $ x ) { $ b = 1 ; $ ans = $ x ; while ( $ x ) { $ cur = ( $ x - 1 ) * $ b + ( $ b - 1 ) ; if ( sumOfDigits ( $ cur ) > sumOfDigits ( $ ans ) || ( sumOfDigits ( $ cur ) == sumOfDigits ( $ ans ) && $ cur > $ ans ) ) $ ans = $ cur ; $ x = ( int ) $ x / 10 ; $ b *= 10 ; } return $ ans ; } $ n = 521 ; echo findMax ( $ n ) ; ? >
Interquartile Range ( IQR ) | Function to give index of the median ; Function to calculate IQR ; Index of median of entire data ; Median of first half ; Median of second half ; IQR calculation ; Driver Function
< ? php function median ( $ a , $ l , $ r ) { $ n = $ r - $ l + 1 ; $ n = ( int ) ( ( $ n + 1 ) / 2 ) - 1 ; return $ n + $ l ; } function IQR ( $ a , $ n ) { sort ( $ a ) ; $ mid_index = median ( $ a , 0 , $ n ) ; $ Q1 = $ a [ median ( $ a , 0 , $ mid_index ) ] ; $ Q3 = $ a [ $ mid_index + median ( $ a , $ mid_index + 1 , $ n ) ] ; return ( $ Q3 - $ Q1 ) ; } $ a = array ( 1 , 19 , 7 , 6 , 5 , 9 , 12 , 27 , 18 , 2 , 15 ) ; $ n = count ( $ a ) ; echo IQR ( $ a , $ n ) ; ? >
Largest palindromic number in an array | Function to check if n is palindrome ; Find the appropriate divisor to extract the leading digit ; If first and last digits are not same then return false ; Removing the leading and trailing digits from the number ; Reducing divisor by a factor of 2 as 2 digits are dropped ; Function to find the largest palindromic number ; Sort the array ; If number is palindrome ; If no palindromic number found ; Driver Code ; print required answer
< ? php function isPalindrome ( $ n ) { $ divisor = 1 ; while ( ( int ) ( $ n / $ divisor ) >= 10 ) $ divisor *= 10 ; while ( $ n != 0 ) { $ leading = ( int ) ( $ n / $ divisor ) ; $ trailing = $ n % 10 ; if ( $ leading != $ trailing ) return false ; $ n = ( int ) ( ( $ n % $ divisor ) / 10 ) ; $ divisor = ( int ) ( $ divisor / 100 ) ; } return true ; } function largestPalindrome ( $ A , $ n ) { sort ( $ A ) ; for ( $ i = $ n - 1 ; $ i >= 0 ; -- $ i ) { if ( isPalindrome ( $ A [ $ i ] ) ) return $ A [ $ i ] ; } return -1 ; } $ A = array ( 1 , 232 , 54545 , 999991 ) ; $ n = sizeof ( $ A ) ; echo largestPalindrome ( $ A , $ n ) ; ? >
Sum of the multiples of two numbers below N | Function to return the sum of all the integers below N which are multiples of either A or B ; If i is a multiple of a or b ; Driver code
< ? php function findSum ( $ n , $ a , $ b ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ i % $ a == 0 $ i % $ b == 0 ) $ sum += $ i ; return $ sum ; } $ n = 10 ; $ a = 3 ; $ b = 5 ; echo findSum ( $ n , $ a , $ b ) ; ? >
Subtract 1 without arithmetic operators | Driver code
< ? php function subtractOne ( $ x ) { return ( ( $ x << 1 ) + ( ~ $ x ) ) ; } print ( subtractOne ( 13 ) ) ; ? >
Pell Number | calculate nth pell number ; Driver Code
< ? php function pell ( $ n ) { if ( $ n <= 2 ) return $ n ; return 2 * pell ( $ n - 1 ) + pell ( $ n - 2 ) ; } $ n = 4 ; echo ( pell ( $ n ) ) ; ? >
Finding LCM of more than two ( or array ) numbers without using GCD | Returns LCM of arr [ 0. . n - 1 ] ; Find the maximum value in arr [ ] ; Initialize result ; Find all factors that are present in two or more array elements . $x = 2 ; Current factor . ; To store indexes of all array elements that are divisible by x . ; If there are 2 or more array elements that are divisible by x . ; Reduce all array elements divisible by x . ; Then multiply all reduced array elements ; Driver code
< ? php function LCM ( $ arr , $ n ) { $ max_num = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ max_num < $ arr [ $ i ] ) $ max_num = $ arr [ $ i ] ; $ res = 1 ; while ( $ x <= $ max_num ) { $ indexes = array ( ) ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( $ arr [ $ j ] % $ x == 0 ) array_push ( $ indexes , $ j ) ; if ( count ( $ indexes ) >= 2 ) { for ( $ j = 0 ; $ j < count ( $ indexes ) ; $ j ++ ) $ arr [ $ indexes [ $ j ] ] = ( int ) ( $ arr [ $ indexes [ $ j ] ] / $ x ) ; $ res = $ res * $ x ; } else $ x ++ ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ res = $ res * $ arr [ $ i ] ; return $ res ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 , 10 , 20 , 35 ) ; $ n = count ( $ arr ) ; echo LCM ( $ arr , $ n ) . " STRNEWLINE " ; ? >
Program for Goldbachâ €™ s Conjecture ( Two Primes with given Sum ) | PHP program to implement Goldbach 's conjecture ; 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 * i * j 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 . ; Function to perform Goldbach 's conjecture ; Return if number is not even or less than 3 ; Check only upto half of number ; find difference by subtracting current prime from n ; Search if the difference is also a prime number ; Express as a sum of primes ; Finding all prime numbers before limit ; Express number as a sum of two primes
< ? php $ MAX = 10000 ; $ primes = array ( ) ; function sieveSundaram ( ) { global $ MAX , $ primes ; $ marked = array_fill ( 0 , ( int ) ( $ 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 findPrimes ( $ n ) { global $ MAX , $ primes ; if ( $ n <= 2 $ n % 2 != 0 ) { print ( " Invalid ▁ Input ▁ STRNEWLINE " ) ; return ; } for ( $ i = 0 ; $ primes [ $ i ] <= $ n / 2 ; $ i ++ ) { $ diff = $ n - $ primes [ $ i ] ; if ( in_array ( $ diff , $ primes ) ) { print ( $ primes [ $ i ] . " + " ▁ . ▁ $ diff ▁ . ▁ " = " ▁ . ▁ $ n ▁ . ▁ " " return ; } } } sieveSundaram ( ) ; findPrimes ( 4 ) ; findPrimes ( 38 ) ; findPrimes ( 100 ) ; ? >
k | A function to generate prime factors of a given number n and return k - th prime factor ; Find the number of 2 's  that divide k ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , store i and divide n ; This condition is to handle the case where n is a prime number greater than 2 ; Driver Code
< ? php function kPrimeFactor ( $ n , $ k ) { while ( $ n % 2 == 0 ) { $ k -- ; $ n = $ n / 2 ; if ( $ k == 0 ) return 2 ; } for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i = $ i + 2 ) { while ( $ n % $ i == 0 ) { if ( $ k == 1 ) return $ i ; $ k -- ; $ n = $ n / $ i ; } } if ( $ n > 2 && $ k == 1 ) return $ n ; return -1 ; } { $ n = 12 ; $ k = 3 ; echo kPrimeFactor ( $ n , $ k ) , " STRNEWLINE " ; $ n = 14 ; $ k = 3 ; echo kPrimeFactor ( $ n , $ k ) ; return 0 ; } ? >
k | PHP program to find k - th prime factor using Sieve Of Eratosthenes . This program is efficient when we have a range of numbers . ; Using SieveOfEratosthenes to find smallest prime factor of all the numbers . For example , if MAX is 10 , s [ 2 ] = s [ 4 ] = s [ 6 ] = s [ 10 ] = 2 s [ 3 ] = s [ 9 ] = 3 s [ 5 ] = 5 s [ 7 ] = 7 ; Create a boolean array " prime [ 0 . . MAX ] " and initialize all entries in it as false . ; Initializing smallest factor equal to 2 for all the even numbers ; For odd numbers less then equal to n ; s ( i ) for a prime is the number itself ; For all multiples of current prime number ; i is the smallest prime factor for number " i * j " . ; Function to generate prime factors and return its k - th prime factor . s [ i ] stores least prime factor of i . ; Keep dividing n by least prime factor while either n is not 1 or count of prime factors is not k . ; To keep track of count of prime factors ; Divide n to find next prime factor ; s [ i ] is going to store prime factor of i .
< ? php $ MAX = 10001 ; function sieveOfEratosthenes ( & $ s ) { global $ MAX ; $ prime = array_fill ( 0 , $ MAX + 1 , false ) ; for ( $ i = 2 ; $ i <= $ MAX ; $ i += 2 ) $ s [ $ i ] = 2 ; for ( $ i = 3 ; $ i <= $ MAX ; $ i += 2 ) { if ( $ prime [ $ i ] == false ) { $ s [ $ i ] = $ i ; for ( $ j = $ i ; $ j * $ i <= $ MAX ; $ j += 2 ) { if ( $ prime [ $ i * $ j ] == false ) { $ prime [ $ i * $ j ] = true ; $ s [ $ i * $ j ] = $ i ; } } } } } function kPrimeFactor ( $ n , $ k , $ s ) { while ( $ n > 1 ) { if ( $ k == 1 ) return $ s [ $ n ] ; $ k -- ; $ n = ( int ) ( $ n / $ s [ $ n ] ) ; } return -1 ; } $ s = array_fill ( 0 , $ MAX + 1 , -1 ) ; sieveOfEratosthenes ( $ s ) ; $ n = 12 ; $ k = 3 ; print ( kPrimeFactor ( $ n , $ k , $ s ) . " " ) ; $ n = 14 ; $ k = 3 ; print ( kPrimeFactor ( $ n , $ k , $ s ) ) ; ? >
Euler 's criterion (Check if square root under modulo p exists) | Returns true if square root of n under modulo p exists ; One by one check all numbers from 2 to p - 1 ; Driver Code
< ? php function squareRootExists ( $ n , $ p ) { $ n = $ n % $ p ; for ( $ x = 2 ; $ x < $ p ; $ x ++ ) if ( ( $ x * $ x ) % $ p == $ n ) return true ; return false ; } $ p = 7 ; $ n = 2 ; if ( squareRootExists ( $ n , $ p ) == true ) echo " Yes " ; else echo " No " ; ? >
Legendre 's formula (Given p and n, find the largest x such that p^x divides n!) | Returns largest power of p that divides n ! ; Initialize result ; Calculate x = n / p + n / ( p ^ 2 ) + n / ( p ^ 3 ) + ... . ; Driver Code
< ? php function largestPower ( $ n , $ p ) { $ x = 0 ; while ( $ n ) { $ n = ( int ) $ n / $ p ; $ x += $ n ; } return floor ( $ x ) ; } $ n = 10 ; $ p = 3 ; echo " The ▁ largest ▁ power ▁ of ▁ " , $ p ; echo " ▁ that ▁ divides ▁ " , $ n , " ! ▁ is ▁ " ; echo largestPower ( $ n , $ p ) ; ? >
Program for factorial of a number | PHP program to find factorial of given number ; single line to find factorial ; Driver Code
< ? php function factorial ( $ n ) { return ( $ n == 1 $ n == 0 ) ? 1 : $ n * factorial ( $ n - 1 ) ; } $ num = 5 ; echo " Factorial ▁ of ▁ " , $ num , " ▁ is ▁ " , factorial ( $ num ) ; ? >
Reverse actual bits of the given number | function to reverse bits of a number ; traversing bits of ' n ' from the right ; bitwise left shift ' rev ' by 1 ; if current bit is '1' ; bitwise right shift ' n ' by 1 ; required number ; Driver code
< ? php function reverseBits ( $ n ) { $ rev = 0 ; while ( $ n > 0 ) { $ rev <<= 1 ; if ( $ n & 1 == 1 ) $ rev ^= 1 ; $ n >>= 1 ; } return $ rev ; } $ n = 11 ; echo reverseBits ( $ n ) ; ? >
Ways to split array into two groups of same XOR value | Return the count number of ways to split array into two groups such that each grouphas equal XOR value . ; We can split only if XOR is 0. Since XOR of all is 0 , we can consider all subsets as one group . ; Driver Code
< ? php function countgroup ( $ a , $ n ) { $ xs = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ xs = $ xs ^ $ a [ $ i ] ; if ( $ xs == 0 ) return ( 1 << ( $ n - 1 ) ) - 1 ; return 0 ; } $ a = array ( 1 , 2 , 3 ) ; $ n = count ( $ a ) ; echo countgroup ( $ a , $ n ) ; ? >
Extract ' k ' bits from a given position in a number . | Function to extract k bits from p position and returns the extracted value as integer ; Driver Code
< ? php function bitExtracted ( $ number , $ k , $ p ) { return ( ( ( 1 << $ k ) - 1 ) & ( $ number >> ( $ p - 1 ) ) ) ; } $ number = 171 ; $ k = 5 ; $ p = 2 ; echo " The ▁ extracted ▁ number ▁ is ▁ " , bitExtracted ( $ number , $ k , $ p ) ; ? >
Efficiently check whether n is a multiple of 4 or not | function to check whether ' n ' is a multiple of 4 or not ; if true , then ' n ' is a multiple of 4 ; else ' n ' is not a multiple of 4 ; Driver Code
< ? php function isAMultipleOf4 ( $ n ) { if ( ( $ n & 3 ) == 0 ) return " Yes " ; return " No " ; } $ n = 16 ; echo isAMultipleOf4 ( $ n ) ; ? >
Calculate square of a number without using * , / and pow ( ) | PHP implementation to calculate square without using * and pow ( ) ; handle negative input ; Initialize result ; Add n to res n - 1 times ; Driver Code
< ? php function square ( $ n ) { if ( $ n < 0 ) $ n = - $ n ; $ res = $ n ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ res += $ n ; return $ res ; } for ( $ n = 1 ; $ n <= 5 ; $ n ++ ) echo " n = " , ▁ $ n , ▁ " , " , ▁ " n ^ 2 = " , square ( $ n ) , " STRNEWLINE ▁ " ; ? >
Find a point that lies inside exactly K given squares | PHP implementation of the approach ; Driver Code
< ? php function PointInKSquares ( $ n , $ a , $ k ) { sort ( $ a ) ; return $ a [ $ n - $ k ] ; } $ k = 2 ; $ a = array ( 1 , 2 , 3 , 4 ) ; $ n = sizeof ( $ a ) ; $ x = PointInKSquares ( $ n , $ a , $ k ) ; echo " ( " . $ x . " , " ▁ . ▁ $ x ▁ . ▁ " ) " ; ? >