codeparrot/xlcost-text-to-code huggingface dataset:
Find the value of XXXX ... . . ( N times ) % M where N is large | Iterative function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is >= p ; If y is odd , multiply x with result ; y must be even now y = y 2 ; Function to return XXX ... . . ( N times ) % M ; Return the mod by M of smaller numbers ; Creating a string of N X 's ; Converting the string to int and calculating the modulo ; Checking the parity of N ; Dividing the number into equal half ; Utilizing the formula for even N ; Dividing the number into equal half ; Utilizing the formula for odd N ; Driver code ; Print XXX ... ( N times ) % M
< ? php function power ( $ x , $ y , $ p ) { $ res = 1 ; $ x = $ x % $ p ; while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ y = $ y >> 1 ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function findModuloByM ( $ X , $ N , $ M ) { if ( $ N < 6 ) { $ temp = chr ( 48 + $ X ) * $ N ; $ res = intval ( $ temp ) % $ M ; return $ res ; } if ( $ N % 2 == 0 ) { $ half = findModuloByM ( $ X , ( int ) ( $ N / 2 ) , $ M ) % $ M ; $ res = ( $ half * power ( 10 , ( int ) ( $ N / 2 ) , $ M ) + $ half ) % $ M ; return $ res ; } else { $ half = findModuloByM ( $ X , ( int ) ( $ N / 2 ) , $ M ) % $ M ; $ res = ( $ half * power ( 10 , ( int ) ( $ N / 2 ) + 1 , $ M ) + $ half * 10 + $ X ) % $ M ; return $ res ; } } $ X = 6 ; $ N = 14 ; $ M = 9 ; print ( findModuloByM ( $ X , $ N , $ M ) ) ; ? >
Length of direct common tangent between two intersecting Circles | Function to find the length of the direct common tangent ; Driver code
< ? php function lengtang ( $ r1 , $ r2 , $ d ) { echo " The ▁ length ▁ of ▁ the ▁ direct ▁ common ▁ tangent ▁ is ▁ " , sqrt ( pow ( $ d , 2 ) - pow ( ( $ r1 - $ r2 ) , 2 ) ) ; } $ r1 = 4 ; $ r2 = 6 ; $ d = 3 ; lengtang ( $ r1 , $ r2 , $ d ) ; ? >
Radius of the circle when the width and height of an arc is given | Function to find the radius ; Driver code
< ? php function rad ( $ d , $ h ) { echo " The ▁ radius ▁ of ▁ the ▁ circle ▁ is ▁ " , ( ( $ d * $ d ) / ( 8 * $ h ) + $ h / 2 ) , " STRNEWLINE " ; } $ d = 4 ; $ h = 1 ; rad ( $ d , $ h ) ; ? >
Shortest distance from the centre of a circle to a chord | Function to find the shortest distance ; Driver code
< ? php function shortdis ( $ r , $ d ) { echo " The ▁ shortest ▁ distance ▁ " ; echo " from ▁ the ▁ chord ▁ to ▁ centre ▁ " ; echo sqrt ( ( $ r * $ r ) - ( ( $ d * $ d ) / 4 ) ) ; } $ r = 4 ; $ d = 3 ; shortdis ( $ r , $ d ) ; ? >
Length of direct common tangent between the two non | Function to find the length of the direct common tangent ; Driver code
< ? php function lengtang ( $ r1 , $ r2 , $ d ) { echo " The ▁ length ▁ of ▁ the ▁ direct " , " ▁ common ▁ tangent ▁ is ▁ " , sqrt ( pow ( $ d , 2 ) - pow ( ( $ r1 - $ r2 ) , 2 ) ) , " STRNEWLINE " ; } $ r1 = 4 ; $ r2 = 6 ; $ d = 12 ; lengtang ( $ r1 , $ r2 , $ d ) ; ? >
Biggest Square that can be inscribed within an Equilateral triangle | Function to find the side of the square ; the side cannot be negative ; side of the square ; Driver code
< ? php function square ( $ a ) { if ( $ a < 0 ) return -1 ; $ x = 0.464 * $ a ; return $ x ; } $ a = 5 ; echo square ( $ a ) ; ? >
Apothem of a n | Function to find the apothem of a regular polygon ; Side and side length cannot be negative ; Degree converted to radians ; Driver code
< ? php function polyapothem ( $ n , $ a ) { if ( $ a < 0 && $ n < 0 ) return -1 ; return $ a / ( 2 * tan ( ( 180 / $ n ) * 3.14159 / 180 ) ) ; } $ a = 9 ; $ n = 6 ; echo polyapothem ( $ n , $ a ) . " STRNEWLINE " ; ? >
Area of a n | Function to find the area of a regular polygon ; Side and side length cannot be negative ; Area degree converted to radians ; Driver code
< ? php function polyarea ( $ n , $ a ) { if ( $ a < 0 && $ n < 0 ) return -1 ; $ A = ( $ a * $ a * $ n ) / ( 4 * tan ( ( 180 / $ n ) * 3.14159 / 180 ) ) ; return $ A ; } $ a = 9 ; $ n = 6 ; echo round ( polyarea ( $ n , $ a ) , 3 ) ; ? >
Side of a regular n | Function to calculate the side of the polygon circumscribed in a circle ; Total sides of the polygon ; Radius of the circumscribing circle
< ? php function calculateSide ( $ n , $ r ) { $ theta ; $ theta_in_radians ; $ theta = 360 / $ n ; $ theta_in_radians = $ theta * 3.14 / 180 ; return 2 * $ r * sin ( $ theta_in_radians / 2 ) ; } $ n = 3 ; $ r = 5 ; echo calculateSide ( $ n , $ r ) ; ? >
Largest right circular cylinder within a frustum | Function to find the biggest right circular cylinder ; radii and height cannot be negative ; radius of right circular cylinder ; height of right circular cylinder ; volume of right circular cylinder ; Driver code
< ? php function cyl ( $ r , $ R , $ h ) { if ( $ h < 0 && $ r < 0 && $ R < 0 ) return -1 ; $ r1 = $ r ; $ h1 = $ h ; $ V = ( 3.14 * pow ( $ r1 , 2 ) * $ h1 ) ; return $ V ; } $ r = 7 ; $ R = 11 ; $ h = 6 ; echo cyl ( $ r , $ R , $ h ) ;
Program to find the Perimeter of a Regular Polygon | Function to calculate the perimeter ; Calculate Perimeter ; Get the number of sides ; Get the length of side ; find perimeter
< ? php function Perimeter ( $ s , $ n ) { $ perimeter = 1 ; $ perimeter = $ n * $ s ; return $ perimeter ; } $ n = 5 ; $ s = 2.5 ; $ peri = Perimeter ( $ s , $ n ) ; echo " Perimeter ▁ of ▁ Regular ▁ Polygon " , " ▁ with ▁ " , $ n , " ▁ sides ▁ of ▁ length ▁ " , $ s , " ▁ = ▁ " , $ peri ; ? >
Area of the biggest possible rhombus that can be inscribed in a rectangle | Function to find the area of the biggest rhombus ; the length and breadth cannot be negative ; area of the rhombus ; Driver code
< ? php function rhombusarea ( $ l , $ b ) { if ( $ l < 0 $ b < 0 ) return -1 ; return ( $ l * $ b ) / 2 ; } $ l = 16 ; $ b = 6 ; echo rhombusarea ( $ l , $ b ) . " STRNEWLINE " ;
Check if a point lies inside a rectangle | Set | function to find if given point lies inside a given rectangle or not . ; bottom - left and top - right corners of rectangle ; given point ; function call
< ? php function FindPoint ( $ x1 , $ y1 , $ x2 , $ y2 , $ x , $ y ) { if ( $ x > $ x1 and $ x < $ x2 and $ y > $ y1 and $ y < $ y2 ) return true ; return false ; } $ x1 = 0 ; $ y1 = 0 ; $ x2 = 10 ; $ y2 = 8 ; $ x = 1 ; $ y = 5 ; if ( FindPoint ( $ x1 , $ y1 , $ x2 , $ y2 , $ x , $ y ) ) echo " Yes " ; else echo " No " ; ? >
Distance between a point and a Plane in 3 D | Function to find distance ; Driver Code ; function call
< ? php function shortest_distance ( $ x1 , $ y1 , $ z1 , $ a , $ b , $ c , $ d ) { $ d = abs ( ( $ a * $ x1 + $ b * $ y1 + $ c * $ z1 + $ d ) ) ; $ e = sqrt ( $ a * $ a + $ b * $ b + $ c * $ c ) ; echo " Perpendicular ▁ distance ▁ is ▁ " . $ d / $ e ; } $ x1 = 4 ; $ y1 = -4 ; $ z1 = 3 ; $ a = 2 ; $ b = -2 ; $ c = 5 ; $ d = 8 ; shortest_distance ( $ x1 , $ y1 , $ z1 , $ a , $ b , $ c , $ d ) ; ? >
Program to find the Volume of a Triangular Prism | function to find the Volume of triangular prism ; formula to find Volume ; Driver Code ; function calling
< ? php function findVolume ( $ l , $ b , $ h ) { $ volume = ( $ l * $ b * $ h ) / 2 ; return $ volume ; } $ l = 18 ; $ b = 12 ; $ h = 9 ; echo " Volume ▁ of ▁ triangular ▁ prism : ▁ " . findVolume ( $ l , $ b , $ h ) ; ? >
Program to find the mid | function to find the midpoint of a line ; Driver Code
< ? php function midpoint ( $ x1 , $ x2 , $ y1 , $ y2 ) { echo ( ( float ) ( $ x1 + $ x2 ) / 2 . " ▁ , ▁ " . ( float ) ( $ y1 + $ y2 ) / 2 ) ; } $ x1 = -1 ; $ y1 = 2 ; $ x2 = 3 ; $ y2 = -6 ; midpoint ( $ x1 , $ x2 , $ y1 , $ y2 ) ; ? >
Arc length from given Angle | function to calculate arc length ; Driver Code
< ? php function arcLength ( $ diameter , $ angle ) { $ pi = 22.0 / 7.0 ; $ arc ; if ( $ angle >= 360 ) { echo " Angle ▁ cannot " , " ▁ be ▁ formed " ; return 0 ; } else { $ arc = ( $ pi * $ diameter ) * ( $ angle / 360.0 ) ; return $ arc ; } } $ diameter = 25.0 ; $ angle = 45.0 ; $ arc_len = arcLength ( $ diameter , $ angle ) ; echo ( $ arc_len ) ; ? >
Check if a line touches or intersects a circle | PHP program to check if a line touches or intersects or outside a circle . ; Finding the distance of line from center . ; Checking if the distance is less than , greater than or equal to radius . ; Driver Code
< ? php function checkCollision ( $ a , $ b , $ c , $ x , $ y , $ radius ) { $ dist = ( abs ( $ a * $ x + $ b * $ y + $ c ) ) / sqrt ( $ a * $ a + $ b * $ b ) ; if ( $ radius == $ dist ) echo " Touch " ; else if ( $ radius > $ dist ) echo " Intersect " ; else echo " Outside " ; } $ radius = 5 ; $ x = 0 ; $ y = 0 ; $ a = 3 ; $ b = 4 ; $ c = 25 ; checkCollision ( $ a , $ b , $ c , $ x , $ y , $ radius ) ; ? >
Program to find area of a triangle | ( X [ i ] , Y [ i ] ) are coordinates of i 'th point. ; Initialize area ; Calculate value of shoelace formula ; j is previous vertex to i ; Return absolute value ; Driver Code
< ? php function polygonArea ( $ X , $ Y , $ n ) { $ area = 0.0 ; $ j = $ n - 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ area += ( $ X [ $ j ] + $ X [ $ i ] ) * ( $ Y [ $ j ] - $ Y [ $ i ] ) ; $ j = $ i ; } return abs ( $ area / 2.0 ) ; } $ X = array ( 0 , 2 , 4 ) ; $ Y = array ( 1 , 3 , 7 ) ; $ n = count ( $ X ) ; echo polygonArea ( $ X , $ Y , $ n ) ; ? >
Find average of two numbers using bit operation | Function to return the average of x and y using bit operations ; Calculate the average Floor value of ( x + y ) / 2 ; Driver code
< ? php function getAverage ( $ x , $ y ) { $ avg = ( $ x & $ y ) + ( ( $ x ^ $ y ) >> 1 ) ; return $ avg ; } $ x = 10 ; $ y = 9 ; echo getAverage ( $ x , $ y ) ; ? >
Smallest index such that there are no 0 or 1 to its right | Function to find the smallest index such that there are no 0 or 1 to its right ; Initially ; Traverse in the array ; Check if array element is 1 ; a [ i ] = 0 ; Return minimum of both ; Driver code
< ? php function smallestIndex ( $ a , $ n ) { $ right1 = 0 ; $ right0 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] == 1 ) $ right1 = $ i ; else $ right0 = $ i ; } return min ( $ right1 , $ right0 ) ; } $ a = array ( 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 ) ; $ n = sizeof ( $ a ) ; echo smallestIndex ( $ a , $ n ) ; ? >
Total position where king can reach on a chessboard in exactly M moves | Set 2 | Function to return the count of squares that can be visited by king in m moves ; To store the count of squares ; Check all squares of the chessboard ; Check if square ( i , j ) is at a distance <= m units from king 's current position ; Return count of squares ; Driver code
< ? php function countSquares ( $ r , $ c , $ m ) { $ squares = 0 ; for ( $ i = 1 ; $ i <= 8 ; $ i ++ ) { for ( $ j = 1 ; $ j <= 8 ; $ j ++ ) { if ( max ( abs ( $ i - $ r ) , abs ( $ j - $ c ) ) <= $ m ) $ squares ++ ; } } return $ squares ; } $ r = 4 ; $ c = 4 ; $ m = 1 ; echo countSquares ( $ r , $ c , $ m ) ; ? >
Numbers in a Range with given Digital Root | Function to return the count of required numbers ; Count of numbers present in given range ; Number of groups of 9 elements starting from L ; Left over elements not covered in factor 9 ; One Number in each group of 9 ; To check if any number in rem satisfy the property ; Driver code
< ? php function countNumbers ( $ L , $ R , $ K ) { if ( $ K == 9 ) $ K = 0 ; $ totalnumbers = $ R - $ L + 1 ; $ factor9 = intval ( $ totalnumbers / 9 ) ; $ rem = $ totalnumbers % 9 ; $ ans = $ factor9 ; for ( $ i = $ R ; $ i > $ R - $ rem ; $ i -- ) { $ rem1 = $ i % 9 ; if ( $ rem1 == $ K ) $ ans ++ ; } return $ ans ; } $ L = 10 ; $ R = 22 ; $ K = 3 ; echo countNumbers ( $ L , $ R , $ K ) ; ? >
Sum of even values and update queries on an array | Function to print the result for every query ; If current element is even ; If element is even then remove it from sum ; If the value becomes even after updating ; Store sum for each query ; Print the result for every query ; Driver code
< ? php function BalanceArray ( $ A , & $ Q ) { $ ANS = array ( ) ; $ sum = 0 ; for ( $ i = 0 ; $ i < count ( $ A ) ; $ i ++ ) if ( $ A [ $ i ] % 2 == 0 ) $ sum = $ sum + $ A [ $ i ] ; for ( $ i = 0 ; $ i < count ( $ Q ) ; $ i ++ ) { $ index = $ Q [ $ i ] [ 0 ] ; $ value = $ Q [ $ i ] [ 1 ] ; if ( $ A [ $ index ] % 2 == 0 ) $ sum = $ sum - $ A [ $ index ] ; $ A [ $ index ] = $ A [ $ index ] + $ value ; if ( $ A [ $ index ] % 2 == 0 ) $ sum = $ sum + $ A [ $ index ] ; array_push ( $ ANS , $ sum ) ; } for ( $ i = 0 ; $ i < count ( $ ANS ) ; $ i ++ ) echo $ ANS [ $ i ] . " ▁ " ; } $ A = array ( 1 , 2 , 3 , 4 ) ; $ Q = array ( array ( 0 , 1 ) , array ( 1 , -3 ) , array ( 0 , -4 ) , array ( 3 , 2 ) ) ; BalanceArray ( $ A , $ Q ) ; ? >
Number of Hamiltonian cycle | Function that calculates number of Hamiltonian cycle ; Calculating factorial ; Driver code
< ? php function Cycles ( $ N ) { $ fact = 1 ; $ result = 0 ; $ result = $ N - 1 ; $ i = $ result ; while ( $ i > 0 ) { $ fact = $ fact * $ i ; $ i -- ; } return floor ( $ fact / 2 ) ; } $ N = 5 ; $ Number = Cycles ( $ N ) ; echo " Hamiltonian ▁ cycles ▁ = ▁ " , $ Number ; ? >
Smallest integer greater than n such that it consists of digit m exactly k times | Function that returns true if n contains digit m exactly k times ; Function to return the smallest integer > n with digit m occurring exactly k times ; Driver code
< ? php function digitWell ( $ n , $ m , $ k ) { $ cnt = 0 ; while ( $ n > 0 ) { if ( $ n % 10 == $ m ) ++ $ cnt ; $ n = floor ( $ n / 10 ) ; } return $ cnt == $ k ; } function findInt ( $ n , $ m , $ k ) { $ i = $ n + 1 ; while ( true ) { if ( digitWell ( $ i , $ m , $ k ) ) return $ i ; $ i ++ ; } } $ n = 111 ; $ m = 2 ; $ k = 2 ; echo findInt ( $ n , $ m , $ k ) ; ? >
Composite XOR and Coprime AND | Function to return the count of odd numbers in the array ; Variable to count odd numbers ; Odd number ; Function to return the count of valid pairs ; Driver Code
< ? php function countOdd ( $ arr , $ n ) { $ odd = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] % 2 == 1 ) $ odd ++ ; } return $ odd ; } function countValidPairs ( $ arr , $ n ) { $ odd = countOdd ( $ arr , $ n ) ; return ( $ odd * ( $ odd - 1 ) ) / 2 ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 ) ; $ n = sizeof ( $ arr ) ; echo countValidPairs ( $ arr , $ n ) ; ? >
Smallest perfect Cube divisible by all elements of an array | Function to return the gcd of two numbers ; Function to return the lcm of all the elements of the array ; To calculate lcm of two numbers multiply them and divide the result by gcd of both the numbers ; Return the LCM of the array elements ; Function to return the smallest perfect cube divisible by all the elements of arr [ ] ; LCM of all the elements of arr [ ] ; If 2 divides lcm cnt number of times ; Check all the numbers that divide lcm ; Return the answer ; Driver code
< ? php function gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; else return gcd ( $ b , $ a % $ b ) ; } function lcmOfArray ( & $ arr , $ n ) { if ( $ n < 1 ) return 0 ; $ lcm = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ lcm = ( $ lcm * $ arr [ $ i ] ) / gcd ( $ lcm , $ arr [ $ i ] ) ; return $ lcm ; } function minPerfectCube ( & $ arr , $ n ) { $ lcm = lcmOfArray ( $ arr , $ n ) ; $ minPerfectCube = $ lcm ; $ cnt = 0 ; while ( $ lcm > 1 && $ lcm % 2 == 0 ) { $ cnt ++ ; $ lcm /= 2 ; } if ( $ cnt % 3 == 2 ) $ minPerfectCube *= 2 ; else if ( $ cnt % 3 == 1 ) $ minPerfectCube *= 4 ; $ i = 3 ; while ( $ lcm > 1 ) { $ cnt = 0 ; while ( $ lcm % $ i == 0 ) { $ cnt ++ ; $ lcm /= $ i ; } if ( $ cnt % 3 == 1 ) $ minPerfectCube *= $ i * $ i ; else if ( $ cnt % 3 == 2 ) $ minPerfectCube *= $ i ; $ i += 2 ; } return $ minPerfectCube ; } $ arr = array ( 10 , 125 , 14 , 42 , 100 ) ; $ n = sizeof ( $ arr ) ; echo ( minPerfectCube ( $ arr , $ n ) ) ; ? >
Check if N is Strong Prime | Utility function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if n is a strong prime ; If n is not a prime number or n is the first prime then return false ; Initialize previous_prime to n - 1 and next_prime to n + 1 ; Find next prime number ; Find previous prime number ; Arithmetic mean ; If n is a strong prime ; Driver code
< ? php function isPrime ( $ n ) { if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return false ; return true ; } function isStrongPrime ( $ n ) { if ( ! isPrime ( $ n ) $ n == 2 ) return false ; $ previous_prime = $ n - 1 ; $ next_prime = $ n + 1 ; while ( ! isPrime ( $ next_prime ) ) $ next_prime ++ ; while ( ! isPrime ( $ previous_prime ) ) $ previous_prime -- ; $ mean = ( $ previous_prime + $ next_prime ) / 2 ; if ( $ n > $ mean ) return true ; else return false ; } $ n = 11 ; if ( isStrongPrime ( $ n ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >
Minimum removals in a number to be divisible by 10 power raised to K | function to return the required number of digits to be removed ; Converting the given number into string ; variable to store number of digits to be removed ; variable to denote if atleast one zero has been found ; zero found ; return size - 1 if K is not zero and atleast one zero is present , otherwise result ; Driver Code to test above function
< ? php function countDigitsToBeRemoved ( $ N , $ K ) { $ s = strval ( $ N ) ; $ res = 0 ; $ f_zero = 0 ; for ( $ i = strlen ( $ s ) - 1 ; $ i >= 0 ; $ i -- ) { if ( $ K == 0 ) return $ res ; if ( $ s [ $ i ] == '0' ) { $ f_zero = 1 ; $ K -- ; } else $ res ++ ; } if ( ! $ K ) return $ res ; else if ( $ f_zero ) return strlen ( $ s ) - 1 ; return -1 ; } $ N = 10904025 ; $ K = 2 ; echo countDigitsToBeRemoved ( $ N , $ K ) . " " ; $ N = 1000 ; $ K = 5 ; echo countDigitsToBeRemoved ( $ N , $ K ) . " " ; $ N = 23985 ; $ K = 2 ; echo countDigitsToBeRemoved ( $ N , $ K ) ; ? >
Program to find the sum of the series ( 1 / a + 2 / a ^ 2 + 3 / a ^ 3 + ... + n / a ^ n ) | Function to return the sum of the series ; variable to store the answer ; Math . pow ( x , y ) returns x ^ y ; Driver code ; Print the sum of the series
< ? php function getSum ( $ a , $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; ++ $ i ) { $ sum += ( $ i / pow ( $ a , $ i ) ) ; } return $ sum ; } $ a = 3 ; $ n = 3 ; echo ( getSum ( $ a , $ n ) ) ; ? >
Check if a number is an Unusual Number or not | Utility function to find largest prime factor of a number ; Initialize the maximum prime factor variable with the lowest one ; Print the number of 2 s that divide n ; $n >>= 1 ; equivalent to n /= 2 ; n must be odd at this point , thus skip the even numbers and iterate only for odd integers ; This condition is to handle the case when n is a prime number greater than 2 ; Function to check Unusual number ; Get the largest Prime Factor of the number ; Check if largest prime factor is greater than sqrt ( n ) ; Driver Code
< ? php function largestPrimeFactor ( $ n ) { $ max = -1 ; while ( $ n % 2 == 0 ) { $ max = 2 ; } for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i += 2 ) { while ( $ n % $ i == 0 ) { $ max = $ i ; $ n = $ n / $ i ; } } if ( $ n > 2 ) $ max = $ n ; return $ max ; } function checkUnusual ( $ n ) { $ factor = largestPrimeFactor ( $ n ) ; if ( $ factor > sqrt ( $ n ) ) { return true ; } else { return false ; } } $ n = 14 ; if ( checkUnusual ( $ n ) ) { echo " YES " . " STRNEWLINE " ; } else { echo " NO " . " STRNEWLINE " ; } ? >
Check if at least half array is reducible to zero by performing some operations | Function to print the desired result after computation ; Driver Code
< ? php function isHalfReducible ( $ arr , $ n , $ m ) { $ frequencyHash = array_fill ( 0 , $ m + 1 , 0 ) ; $ i = 0 ; for ( ; $ i < $ n ; $ i ++ ) { $ frequencyHash [ ( $ arr [ $ i ] % ( $ m + 1 ) ) ] ++ ; } for ( $ i = 0 ; $ i <= $ m ; $ i ++ ) { if ( $ frequencyHash [ $ i ] >= ( $ n / 2 ) ) break ; } if ( $ i <= $ m ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; } $ arr = array ( 8 , 16 , 32 , 3 , 12 ) ; $ n = sizeof ( $ arr ) ; $ m = 7 ; isHalfReducible ( $ arr , $ n , $ m ) ; ? >
Check whether the given number is Wagstaff prime or not | Function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Utility function to check power of two ; Driver Code ; Check if number is prime and of the form ( 2 ^ q + 1 ) / 3
< ? php function isPrime ( $ n ) { if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ; if ( $ n % 2 == 0 or $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) { if ( $ n % $ i == 0 or $ n % ( $ i + 2 ) == 0 ) { return false ; } } return true ; } function isPowerOfTwo ( $ n ) { return ( $ n && ! ( $ n & ( $ n - 1 ) ) ) ; } $ n = 43 ; if ( isPrime ( $ n ) && ( isPowerOfTwo ( $ n * 3 - 1 ) ) ) { echo " YES " ; } else { echo " NO " ; } ? >
Area of a square inscribed in a circle which is inscribed in a hexagon | Function to find the area of the square ; side of hexagon cannot be negative ; area of the square ; Driver code
< ? php function area ( $ a ) { if ( $ a < 0 ) return -1 ; $ area = pow ( ( $ a * sqrt ( 3 ) ) / ( sqrt ( 2 ) ) , 2 ) ; return $ area ; } $ a = 5 ; echo area ( $ a ) . " STRNEWLINE " ; ? >
Program to find Nth term of series 1 , 6 , 17 , 34 , 56 , 86 , 121 , 162 , ... ... . | calculate Nth term of series ; Driver code
< ? php function nthTerm ( $ n ) { return 3 * pow ( $ n , 2 ) - 4 * $ n + 2 ; } $ N = 4 ; echo nthTerm ( $ N ) . " STRNEWLINE " ; ? >
Sum of the first N terms of the series 2 , 10 , 30 , 68 , ... . | Function to calculate the sum ; number of terms to be included in the sum ; find the Sum
< ? php function calculateSum ( $ n ) { return $ n * ( $ n + 1 ) / 2 + pow ( ( $ n * ( $ n + 1 ) / 2 ) , 2 ) ; } $ n = 3 ; echo " Sum = " ? >
Check if two arrays are permutations of each other using Mathematical Operation | Function to check if arrays are permutations of each other . ; Calculating sum and multiply of first array ; Calculating sum and multiply of second array ; If sum and mul of both arrays are equal , return true , else return false . ; Driver code
< ? php function arePermutations ( $ a , $ b , $ n , $ m ) { $ sum1 = 0 ; $ sum2 = 0 ; $ mul1 = 1 ; $ mul2 = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum1 += $ a [ $ i ] ; $ mul1 *= $ a [ $ i ] ; } for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { $ sum2 += $ b [ $ i ] ; $ mul2 *= $ b [ $ i ] ; } return ( ( $ sum1 == $ sum2 ) && ( $ mul1 == $ mul2 ) ) ; } $ a = array ( 1 , 3 , 2 ) ; $ b = array ( 3 , 1 , 2 ) ; $ n = sizeof ( $ a ) ; $ m = sizeof ( $ b ) ; if ( arePermutations ( $ a , $ b , $ n , $ m ) ) echo " Yes " . " STRNEWLINE " ; else echo " No " . " STRNEWLINE " ;
Program to find the head start in a race | Function to find the B start to C ; When B completed it 's 100 meter  then Completed meters by C is ; Driver Code ; When A completed it 's 100 meter Then completed meters of B and C is
< ? php function Race ( $ B , $ C ) { $ result = 0 ; $ result = ( ( $ C * 100 ) / $ B ) ; return 100 - $ result ; } $ B = 10 ; $ C = 28 ; $ B = 100 - $ B ; $ C = 100 - $ C ; echo Race ( $ B , $ C ) . " ▁ meters " ; ? >
Minimum time required to fill a cistern using N pipes | Function to calculate the time ; Driver Code
< ? php function T_ime ( $ arr , $ n , $ Emptypipe ) { $ fill = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ fill += 1 / $ arr [ $ i ] ; $ fill = $ fill - ( 1 / $ Emptypipe ) ; return 1 / $ fill ; } $ arr = array ( 12 , 14 ) ; $ Emptypipe = 30 ; $ n = count ( $ arr ) ; echo ( int ) T_ime ( $ arr , $ n , $ Emptypipe ) . " ▁ Hours " ; ? >
Check if Decimal representation of an Octal number is divisible by 7 | Function to check Divisibility ; Sum of all individual digits ; Condition ; Octal number
< ? php function check ( $ n ) { $ sum = 0 ; while ( $ n != 0 ) { $ sum += $ n % 10 ; $ n = ( int ) ( $ n / 10 ) ; } if ( $ sum % 7 == 0 ) return 1 ; else return 0 ; } $ n = 25 ; ( check ( $ n ) == 1 ) ? print ( " YES STRNEWLINE " ) : print ( " NO STRNEWLINE " ) ; ? >