codeparrot/xlcost-text-to-code huggingface dataset:
Check if two strings are k | Optimized PHP program to check if two strings are k anagram or not . ; Function to check if str1 and str2 are k - anagram or not ; If both strings are not of equal length then return false ; Store the occurrence of all characters in a hash_array ; Store the occurrence of all characters in a hash_array ; Return true if count is less than or equal to k ; Driver code
< ? php $ MAX_CHAR = 26 ; function areKAnagrams ( $ str1 , $ str2 , $ k ) { global $ MAX_CHAR ; $ n = strlen ( $ str1 ) ; if ( strlen ( $ str2 ) != $ n ) return false ; $ hash_str1 = array ( 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ hash_str1 [ $ str1 [ $ i ] - ' a ' ] ++ ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ hash_str1 [ $ str2 [ $ i ] - ' a ' ] > 0 ) $ hash_str1 [ $ str2 [ $ i ] - ' a ' ] -- ; else $ count ++ ; if ( $ count > $ k ) return false ; } return true ; } $ str1 = " fodr " ; $ str2 = " gork " ; $ k = 2 ; if ( areKAnagrams ( $ str1 , $ str2 , $ k ) == true ) echo " Yes " ; else echo " No " ; ? >
Nth character in Concatenated Decimal String | Method to get dth digit of number N ; Method to return Nth character in concatenated decimal string ; sum will store character escaped till now ; dist will store numbers escaped till now ; loop for number lengths ; nine * len will be incremented characters and nine will be incremented numbers ; restore variables to previous correct state ; get distance from last one digit less maximum number ; d will store dth digit of current number ; method will return dth numbered digit of ( dist + diff ) number ; Driver code
< ? php function getDigit ( $ N , $ d ) { $ string = strval ( $ N ) ; return $ string [ $ d - 1 ] ; } function getNthChar ( $ N ) { $ sum = 0 ; $ nine = 9 ; $ dist = 0 ; for ( $ len = 1 ; $ len < $ N ; $ len ++ ) { $ sum += $ nine * $ len ; $ dist += $ nine ; if ( $ sum >= $ N ) { $ sum -= $ nine * $ len ; $ dist -= $ nine ; $ N -= $ sum ; break ; } $ nine *= 10 ; } $ diff = ( $ N / $ len ) + 1 ; $ d = $ N % $ len ; if ( $ d == 0 ) $ d = $ len ; return getDigit ( $ dist + $ diff , $ d ) ; } $ N = 251 ; echo getNthChar ( $ N ) ; ? >
Count characters at same position as in English alphabet | Function to count the number of characters at same position as in English alphabets ; Traverse the input string ; Check that index of characters of string is same as of English alphabets by using ASCII values and the fact that all lower case alphabetic characters come together in same order in ASCII table . And same is true for upper case . ; Driver Code
< ? php function findCount ( $ str ) { $ result = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( ( $ i == ord ( $ str [ $ i ] ) - ord ( ' a ' ) ) or ( $ i == ord ( $ str [ $ i ] ) - ord ( ' A ' ) ) ) $ result += 1 ; } return $ result ; } $ str = " AbgdeF " ; print ( findCount ( $ str ) ) ? >
Generate all binary strings from given pattern | Recursive function to generate all binary strings formed by replacing each wildcard character by 0 or 1 ; replace ' ? ' by '0' and recurse ; replace ' ? ' by '1' and recurse ; No need to backtrack as string is passed by value to the function ; Driver code
< ? php function print1 ( $ str , $ index ) { if ( $ index == strlen ( $ str ) ) { echo $ str . " STRNEWLINE " ; return ; } if ( $ str [ $ index ] == ' ? ' ) { $ str [ $ index ] = '0' ; print1 ( $ str , $ index + 1 ) ; $ str [ $ index ] = '1' ; print1 ( $ str , $ index + 1 ) ; } else print1 ( $ str , $ index + 1 ) ; } $ str = "1 ? ? 0?101" ; print1 ( $ str , 0 ) ; ? >
Find the most frequent digit without using array / string | Simple function to count occurrences of digit d in x ; Initialize count of digit d ; Increment count if current digit is same as d ; Returns the max occurring digit in x ; Handle negative number ; Traverse through all digits ; Count occurrences of current digit ; Update max_count and result if needed ; Driver Code
< ? php function countOccurrences ( $ x , $ d ) { $ count = 0 ; while ( $ x ) { if ( $ x % 10 == $ d ) $ count ++ ; $ x = ( int ) ( $ x / 10 ) ; } return $ count ; } function maxOccurring ( $ x ) { if ( $ x < 0 ) $ x = - $ x ; for ( $ d = 0 ; $ d <= 9 ; $ d ++ ) { $ count = countOccurrences ( $ x , $ d ) ; if ( $ count >= $ max_count ) { $ max_count = $ count ; $ result = $ d ; } } return $ result ; } $ x = 1223355 ; echo " Max ▁ occurring ▁ digit ▁ is ▁ " . maxOccurring ( $ x ) ; ? >
Find the maximum subarray XOR in a given array | A simple PHP program to find max subarray XOR ; Initialize result ; Pick starting points of subarrays ; to store xor of current subarray ; Pick ending points of subarrays starting with i ; Driver Code
< ? php function maxSubarrayXOR ( $ arr , $ n ) { $ ans = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ curr_xor = 0 ; for ( $ j = $ i ; $ j < $ n ; $ j ++ ) { $ curr_xor = $ curr_xor ^ $ arr [ $ j ] ; $ ans = max ( $ ans , $ curr_xor ) ; } } return $ ans ; } $ arr = array ( 8 , 1 , 2 , 12 ) ; $ n = count ( $ arr ) ; echo " Max ▁ subarray ▁ XOR ▁ is ▁ " , maxSubarrayXOR ( $ arr , $ n ) ; ? >
Given a string , find its first non | PHP program to find first non - repeating character ; calculate count of characters in the passed string ; The method returns index of first non - repeating character in a string . If all characters are repeating then returns - 1 ; Driver code
< ? php $ NO_OF_CHARS = 256 ; $ count = array_fill ( 0 , 200 , 0 ) ; function getCharCountArray ( $ str ) { global $ count ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) $ count [ ord ( $ str [ $ i ] ) ] ++ ; } function firstNonRepeating ( $ str ) { global $ count ; getCharCountArray ( $ str ) ; $ index = -1 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ count [ ord ( $ str [ $ i ] ) ] == 1 ) { $ index = $ i ; break ; } } return $ index ; } $ str = " geeksforgeeks " ; $ index = firstNonRepeating ( $ str ) ; if ( $ index == -1 ) echo " Either ▁ all ▁ characters ▁ are " . " ▁ repeating ▁ or ▁ string ▁ is ▁ empty " ; else echo " First ▁ non - repeating ▁ " . " character ▁ is ▁ " . $ str [ $ index ] ; ? >
Divide a string in N equal parts | Function to print n equal parts of str ; Check if string can be divided in n equal parts ; Calculate the size of parts to find the division point ; Driver Code length od string is 28 ; Print 4 equal parts of the string
< ? php function divideString ( $ str , $ n ) { $ str_size = strlen ( $ str ) ; $ i ; $ part_size ; if ( $ str_size % $ n != 0 ) { echo " Invalid ▁ Input : ▁ String ▁ size " ; echo " ▁ is ▁ not ▁ divisible ▁ by ▁ n " ; return ; } $ part_size = $ str_size / $ n ; for ( $ i = 0 ; $ i < $ str_size ; $ i ++ ) { if ( $ i % $ part_size == 0 ) echo " STRNEWLINE " ; echo $ str [ $ i ] ; } } $ str = " a _ simple _ divide _ string _ quest " ; divideString ( $ str , 4 ) ; ? >
Find Nth term ( A matrix exponentiation example ) | PHP program to find n - th term of a recursive function using matrix exponentiation . ; This power function returns first row of { Transformation Matrix } ^ n - 1 * Initial Vector ; This is an identity matrix . ; this is Transformation matrix . ; Matrix exponentiation to calculate power of { tMat } ^ n - 1 store res in " res " matrix . ; res store { Transformation matrix } ^ n - 1 hence will be first row of res * Initial Vector . ; Driver code
< ? php $ MOD = 1000000009 ; function power ( $ n ) { global $ MOD ; if ( $ n <= 1 ) return 1 ; $ n -- ; $ res = array ( array ( 1 , 0 ) , array ( 0 , 1 ) ) ; $ tMat = array ( array ( 2 , 3 ) , array ( 1 , 0 ) ) ; while ( $ n ) { if ( $ n & 1 ) { $ tmp = array_fill ( 0 , 2 , array_fill ( 0 , 2 , 0 ) ) ; $ tmp [ 0 ] [ 0 ] = ( $ res [ 0 ] [ 0 ] * $ tMat [ 0 ] [ 0 ] + $ res [ 0 ] [ 1 ] * $ tMat [ 1 ] [ 0 ] ) % $ MOD ; $ tmp [ 0 ] [ 1 ] = ( $ res [ 0 ] [ 0 ] * $ tMat [ 0 ] [ 1 ] + $ res [ 0 ] [ 1 ] * $ tMat [ 1 ] [ 1 ] ) % $ MOD ; $ tmp [ 1 ] [ 0 ] = ( $ res [ 1 ] [ 0 ] * $ tMat [ 0 ] [ 0 ] + $ res [ 1 ] [ 1 ] * $ tMat [ 1 ] [ 0 ] ) % $ MOD ; $ tmp [ 1 ] [ 1 ] = ( $ res [ 1 ] [ 0 ] * $ tMat [ 0 ] [ 1 ] + $ res [ 1 ] [ 1 ] * $ tMat [ 1 ] [ 1 ] ) % $ MOD ; $ res [ 0 ] [ 0 ] = $ tmp [ 0 ] [ 0 ] ; $ res [ 0 ] [ 1 ] = $ tmp [ 0 ] [ 1 ] ; $ res [ 1 ] [ 0 ] = $ tmp [ 1 ] [ 0 ] ; $ res [ 1 ] [ 1 ] = $ tmp [ 1 ] [ 1 ] ; } $ n = ( int ) ( $ n / 2 ) ; $ tmp = array_fill ( 0 , 2 , array_fill ( 0 , 2 , 0 ) ) ; $ tmp [ 0 ] [ 0 ] = ( $ tMat [ 0 ] [ 0 ] * $ tMat [ 0 ] [ 0 ] + $ tMat [ 0 ] [ 1 ] * $ tMat [ 1 ] [ 0 ] ) % $ MOD ; $ tmp [ 0 ] [ 1 ] = ( $ tMat [ 0 ] [ 0 ] * $ tMat [ 0 ] [ 1 ] + $ tMat [ 0 ] [ 1 ] * $ tMat [ 1 ] [ 1 ] ) % $ MOD ; $ tmp [ 1 ] [ 0 ] = ( $ tMat [ 1 ] [ 0 ] * $ tMat [ 0 ] [ 0 ] + $ tMat [ 1 ] [ 1 ] * $ tMat [ 1 ] [ 0 ] ) % $ MOD ; $ tmp [ 1 ] [ 1 ] = ( $ tMat [ 1 ] [ 0 ] * $ tMat [ 0 ] [ 1 ] + $ tMat [ 1 ] [ 1 ] * $ tMat [ 1 ] [ 1 ] ) % $ MOD ; $ tMat [ 0 ] [ 0 ] = $ tmp [ 0 ] [ 0 ] ; $ tMat [ 0 ] [ 1 ] = $ tmp [ 0 ] [ 1 ] ; $ tMat [ 1 ] [ 0 ] = $ tmp [ 1 ] [ 0 ] ; $ tMat [ 1 ] [ 1 ] = $ tmp [ 1 ] [ 1 ] ; } return ( $ res [ 0 ] [ 0 ] * 1 + $ res [ 0 ] [ 1 ] * 1 ) % $ MOD ; } $ n = 3 ; echo power ( $ n ) ; ? >
Place k elements such that minimum distance is maximized | Returns true if it is possible to arrange k elements of arr [ 0. . n - 1 ] with minimum distance given as mid . ; Place first element at arr [ 0 ] position ; Initialize count of elements placed . ; Try placing k elements with minimum distance mid . ; Place next element if its distance from the previously placed element is greater than current mid ; Return if all elements are placed successfully ; Returns largest minimum distance for k elements in arr [ 0. . n - 1 ] . If elements can 't be placed, returns -1. ; Sort the positions ; Initialize result . ; Consider the maximum possible distance ; Do binary search for largest minimum distance ; If it is possible to place k elements with minimum distance mid , search for higher distance . ; Change value of variable max to mid iff all elements can be successfully placed ; If not possible to place k elements , search for lower distance ; Driver Code
< ? php function isFeasible ( $ mid , $ arr , $ n , $ k ) { $ pos = $ arr [ 0 ] ; $ elements = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] - $ pos >= $ mid ) { $ pos = $ arr [ $ i ] ; $ elements ++ ; if ( $ elements == $ k ) return true ; } } return 0 ; } function largestMinDist ( $ arr , $ n , $ k ) { sort ( $ arr ) ; $ res = -1 ; $ left = 1 ; $ right = $ arr [ $ n - 1 ] ; while ( $ left < $ right ) { $ mid = ( $ left + $ right ) / 2 ; if ( isFeasible ( $ mid , $ arr , $ n , $ k ) ) { $ res = max ( $ res , $ mid ) ; $ left = $ mid + 1 ; } else $ right = $ mid ; } return $ res ; } $ arr = array ( 1 , 2 , 8 , 4 , 9 ) ; $ n = sizeof ( $ arr ) ; $ k = 3 ; echo largestMinDist ( $ arr , $ n , $ k ) ; ? >
Square root of an integer | Returns floor of square root of x ; Base cases ; Do Binary Search for floor ( sqrt ( x ) ) ; If x is a perfect square ; Since we need floor , we update answer when mid * mid is smaller than x , and move closer to sqrt ( x ) ; If mid * mid is greater than x ; Driver Code
< ? php function floorSqrt ( $ x ) { if ( $ x == 0 $ x == 1 ) return $ x ; $ start = 1 ; $ end = $ x ; $ ans ; while ( $ start <= $ end ) { $ mid = ( $ start + $ end ) / 2 ; if ( $ mid * $ mid == $ x ) return $ mid ; if ( $ mid * $ mid < $ x ) { $ start = $ mid + 1 ; $ ans = $ mid ; } else $ end = $ mid - 1 ; } return $ ans ; } $ x = 11 ; echo floorSqrt ( $ x ) , " STRNEWLINE " ; ? >
Count of distinct rectangles inscribed in an equilateral triangle | Function to return the count of rectangles when n is odd ; Calculating number of dots in vertical level ; Calculating number of ways to select two points in the horizontal level i ; Multiply both to obtain the number of rectangles formed at that level ; Calculating number of dots in vertical level ; Calculating number of ways to select two points in the horizontal level i ; Multiply both to obtain the number of rectangles formed at that level ; Function to return the count of rectangles when n is even ; Driver code ; If n is odd
< ? php function countOdd ( $ n ) { $ coun = 0 ; for ( $ i = $ n - 2 ; $ i >= 1 ; $ i -- ) { if ( $ i & 1 ) { $ m = ( $ n - $ i ) / 2 ; $ j = ( $ i * ( $ i + 1 ) ) / 2 ; $ coun += $ j * $ m ; } else { $ m = ( ( $ n - 1 ) - $ i ) / 2 ; $ j = ( $ i * ( $ i + 1 ) ) / 2 ; $ coun += $ j * $ m ; } } return $ coun ; } function countEven ( $ n ) { $ coun = 0 ; for ( $ i = $ n - 2 ; $ i >= 1 ; $ i -- ) { if ( $ i & 1 ) { $ m = ( ( $ n - 1 ) - i ) / 2 ; $ j = ( $ i * ( $ i + 1 ) ) / 2 ; $ coun += $ j * $ m ; } else { $ m = ( $ n - $ i ) / 2 ; $ j = ( $ i * ( $ i + 1 ) ) / 2 ; $ coun += $ j * $ m ; } } return $ coun ; } $ n = 5 ; if ( $ n & 1 ) echo countOdd ( $ n ) ; else echo countEven ( $ n ) ; ? >
Area of the biggest ellipse inscribed within a rectangle | Function to find the area of the ellipse ; The sides cannot be negative ; Area of the ellipse ; Driver code
< ? php function ellipse ( $ l , $ b ) { if ( $ l < 0 $ b < 0 ) return -1 ; $ x = ( 3.14 * $ l * $ b ) / 4 ; return $ x ; } $ l = 5 ; $ b = 3 ; echo ellipse ( $ l , $ b ) . " STRNEWLINE " ; ? >
Determine the number of squares of unit area that a given line will pass through . | Function to return the required position ;  ; Driver Code
< ? php function noOfSquares ( $ x1 , $ y1 , $ x2 , $ y2 ) { $ dx = abs ( $ x2 - $ x1 ) ; $ dy = abs ( $ y2 - $ y1 ) ; $ ans = $ dx + $ dy - gcd ( $ dx , $ dy ) ; echo ( $ ans ) ; } function gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return gcd ( $ b , $ a % $ b ) ; } $ x1 = 1 ; $ y1 = 1 ; $ x2 = 4 ; $ y2 = 3 ; noOfSquares ( $ x1 , $ y1 , $ x2 , $ y2 ) ; ? >
Count paths with distance equal to Manhattan distance | Function to return the value of nCk ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; Function to return the number of paths ; Difference between the ' x ' coordinates of the given points ; Difference between the ' y ' coordinates of the given points ; Driver code
< ? php function binomialCoeff ( $ n , $ k ) { $ res = 1 ; if ( $ k > $ n - $ k ) $ k = $ n - $ k ; for ( $ i = 0 ; $ i < $ k ; ++ $ i ) { $ res *= ( $ n - $ i ) ; $ res /= ( $ i + 1 ) ; } return $ res ; } function countPaths ( $ x1 , $ y1 , $ x2 , $ y2 ) { $ m = abs ( $ x1 - $ x2 ) ; $ n = abs ( $ y1 - $ y2 ) ; return ( binomialCoeff ( $ m + $ n , $ n ) ) ; } { $ x1 = 2 ; $ y1 = 3 ; $ x2 = 4 ; $ y2 = 5 ; echo ( countPaths ( $ x1 , $ y1 , $ x2 , $ y2 ) ) ; }
Find the area of largest circle inscribed in ellipse | PHP program to find the area of the circle ; Area of the Reuleaux triangle ; Driver Code
< ? php $ GLOBALS [ ' pi ' ] = 3.1415926 ; function areaCircle ( $ b ) { $ area = $ GLOBALS [ ' pi ' ] * $ b * $ b ; return $ area ; } $ a = 10 ; $ b = 8 ; echo round ( areaCircle ( $ b ) , 3 ) ; ? >
Section formula for 3 D | Function to find the section of the line ; Applying section formula ; Printing result ; Driver code
< ? php function section ( $ x1 , $ x2 , $ y1 , $ y2 , $ z1 , $ z2 , $ m , $ n ) { $ x = ( ( $ m * $ x2 ) + ( $ n * $ x1 ) ) / ( $ m + $ n ) ; $ y = ( ( $ m * $ y2 ) + ( $ n * $ y1 ) ) / ( $ m + $ n ) ; $ z = ( ( $ m * $ z2 ) + ( $ n * $ z1 ) ) / ( $ m + $ n ) ; echo " ( " . $ x . " , " ; ▁ echo ▁ $ y ▁ . ▁ " , " ; ▁ echo ▁ $ z ▁ . ▁ " ) " ▁ . " " } $ x1 = 2 ; $ x2 = 4 ; $ y1 = -1 ; $ y2 = 3 ; $ z1 = 4 ; $ z2 = 2 ; $ m = 2 ; $ n = 3 ; section ( $ x1 , $ x2 , $ y1 , $ y2 , $ z1 , $ z2 , $ m , $ n ) ;
Program to find the Circumcircle of any regular polygon | Function to find the radius of the circumcircle ; these cannot be negative ; Radius of the circumcircle ; Return the radius ; Driver code ; Find the radius of the circumcircle
< ? php function findRadiusOfcircumcircle ( $ n , $ a ) { if ( $ n < 0 $ a < 0 ) return -1 ; $ radius = $ a / sqrt ( 2 - ( 2 * cos ( 360 / $ n ) ) ) ; return $ radius ; } $ n = 5 ; $ a = 6 ; echo findRadiusOfcircumcircle ( $ n , $ a ) ; ? >
Program to find the Radius of the incircle of the triangle | Function to find the radius of the incircle ; the sides cannot be negative ; semi - perimeter of the circle ; area of the triangle ; Radius of the incircle ; Return the radius ; Get the sides of the triangle ; Find the radius of the incircle
< ? php function findRadiusOfIncircle ( $ a , $ b , $ c ) { if ( $ a < 0 $ b < 0 $ c < 0 ) return -1 ; $ p = ( $ a + $ b + $ c ) / 2 ; $ area = sqrt ( $ p * ( $ p - $ a ) * ( $ p - $ b ) * ( $ p - $ c ) ) ; $ radius = $ area / $ p ; return $ radius ; } $ a = 2 ; $ b = 2 ; $ c = 3 ; echo findRadiusOfIncircle ( $ a , $ b , $ c ) . " " ;
Find area of triangle if two vectors of two adjacent sides are given | function to calculate area of triangle ; Driver Code
< ? php function area ( $ x1 , $ y1 , $ z1 , $ x2 , $ y2 , $ z2 ) { $ area = sqrt ( pow ( ( $ y1 * $ z2 - $ y2 * $ z1 ) , 2 ) + pow ( ( $ x1 * $ z2 - $ x2 * $ z1 ) , 2 ) + pow ( ( $ x1 * $ y2 - $ x2 * $ y1 ) , 2 ) ) ; $ area = $ area / 2 ; return $ area ; } $ x1 = -2 ; $ y1 = 0 ; $ z1 = -5 ; $ x2 = 1 ; $ y2 = -2 ; $ z2 = -1 ; $ a = area ( $ x1 , $ y1 , $ z1 , $ x2 , $ y2 , $ z2 ) ; echo " Area = " . $ a ▁ . " " ? >
Largest trapezoid that can be inscribed in a semicircle | Function to find the area of the biggest trapezoid ; the radius cannot be negative ; area of the trapezoid ; Driver code
< ? php function trapezoidarea ( $ r ) { if ( $ r < 0 ) return -1 ; $ a = ( 3 * sqrt ( 3 ) * pow ( $ r , 2 ) ) / 4 ; return $ a ; } $ r = 5 ; echo trapezoidarea ( $ r ) . " STRNEWLINE " ; ? >
Largest rectangle that can be inscribed in a semicircle | Function to find the area of the biggest rectangle ; the radius cannot be negative ; area of the rectangle ; Driver code
< ? php function rectanglearea ( $ r ) { if ( $ r < 0 ) return -1 ; $ a = $ r * $ r ; return $ a ; } $ r = 5 ; echo rectanglearea ( $ r ) . " STRNEWLINE " ; ? >
Maximum distinct lines passing through a single point | function to find maximum lines which passes through a single point ; Driver Code
< ? php function maxLines ( $ n , $ x1 , $ y1 , $ x2 , $ y2 ) { $ s = array ( ) ; $ slope ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { if ( $ x1 [ $ i ] == $ x2 [ $ i ] ) $ slope = PHP_INT_MAX ; else $ slope = ( $ y2 [ $ i ] - $ y1 [ $ i ] ) * 1.0 / ( $ x2 [ $ i ] - $ x1 [ $ i ] ) * 1.0 ; array_push ( $ s , $ slope ) ; } return count ( $ s ) ; } $ n = 2 ; $ x1 = array ( 1 , 2 ) ; $ y1 = array ( 1 , 2 ) ; $ x2 = array ( 2 , 4 ) ; $ y2 = array ( 2 , 10 ) ; echo maxLines ( $ n , $ x1 , $ y1 , $ x2 , $ y2 ) ; ? >
Find area of parallelogram if vectors of two adjacent sides are given | Function to calculate area of parallelogram ; Driver Code
< ? php function area ( $ x1 , $ y1 , $ z1 , $ x2 , $ y2 , $ z2 ) { $ area = sqrt ( pow ( ( $ y1 * $ z2 - $ y2 * $ z1 ) , 2 ) + pow ( ( $ x1 * $ z2 - $ x2 * $ z1 ) , 2 ) + pow ( ( $ x1 * $ y2 - $ x2 * $ y1 ) , 2 ) ) ; return $ area ; } $ x1 = 3 ; $ y1 = 1 ; $ z1 = -2 ; $ x2 = 1 ; $ y2 = -3 ; $ z2 = 4 ; $ a = area ( $ x1 , $ y1 , $ z1 , $ x2 , $ y2 , $ z2 ) ; echo ( " Area ▁ = ▁ " ) ; echo ( $ a ) ; ? >
Maximum possible intersection by moving centers of line segments | Function to print the maximum intersection ; Case 1 ; Case 2 ; Case 3 ; Driver Code
< ? php function max_intersection ( $ center , $ length , $ k ) { sort ( $ center ) ; if ( $ center [ 2 ] - $ center [ 0 ] >= 2 * $ k + $ length ) { return 0 ; } else if ( $ center [ 2 ] - $ center [ 0 ] >= 2 * $ k ) { return ( 2 * $ k - ( $ center [ 2 ] - $ center [ 0 ] - $ length ) ) ; } else return $ length ; } $ center = array ( 1 , 2 , 3 ) ; $ L = 1 ; $ K = 1 ; echo max_intersection ( $ center , $ L , $ K ) ; ? >
Haversine formula to find distance between two points on a sphere | PHP program for the haversine formula ; distance between latitudes and longitudes ; convert to radians ; apply formulae ; Driver code
< ? php function haversine ( $ lat1 , $ lon1 , $ lat2 , $ lon2 ) { $ dLat = ( $ lat2 - $ lat1 ) * M_PI / 180.0 ; $ dLon = ( $ lon2 - $ lon1 ) * M_PI / 180.0 ; $ lat1 = ( $ lat1 ) * M_PI / 180.0 ; $ lat2 = ( $ lat2 ) * M_PI / 180.0 ; $ a = pow ( sin ( $ dLat / 2 ) , 2 ) + pow ( sin ( $ dLon / 2 ) , 2 ) * cos ( $ lat1 ) * cos ( $ lat2 ) ; $ rad = 6371 ; $ c = 2 * asin ( sqrt ( $ a ) ) ; return $ rad * $ c ; } $ lat1 = 51.5007 ; $ lon1 = 0.1246 ; $ lat2 = 40.6892 ; $ lon2 = 74.0445 ; echo haversine ( $ lat1 , $ lon1 , $ lat2 , $ lon2 ) . " ▁ K . M . " ; ? >
Pentatope number | Function that returns nth pentatope number ; For 5 th PentaTope Number ; For 11 th PentaTope Number
< ? php function pentatopeNum ( $ n ) { return ( $ n * ( $ n + 1 ) * ( $ n + 2 ) * ( $ n + 3 ) ) / 24 ; } $ n = 5 ; echo pentatopeNum ( $ n ) , " STRNEWLINE " ; $ n = 11 ; echo pentatopeNum ( $ n ) , " STRNEWLINE " ; ? >
Heptagonal number | Function to return Nth Heptagonal number ; Driver Code
< ? php function heptagonalNumber ( $ n ) { return ( ( 5 * $ n * $ n ) - ( 3 * $ n ) ) / 2 ; } $ n = 2 ; echo heptagonalNumber ( $ n ) , " STRNEWLINE " ; $ n = 15 ; echo heptagonalNumber ( $ n ) ; ? >
Icosidigonal number | Function to calculate Icosidigonal number ; Formula for finding nth Icosidigonal number ; Driver Code
< ? php function icosidigonal_num ( $ n ) { return ( 20 * $ n * $ n - 18 * $ n ) / 2 ; } $ n = 4 ; echo $ n , " th ▁ Icosidigonal ▁ number ▁ : ▁ " , icosidigonal_num ( $ n ) ; echo " STRNEWLINE " ; $ n = 8 ; echo $ n , " th ▁ Icosidigonal ▁ number ▁ : ▁ " , icosidigonal_num ( $ n ) ; ? >
Hypercube Graph | PHP program to find vertices in a hypercube graph of order n ; Function to find power of 2 ; Driver Code
< ? php { function power ( $ n ) { if ( $ n == 1 ) return 2 ; return 2 * power ( $ n - 1 ) ; } { $ n = 4 ; echo ( power ( $ n ) ) ; } } ? >
Reflection of a point at 180 degree rotation of another point | PHP Program for find the 180 degree reflection of one point around another point . ; Driver Code
< ? php function findPoint ( $ x1 , $ y1 , $ x2 , $ y2 ) { echo " ( " , 2 * $ x2 - $ x1 , " , ▁ " , 2 * $ y2 - $ y1 , " ) " ; } $ x1 = 0 ; $ y1 = 0 ; $ x2 = 1 ; $ y2 = 1 ; findPoint ( $ x1 , $ y1 , $ x2 , $ y2 ) ; ? >
Program to check if the points are parallel to X axis or Y axis | To check for parallel line ; checking for parallel to X and Y axis condition ; To display the output ; Driver 's Code
< ? php function parallel ( $ n , $ a ) { $ x = true ; $ y = true ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ a [ $ i ] [ 0 ] != $ a [ $ i + 1 ] [ 0 ] ) $ x = false ; if ( $ a [ $ i ] [ 1 ] != $ a [ $ i + 1 ] [ 1 ] ) $ y = false ; } if ( $ x ) echo " parallel ▁ to ▁ Y ▁ Axis " ; else if ( y ) echo " parallel ▁ to ▁ X ▁ Axis " ; else echo " Not ▁ parallel ▁ to ▁ X " , " ▁ and ▁ Y ▁ Axis " ; } $ a = array ( array ( 1 , 2 ) , array ( 1 , 4 ) , array ( 1 , 6 ) , array ( 1 , 0 ) ) ; $ n = count ( $ a ) ; parallel ( $ n , $ a ) ; ? >
Triangular Matchstick Number | PHP program to find X - th triangular matchstick number ; Driver code
< ? php function numberOfSticks ( $ x ) { return ( 3 * $ x * ( $ x + 1 ) ) / 2 ; } echo ( numberOfSticks ( 7 ) ) ; ? >
Area of square Circumscribed by Circle | Function to find area of square ; Radius of a circle ; Call Function to find an area of square
< ? php function find_Area ( $ r ) { return ( 2 * $ r * $ r ) ; } $ r = 3 ; echo ( " Area ▁ of ▁ square ▁ = ▁ " ) ; echo ( find_Area ( $ r ) ) ; ? >
Check whether triangle is valid or not if sides are given | function to check if three sider form a triangle or not ; check condition ; Driver Code ; function calling and print output
< ? php function checkValidity ( $ a , $ b , $ c ) { if ( $ a + $ b <= $ c $ a + $ c <= $ b $ b + $ c <= $ a ) return false ; else return true ; } $ a = 7 ; $ b = 10 ; $ c = 5 ; if ( checkValidity ( $ a , $ b , $ c ) ) echo " Valid " ; else echo " Invalid " ; ? >