codeparrot/xlcost-text-to-code huggingface dataset:
Centrosymmetric Matrix | PHP Program to check whether given matrix is centrosymmetric or not . ; Finding the middle row of the matrix ; for each row upto middle row . ; If each element and its corresponding element is not equal then return false . ; Driver Code
< ? php function checkCentrosymmetricted ( $ n , $ m ) { $ mid_row ; if ( $ n & 1 ) $ mid_row = $ n / 2 + 1 ; else $ mid_row = $ n / 2 ; for ( $ i = 0 ; $ i < $ mid_row ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ m [ $ i ] [ $ j ] != $ m [ $ n - $ i - 1 ] [ $ n - $ j - 1 ] ) return false ; } } return true ; } $ n = 3 ; $ m = array ( array ( 1 , 3 , 5 ) , array ( 6 , 8 , 6 ) , array ( 5 , 3 , 1 ) ) ; if ( checkCentrosymmetricted ( $ n , $ m ) ) echo " Yes " ; else echo " No " ; ? >
Centered triangular number | function for Centered Triangular number ; formula for find Centered Triangular number nth term ; For 3 rd Centered Triangular number ; For 12 th Centered Triangular number
< ? php function Centered_Triangular_num ( $ n ) { return ( 3 * $ n * $ n + 3 * $ n + 2 ) / 2 ; } $ n = 3 ; echo Centered_Triangular_num ( $ n ) , " STRNEWLINE " ; $ n = 12 ; echo Centered_Triangular_num ( $ n ) , " STRNEWLINE " ; ? >
Combinatorics on ordered trees | Function returns value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to calculate the number of trees with exactly k leaves . ; Function to calculate total number of nodes of degree d in these trees . ; Function to calculate the number of trees in which the root has degree r . ; Driver program to test above functions Number of trees having 3 edges and exactly 2 leaves ; Number of nodes of degree 3 in a tree having 4 edges ; Number of trees having 3 edges where root has degree 2
< ? php function binomialCoeff ( $ n , $ k ) { $ C = array ( array ( ) ) ; $ i ; $ j ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= min ( $ i , $ k ) ; $ j ++ ) { if ( $ j == 0 or $ j == $ i ) $ C [ $ i ] [ $ j ] = 1 ; else $ C [ $ i ] [ $ j ] = $ C [ $ i - 1 ] [ $ j - 1 ] + $ C [ $ i - 1 ] [ $ j ] ; } } return $ C [ $ n ] [ $ k ] ; } function k_Leaves ( $ n , $ k ) { $ ans = ( binomialCoeff ( $ n , $ k ) * binomialCoeff ( $ n , $ k - 1 ) ) / $ n ; echo " Number ▁ of ▁ trees ▁ having ▁ 4 ▁ edges ▁ and ▁ " , " exactly ▁ 2 ▁ leaves ▁ : ▁ " , $ ans , " STRNEWLINE " ; return 0 ; } function numberOfNodes ( $ n , $ d ) { $ ans = binomialCoeff ( 2 * $ n - 1 - $ d , $ n - 1 ) ; echo " Number ▁ of ▁ nodes ▁ of ▁ degree ▁ 1 ▁ in " , " ▁ a ▁ tree ▁ having ▁ 4 ▁ edges ▁ : ▁ " , $ ans , " STRNEWLINE " ; return 0 ; } function rootDegreeR ( $ n , $ r ) { $ ans = $ r * binomialCoeff ( 2 * $ n - 1 - $ r , $ n - 1 ) ; $ ans = $ ans / $ n ; echo " Number ▁ of ▁ trees ▁ having ▁ 4 ▁ edges " , " ▁ where ▁ root ▁ has ▁ degree ▁ 2 ▁ : ▁ " , $ ans ; return 0 ; } k_Leaves ( 3 , 2 ) ; numberOfNodes ( 3 , 1 ) ; rootDegreeR ( 3 , 2 ) ; ? >
Repeated Unit Divisibility | To find least value of k ; To check n is coprime or not ; to store R ( k ) mod n and 10 ^ k mod n value ; Driver Code
< ? php function repUnitValue ( $ n ) { if ( $ n % 2 == 0 $ n % 5 == 0 ) return 0 ; $ rem = 1 ; $ power = 1 ; $ k = 1 ; while ( $ rem % $ n != 0 ) { $ k ++ ; $ power = $ power * 10 % $ n ; $ rem = ( $ rem + $ power ) % $ n ; } return $ k ; } $ n = 13 ; echo repUnitValue ( $ n ) ; ? >
First N natural can be divided into two sets with given difference and co | PHP code to determine whether numbers 1 to N can be divided into two sets such that absolute difference between sum of these two sets is M and these two sum are co - prime ; function that returns boolean value on the basis of whether it is possible to divide 1 to N numbers into two sets that satisfy given conditions . ; initializing total sum of 1 to n numbers ; since ( 1 ) total_sum = sum_s1 + sum_s2 and ( 2 ) m = sum_s1 - sum_s2 assuming sum_s1 > sum_s2 . solving these 2 equations to get sum_s1 and sum_s2 ; total_sum = sum_s1 + sum_s2 and therefore ; if total sum is less than the absolute difference then there is no way we can split n numbers into two sets so return false ; check if these two sums are integers and they add up to total sum and also if their absolute difference is m . ; Now if two sum are co - prime then return true , else return false . ; if two sums don 't add up to total  sum or if their absolute difference  is not m, then there is no way to  split n numbers, hence return false ; Driver code ; function call to determine answer
< ? php function __gcd ( $ a , $ b ) { return $ b == 0 ? $ a : __gcd ( $ b , $ a % $ b ) ; } function isSplittable ( $ n , $ m ) { $ total_sum = ( int ) ( ( $ n * ( $ n + 1 ) ) / 2 ) ; $ sum_s1 = ( int ) ( ( $ total_sum + $ m ) / 2 ) ; $ sum_s2 = $ total_sum - $ sum_s1 ; if ( $ total_sum < $ m ) return false ; if ( $ sum_s1 + $ sum_s2 == $ total_sum && $ sum_s1 - $ sum_s2 == $ m ) return ( __gcd ( $ sum_s1 , $ sum_s2 ) == 1 ) ; return false ; } $ n = 5 ; $ m = 7 ; if ( isSplittable ( $ n , $ m ) ) echo " Yes " ; else echo " No " ;
Making zero array by decrementing pairs of adjacent | PHP implementation of the above approach ; converting array element into number ; Check if divisible by 11 ; Driver Code
< ? php function isPossibleToZero ( $ a , $ n ) { $ num = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ num = $ num * 10 + $ a [ $ i ] ; return ( $ num % 11 == 0 ) ; } $ arr = array ( 0 , 1 , 1 , 0 ) ; $ n = sizeof ( $ arr ) ; if ( isPossibleToZero ( $ arr , $ n ) ) echo " YES " ; else echo " NO " ;
Blum Integer | Function to cheek if number is Blum Integer ; to store prime numbers from 2 to n ; If prime [ i ] is not changed , then it is a prime ; Update all multiples of p ; to check if the given odd integer is Blum Integer or not ; checking the factors are of 4 t + 3 form or not ; give odd integer greater than 20
< ? php function isBlumInteger ( $ n ) { $ prime = array_fill ( 0 , $ n + 1 , true ) ; for ( $ i = 2 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ prime [ $ i ] == true ) { for ( $ j = $ i * 2 ; $ j <= $ n ; $ j += $ i ) $ prime [ $ j ] = false ; } } for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { if ( $ prime [ $ i ] ) { if ( ( $ n % $ i == 0 ) && ( ( $ i - 3 ) % 4 ) == 0 ) { $ q = ( int ) $ n / $ i ; return ( $ q != $ i && $ prime [ $ q ] && ( $ q - 3 ) % 4 == 0 ) ; } } } return false ; } $ n = 249 ; if ( isBlumInteger ( $ n ) ) echo " Yes " ; else echo " No " ; ? >
Program to calculate value of nCr | PHP program To calculate the Value Of nCr ; Returns factorial of n ; Driver code
< ? php function nCr ( $ n , $ r ) { return fact ( $ n ) / ( fact ( $ r ) * fact ( $ n - $ r ) ) ; } function fact ( $ n ) { $ res = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res = $ res * $ i ; return $ res ; } $ n = 5 ; $ r = 3 ; echo nCr ( $ n , $ r ) ; ? >
Program to print the sum of the given nth term | function to calculate sum of series ; Sum of n terms is n ^ 2 ; Driver Code
< ? php function summingSeries ( $ n ) { return pow ( $ n , 2 ) ; } $ n = 100 ; echo " The ▁ sum ▁ of ▁ n ▁ term ▁ is : ▁ " , summingSeries ( $ n ) ; ? >
Brahmagupta Fibonacci Identity | PHP code to verify Brahmagupta Fibonacci identity ; represent the product as sum of 2 squares ; check identity criteria ; 1 ^ 2 + 2 ^ 2 ; 3 ^ 2 + 4 ^ 2 ; express product of sum of 2 squares as sum of ( sumof 2 squares )
< ? php function find_sum_of_two_squares ( $ a , $ b ) { $ ab = $ a * $ b ; for ( $ i = 0 ; $ i * $ i <= $ ab ; $ i ++ ) { for ( $ j = $ i ; $ i * $ i + $ j * $ j <= $ ab ; $ j ++ ) { if ( $ i * $ i + $ j * $ j == $ ab ) echo $ i , " ^ 2 ▁ + ▁ " , $ j , " ^ 2 ▁ = ▁ " , $ ab , " STRNEWLINE " ; } } } $ a = 1 * 1 + 2 * 2 ; $ b = 3 * 3 + 4 * 4 ; echo " Representation ▁ of ▁ a ▁ * ▁ b ▁ " . " as ▁ sum ▁ of ▁ 2 ▁ squares : STRNEWLINE " ; find_sum_of_two_squares ( $ a , $ b ) ; ? >
Tetrahedral Numbers | Function to find Tetrahedral Number ; Driver Code
< ? php function tetrahedralNumber ( $ n ) { return ( $ n * ( $ n + 1 ) * ( $ n + 2 ) ) / 6 ; } $ n = 5 ; echo tetrahedralNumber ( $ n ) ; ? >
Euler 's Four Square Identity | function to check euler four square identity ; loops checking the sum of squares ; sum of 2 squares ; sum of 3 squares ; sum of 4 squares ; product of 2 numbers represented as sum of four squares i , j , k , l ; product of 2 numbers a and b represented as sum of four squares i , j , k , l ; given numbers can be represented as sum of 4 squares By euler 's four square identity product also can be represented as sum of 4 squares
< ? php function check_euler_four_square_identity ( $ a , $ b , $ ab ) { $ s = 0 ; for ( $ i = 0 ; $ i * $ i <= $ ab ; $ i ++ ) { $ s = $ i * $ i ; for ( $ j = $ i ; $ j * $ j <= $ ab ; $ j ++ ) { $ s = $ j * $ j + $ i * $ i ; for ( $ k = $ j ; $ k * $ k <= $ ab ; $ k ++ ) { $ s = $ k * $ k + $ j * $ j + $ i * $ i ; for ( $ l = $ k ; $ l * $ l <= $ ab ; $ l ++ ) { $ s = $ l * $ l + $ k * $ k + $ j * $ j + $ i * $ i ; if ( $ s == $ ab ) { echo ( " i ▁ = ▁ " . $ i . " STRNEWLINE " ) ; echo ( " j ▁ = ▁ " . $ j . " STRNEWLINE " ) ; echo ( " k ▁ = ▁ " . $ k . " STRNEWLINE " ) ; echo ( " l ▁ = ▁ " . $ l . " STRNEWLINE " ) ; echo " " . " Product ▁ of ▁ " . $ a . " ▁ and ▁ " . $ b ; echo " ▁ can ▁ be ▁ written " . " ▁ as ▁ sum ▁ of ▁ squares ▁ of ▁ i , ▁ " . " j , ▁ k , ▁ l STRNEWLINE " ; echo $ ab . " ▁ = ▁ " ; echo $ i . " * " ▁ . ▁ $ i . ▁ " + " echo $ j . " * " . $ j . " ▁ + ▁ " ; echo $ k . " * " ▁ . ▁ $ k ▁ . ▁ " + " echo $ l . " * " . $ l . " STRNEWLINE " ; echo " STRNEWLINE " ; } } } } } } $ ab = $ a * $ b ; check_euler_four_square_identity ( $ a , $ b , $ ab ) ; ? >
Number of solutions to Modular Equations | Returns the number of divisors of ( A - B ) greater than B ; if N is divisible by i ; count only the divisors greater than B ; checking if a divisor isnt counted twice ; Utility function to calculate number of all possible values of X for which the modular equation holds true ; if A = B there are infinitely many solutions to equation or we say X can take infinitely many values > A . We return - 1 in this case ; if A < B , there are no possible values of X satisfying the equation ; the last case is when A > B , here we calculate the number of divisors of ( A - B ) , which are greater than B ; Wrapper function for numberOfPossibleWaysUtil ( ) ; if infinitely many solutions available ; Driver code
< ? php function calculateDivisors ( $ A , $ B ) { $ N = ( $ A - $ B ) ; $ noOfDivisors = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ N ) ; $ i ++ ) { if ( ( $ N % $ i ) == 0 ) { if ( $ i > $ B ) $ noOfDivisors ++ ; if ( ( $ N / $ i ) != $ i && ( $ N / $ i ) > $ B ) $ noOfDivisors ++ ; } } return $ noOfDivisors ; } function numberOfPossibleWaysUtil ( $ A , $ B ) { if ( $ A == $ B ) return -1 ; if ( $ A < $ B ) return 0 ; $ noOfDivisors = 0 ; $ noOfDivisors = calculateDivisors ( $ A , $ B ) ; return $ noOfDivisors ; } function numberOfPossibleWays ( $ A , $ B ) { $ noOfSolutions = numberOfPossibleWaysUtil ( $ A , $ B ) ; if ( $ noOfSolutions == -1 ) { echo " For A = " ▁ , ▁ $ A , ▁ " and B = " ▁ , $ B , STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL " X can take Infinitely many values greater than " ▁ , ▁ $ A ▁ , ▁ " " ; STRNEWLINE TABSYMBOL } STRNEWLINE TABSYMBOL else ▁ { STRNEWLINE TABSYMBOL TABSYMBOL echo ▁ " For A = " , ▁ $ A ▁ , ▁ " and B = " ▁ , $ B , STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL " X can take " , $ noOfSolutions , STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL " values " } } $ A = 26 ; $ B = 2 ; numberOfPossibleWays ( $ A , $ B ) ; $ A = 21 ; $ B = 5 ; numberOfPossibleWays ( $ A , $ B ) ;
Perfect power ( 1 , 4 , 8 , 9 , 16 , 25 , 27 , ... ) | Function that keeps all the odd power numbers upto n ; We need exclude perfect squares . ; sort the vector ; Return sum of odd and even powers . ; Driver Code
< ? php function powerNumbers ( $ n ) { $ v = array ( ) ; for ( $ i = 2 ; $ i * $ i * $ i <= $ n ; $ i ++ ) { $ j = $ i * $ i ; while ( $ j * $ i <= $ n ) { $ j *= $ i ; $ s = sqrt ( $ j ) ; if ( $ s * $ s != $ j ) array_push ( $ v , $ j ) ; } } sort ( $ v ) ; $ uni = array_unique ( $ v ) ; for ( $ i = 0 ; $ i < count ( $ uni ) ; $ i ++ ) { $ key = array_search ( $ uni [ $ i ] , $ v ) ; unset ( $ v [ $ key ] ) ; } return count ( $ v ) + 3 + intval ( sqrt ( $ n ) ) ; } echo ( powerNumbers ( 50 ) ) ; ? >
Variance and standard | variance function declaration Function for calculating mean ; Calculating sum ; Returning mean ; Function for calculating variance ; subtracting mean from elements ; a [ i ] [ j ] = fabs ( a [ i ] [ j ] ) ; squaring each terms ; taking sum ; declaring and initializing matrix ; for mean ; for variance ; for standard deviation ; displaying variance and deviation
< ? php function mean ( $ a , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ sum += $ a [ $ i ] [ $ j ] ; return floor ( ( int ) $ sum / ( $ n * $ n ) ) ; } function variance ( $ a , $ n , $ m ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ a [ $ i ] [ $ j ] -= $ m ; $ a [ $ i ] [ $ j ] *= $ a [ $ i ] [ $ j ] ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ sum += $ a [ $ i ] [ $ j ] ; return floor ( ( int ) $ sum / ( $ n * $ n ) ) ; } $ mat = array ( array ( 1 , 2 , 3 ) , array ( 4 , 5 , 6 ) , array ( 7 , 8 , 9 ) ) ; $ m = mean ( $ mat , 3 ) ; $ var = variance ( $ mat , 3 , $ m ) ; $ dev = sqrt ( $ var ) ; echo " Mean : " ▁ , ▁ $ m ▁ , ▁ " " , STRNEWLINE TABSYMBOL " Variance : " ▁ , ▁ $ var ▁ , STRNEWLINE TABSYMBOL " " , ▁ " Deviation : " floor ( $ dev ) , " STRNEWLINE " ; ? >
Find N Arithmetic Means between A and B | Prints N arithmetic means between A and B . ; calculate common difference ( d ) ; for finding N the arithmetic mean between A and B ; Driver Code
< ? php function printAMeans ( $ A , $ B , $ N ) { $ d = ( $ B - $ A ) / ( $ N + 1 ) ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) echo ( $ A + $ i * $ d ) , " ▁ " ; } $ A = 20 ; $ B = 32 ; $ N = 5 ; printAMeans ( $ A , $ B , $ N ) ; ? >
Sum of the series 1.2 . 3 + 2.3 . 4 + ... + n ( n + 1 ) ( n + 2 ) | function to calculate sum of series ; Driver Code
< ? php function sumofseries ( $ n ) { return ( $ n * ( $ n + 1 ) * ( $ n + 2 ) * ( $ n + 3 ) / 4 ) ; } echo sumofseries ( 3 ) ; ? >
Largest number in [ 2 , 3 , . . n ] which is co | Returns true if i is co - prime with numbers in set [ 2 , 3 , ... m ] ; Running the loop till square root of n to reduce the time complexity from n ; Find the minimum of square root of n and m to run the loop until the smaller one ; Check from 2 to min ( m , sqrt ( n ) ) ; Function to find the largest number less than n which is Co - prime with all numbers from 2 to m ; Iterating from n to m + 1 to find the number ; checking every number for the given conditions ; The first number which satisfy the conditions is the answer ; If there is no number which satisfy the conditions , then print number does not exist . ; Driver Code
< ? php function isValid ( $ i , $ m ) { $ sq_i = sqrt ( $ i ) ; $ sq = min ( $ m , $ sq_i ) ; for ( $ j = 2 ; $ j <= $ sq ; $ j ++ ) if ( $ i % $ j == 0 ) return false ; return true ; } function findLargestNum ( $ n , $ m ) { for ( $ i = $ n ; $ i > $ m ; $ i -- ) { if ( isValid ( $ i , $ m ) ) { echo $ i , " STRNEWLINE " ; return ; } } echo " Number ▁ Doesn ' t ▁ Exists STRNEWLINE " ; } $ n = 16 ; $ m = 3 ; findLargestNum ( $ n , $ m ) ; ? >
Check whether a given matrix is orthogonal or not | Function to check orthogonalilty ; Find transpose ; Find product of a [ ] [ ] and its transpose ; Since we are multiplying with transpose of itself . We use ; Check if product is identity matrix ; Driver Code
< ? php function isOrthogonal ( $ a , $ m , $ n ) { if ( $ m != $ n ) return false ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ trans [ $ i ] [ $ j ] = $ a [ $ j ] [ $ i ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ sum = 0 ; for ( $ k = 0 ; $ k < $ n ; $ k ++ ) { $ sum = $ sum + ( $ a [ $ i ] [ $ k ] * $ a [ $ j ] [ $ k ] ) ; } $ prod [ $ i ] [ $ j ] = $ sum ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ i != $ j && $ prod [ $ i ] [ $ j ] != 0 ) return false ; if ( $ i == $ j && $ prod [ $ i ] [ $ j ] != 1 ) return false ; } } return true ; } $ a = array ( array ( 1 , 0 , 0 ) , array ( 0 , 1 , 0 ) , array ( 0 , 0 , 1 ) ) ; if ( isOrthogonal ( $ a , 3 , 3 ) ) echo " Yes " ; else echo " No " ; ? >
Check if given number is perfect square | PHP program to find if x is a perfect square . ; Find floating point value of square root of x . ; If square root is an integer ; Driver code
< ? php function isPerfectSquare ( $ x ) { $ sr = sqrt ( $ x ) ; return ( ( $ sr - floor ( $ sr ) ) == 0 ) ; } $ x = 2502 ; if ( isPerfectSquare ( $ x ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >
Program to print GP ( Geometric Progression ) | function to print GP ; starting number ; Common ratio ; N th term to be find
< ? php function printGP ( $ a , $ r , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ curr_term = $ a * pow ( $ r , $ i ) ; echo $ curr_term , " " ; } } $ a = 2 ; $ r = 3 ; $ n = 5 ; printGP ( $ a , $ r , $ n ) ; ? >
Space efficient iterative method to Fibonacci number | get second MSB ; consectutively set all the bits ; returns the second MSB ; Multiply function ; Function to calculate F [ ] [ ] raise to the power n ; Base case ; take 2D array to store number 's ; run loop till MSB > 0 ; To return fibonacci number ; Given n
< ? php function getMSB ( $ n ) { $ n |= $ n >> 1 ; $ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ; return ( ( $ n + 1 ) >> 2 ) ; } function multiply ( & $ F , & $ M ) { $ x = $ F [ 0 ] [ 0 ] * $ M [ 0 ] [ 0 ] + $ F [ 0 ] [ 1 ] * $ M [ 1 ] [ 0 ] ; $ y = $ F [ 0 ] [ 0 ] * $ M [ 0 ] [ 1 ] + $ F [ 0 ] [ 1 ] * $ M [ 1 ] [ 1 ] ; $ z = $ F [ 1 ] [ 0 ] * $ M [ 0 ] [ 0 ] + $ F [ 1 ] [ 1 ] * $ M [ 1 ] [ 0 ] ; $ w = $ F [ 1 ] [ 0 ] * $ M [ 0 ] [ 1 ] + $ F [ 1 ] [ 1 ] * $ M [ 1 ] [ 1 ] ; $ F [ 0 ] [ 0 ] = $ x ; $ F [ 0 ] [ 1 ] = $ y ; $ F [ 1 ] [ 0 ] = $ z ; $ F [ 1 ] [ 1 ] = $ w ; } function power ( & $ F , $ n ) { if ( $ n == 0 $ n == 1 ) return ; $ M = array ( array ( 1 , 1 ) , array ( 1 , 0 ) ) ; for ( $ m = getMSB ( $ n ) ; $ m ; $ m = $ m >> 1 ) { multiply ( $ F , $ F ) ; if ( $ n & $ m ) { multiply ( $ F , $ M ) ; } } } function fib ( $ n ) { $ F = array ( array ( 1 , 1 ) , array ( 1 , 0 ) ) ; if ( $ n == 0 ) return 0 ; power ( $ F , $ n - 1 ) ; return $ F [ 0 ] [ 0 ] ; } $ n = 6 ; echo fib ( $ n ) . " " ; ? >
Stern | PHP program to print Brocot Sequence ; loop to create sequence ; adding sum of considered element and it 's precedent ; adding next considered element ; printing sequence . . ; Driver code ; adding first two element in the sequence
< ? php function SternSequenceFunc ( & $ BrocotSequence , $ n ) { for ( $ i = 1 ; count ( $ BrocotSequence ) < $ n ; $ i ++ ) { $ considered_element = $ BrocotSequence [ $ i ] ; $ precedent = $ BrocotSequence [ $ i - 1 ] ; array_push ( $ BrocotSequence , $ considered_element + $ precedent ) ; array_push ( $ BrocotSequence , $ considered_element ) ; } for ( $ i = 0 ; $ i < 15 ; ++ $ i ) echo ( $ BrocotSequence [ $ i ] . " ▁ " ) ; } $ n = 15 ; $ BrocotSequence = array ( ) ; array_push ( $ BrocotSequence , 1 ) ; array_push ( $ BrocotSequence , 1 ) ; SternSequenceFunc ( $ BrocotSequence , $ n ) ; ? >
Counting numbers whose difference from reverse is a product of k | PHP program to Count the numbers within a given range in which when you subtract a number from its reverse , the difference is a product of k ; function to check if the number and its reverse have their absolute difference divisible by k ; reverse the number ; Driver code
< ? php function isRevDiffDivisible ( $ x , $ k ) { $ n = $ x ; $ m = 0 ; $ flag ; while ( $ x > 0 ) { $ m = $ m * 10 + $ x % 10 ; $ x = ( int ) $ x / 10 ; } return ( abs ( $ n - $ m ) % $ k == 0 ) ; } function countNumbers ( $ l , $ r , $ k ) { $ count = 0 ; for ( $ i = $ l ; $ i <= $ r ; $ i ++ ) if ( isRevDiffDivisible ( $ i , $ k ) ) ++ $ count ; return $ count ; } $ l = 20 ; $ r = 23 ; $ k = 6 ; echo countNumbers ( $ l , $ r , $ k ) ; ? >
Check for Amicable Pair | Function to calculate sum of all proper divisors of a given number ; Sum of divisors ; find all divisors which divides ' num ' ; if ' i ' is divisor of ' n ' ; if both divisors are same then add it once else add both ; Add 1 and n to result as above loop considers proper divisors greater than 1. ; Returns true if x and y are Amicable else false . ; Driver Code
< ? php function divSum ( $ n ) { $ result = 0 ; for ( $ i = 2 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ i == ( $ n / $ i ) ) $ result += $ i ; else $ result += ( $ i + $ n / $ i ) ; } } return ( $ result + 1 ) ; } function areAmicable ( $ x , $ y ) { if ( divSum ( $ x ) != $ y ) return false ; return ( divSum ( $ y ) == $ x ) ; } $ x = 220 ; $ y = 284 ; if ( areAmicable ( $ x , $ y ) ) echo " Yes " ; else echo " No " ; ? >
Program to print non square numbers | Function to check perfect square ; function to print all non square number ; variable which stores the count ; not perfect square ; Driver Code
< ? php function isPerfectSquare ( $ n ) { if ( $ n < 0 ) return false ; $ root = round ( sqrt ( $ n ) ) ; return $ n == $ root * $ root ; } function printnonsquare ( $ n ) { $ count = 0 ; for ( $ i = 1 ; $ count < $ n ; ++ $ i ) { if ( ! isPerfectSquare ( $ i ) ) { echo $ i . " " ; $ count ++ ; } } } $ n = 10 ; printnonsquare ( $ n ) ; ? >
Program to print non square numbers | Returns n - th non - square number . ; loop to print non squares below n ; Driver Code
< ? php function nonsquare ( $ n ) { return $ n + ( int ) ( 0.5 + sqrt ( $ n ) ) ; } function printNonSquare ( $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) printf ( nonsquare ( $ i ) . " ▁ " ) ; } $ n = 10 ; printNonSquare ( $ n ) ; ? >
Program to compare two fractions | Get max of the two fractions ; Declare nume1 and nume2 for get the value of first numerator and second numerator ; Compute ad - bc ; Driver Code
< ? php function maxFraction ( $ first , $ sec ) { $ a = $ first [ 0 ] ; $ b = $ first [ 1 ] ; $ c = $ sec [ 0 ] ; $ d = $ sec [ 1 ] ; $ Y = $ a * $ d - $ b * $ c ; return ( $ Y ) ? $ first : $ sec ; } $ first = array ( 3 , 2 ) ; $ sec = array ( 3 , 4 ) ; $ res = maxFraction ( $ first , $ sec ) ; echo $ res [ 0 ] . " / " . $ res [ 1 ] ; ? >
Program to check if N is a Pentagonal Number | Function to determine if N is pentagonal or not . ; Substitute values of i in the formula . ; Driver Code
< ? php function isPentagonal ( int $ N ) { $ i = 1 ; $ M ; do { $ M = ( 3 * $ i * $ i - $ i ) / 2 ; $ i += 1 ; } while ( $ M < $ N ) ; return ( $ M == $ N ) ; } $ N = 12 ; if ( isPentagonal ( $ N ) ) echo $ N , " ▁ is ▁ pentagonal ▁ " ; else echo $ N , " ▁ is ▁ not ▁ pentagonal " ; ? >
Sum of fourth powers of the first n natural numbers | Return the sum of fourth power of first n natural numbers ; Driver Code
< ? php function fourthPowerSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum = $ sum + ( $ i * $ i * $ i * $ i ) ; return $ sum ; } $ n = 6 ; echo ( fourthPowerSum ( $ n ) ) ; ? >
Sum of fourth powers of the first n natural numbers | Return the sum of fourth power of first n natural numbers ; Driver Code
< ? php function fourthPowerSum ( $ n ) { return ( ( 6 * $ n * $ n * $ n * $ n * $ n ) + ( 15 * $ n * $ n * $ n * $ n ) + ( 10 * $ n * $ n * $ n ) - $ n ) / 30 ; } $ n = 6 ; echo ( fourthPowerSum ( $ n ) ) ; ? >
Find unit digit of x raised to power y | find unit digit ; Get last digit of x ; Last cyclic modular value ; here we simply return the unit digit or the power of a number ; Driver code ; get unit digit number here we pass the unit digit of x and the last cyclicity number that is y % 4
< ? php function unitnumber ( $ x , $ y ) { $ x = $ x % 10 ; if ( $ y != 0 ) $ y = $ y % 4 + 4 ; return ( ( ( int ) ( pow ( $ x , $ y ) ) ) % 10 ) ; } $ x = 133 ; $ y = 5 ; echo ( unitnumber ( $ x , $ y ) ) ; ? >
Aliquot sum | Function to calculate sum of all proper divisors ; Driver Code
< ? php function aliquotSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) if ( $ n % $ i == 0 ) $ sum += $ i ; return $ sum ; } $ n = 12 ; echo ( aliquotSum ( $ n ) ) ; ? >