codeparrot/xlcost-text-to-code huggingface dataset:
Count number of substrings with exactly k distinct characters | Function to count number of substrings with exactly k unique characters ; Initialize result ; To store count of characters from ' a ' to ' z ' ; Consider all substrings beginning with str [ i ] ; Initializing count array with 0 ; Consider all substrings between str [ i . . j ] ; If this is a new character for this substring , increment dist_count . ; Increment count of current character ; If distinct character count becomes k , then increment result . ; Driver code
< ? php function countkDist ( $ str , $ k ) { $ res = 0 ; $ n = strlen ( $ str ) ; $ cnt = array ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ dist_count = 0 ; $ cnt = array_fill ( 0 , 0 , true ) ; for ( $ j = $ i ; $ j < $ n ; $ j ++ ) { if ( $ cnt [ ord ( $ str [ $ j ] ) - ord ( ' a ' ) ] == 0 ) $ dist_count ++ ; $ cnt [ ord ( $ str [ $ j ] ) - ord ( ' a ' ) ] ++ ; if ( $ dist_count == $ k ) $ res ++ ; } } return $ res ; } { $ ch = " abcbaa " ; $ k = 3 ; echo ( " Total ▁ substrings ▁ with ▁ exactly ▁ " . $ k . " ▁ distinct ▁ characters ▁ : ▁ " . countkDist ( $ ch , $ k ) ) ; }
Remove repeated digits in a given number | PHP program to remove repeated digits ; Store first digits as previous digit ; Initialize power ; Iterate through all digits of n , note that the digits are processed from least significant digit to most significant digit . ; Store current digit ; Add the current digit to the beginning of result ; Update previous result and power ; Remove last digit from n ; Driver Code
< ? php function removeRecur ( $ n ) { $ prev_digit = $ n % 10 ; $ pow = 10 ; $ res = $ prev_digit ; while ( $ n ) { $ curr_digit = $ n % 10 ; if ( $ curr_digit != $ prev_digit ) { $ res += $ curr_digit * $ pow ; $ prev_digit = $ curr_digit ; $ pow *= 10 ; } $ n = $ n / 10 ; } return $ res ; } $ n = 12224 ; echo removeRecur ( $ n ) ; ? >
Generate all the binary strings of N bits | Function to print the output ; Function to generate all binary strings ; First assign "0" at ith position and try for all other permutations for remaining positions ; And then assign "1" at ith position and try for all other permutations for remaining positions ; Driver Code ; Print all binary strings
< ? php function printTheArray ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { echo $ arr [ $ i ] , " " ; } echo " STRNEWLINE " ; } function generateAllBinaryStrings ( $ n , $ arr , $ i ) { if ( $ i == $ n ) { printTheArray ( $ arr , $ n ) ; return ; } $ arr [ $ i ] = 0 ; generateAllBinaryStrings ( $ n , $ arr , $ i + 1 ) ; $ arr [ $ i ] = 1 ; generateAllBinaryStrings ( $ n , $ arr , $ i + 1 ) ; } $ n = 4 ; $ arr = array_fill ( 0 , $ n , 0 ) ; generateAllBinaryStrings ( $ n , $ arr , 0 ) ; ? >
Largest number N which can be reduced to 0 in K steps | Utility function to return the first digit of a number . ; Remove last digit from number till only one digit is left ; return the first digit ; Utility function that returns the count of numbers written down when starting from n ; Function to find the largest number N which can be reduced to 0 in K steps ; Get the sequence length of the mid point ; Until k sequence length is reached ; Update mid point ; Get count of the new mid point ; Update right to mid ; Update left to mid ; Increment mid point by one while count is equal to k to get the maximum value of mid point ; Driver Code
< ? php function firstDigit ( $ n ) { while ( $ n >= 10 ) { $ n = ( int ) ( $ n / 10 ) ; } return $ n ; } function getCount ( $ n ) { $ count = 1 ; while ( $ n != 0 ) { $ leadDigit = firstDigit ( $ n ) ; $ n -= $ leadDigit ; $ count ++ ; } return $ count ; } function getLargestNumber ( $ k ) { $ left = $ k ; $ right = $ k * 10 ; $ mid = ( int ) ( ( $ left + $ right ) / 2 ) ; $ len = getCount ( $ mid ) ; while ( $ len != $ k ) { $ mid = ( int ) ( ( $ left + $ right ) / 2 ) ; $ len = getCount ( $ mid ) ; if ( $ len > $ k ) { $ right = $ mid ; } else { $ left = $ mid ; } } while ( $ len == $ k ) { if ( $ len != getCount ( $ mid + 1 ) ) { break ; } $ mid ++ ; } return ( $ mid ) ; } $ k = 3 ; echo ( getLargestNumber ( $ k ) ) ; ? >
Find the Diameter or Longest chord of a Circle | Function to find the longest chord ; Get the radius ; Find the diameter
< ? php function diameter ( $ r ) { echo " The ▁ length ▁ of ▁ the ▁ longest ▁ chord " , " ▁ or ▁ diameter ▁ of ▁ the ▁ circle ▁ is ▁ " , 2 * $ r << " STRNEWLINE " ; } $ r = 4 ; diameter ( $ r ) ; ? >
Slope of the line parallel to the line with the given slope | Function to return the slope of the line which is parallel to the line with the given slope ; Driver code
< ? php function getSlope ( $ m ) { return $ m ; } $ m = 2 ; echo getSlope ( $ m ) ; ? >
Total number of triangles formed when there are H horizontal and V vertical lines | Function to return total triangles ; Only possible triangle is the given triangle ; If only vertical lines are present ; If only horizontal lines are present ; Return total triangles ; Driver code
< ? php function totalTriangles ( $ h , $ v ) { if ( $ h == 0 && $ v == 0 ) return 1 ; if ( $ h == 0 ) return ( ( $ v + 1 ) * ( $ v + 2 ) / 2 ) ; if ( $ v == 0 ) return ( $ h + 1 ) ; $ Total = ( $ h + 1 ) * ( ( $ v + 1 ) * ( $ v + 2 ) / 2 ) ; return $ Total ; } $ h = 2 ; $ v = 2 ; echo totalTriangles ( $ h , $ v ) ; ? >
Largest sphere that can be inscribed in a right circular cylinder inscribed in a frustum | Function to find the biggest sphere ; the radii and height cannot be negative ; radius of the sphere ; volume of the sphere ; Driver code
< ? php function sph ( $ r , $ R , $ h ) { if ( $ r < 0 && $ R < 0 && $ h < 0 ) return -1 ; $ x = $ r ; $ V = ( 4 * 3.14 * pow ( $ r , 3 ) ) / 3 ; return $ V ; } $ r = 5 ; $ R = 8 ; $ h = 11 ; echo sph ( $ r , $ R , $ h ) ; #This  Code is contributed by ajit.. NEW_LINE ? >
Check whether two straight lines are orthogonal or not | Function to check if two straight lines are orthogonal or not ; Both lines have infinite slope ; Only line 1 has infinite slope ; Only line 2 has infinite slope ; Find slopes of the lines ; Check if their product is - 1 ; Driver code
< ? php function checkOrtho ( $ x1 , $ y1 , $ x2 , $ y2 , $ x3 , $ y3 , $ x4 , $ y4 ) { if ( $ x2 - $ x1 == 0 && $ x4 - $ x3 == 0 ) return false ; else if ( $ x2 - $ x1 == 0 ) { $ m2 = ( int ) ( ( $ y4 - $ y3 ) / ( $ x4 - $ x3 ) ) ; if ( $ m2 == 0 ) return true ; else return false ; } else if ( $ x4 - $ x3 == 0 ) { $ m1 = ( int ) ( ( $ y2 - $ y1 ) / ( $ x2 - $ x1 ) ) ; if ( $ m1 == 0 ) return true ; else return false ; } else { $ m1 = ( int ) ( ( $ y2 - $ y1 ) / ( $ x2 - $ x1 ) ) ; $ m2 = ( int ) ( ( $ y4 - $ y3 ) / ( $ x4 - $ x3 ) ) ; if ( $ m1 * $ m2 == -1 ) return true ; else return false ; } } $ x1 = 0 ; $ y1 = 4 ; $ x2 = 0 ; $ y2 = -9 ; $ x3 = 2 ; $ y3 = 0 ; $ x4 = -1 ; $ y4 = 0 ; if ( checkOrtho ( $ x1 , $ y1 , $ x2 , $ y2 , $ x3 , $ y3 , $ x4 , $ y4 ) ) print ( " Yes " ) ; else print ( " No " ) ; ? >
Diagonal of a Regular Pentagon | Function to find the diagonal of a regular pentagon ; Side cannot be negative ; Length of the diagonal ; Driver code
< ? php function pentdiagonal ( $ a ) { if ( $ a < 0 ) return -1 ; $ d = 1.22 * $ a ; return $ d ; } $ a = 6 ; echo pentdiagonal ( $ a ) ; ? >
Area of hexagon with given diagonal length | Function to calculate area ; Formula to find area ; Driver Code
< ? php function hexagonArea ( $ d ) { return ( 3 * sqrt ( 3 ) * pow ( $ d , 2 ) ) / 8 ; } $ d = 10 ; echo " Area ▁ of ▁ hexagon : ▁ " , hexagonArea ( $ d ) ; ? >
Number of squares of side length required to cover an N * M rectangle | function to find number of squares of a * a required to cover n * m rectangle ; Driver code ; function call
< ? php function Squares ( $ n , $ m , $ a ) { return ( ( int ) ( ( $ m + $ a - 1 ) / $ a ) ) * ( ( int ) ( ( $ n + $ a - 1 ) / $ a ) ) ; } $ n = 6 ; $ m = 6 ; $ a = 4 ; echo Squares ( $ n , $ m , $ a ) ; ? >
Length of the Diagonal of the Octagon | Function to find the diagonal of the octagon ; side cannot be negative ; diagonal of the octagon ; Driver code
< ? php function octadiagonal ( $ a ) { if ( $ a < 0 ) return -1 ; return $ a * sqrt ( 4 + ( 2 * sqrt ( 2 ) ) ) ; } $ a = 4 ; echo octadiagonal ( $ a ) ; ? >
Program to Calculate the Perimeter of a Decagon | Function for finding the perimeter ; Driver Code
< ? php function CalPeri ( $ s ) { $ Perimeter = 10 * $ s ; echo " The ▁ Perimeter ▁ of ▁ Decagon ▁ is ▁ : ▁ $ Perimeter " ; } $ s = 5 ; CalPeri ( $ s ) ; ? >
Sum of lengths of all 12 edges of any rectangular parallelepiped | function to find the sum of all the edges of parallelepiped ; to calculate the length of one edge ; sum of all the edges of one side ; net sum will be equal to the summation of edges of all the sides ; initialize the area of three faces which has a common vertex
< ? php function findEdges ( $ s1 , $ s2 , $ s3 ) { $ a = sqrt ( $ s1 * $ s2 / $ s3 ) ; $ b = sqrt ( $ s3 * $ s1 / $ s2 ) ; $ c = sqrt ( $ s3 * $ s2 / $ s1 ) ; $ sum = $ a + $ b + $ c ; return 4 * $ sum ; } $ s1 ; $ s2 ; $ s3 ; $ s1 = 65 ; $ s2 = 156 ; $ s3 = 60 ; echo findEdges ( $ s1 , $ s2 , $ s3 ) ; ? >
Maximum number of pieces in N cuts | Function for finding maximum pieces with n cuts . ; to maximize number of pieces x is the horizontal cuts ; Now ( x ) is the horizontal cuts and ( n - x ) is vertical cuts , then maximum number of pieces = ( x + 1 ) * ( n - x + 1 ) ; Taking the maximum number of cuts allowed as 3 ; Finding and printing the max number of pieces
< ? php function findMaximumPieces ( $ n ) { $ x = ( int ) ( $ n / 2 ) ; return ( ( $ x + 1 ) * ( $ n - $ x + 1 ) ) ; } $ n = 3 ; echo " Max ▁ number ▁ of ▁ pieces ▁ for ▁ n ▁ = ▁ " . $ n . " ▁ is ▁ " . findMaximumPieces ( 3 ) ; ? >
Program to check whether 4 points in a 3 | Function to find equation of plane . ; checking if the 4 th point satisfies the above equation ; Driver Code ; function calling
< ? php function equation_plane ( $ x1 , $ y1 , $ z1 , $ x2 , $ y2 , $ z2 , $ x3 , $ y3 , $ z3 , $ x , $ y , $ z ) { $ a1 = $ x2 - $ x1 ; $ b1 = $ y2 - $ y1 ; $ c1 = $ z2 - $ z1 ; $ a2 = $ x3 - $ x1 ; $ b2 = $ y3 - $ y1 ; $ c2 = $ z3 - $ z1 ; $ a = $ b1 * $ c2 - $ b2 * $ c1 ; $ b = $ a2 * $ c1 - $ a1 * $ c2 ; $ c = $ a1 * $ b2 - $ b1 * $ a2 ; $ d = ( - $ a * $ x1 - $ b * $ y1 - $ c * $ z1 ) ; if ( $ a * $ x + $ b * $ y + $ c * $ z + $ d == 0 ) echo ( " Coplanar " ) ; else echo ( " Not ▁ Coplanar " ) ; } $ x1 = 3 ; $ y1 = 2 ; $ z1 = -5 ; $ x2 = -1 ; $ y2 = 4 ; $ z2 = -3 ; $ x3 = -3 ; $ y3 = 8 ; $ z3 = -5 ; $ x4 = -3 ; $ y4 = 2 ; $ z4 = 1 ; equation_plane ( $ x1 , $ y1 , $ z1 , $ x2 , $ y2 , $ z2 , $ x3 , $ y3 , $ z3 , $ x4 , $ y4 , $ z4 ) ; ? >
Angle between two Planes in 3D | Function to find Angle ; Driver Code
< ? php function distance ( $ a1 , $ b1 , $ c1 , $ a2 , $ b2 , $ c2 ) { $ d = ( $ a1 * $ a2 + $ b1 * $ b2 + $ c1 * $ c2 ) ; $ e1 = sqrt ( $ a1 * $ a1 + $ b1 * $ b1 + $ c1 * $ c1 ) ; $ e2 = sqrt ( $ a2 * $ a2 + $ b2 * $ b2 + $ c2 * $ c2 ) ; $ d = $ d / ( $ e1 * $ e2 ) ; $ pi = 3.14159 ; $ A = ( 180 / $ pi ) * ( acos ( $ d ) ) ; echo sprintf ( " Angle ▁ is ▁ % .2f ▁ degree " , $ A ) ; } $ a1 = 1 ; $ b1 = 1 ; $ c1 = 2 ; $ d1 = 1 ; $ a2 = 2 ; $ b2 = -1 ; $ c2 = 1 ; $ d2 = -4 ; distance ( $ a1 , $ b1 , $ c1 , $ a2 , $ b2 , $ c2 ) ; ? >
Mirror of a point through a 3 D plane | Function to mirror image ; Driver Code ; function call
< ? php function mirror_point ( $ a , $ b , $ c , $ d , $ x1 , $ y1 , $ z1 ) { $ k = ( - $ a * $ x1 - $ b * $ y1 - $ c * $ z1 - $ d ) / ( $ a * $ a + $ b * $ b + $ c * $ c ) ; $ x2 = $ a * $ k + $ x1 ; $ y2 = $ b * $ k + $ y1 ; $ z2 = $ c * $ k + $ z1 ; $ x3 = 2 * $ x2 - $ x1 ; $ y3 = 2 * $ y2 - $ y1 ; $ z3 = 2 * $ z2 - $ z1 ; echo sprintf ( " x3 ▁ = ▁ % .1f ▁ " , $ x3 ) ; echo sprintf ( " y3 ▁ = ▁ % .1f ▁ " , $ y3 ) ; echo sprintf ( " z3 ▁ = ▁ % .1f ▁ " , $ z3 ) ; } $ a = 1 ; $ b = -2 ; $ c = 0 ; $ d = 0 ; $ x1 = -1 ; $ y1 = 3 ; $ z1 = 4 ; mirror_point ( $ a , $ b , $ c , $ d , $ x1 , $ y1 , $ z1 ) ; ? >
Number of rectangles in a circle of radius R | Function to return the total possible rectangles that can be cut from the circle ; Diameter = 2 * $Radius ; Square of diameter which is the square of the maximum length diagonal ; generate all combinations of a and b in the range ( 1 , ( 2 * Radius - 1 ) ) ( Both inclusive ) ; Calculate the Diagonal length of this rectangle ; If this rectangle 's Diagonal  Length is less than the  Diameter, it is a valid  rectangle, thus increment counter ; Radius of the circle
< ? php function countRectangles ( $ radius ) { $ rectangles = 0 ; $ diameter = 2 * $ radius ; $ diameterSquare = $ diameter * $ diameter ; for ( $ a = 1 ; $ a < 2 * $ radius ; $ a ++ ) { for ( $ b = 1 ; $ b < 2 * $ radius ; $ b ++ ) { $ diagonalLengthSquare = ( $ a * $ a + $ b * $ b ) ; if ( $ diagonalLengthSquare <= $ diameterSquare ) { $ rectangles ++ ; } } } return $ rectangles ; } $ radius = 2 ; $ totalRectangles ; $ totalRectangles = countRectangles ( $ radius ) ; echo $ totalRectangles , " ▁ rectangles ▁ can ▁ be ▁ " , " cut ▁ from ▁ a ▁ circle ▁ of ▁ Radius ▁ " , $ radius ; ? >
Program to check similarity of given two triangles | Function for AAA similarity ; Check for AAA ; Function for SAS similarity ; angle b / w two smallest sides is largest . ; since we take angle b / w the sides . ; Function for SSS similarity ; Check for SSS ; Driver Code ; function call for AAA similarity ; function call for SSS similarity ; function call for SAS similarity ; Check if triangles are similar or not
< ? php function simi_aaa ( $ a1 , $ a2 ) { sort ( $ a1 ) ; sort ( $ a2 ) ; if ( $ a1 [ 0 ] == $ a2 [ 0 ] && $ a1 [ 1 ] == $ a2 [ 1 ] && $ a1 [ 2 ] == $ a2 [ 2 ] ) return 1 ; else return 0 ; } function simi_sas ( $ s1 , $ s2 , $ a1 , $ a2 ) { sort ( $ a1 ) ; sort ( $ a2 ) ; sort ( $ s1 ) ; sort ( $ s2 ) ; if ( $ s1 [ 0 ] / $ s2 [ 0 ] == $ s1 [ 1 ] / $ s2 [ 1 ] ) { if ( $ a1 [ 2 ] == $ a2 [ 2 ] ) return 1 ; } if ( $ s1 [ 1 ] / $ s2 [ 1 ] == $ s1 [ 2 ] / $ s2 [ 2 ] ) { if ( $ a1 [ 0 ] == $ a2 [ 0 ] ) return 1 ; } if ( $ s1 [ 2 ] / $ s2 [ 2 ] == $ s1 [ 0 ] / $ s2 [ 0 ] ) { if ( $ a1 [ 1 ] == $ a2 [ 1 ] ) return 1 ; } return 0 ; } function simi_sss ( $ s1 , $ s2 ) { sort ( $ s1 ) ; sort ( $ s2 ) ; if ( $ s1 [ 0 ] / $ s2 [ 0 ] == $ s1 [ 1 ] / $ s2 [ 1 ] && $ s1 [ 1 ] / $ s2 [ 1 ] == $ s1 [ 2 ] / $ s2 [ 2 ] && $ s1 [ 2 ] / $ s2 [ 2 ] == $ s1 [ 0 ] / $ s2 [ 0 ] ) return 1 ; return 0 ; } $ s1 = array ( 2 , 3 , 3 ) ; $ s2 = array ( 4 , 6 , 6 ) ; $ a1 = array ( 80 , 60 , 40 ) ; $ a2 = array ( 40 , 60 , 80 ) ; $ aaa = simi_aaa ( $ a1 , $ a2 ) ; $ sss = simi_sss ( $ s1 , $ s2 ) ; $ sas = simi_sas ( $ s1 , $ s2 , $ a1 , $ a2 ) ; if ( $ aaa == 1 $ sss == 1 $ sas == 1 ) { echo " Triangles ▁ are ▁ similar ▁ by ▁ " ; if ( $ aaa == 1 ) echo " AAA ▁ " ; if ( $ sss == 1 ) echo " SSS ▁ " ; if ( $ sas == 1 ) echo " SAS . " ; } else echo " Triangles ▁ are ▁ not ▁ similar " ; ? >
Centered Pentadecagonal Number | centered pentadecagonal function ; Formula to calculate nth centered pentadecagonal number ; Driver Code
< ? php function center_pentadecagonal_num ( $ n ) { return ( 15 * $ n * $ n - 15 * $ n + 2 ) / 2 ; } $ n = 3 ; echo $ n , " th ▁ number ▁ : ▁ " , center_pentadecagonal_num ( $ n ) ; echo " STRNEWLINE " ; $ n = 10 ; echo $ n , " th ▁ number ▁ : ▁ " , center_pentadecagonal_num ( $ n ) ; ? >
Centered nonadecagonal number | centered nonadecagonal function ; Formula to calculate nth centered nonadecagonal number ; Driver Code
< ? php function center_nonadecagon_num ( $ n ) { return ( 19 * $ n * $ n - 19 * $ n + 2 ) / 2 ; } $ n = 2 ; echo $ n , " th ▁ centered ▁ " + " nonadecagonal ▁ number ▁ : ▁ " , center_nonadecagon_num ( $ n ) ; echo " STRNEWLINE " ; $ n = 7 ; echo $ n , " th ▁ centered ▁ " + " nonadecagonal ▁ number ▁ : ▁ " , center_nonadecagon_num ( $ n ) ; ? >
Hendecagonal number | Function to find Hendecagonal number ; Formula to calculate nth Hendecagonal number ; Driver Code
< ? php function hendecagonal_num ( $ n ) { return ( 9 * $ n * $ n - 7 * $ n ) / 2 ; } $ n = 3 ; echo $ n , " th ▁ Hendecagonal ▁ number : ▁ " ; echo hendecagonal_num ( $ n ) ; echo " STRNEWLINE " ; $ n = 10 ; echo $ n , " th ▁ Hendecagonal ▁ number : ▁ " ; echo hendecagonal_num ( $ n ) ; ? >
Centered Octagonal Number | Centered octagonal number function ; Formula to calculate nth centered octagonal number & return it into main function . ; Driver Code
< ? php function cen_octagonalnum ( $ n ) { return ( 4 * $ n * $ n - 4 * $ n + 1 ) ; } $ n = 6 ; echo $ n , " th ▁ centered " , " ▁ octagonal ▁ number ▁ : ▁ " ; echo cen_octagonalnum ( $ n ) ; echo " STRNEWLINE " ; $ n = 11 ; echo $ n , " th ▁ centered " , " ▁ octagonal ▁ number ▁ : ▁ " ; echo cen_octagonalnum ( $ n ) ; ? >
Number of ordered points pair satisfying line equation | Checks if ( i , j ) is valid , a point ( i , j ) is valid if point ( arr [ i ] , arr [ j ] ) satisfies the equation y = mx + c And i is not equal to j ; check if i equals to j ; Equation LHS = y , and RHS = mx + c ; Returns the number of ordered pairs ( i , j ) for which point ( arr [ i ] , arr [ j ] ) satisfies the equation of the line y = mx + c ; for every possible ( i , j ) check if ( a [ i ] , a [ j ] ) satisfies the equation y = mx + c ; ( firstIndex , secondIndex ) is same as ( i , j ) ; check if ( firstIndex , secondIndex ) is a valid point ; Driver Code ; equation of line is y = mx + c
< ? php function isValid ( $ arr , $ i , $ j , $ m , $ c ) { if ( $ i == $ j ) return false ; $ lhs = $ arr [ $ j ] ; $ rhs = $ m * $ arr [ $ i ] + $ c ; return ( $ lhs == $ rhs ) ; } function findOrderedPoints ( $ arr , $ n , $ m , $ c ) { $ counter = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ firstIndex = $ i ; $ secondIndex = $ j ; if ( isValid ( $ arr , $ firstIndex , $ secondIndex , $ m , $ c ) ) $ counter ++ ; } } return $ counter ; } $ arr = array ( 1 , 2 , 3 , 4 , 2 ) ; $ n = count ( $ arr ) ; $ m = 1 ; $ c = 1 ; echo ( findOrderedPoints ( $ arr , $ n , $ m , $ c ) ) ; ? >
Check if a given circle lies completely inside the ring formed by two concentric circles | Function to check if circle lies in the ring ; distance between center of circle center of concentric circles ( origin ) using Pythagoras theorem ; Condition to check if circle is strictly inside the ring ; Both circle with radius ' r ' and ' R ' have center ( 0 , 0 )
< ? php function checkcircle ( $ r , $ R , $ r1 , $ x1 , $ y1 ) { $ dis = sqrt ( $ x1 * $ x1 + $ y1 * $ y1 ) ; return ( $ dis - $ r1 >= $ R && $ dis + $ r1 <= $ r ) ; } $ r = 8 ; $ R = 4 ; $ r1 = 2 ; $ x1 = 6 ; $ y1 = 0 ; if ( checkcircle ( $ r , $ R , $ r1 , $ x1 , $ y1 ) ) echo " yes " , " STRNEWLINE " ; else echo " no " , " STRNEWLINE " ; ? >
Program for Surface Area of Octahedron | utility Function ; Driver Code
< ? php function surface_area_octahedron ( $ side ) { return ( 2 * ( sqrt ( 3 ) ) * ( $ side * $ side ) ) ; } $ side = 7 ; echo ( " Surface ▁ area ▁ of ▁ octahedron ▁ = " ) ; echo ( surface_area_octahedron ( $ side ) ) ; ? >
Count of different straight lines with total n points with m collinear | Returns value of binomial coefficient ; $C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; function to calculate number of straight lines can be formed ; Driver Code
< ? php function nCk ( $ n , $ k ) { $ C = array_fill ( 0 , $ k + 1 , NULL ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = min ( $ i , $ k ) ; $ j > 0 ; $ j -- ) $ C [ $ j ] = $ C [ $ j ] + $ C [ $ j - 1 ] ; } return $ C [ $ k ] ; } function count_Straightlines ( $ n , $ m ) { return ( nCk ( $ n , 2 ) - nCk ( $ m , 2 ) + 1 ) ; } $ n = 4 ; $ m = 3 ; echo ( count_Straightlines ( $ n , $ m ) ) ; ? >
Calculate Volume of Dodecahedron | utility Function ; Driver Function
< ? php function vol_of_dodecahedron ( $ side ) { return ( ( ( 15 + ( 7 * ( sqrt ( 5 ) ) ) ) / 4 ) * ( pow ( $ side , 3 ) ) ) ; } $ side = 4 ; echo ( " Volume ▁ of ▁ dodecahedron ▁ = ▁ " ) ; echo ( vol_of_dodecahedron ( $ side ) ) ; ? >
Program to check if water tank overflows when n solid balls are dipped in the water tank | function to find if tank will overflow or not ; cylinder capacity ; volume of water in tank ; volume of n balls ; total volume of water and n dipped balls ; condition to check if tank is in overflow state or not ; giving dimensions ; calling function
< ? php function overflow ( $ H , $ r , $ h , $ N , $ R ) { $ tank_cap = 3.14 * $ r * $ r * $ H ; $ water_vol = 3.14 * $ r * $ r * $ h ; $ balls_vol = $ N * ( 4 / 3 ) * 3.14 * $ R * $ R * $ R ; $ vol = $ water_vol + $ balls_vol ; if ( $ vol > $ tank_cap ) { echo " Overflow " , " STRNEWLINE " ; } else { echo " Not ▁ in ▁ overflow ▁ state " , " STRNEWLINE " ; } } $ H = 10 ; $ r = 5 ; $ h = 5 ; $ N = 2 ; $ R = 2 ; overflow ( $ H , $ r , $ h , $ N , $ R ) ; ? >
Program to check if tank will overflow , underflow or filled in given time | function to calculate the volume of tank ; function to print overflow / filled / underflow accordingly ; radius of the tank ; height of the tank ; rate of flow of water ; time given ; calculate the required time ; printing the result
< ? php function volume ( $ radius , $ height ) { return ( ( 22 / 7 ) * $ radius * $ radius * $ height ) ; } function check_and_print ( $ required_time , $ given_time ) { if ( $ required_time < $ given_time ) echo ( " Overflow " ) ; else if ( $ required_time > $ given_time ) echo ( " Underflow " ) ; else echo ( " Filled " ) ; } $ radius = 5 ; $ height = 10 ; $ rate_of_flow = 10 ; $ given_time = 70.0 ; $ required_time = volume ( $ radius , $ height ) / $ rate_of_flow ; check_and_print ( $ required_time , $ given_time ) ; ? >
Program to find third side of triangle using law of cosines | Function to calculate cos value of angle c ; Converting degrees to radian ; Maps the sum along the series ; Holds the actual value of sin ( n ) ; Function to find third side ; Driver Code ; function call
< ? php function cal_cos ( $ n ) { $ accuracy = 0.0001 ; $ x1 ; $ denominator ; $ cosx ; $ cosval ; $ n = $ n * ( 3.142 / 180.0 ) ; $ x1 = 1 ; $ cosx = $ x1 ; $ cosval = cos ( $ n ) ; $ i = 1 ; do { $ denominator = 2 * $ i * ( 2 * $ i - 1 ) ; $ x1 = - $ x1 * $ n * $ n / $ denominator ; $ cosx = $ cosx + $ x1 ; $ i = $ i + 1 ; } while ( $ accuracy <= ( $ cosval - $ cosx ) ) ; return $ cosx ; } function third_side ( $ a , $ b , $ c ) { $ angle = cal_cos ( $ c ) ; return sqrt ( ( $ a * $ a ) + ( $ b * $ b ) - 2 * $ a * $ b * $ angle ) ; } $ c = 49 ; $ a = 5 ; $ b = 8 ; echo third_side ( $ a , $ b , $ c ) ; ? >
Check whether given circle resides in boundary maintained by two other circles | function to check if given circle fit in boundary or not ; Distance from the center ; Checking the corners of circle ; Radius of outer circle and inner circle respectively ; Co - ordinates and radius of the circle to be checked
< ? php function fitOrNotFit ( $ R , $ r , $ x , $ y , $ rad ) { $ val = sqrt ( pow ( $ x , 2 ) + pow ( $ y , 2 ) ) ; if ( $ val + $ rad <= $ R && $ val - $ rad >= $ R - $ r ) echo " Fits STRNEWLINE " ; else echo " Doesn ' t ▁ Fit STRNEWLINE " ; } $ R = 8 ; $ r = 4 ; $ x = 5 ; $ y = 3 ; $ rad = 3 ; fitOrNotFit ( $ R , $ r , $ x , $ y , $ rad ) ; ? >