codeparrot/xlcost-text-to-code huggingface dataset:
Number of sub arrays with odd sum | PHP code to find count of sub - arrays with odd sum ; Find sum of all subarrays and increment result if sum is odd ; Driver code
< ? php function countOddSum ( & $ ar , $ n ) { $ result = 0 ; for ( $ i = 0 ; $ i <= $ n - 1 ; $ i ++ ) { $ val = 0 ; for ( $ j = $ i ; $ j <= $ n - 1 ; $ j ++ ) { $ val = $ val + $ ar [ $ j ] ; if ( $ val % 2 != 0 ) $ result ++ ; } } return ( $ result ) ; } $ ar = array ( 5 , 4 , 4 , 5 , 1 , 3 ) ; $ n = sizeof ( $ ar ) ; echo " The ▁ Number ▁ of ▁ Subarrays ▁ with ▁ odd ▁ " ; echo " sum ▁ is ▁ " . countOddSum ( $ ar , $ n ) ; ? >
Number of sub arrays with odd sum | PHP proggram to find count of sub - arrays with odd sum ; 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 is a single odd 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 / odd count ; An odd can be formed by even - odd pair ; Driver code
< ? php function countOddSum ( $ ar , $ n ) { $ temp = array ( 1 , 0 ) ; $ result = 0 ; $ val = 0 ; for ( $ i = 0 ; $ i <= $ n - 1 ; $ i ++ ) { $ val = ( ( $ val + $ ar [ $ i ] ) % 2 + 2 ) % 2 ; $ temp [ $ val ] ++ ; } $ result = ( $ temp [ 0 ] * $ temp [ 1 ] ) ; return ( $ result ) ; } $ ar = array ( 5 , 4 , 4 , 5 , 1 , 3 ) ; $ n = sizeof ( $ ar ) ; echo " The ▁ Number ▁ of ▁ Subarrays ▁ with ▁ odd " . " ▁ sum ▁ is ▁ " . countOddSum ( $ ar , $ n ) ; ? >
Program to print factors of a number in pairs | PHP program to print prime factors in pairs . ; Driver code
< ? php function printPFsInPairs ( $ n ) { for ( $ i = 1 ; $ i * $ i <= $ n ; $ i ++ ) if ( $ n % $ i == 0 ) echo $ i . " * " . ▁ $ n ▁ / ▁ $ i ▁ . " " } $ n = 24 ; printPFsInPairs ( $ n ) ; return 0 ; ? >
Sum of elements in range L | Function to find the sum between L and R ; array created ; fill the first half of array ; fill the second half of array ; find the sum between range ; Driver Code
< ? php function rangesum ( $ n , $ l , $ r ) { $ arr = array_fill ( 0 , $ n , 0 ) ; $ c = 1 ; $ i = 0 ; while ( $ c <= $ n ) { $ arr [ $ i ++ ] = $ c ; $ c += 2 ; } $ c = 2 ; while ( $ c <= $ n ) { $ arr [ $ i ++ ] = $ c ; $ c += 2 ; } $ sum = 0 ; for ( $ i = $ l - 1 ; $ i < $ r ; $ i ++ ) { $ sum += $ arr [ $ i ] ; } return $ sum ; } $ n = 12 ; $ l = 1 ; $ r = 11 ; echo ( rangesum ( $ n , $ l , $ r ) ) ; ? >
Program to find the Interior and Exterior Angle of a Regular Polygon | function to find the interior and exterior angle ; formula to find the interior angle ; formula to find the exterior angle ; Displaying the output ; Driver code ; Function calling
< ? php function findAngle ( $ n ) { $ interiorAngle ; $ exteriorAngle ; $ interiorAngle = ( $ n - 2 ) * 180 / $ n ; $ exteriorAngle = 360 / $ n ; echo " Interior ▁ angle : ▁ " , $ interiorAngle , " STRNEWLINE " ; echo " Exterior ▁ angle : ▁ " , $ exteriorAngle ; } $ n = 10 ; findAngle ( $ n ) ; ? >
Program to calculate distance between two points in 3 D | function to print distance ; Driver Code ; function call for distance
< ? php function distance ( $ x1 , $ y1 , $ z1 , $ x2 , $ y2 , $ z2 ) { $ d = sqrt ( pow ( $ x2 - $ x1 , 2 ) + pow ( $ y2 - $ y1 , 2 ) + pow ( $ z2 - $ z1 , 2 ) * 1.0 ) ; echo " Distance ▁ is ▁ " . $ d ; } $ x1 = 2 ; $ y1 = -5 ; $ z1 = 7 ; $ x2 = 3 ; $ y2 = 4 ; $ z2 = 5 ; distance ( $ x1 , $ y1 , $ z1 , $ x2 , $ y2 , $ z2 ) ; ? >
Check if the large number formed is divisible by 41 or not | Check if a number is divisible by 41 or not ; array to store all the digits ; base values ; calculate remaining digits ; calculate answer ; check for divisibility ; Driver Code
< ? php function DivisibleBy41 ( $ first , $ second , $ c , $ n ) { $ digit [ $ n ] = range ( 1 , $ n ) ; $ digit [ 0 ] = $ first ; $ digit [ 1 ] = $ second ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) $ digit [ $ i ] = ( $ digit [ $ i - 1 ] * $ c + $ digit [ $ i - 2 ] ) % 10 ; $ ans = $ digit [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ ans = ( $ ans * 10 + $ digit [ $ i ] ) % 41 ; if ( $ ans % 41 == 0 ) return true ; else return false ; } $ first = 1 ; $ second = 2 ; $ c = 1 ; $ n = 3 ; if ( DivisibleBy41 ( $ first , $ second , $ c , $ n ) ) echo " YES " ; else echo " NO " ; ? >
Program to print pentatope numbers upto Nth term | Function to generate nth tetrahedral number ; Function to print pentatope number series upto nth term . ; Initialize prev as 0. It store the sum of all previously generated pentatope numbers ; Loop to print pentatope series ; Find ith tetrahedral number ; Add ith tetrahedral number to sum of all previously generated tetrahedral number to get ith pentatope number ; Update sum of all previously generated tetrahedral number ; Driver code ; Function call to print pentatope number series
< ? php function findTetrahedralNumber ( $ n ) { return ( ( $ n * ( $ n + 1 ) * ( $ n + 2 ) ) / 6 ) ; } function printSeries ( $ n ) { $ prev = 0 ; $ curr ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ curr = findTetrahedralNumber ( $ i ) ; $ curr = $ curr + $ prev ; echo ( $ curr . " " ) ; $ prev = $ curr ; } } $ n = 10 ; printSeries ( $ n ) ; ? >
Program to print pentatope numbers upto Nth term | Function to print pentatope series up to nth term ; Loop to print pentatope number series ; calculate and print ith pentatope number ; Driver code ; Function call to print pentatope number series
< ? php function printSeries ( $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ num = ( $ i * ( $ i + 1 ) * ( $ i + 2 ) * ( $ i + 3 ) / 24 ) ; echo ( $ num . " " ) ; } } $ n = 10 ; printSeries ( $ n ) ; ? >
Find unique pairs such that each element is less than or equal to N | Finding the number of unique pairs ; Using the derived formula ; Printing the unique pairs ; Driver Code
< ? php function No_Of_Pairs ( $ N ) { $ i = 1 ; while ( ( $ i * $ i * $ i ) + ( 2 * $ i * $ i ) + $ i <= $ N ) $ i ++ ; return ( $ i - 1 ) ; } function print_pairs ( $ pairs ) { $ i = 1 ; $ mul ; for ( $ i = 1 ; $ i <= $ pairs ; $ i ++ ) { $ mul = $ i * ( $ i + 1 ) ; echo " Pair ▁ no . " , $ i , " ▁ - - > ▁ ( " , ( $ mul * $ i ) , " , ▁ " , $ mul * ( $ i + 1 ) , " ) ▁ STRNEWLINE " ; } } $ N = 500 ; $ pairs ; $ mul ; $ i = 1 ; $ pairs = No_Of_Pairs ( $ N ) ; echo " No . ▁ of ▁ pairs ▁ = ▁ " , $ pairs , " ▁ STRNEWLINE " ; print_pairs ( $ pairs ) ; ? >
Program to print tetrahedral numbers upto Nth term | function to generate nth triangular number ; function to print tetrahedral number series up to n ; Initialize prev as 0. It store the sum of all previously generated triangular number ; Loop to print series ; Find ithh triangular number ; Add ith triangular number to sum of all previously generated triangular number to get ith tetrahedral number ; Update sum of all previously generated triangular number ; Driver code ; function call to print series
< ? php function findTriangularNumber ( $ n ) { return ( $ n * ( $ n + 1 ) ) / 2 ; } function printSeries ( $ n ) { $ prev = 0 ; $ curr ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ curr = findTriangularNumber ( $ i ) ; $ curr = $ curr + $ prev ; echo ( $ curr . " " ) ; $ prev = $ curr ; } } $ n = 10 ; printSeries ( $ n ) ; ? >
Program to print tetrahedral numbers upto Nth term | function to print tetrahedral number series up to n ; loop to print series ; Calculate and print ith tetrahedral number ; Driver code ; function call to print series
< ? php function printSeries ( $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ num = $ i * ( $ i + 1 ) * ( $ i + 2 ) / 6 ; echo ( $ num . " " ) ; } } $ n = 10 ; printSeries ( $ n ) ; ? >
Number of odd and even results for every value of x in range [ min , max ] after performing N steps | Function that prints the number of odd and even results ; If constant at layer i is even , beven is true , otherwise false . If the coefficient of x at layer i is even , aeven is true , otherwise false . ; If any of the coefficients at any layer is found to be even , then the product of all the coefficients will always be even . ; Checking whether the constant added after all layers is even or odd . ; Assuming input x is even . ; Assuming input x is odd . ; Displaying the counts . ; Driver Code
< ? php function count_even_odd ( $ min , $ max , $ steps ) { $ beven = true ; $ aeven = false ; $ n = 2 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ a = $ steps [ $ i ] [ 0 ] ; $ b = $ steps [ $ i ] [ 1 ] ; if ( ! ( $ aeven $ a & 1 ) ) $ aeven = true ; if ( $ beven ) { if ( $ b & 1 ) $ beven = false ; } else if ( ! ( $ a & 1 ) ) { if ( ! ( $ b & 1 ) ) $ beven = true ; } else { if ( $ b & 1 ) $ beven = true ; } } if ( $ beven ) { $ even = ( int ) $ max / 2 - ( int ) ( $ min - 1 ) / 2 ; $ odd = 0 ; } else { $ even = ( int ) $ max / 2 - ( int ) ( $ min - 1 ) / 2 ; $ odd = 0 ; } if ( ! ( $ beven ^ $ aeven ) ) $ even += $ max - $ min + 1 - ( int ) $ max / 2 + ( int ) ( $ min - 1 ) / 2 ; else $ odd += $ max - $ min + 1 - ( int ) $ max / 2 + ( int ) ( $ min - 1 ) / 2 ; echo " even = " ▁ , ▁ $ even , STRNEWLINE TABSYMBOL TABSYMBOL " , odd = " , ▁ $ odd , ▁ " " } $ min = 1 ; $ max = 4 ; $ steps = array ( array ( 1 , 2 ) , array ( 3 , 4 ) ) ; count_even_odd ( $ min , $ max , $ steps ) ; ? >
Maximum number of ones in a N * N matrix with given constraints | Function that returns the maximum number of ones ; Minimum number of zeroes ; Totol cells = square of the size of the matrices ; Initialising the answer ; Initialising the variables
< ? php function getMaxOnes ( $ n , $ x ) { $ zeroes = ( int ) ( $ n / $ x ) ; $ zeroes = $ zeroes * $ zeroes ; $ total = $ n * $ n ; $ ans = $ total - $ zeroes ; return $ ans ; } $ n = 5 ; $ x = 2 ; echo getMaxOnes ( $ n , $ x ) ; ? >
Check if a M | PHP program to check if M - th fibonacci divides N - th fibonacci ; exceptional case for F ( 2 ) ; if none of the above cases , hence not divisible ; Driver Code
< ? php function check ( $ n , $ m ) { if ( $ n == 2 $ m == 2 $ n % $ m == 0 ) { echo " Yes " , " STRNEWLINE " ; } else { echo " No " , " ▁ STRNEWLINE " ; } } $ m = 3 ; $ n = 9 ; check ( $ n , $ m ) ; ? >
Surface Area and Volume of Hexagonal Prism | Function to calculate Surface area ; Formula to calculate surface area ; Display surface area ; Function to calculate Volume ; formula to calculate Volume ; Display Volume ; Driver Code ; surface area function call ; volume function call
< ? php function findSurfaceArea ( $ a , $ h ) { $ Area ; $ Area = 6 * $ a * $ h + 3 * sqrt ( 3 ) * $ a * $ a ; echo " Surface ▁ Area : ▁ " , $ Area , " STRNEWLINE " ; } function findVolume ( $ a , $ h ) { $ Volume ; $ Volume = 3 * sqrt ( 3 ) * $ a * $ a * $ h / 2 ; echo " Volume : ▁ " , $ Volume ; } $ a = 5 ; $ h = 10 ; findSurfaceArea ( $ a , $ h ) ; findVolume ( $ a , $ h ) ; ? >
Minimum number of mails required to distribute all the questions | Function returns the min no of mails required ; Using the formula derived above ; no of questions ; no of students ; maximum no of questions a mail can hold ; Calling function
< ? php function MinimumMail ( $ n , $ k , $ x ) { $ m = ( $ n - 1 ) + ceil ( ( $ n - 1 ) * 1.0 / $ x ) * ( $ n - 1 ) + ceil ( $ n * 1.0 / $ x ) * ( $ k - $ n ) ; return $ m ; } $ N = 4 ; $ K = 9 ; $ X = 2 ; echo MinimumMail ( $ N , $ K , $ X ) , " STRNEWLINE " ; ? >
Program to find the Area of an Ellipse | Function to find area of an ellipse . ; formula to find the area of an Ellipse . ; Display the result ; Driver code
< ? php function findArea ( $ a , $ b ) { $ Area ; $ Area = 3.142 * $ a * $ b ; echo " Area : ▁ " . $ Area ; } $ a = 5 ; $ b = 4 ; findArea ( $ a , $ b ) ; ? >
Compute power of power k times % m | Function to compute the given value ; compute power k times ; Driver Code ; Calling function
< ? php function calculate ( $ x , $ k , $ m ) { $ result = $ x ; $ k -- ; while ( $ k -- ) { $ result = pow ( $ result , $ x ) ; if ( $ result > $ m ) $ result %= $ m ; } return $ result ; } $ x = 5 ; $ k = 2 ; $ m = 3 ; echo calculate ( $ x , $ k , $ m ) ; ? >
Recursive program to check if number is palindrome or not | Recursive function that returns the reverse of digits ; base case ; stores the reverse of a number ; Driver Code
< ? php function rev ( $ n , $ temp ) { if ( $ n == 0 ) return $ temp ; $ temp = ( $ temp * 10 ) + ( $ n % 10 ) ; return rev ( $ n / 10 , $ temp ) ; } $ n = 121 ; $ temp = rev ( $ n , 0 ) ; if ( $ temp != $ n ) echo " yes " ; else echo " no " ; ? >
Program to find greater value between a ^ n and b ^ n | Function to find the greater value ; If n is even ; Driver code
< ? php function findGreater ( $ a , $ b , $ n ) { if ( ! ( $ n & 1 ) ) { $ a = abs ( $ a ) ; $ b = abs ( $ b ) ; } if ( $ a == $ b ) echo " a ^ n ▁ is ▁ equal ▁ to ▁ b ^ n " ; else if ( $ a > $ b ) echo " a ^ n ▁ is ▁ greater ▁ than ▁ b ^ n " ; else echo " b ^ n ▁ is ▁ greater ▁ than ▁ a ^ n " ; } $ a = 12 ; $ b = 24 ; $ n = 5 ; findGreater ( $ a , $ b , $ n ) ; ? >
Print first n Fibonacci Numbers using direct formula | Function to calculate fibonacci using recurrence relation formula ; Using direct formula ; Driver code
< ? php function fibonacci ( $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ fib = ( pow ( ( 1 + sqrt ( 5 ) ) , $ i ) - pow ( ( 1 - sqrt ( 5 ) ) , $ i ) ) / ( pow ( 2 , $ i ) * sqrt ( 5 ) ) ; echo $ fib , " " ; } } $ n = 8 ; fibonacci ( $ n ) ; ? >
Centered Hexadecagonal Number | centered hexadecagonal function ; Formula to calculate nth centered hexadecagonal number ; Driver Code
< ? php function center_hexadecagonal_num ( $ n ) { return 8 * $ n * $ n - 8 * $ n + 1 ; } $ n = 2 ; echo $ n , " th ▁ centered ▁ hexadecagonal ▁ number ▁ : ▁ " , center_hexadecagonal_num ( $ n ) ; echo " STRNEWLINE " ; $ n = 12 ; echo $ n , " th ▁ centered ▁ hexadecagonal ▁ numbe ▁ : ▁ " , center_hexadecagonal_num ( $ n ) ; ? >
Check if the n | PHP Program to check if the nth is odd or even in a sequence where each term is sum of previous two term ; Return if the nth term is even or odd . ; Return true if odd ; Driver Code
< ? php $ MAX = 100 ; function findNature ( $ a , $ b , $ n ) { global $ MAX ; $ seq = array_fill ( 0 , $ MAX , 0 ) ; $ seq [ 0 ] = $ a ; $ seq [ 1 ] = $ b ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ seq [ $ i ] = $ seq [ $ i - 1 ] + $ seq [ $ i - 2 ] ; return ( $ seq [ $ n ] & 1 ) ; } $ a = 2 ; $ b = 4 ; $ n = 3 ; if ( findNature ( $ a , $ b , $ n ) ) echo " Odd " ; else echo " Even " ; ? >
Program to compare m ^ n and n ^ m | function to compare m ^ n and n ^ m ; m ^ n ; n ^ m ; Driver Code ; function call to compare m ^ n and n ^ m
< ? php function check ( $ m , $ n ) { $ RHS = $ m * log ( $ n ) ; $ LHS = $ n * log ( $ m ) ; if ( $ LHS > $ RHS ) echo " m ^ n ▁ > ▁ n ^ m " ; else if ( $ LHS < $ RHS ) echo " m ^ n ▁ < ▁ n ^ m " ; else echo " m ^ n ▁ = ▁ n ^ m " ; } $ m = 987654321 ; $ n = 123456987 ; check ( $ m , $ n ) ; ? >
Find the GCD that lies in given range | Return the greatest common divisor of two numbers ; Return the gretest common divisor of a and b which lie in the given range . ; Loop from 1 to sqrt ( GCD ( a , b ) . ; if i divides the GCD ( a , b ) , then find maximum of three numbers res , i and g / i ; Driver Code
< ? php function gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return gcd ( $ b , $ a % $ b ) ; } function maxDivisorRange ( $ a , $ b , $ l , $ h ) { $ g = gcd ( $ a , $ b ) ; $ res = -1 ; for ( $ i = $ l ; $ i * $ i <= $ g and $ i <= $ h ; $ i ++ ) if ( $ g % $ i == 0 ) $ res = max ( $ res , max ( $ i , $ g / $ i ) ) ; return $ res ; } $ a = 3 ; $ b = 27 ; $ l = 1 ; $ h = 5 ; echo maxDivisorRange ( $ a , $ b , $ l , $ h ) ; ? >
Number expressed as sum of five consecutive integers | function to check if a number can be expressed as sum of five consecutive integers . ; if n is 0 ; if n is positive , increment loop by 1. ; if n is negative , decrement loop by 1. ; Running loop from 0 to n - 4 ; check if sum of five consecutive integer is equal to n . ; Driver Code
< ? php function checksum ( $ n ) { if ( $ n == 0 ) { echo " - 2 ▁ - 1 ▁ 0 ▁ 1 ▁ 2" , " STRNEWLINE " ; return ; } $ inc ; if ( $ n > 0 ) $ inc = 1 ; else $ inc = -1 ; for ( $ i = 0 ; $ i <= $ n - 4 ; $ i += $ inc ) { if ( $ i + $ i + 1 + $ i + 2 + $ i + 3 + $ i + 4 == $ n ) { echo $ i , " " ▁ , ▁ $ i ▁ + ▁ 1 , STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL " " ▁ , ▁ $ i ▁ + ▁ 2 , STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL " " ▁ , ▁ $ i ▁ + ▁ 3 , STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL " " return ; } } echo " - 1" ; } $ n = 15 ; checksum ( $ n ) ; ? >
Number expressed as sum of five consecutive integers | function to check if a number can be expressed as sum of five consecutive integers . ; if n is multiple of 5 ; else print " - 1" . ; Driver Code
< ? php function checksum ( $ n ) { if ( $ n % 5 == 0 ) echo $ n / 5 - 2 , " ▁ " , $ n / 5 - 1 , " ▁ " , $ n / 5 , " ▁ " , $ n / 5 + 1 , " ▁ " , $ n / 5 + 2 ; else echo " - 1" ; } $ n = 15 ; checksum ( $ n ) ; ? >
n | Function to find the nth term of series ; Loop to add 4 th powers ; Driver code
< ? php function sumOfSeries ( $ n ) { $ ans = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ ans += $ i * $ i * $ i * $ i ; return $ ans ; } $ n = 4 ; echo sumOfSeries ( $ n ) ; ? >
Number of unmarked integers in a special sieve | PHP Program to determine the number of unmarked integers in a special sieve ; Driver Code
< ? php function countUnmarked ( $ N ) { if ( $ N % 2 == 0 ) return $ N / 2 ; else return $ N / 2 + 1 ; } $ N = 4 ; echo " Number ▁ of ▁ unmarked ▁ elements : ▁ " , countUnmarked ( $ N ) ; ? >
Sum of series 1 * 1 ! + 2 * 2 ! + …… . . + n * n ! | PHP program to find sum of the series . ; Function to calculate required series ; Driver code
< ? php function factorial ( $ n ) { $ res = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res = $ res * $ i ; return $ res ; } function calculateSeries ( $ n ) { return factorial ( $ n + 1 ) - 1 ; } $ n = 3 ; echo calculateSeries ( $ n ) ; ? >
Sum of series 1 * 1 * 2 ! + 2 * 2 * 3 ! + …… . . + n * n * ( n + 1 ) ! | PHP program to find sum of the series . ; Function to calculate required series ; Driver code
< ? php function factorial ( $ n ) { $ res = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res = $ res * $ i ; return $ res ; } function calculateSeries ( $ n ) { return 2 + ( $ n * $ n + $ n - 2 ) * factorial ( $ n + 1 ) ; } $ n = 3 ; echo calculateSeries ( $ n ) ; ? >
Forming smallest array with given constraints | Return the size of smallest array with given constraint . ; Driver Code
< ? php function minimumLength ( $ x , $ y , $ z ) { return 1 + abs ( $ x - $ y ) + abs ( $ y - $ z ) ; } $ x = 3 ; $ y = 1 ; $ z = 2 ; echo minimumLength ( $ x , $ y , $ z ) ; ? >
Find the other | function to find the other - end point of diameter ; find end point for x coordinates ; find end point for y coordinates ; Driven Program
< ? php function endPointOfDiameterofCircle ( $ x1 , $ y1 , $ c1 , $ c2 ) { echo " x2 = " , (2 ▁ * ▁ $ c1 ▁ - ▁ $ x1 ) , " " ; STRNEWLINE TABSYMBOL echo ▁ " y2 = " } $ x1 = -4 ; $ y1 = -1 ; $ c1 = 3 ; $ c2 = 5 ; endPointOfDiameterofCircle ( $ x1 , $ y1 , $ c1 , $ c2 ) ; ? >
Newton 's Divided Difference Interpolation Formula | Function to find the product term ; Function for calculating divided difference table ; Function for applying Newton 's  divided difference formula  ; Function for displaying divided difference table ; number of inputs given ; y [ ] [ ] is used for divided difference table where y [ ] [ 0 ] is used for input ; calculating divided difference table ; displaying divided difference table ; value to be interpolated ; printing the value
< ? php function proterm ( $ i , $ value , $ x ) { $ pro = 1 ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) { $ pro = $ pro * ( $ value - $ x [ $ j ] ) ; } return $ pro ; } function dividedDiffTable ( $ x , & $ y , $ n ) { for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n - $ i ; $ j ++ ) { $ y [ $ j ] [ $ i ] = ( $ y [ $ j ] [ $ i - 1 ] - $ y [ $ j + 1 ] [ $ i - 1 ] ) / ( $ x [ $ j ] - $ x [ $ i + $ j ] ) ; } } } function applyFormula ( $ value , $ x , $ y , $ n ) { $ sum = $ y [ 0 ] [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ sum = $ sum + ( proterm ( $ i , $ value , $ x ) * $ y [ 0 ] [ $ i ] ) ; } return $ sum ; } function printDiffTable ( $ y , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n - $ i ; $ j ++ ) { echo round ( $ y [ $ i ] [ $ j ] , 4 ) . " " ; } echo " " ; } } $ n = 4 ; $ y = array_fill ( 0 , 10 , array_fill ( 0 , 10 , 0 ) ) ; $ x = array ( 5 , 6 , 9 , 11 ) ; $ y [ 0 ] [ 0 ] = 12 ; $ y [ 1 ] [ 0 ] = 13 ; $ y [ 2 ] [ 0 ] = 14 ; $ y [ 3 ] [ 0 ] = 16 ; dividedDiffTable ( $ x , $ y , $ n ) ; printDiffTable ( $ y , $ n ) ; $ value = 7 ; echo " Value at " ▁ . ▁ $ value ▁ . ▁ " is " round ( applyFormula ( $ value , $ x , $ y , $ n ) , 2 ) . " STRNEWLINE " ? >
Centered heptagonal number | Function to find Centered heptagonal number ; Formula to calculate nth Centered heptagonal number ; Driver Code
< ? php function centered_heptagonal_num ( $ n ) { return ( 7 * $ n * $ n - 7 * $ n + 2 ) / 2 ; } $ n = 5 ; echo $ n , " th ▁ Centered ▁ heptagonal ▁ number ▁ : ▁ " ; echo centered_heptagonal_num ( $ n ) ; ? >
Sum of square | Function to find sum of sum of square of first n natural number ; Driver Code
< ? php function findSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum += ( ( $ i * ( $ i + 1 ) * ( 2 * $ i + 1 ) ) / 6 ) ; return $ sum ; } $ n = 3 ; echo findSum ( $ n ) ; ? >
Check if a given matrix is Hankel or not | PHP Program to check if given matrix is Hankel Matrix or not . ; Function to check if given matrix is Hankel Matrix or not . ; for each row ; for each column ; checking if i + j is less than n ; checking if the element is equal to the corresponding diagonal constant ; checking if the element is equal to the corresponding diagonal constant ; Driver code
< ? php $ N = 4 ; function checkHankelMatrix ( $ n , $ m ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ i + $ j < $ n ) { if ( $ m [ $ i ] [ $ j ] != $ m [ $ i + $ j ] [ 0 ] ) return false ; } else { if ( $ m [ $ i ] [ $ j ] != $ m [ $ i + $ j - $ n + 1 ] [ $ n - 1 ] ) return false ; } } } return true ; } $ n = 4 ; $ m = array ( array ( 1 , 2 , 3 , 5 ) , array ( 2 , 3 , 5 , 8 ) , array ( 3 , 5 , 8 , 0 ) , array ( 5 , 8 , 0 , 9 ) ) ; if ( checkHankelMatrix ( $ n , $ m ) ) echo " Yes " ; else echo " No " ; ? >
Check if a number can be expressed as power | Set 2 ( Using Log ) | PHP program to find if a number can be expressed as x raised to power y . ; Find Log n in different bases and check if the value is an integer ; Driver code
< ? php function isPower ( $ n ) { for ( $ x = 2 ; $ x <= sqrt ( $ n ) ; $ x ++ ) { $ f = log ( $ n ) / log ( $ x ) ; if ( ( $ f - ( int ) $ f ) == 0.0 ) return true ; } return false ; } for ( $ i = 2 ; $ i < 100 ; $ i ++ ) if ( isPower ( ( int ) $ i ) ) echo $ i . " " ; ? >
Queries on sum of odd number digit sums of all the factors of a number | PHP Program to answer queries on sum of sum of odd number digits of all the factors of a number ; finding sum of odd digit number in each integer . ; for each number ; using previous number sum , finding the current number num of odd digit also , adding last digit if it is odd . ; finding sum of sum of odd digit of all the factors of a number . ; for each possible factor ; adding the contribution . ; Wrapper function ; Driver Code
< ? php $ N = 1000005 ; function sumOddDigit ( & $ digitSum ) { global $ N ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) { $ digitSum [ $ i ] = $ digitSum [ intval ( $ i / 10 ) ] + intval ( $ i & 1 ) * ( $ i % 10 ) ; } } function sumFactor ( $ digitSum , & $ factorDigitSum ) { global $ N ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) { for ( $ j = $ i ; $ j < $ N ; $ j += $ i ) { $ factorDigitSum [ $ j ] += $ digitSum [ $ i ] ; } } } function wrapper ( $ q , $ n ) { global $ N ; $ digitSum = array ( ) ; $ factorDigitSum = array ( ) ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ digitSum [ $ i ] = 0 ; $ factorDigitSum [ $ i ] = 0 ; } sumOddDigit ( $ digitSum ) ; sumFactor ( $ digitSum , $ factorDigitSum ) ; for ( $ i = 0 ; $ i < $ q ; $ i ++ ) echo ( $ factorDigitSum [ $ n [ $ i ] ] . " ▁ " ) ; } $ q = 2 ; $ n = array ( 10 , 36 ) ; wrapper ( $ q , $ n ) ; ? >