codeparrot/xlcost-text-to-code huggingface dataset:
Powers of 2 to required sum | PHP program to find the blocks for given number . ; Convert decimal number to its binary equivalent ; Displaying the output when the bit is '1' in binary equivalent of number . ; Driver Code
< ? php function block ( $ x ) { $ v = array ( ) ; echo ' locks for ' . $ x . ' ▁ : ▁ ' ; while ( $ x > 0 ) { array_push ( $ v , intval ( $ x % 2 ) ) ; $ x = intval ( $ x / 2 ) ; } for ( $ i = 0 ; $ i < sizeof ( $ v ) ; $ i ++ ) { if ( $ v [ $ i ] == 1 ) { print $ i ; if ( $ i != sizeof ( $ v ) - 1 ) echo ' , ▁ ' ; } } echo " STRNEWLINE " ; } block ( 71307 ) ; block ( 1213 ) ; block ( 29 ) ; block ( 100 ) ; ? >
Given a number N in decimal base , find number of its digits in any base ( base b ) | function to print number of digits ; Calculating log using base changing property and then taking it floor and then adding 1. ; printing output ; taking inputs ; calling the method
< ? php function findNumberOfDigits ( $ n , $ b ) { $ dig = ( int ) ( floor ( log ( $ n ) / log ( $ b ) ) + 1 ) ; echo ( " The ▁ Number ▁ of ▁ digits " . " ▁ of ▁ Number ▁ " . $ n . " ▁ in ▁ base ▁ " . $ b . " ▁ is ▁ " . $ dig ) ; } $ n = 1446 ; $ b = 7 ; findNumberOfDigits ( $ n , $ b ) ; ? >
Nesbitt 's Inequality | PHP code to verify Nesbitt 's Inequality ; 3 parts of the inequality sum ; Driver Code
< ? php function isValidNesbitt ( $ a , $ b , $ c ) { $ A = $ a / ( $ b + $ c ) ; $ B = $ b / ( $ a + $ c ) ; $ C = $ c / ( $ a + $ b ) ; $ inequality = $ A + $ B + $ C ; return ( $ inequality >= 1.5 ) ; } $ a = 1.0 ; $ b = 2.0 ; $ c = 3.0 ; if ( isValidNesbitt ( $ a , $ b , $ c ) ) echo " Nesbitt ' s ▁ inequality ▁ satisfied . " , " for ▁ real ▁ numbers ▁ " , $ a , " , ▁ " , $ b , " , ▁ " , $ c , " STRNEWLINE " ; else cout << " Not ▁ satisfied " ; ? >
Cube Free Numbers smaller than n | Efficient PHP Program to print all cube free numbers smaller than or equal to n . ; Initialize all numbers as not cube free ; Traverse through all possible cube roots ; If i itself is cube free ; Mark all multiples of i as not cube free ; Print all cube free numbers ; Driver Code
< ? php function printCubeFree ( $ n ) { $ cubFree = array_fill ( 0 , ( $ n + 1 ) , 1 ) ; $ i = 2 ; while ( $ i * $ i * $ i <= $ n ) { if ( $ cubFree [ $ i ] == 1 ) { $ multiple = 1 ; while ( $ i * $ i * $ i * $ multiple <= $ n ) { $ cubFree [ $ i * $ i * $ i * $ multiple ] = 0 ; $ multiple += 1 ; } } $ i += 1 ; } for ( $ i = 2 ; $ i < $ n + 1 ; $ i ++ ) if ( $ cubFree [ $ i ] == 1 ) echo $ i . " " ; } printCubeFree ( 20 ) ; ? >
Squared triangular number ( Sum of cubes ) | Returns root of n ( n + 1 ) / 2 = num if num is triangular ( or integerroot exists ) . Else returns - 1. ; Considering the equation n * ( n + 1 ) / 2 = num . The equation is : a ( n ^ 2 ) + bn + c = 0 "; ; Find roots of equation ; checking if root1 is natural ; checking if root2 is natural ; Returns square root of x if it is perfect square . Else returns - 1. ; Find floating point value of square root of x . ; If square root is an integer ; Function to find if the given number is sum of the cubes of first n natural numbers ; Driver code
< ? php function isTriangular ( $ num ) { if ( $ num < 0 ) return false ; $ c = ( -2 * $ num ) ; $ b = 1 ; $ a = 1 ; $ d = ( $ b * $ b ) - ( 4 * $ a * $ c ) ; if ( $ d < 0 ) return -1 ; $ root1 = ( - $ b + sqrt ( $ d ) ) / ( 2 * $ a ) ; $ root2 = ( - $ b - sqrt ( $ d ) ) / ( 2 * $ a ) ; if ( $ root1 > 0 && floor ( $ root1 ) == $ root1 ) return $ root1 ; if ( $ root2 > 0 && floor ( $ root2 ) == $ root2 ) return $ root2 ; return -1 ; } function isPerfectSquare ( $ x ) { $ sr = sqrt ( $ x ) ; if ( ( $ sr - floor ( $ sr ) ) == 0 ) return floor ( $ sr ) ; else return -1 ; } function findS ( $ s ) { $ sr = isPerfectSquare ( $ s ) ; if ( $ sr == -1 ) return -1 ; return isTriangular ( $ sr ) ; } $ s = 9 ; $ n = findS ( $ s ) ; if ( $ n == -1 ) echo " - 1" ; else echo $ n ; ? >
Smallest even digits number not less than N | function to return the answer when the first odd digit is 9 ; traverse towwars the left to find the non - 8 digit ; index digit ; if digit is not 8 , then break ; if on the left side of the '9' , no 8 is found then we return by adding a 2 and 0 's ; till non - 8 digit add all numbers ; if non - 8 is even or odd than add the next even . ; add 0 to right of 9 ; function to return the smallest number with all digits even ; convert the number to string to perform operations ; find out the first odd number ; if no odd numbers are there , than n is the answer ; if the odd number is 9 , than tricky case handles it ; add all digits till first odd ; increase the odd digit by 1 ; add 0 to the right of the odd number ; Driver Code
< ? php function trickyCase ( $ s , $ index ) { $ index1 = -1 ; for ( $ i = $ index - 1 ; $ i >= 0 ; $ i -- ) { $ digit = $ s [ $ i ] - '0' ; if ( $ digit != 8 ) { $ index1 = $ i ; break ; } } if ( $ index1 == -1 ) return 2 * pow ( 10 , strlen ( $ s ) ) ; $ num = 0 ; for ( $ i = 0 ; $ i < $ index1 ; $ i ++ ) $ num = $ num * 10 + ( $ s [ $ i ] - '0' ) ; if ( $ s [ $ index1 ] % 2 == 0 ) $ num = $ num * 10 + ( $ s [ $ index1 ] - '0' + 2 ) ; else $ num = $ num * 10 + ( $ s [ $ index1 ] - '0' + 1 ) ; for ( $ i = $ index1 + 1 ; $ i < strlen ( $ s ) ; $ i ++ ) $ num = $ num * 10 ; return $ num ; } function smallestNumber ( $ n ) { $ num = 0 ; $ s = " " ; $ duplicate = $ n ; while ( $ n ) { $ s = chr ( $ n % 10 + 48 ) . $ s ; $ n = ( int ) ( $ n / 10 ) ; } $ index = -1 ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { $ digit = $ s [ $ i ] - '0' ; if ( $ digit & 1 ) { $ index = $ i ; break ; } } if ( $ index == -1 ) return $ duplicate ; if ( $ s [ $ index ] == '9' ) { $ num = trickyCase ( $ s , $ index ) ; return $ num ; } for ( $ i = 0 ; $ i < $ index ; $ i ++ ) $ num = $ num * 10 + ( $ s [ $ i ] - '0' ) ; $ num = $ num * 10 + ( $ s [ $ index ] - '0' + 1 ) ; for ( $ i = $ index + 1 ; $ i < strlen ( $ s ) ; $ i ++ ) $ num = $ num * 10 ; return $ num ; } $ N = 2397 ; echo smallestNumber ( $ N ) ; ? >
n | Simple PHP program to find n - th number with sum of digits as 10. ; Find sum of digits in current no . ; If sum is 10 , we increment count ; If count becomes n , we return current number . ; Driver Code
< ? php function findNth ( $ n ) { $ count = 0 ; for ( $ curr = 19 ; ; $ curr += 9 ) { $ sum = 0 ; for ( $ x = $ curr ; $ x > 0 ; $ x = ( int ) $ x / 10 ) $ sum = $ sum + $ x % 10 ; if ( $ sum == 10 ) $ count ++ ; if ( $ count == $ n ) return $ curr ; } return -1 ; } echo findNth ( 5 ) ; ? >
Sum of pairwise products | Simple PHP program to find sum of given series . ; Driver Code
< ? php function findSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) for ( $ j = $ i ; $ j <= $ n ; $ j ++ ) $ sum = $ sum + $ i * $ j ; return $ sum ; } $ n = 5 ; echo findSum ( $ n ) ; ? >
Sum of pairwise products | Efficient PHP program to find sum of given series . ; Sum of multiples of 1 is 1 * ( 1 + 2 + . . ) ; Adding sum of multiples of numbers other than 1 , starting from 2. ; Subtract previous number from current multiple . ; For example , for 2 , we get sum as ( 2 + 3 + 4 + ... . ) * 2 ; Driver code
< ? php function findSum ( $ n ) { $ multiTerms = ( int ) ( $ n * ( $ n + 1 ) / 2 ) ; $ sum = $ multiTerms ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ multiTerms = $ multiTerms - ( $ i - 1 ) ; $ sum = $ sum + $ multiTerms * $ i ; } return $ sum ; } $ n = 5 ; echo findSum ( $ n ) ; ? >
Sum of n digit numbers divisible by a given number | find the Sum of having n digit and divisible by the number ; compute the first and last term ; first number which is divisible by given number ; last number which is divisible by given number ; total divisible number ; return the total sum ; Driver Code
< ? php function totalSumDivisibleByNum ( $ digit , $ number ) { $ firstnum = pow ( 10 , $ digit - 1 ) ; $ lastnum = pow ( 10 , $ digit ) ; $ firstnum = ( $ firstnum - $ firstnum % $ number ) + $ number ; $ lastnum = ( $ lastnum - $ lastnum % $ number ) ; $ count = ( ( $ lastnum - $ firstnum ) / $ number + 1 ) ; return ( ( $ lastnum + $ firstnum ) * $ count ) / 2 ; } $ n = 3 ; $ number = 7 ; echo totalSumDivisibleByNum ( $ n , $ number ) ; ? >
Program for N | PHP Program to find nth term of Arithmetic progression ; using formula to find the Nth term t ( n ) = a ( 1 ) + ( n - 1 ) * d ; starting number ; Common difference ; N th term to be find ; Display the output
< ? php function Nth_of_AP ( $ a , $ d , $ N ) { return ( $ a + ( $ N - 1 ) * $ d ) ; } $ a = 2 ; $ d = 1 ; $ N = 5 ; echo ( " The ▁ " . $ N . " th ▁ term ▁ of ▁ the ▁ series ▁ is ▁ : ▁ " . Nth_of_AP ( $ a , $ d , $ N ) ) ; ? >
Fibbinary Numbers ( No consecutive 1 s in binary ) | function to check if binary representation of an integer has consecutive 1 s ; stores the previous last bit initially as 0 ; if current last bit and previous last bit is 1 ; stores the last bit ; right shift the number ; Driver code
< ? php function checkFibinnary ( $ n ) { $ prev_last = 0 ; while ( $ n ) { if ( ( $ n & 1 ) && $ prev_last ) return false ; $ prev_last = $ n & 1 ; $ n >>= 1 ; } return true ; } $ n = 10 ; if ( checkFibinnary ( $ n ) ) echo " YES " ; else echo " NO " ; ? >
Sum of the series 5 + 55 + 555 + . . up to n terms | function which return the the sum of series ; Driver code
< ? php function sumOfSeries ( $ n ) { return ( int ) ( 0.6172 * ( pow ( 10 , $ n ) - 1 ) - 0.55 * $ n ) ; } $ n = 2 ; echo ( sumOfSeries ( $ n ) ) ; ? >
Nonagonal number | Function to find nth nonagonal number . ; Formula to find nth nonagonal number . ; Driver Code
< ? php function Nonagonal ( $ n ) { return $ n * ( 7 * $ n - 5 ) / 2 ; } $ n = 10 ; echo Nonagonal ( $ n ) ; ? >
Check if a large number is divisible by 20 | PHP program to check if a large number is divisible by 20. ; Get number with last two digits ; Check if the number formed by last two digits is divisible by 5 and 4. ; Driver Code
< ? php function divisibleBy20 ( $ num ) { $ lastTwoDigits = intval ( substr ( $ num , ( strlen ( $ num ) - 2 ) , 2 ) ) ; return ( ( $ lastTwoDigits % 5 == 0 ) && ( $ lastTwoDigits % 4 == 0 ) ) ; } $ num = "63284689320" ; if ( divisibleBy20 ( $ num ) ) echo " Yes " ; else echo " No " ; ? >
Divisibility by 12 for a large number | PHP Program to check if number is divisible by 12 ; if number greater then 3 ; find last digit ; no is odd ; find second last digit ; find sum of all digits ; if number is less then or equal to 100 ; Driver Code
< ? php function isDvisibleBy12 ( $ num ) { if ( strlen ( $ num ) >= 3 ) { $ d1 = ( int ) $ num [ strlen ( $ num ) - 1 ] ; if ( $ d1 % 2 != 0 ) return ( 0 ) ; $ d2 = ( int ) $ num [ strlen ( $ num ) - 2 ] ; $ sum = 0 ; for ( $ i = 0 ; $ i < strlen ( $ num ) ; $ i ++ ) $ sum += $ num [ $ i ] ; return ( $ sum % 3 == 0 && ( $ d2 * 10 + $ d1 ) % 4 == 0 ) ; } else { $ number = stoi ( $ num ) ; return ( $ number % 12 == 0 ) ; } } $ num = "12244824607284961224" ; if ( isDvisibleBy12 ( $ num ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >
Largest number that is not a perfect square | PHP program to find the largest non perfect square number among n numbers ; takes the sqrt of the number ; checks if it is a perfect square number ; function to find the largest non perfect square number ; stores the maximum of all non perfect square numbers ; traverse for all elements in the array ; store the maximum if not a perfect square ; Driver Code ; function call
< ? php function check ( $ n ) { $ d = sqrt ( $ n ) ; if ( $ d * $ d == $ n ) return true ; return false ; } function largestNonPerfectSquareNumber ( $ a , $ n ) { $ maxi = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( ! check ( $ a [ $ i ] ) ) $ maxi = max ( $ a [ $ i ] , $ maxi ) ; } return $ maxi ; } $ a = array ( 16 , 20 , 25 , 2 , 3 , 10 ) ; $ n = count ( $ a ) ; echo largestNonPerfectSquareNumber ( $ a , $ n ) ; ? >
Program to print Arithmetic Progression series | PHP Program to print an arithmetic progression series ; Printing AP by simply adding d to previous term . ; starting number ; Common difference ; N th term to be find
< ? php function printAP ( $ a , $ d , $ n ) { $ curr_term ; $ curr_term = a ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { echo ( $ curr_term . " " ) ; $ curr_term += $ d ; } } $ a = 2 ; $ d = 1 ; $ n = 5 ; printAP ( $ a , $ d , $ n ) ; ? >
Program to print non square numbers | PHP program to print first n non - square numbers . ; Print curr_count numbers . curr_count is current gap between two square numbers . ; skip a square number . ; Count of next non - square numbers is next even number . ; Driver code
< ? php function printNonSquare ( $ n ) { $ curr_count = 2 ; $ num = 2 ; $ count = 0 ; while ( $ count < $ n ) { for ( $ i = 0 ; $ i < $ curr_count && $ count < $ n ; $ i ++ ) { echo ( $ num . " " ) ; $ count ++ ; $ num ++ ; } $ num ++ ; $ curr_count += 2 ; } } $ n = 10 ; printNonSquare ( $ n ) ; ? >
Count number of trailing zeros in product of array | Returns count of zeros in product of array ; count number of 2 s in each element ; count number of 5 s in each element ; return the minimum ; Driver Code
< ? php function countZeros ( $ a , $ n ) { $ count2 = 0 ; $ count5 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { while ( $ a [ $ i ] % 2 == 0 ) { $ a [ $ i ] = $ a [ $ i ] / 2 ; $ count2 ++ ; } while ( $ a [ $ i ] % 5 == 0 ) { $ a [ $ i ] = $ a [ $ i ] / 5 ; $ count5 ++ ; } } return ( $ count2 < $ count5 ) ? $ count2 : $ count5 ; } $ a = array ( 10 , 100 , 20 , 30 , 50 , 90 , 12 , 80 ) ; $ n = sizeof ( $ a ) ; echo ( countZeros ( $ a , $ n ) ) ; ? >
Sum of square of first n even numbers | Efficient PHP method to find sum of square of first n even numbers . ; Driver code
< ? php function squareSum ( $ n ) { return 2 * $ n * ( $ n + 1 ) * ( 2 * $ n + 1 ) / 3 ; } echo squareSum ( 8 ) ; ? >
MÃ ¼ nchhausen Number | pwr [ i ] is going to store i raised to power i . ; Function to check out whether the number is MA14nchhausen Number or not ; Precompute i raised to power i for every i ; The input here is fixed i . e . it will check up to n ; check the integer for MA14nchhausen Number , if yes then print out the number ; Driver Code
< ? php $ pwr = array_fill ( 0 , 10 , 0 ) ; function isMunchhausen ( $ n ) { global $ pwr ; $ sm = 0 ; $ temp = $ n ; while ( $ temp ) { $ sm = $ sm + $ pwr [ ( $ temp % 10 ) ] ; $ temp = ( int ) ( $ temp / 10 ) ; } return ( $ sm == $ n ) ; } function printMunchhausenNumbers ( $ n ) { global $ pwr ; for ( $ i = 0 ; $ i < 10 ; $ i ++ ) $ pwr [ $ i ] = pow ( ( float ) ( $ i ) , ( float ) ( $ i ) ) ; for ( $ i = 1 ; $ i < $ n + 1 ; $ i ++ ) if ( isMunchhausen ( $ i ) ) print ( $ i . " STRNEWLINE " ) ; } $ n = 10000 ; printMunchhausenNumbers ( $ n ) ; ? >
K | To compute k - th digit in a ^ b ; computing a ^ b ; getting last digit ; increasing count by 1 ; if current number is required digit ; remove last digit ; Driver Code
< ? php function kthdigit ( $ a , $ b , $ k ) { $ p = pow ( $ a , $ b ) ; $ count = 0 ; while ( $ p > 0 and $ count < $ k ) { $ rem = $ p % 10 ; $ count ++ ; if ( $ count == $ k ) return $ rem ; $ p = $ p / 10 ; } return 0 ; } $ a = 5 ; $ b = 2 ; $ k = 1 ; echo kthdigit ( $ a , $ b , $ k ) ; ? >
Recursive sum of digit in n ^ x , where n and x are very large | function to get sum of digits of a number ; function to return sum ; Find sum of digits in n ; Find remainder of exponent ; Driver code
< ? php function digSum ( $ n ) { if ( $ n == 0 ) return 0 ; return ( $ n % 9 == 0 ) ? 9 : ( $ n % 9 ) ; } function PowDigSum ( $ n , $ x ) { $ sum = digSum ( $ n ) ; $ rem = $ x % 6 ; if ( ( $ sum == 3 $ sum == 6 ) && $ x > 1 ) return 9 ; else if ( $ x == 1 ) return $ sum ; else if ( $ x == 0 ) return 1 ; else if ( $ rem == 0 ) return digSum ( pow ( $ sum , 6 ) ) ; else return digSum ( pow ( $ sum , $ rem ) ) ; } $ n = 33333 ; $ x = 332654 ; echo PowDigSum ( $ n , $ x ) ; ? >
Container with Most Water | PHP code for Max Water Container ; Calculating the max area ; Driver code
< ? php function maxArea ( $ A , $ len ) { $ l = 0 ; $ r = $ len - 1 ; $ area = 0 ; while ( $ l < $ r ) { $ area = max ( $ area , min ( $ A [ $ l ] , $ A [ $ r ] ) * ( $ r - $ l ) ) ; if ( $ A [ $ l ] < $ A [ $ r ] ) $ l += 1 ; else $ r -= 1 ; } return $ area ; } $ a = array ( 1 , 5 , 4 , 3 ) ; $ b = array ( 3 , 1 , 2 , 4 , 5 ) ; $ len1 = sizeof ( $ a ) / sizeof ( $ a [ 0 ] ) ; echo maxArea ( $ a , $ len1 ) . " STRNEWLINE " ; $ len2 = sizeof ( $ b ) / sizeof ( $ b [ 0 ] ) ; echo maxArea ( $ b , $ len2 ) ; ? >
Program for Mobius Function | Returns value of mobius ( ) ; Handling 2 separately ; If 2 ^ 2 also divides N ; Check for all other prime factors ; If i divides n ; If i ^ 2 also divides N ; Driver code
< ? php function mobius ( $ n ) { $ p = 0 ; if ( $ n % 2 == 0 ) { $ n = $ n / 2 ; $ p ++ ; if ( $ n % 2 == 0 ) return 0 ; } for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i = $ i + 2 ) { if ( $ n % $ i == 0 ) { $ n = $ n / $ i ; $ p ++ ; if ( $ n % $ i == 0 ) return 0 ; } } return ( $ p % 2 == 0 ) ? -1 : 1 ; } $ N = 17 ; echo " Mobius ▁ Functions ▁ M ( N ) ▁ at ▁ N ▁ = ▁ " , $ N , " ▁ is : ▁ " , mobius ( $ N ) , " STRNEWLINE " ; echo " Mobius ▁ Functions ▁ M ( N ) ▁ at ▁ N ▁ = ▁ " , 25 , " ▁ is : ▁ " , mobius ( 25 ) , " STRNEWLINE " ; echo " Mobius ▁ Functions ▁ M ( N ) ▁ at ▁ N ▁ = ▁ " , 6 , " ▁ is : ▁ " , mobius ( 6 ) ; ? >
Sum of squares of binomial coefficients | function to return product of number from start to end . ; Return the sum of square of binomial coefficient ; Driver Code
< ? php function factorial ( $ start , $ end ) { $ res = 1 ; for ( $ i = $ start ; $ i <= $ end ; $ i ++ ) $ res *= $ i ; return $ res ; } function sumofsquare ( $ n ) { return factorial ( $ n + 1 , 2 * $ n ) / factorial ( 1 , $ n ) ; } $ n = 4 ; echo sumofsquare ( $ n ) , " STRNEWLINE " ; ? >
Find nth Fibonacci number using Golden ratio | Approximate value of golden ratio ; Function to find nth Fibonacci number ; Fibonacci numbers for n < 6 ; Else start counting from 5 th term ; Driver Code
< ? php $ PHI = 1.6180339 ; function fib ( $ n ) { global $ PHI ; $ f = array ( 0 , 1 , 1 , 2 , 3 , 5 ) ; if ( $ n < 6 ) return $ f [ $ n ] ; $ t = 5 ; $ fn = 5 ; while ( $ t < $ n ) { $ fn = round ( $ fn * $ PHI ) ; $ t ++ ; } return $ fn ; } $ n = 9 ; echo $ n , " th ▁ Fibonacci ▁ Number ▁ = ▁ " , fib ( $ n ) , " STRNEWLINE " ; ? >
Euler Method for solving differential equation | Consider a differential equation dy / dx = ( x + y + xy ) ; Function for Euler formula ; Iterating till the point at which we need approximation ; Printing approximation ; Initial Values ; Value of x at which we need approximation
< ? php function func ( $ x , $ y ) { return ( $ x + $ y + $ x * $ y ) ; } function euler ( $ x0 , $ y , $ h , $ x ) { $ temp = -0 ; while ( $ x0 < $ x ) { $ temp = $ y ; $ y = $ y + $ h * func ( $ x0 , $ y ) ; $ x0 = $ x0 + $ h ; } echo " Approximate ▁ solution ▁ at ▁ x ▁ = ▁ " , $ x , " ▁ is ▁ " , $ y , " STRNEWLINE " ; } $ x0 = 0 ; $ y0 = 1 ; $ h = 0.025 ; $ x = 0.1 ; euler ( $ x0 , $ y0 , $ h , $ x ) ; ? >
Find x and y satisfying ax + by = n | function to find the solution ; traverse for all possible values ; check if it is satisfying the equation ; Driver Code
< ? php function solution ( $ a , $ b , $ n ) { for ( $ i = 0 ; $ i * $ a <= $ n ; $ i ++ ) { if ( ( $ n - ( $ i * $ a ) ) % $ b == 0 ) { echo " x = " ▁ , ▁ $ i ▁ , ▁ " , y = " ( $ n - ( $ i * $ a ) ) / $ b ; return ; } } echo " No ▁ solution " ; } $ a = 2 ; $ b = 3 ; $ n = 7 ; solution ( $ a , $ b , $ n ) ; ? >
Sum of Binomial coefficients | Returns value of Binomial Coefficient Sum which is 2 raised to power n . ; Driver Code
< ? php function binomialCoeffSum ( $ n ) { return ( 1 << $ n ) ; } $ n = 4 ; echo binomialCoeffSum ( $ n ) ; ? >
Program to compute division upto n decimal places | PHP program to compute division upto n decimal places . ; Base cases ; Since n <= 0 , don 't   compute after the decimal ; Handling negative numbers ; Integral division ; Now one by print digits after dot using school division method . ; Driver Code
< ? php function precisionCompute ( $ x , $ y , $ n ) { if ( $ y == 0 ) { echo " Infinite " , " STRNEWLINE " ; return ; } if ( $ x == 0 ) { echo 0 , " STRNEWLINE " ; return ; } if ( $ n <= 0 ) { echo $ x / $ y , " STRNEWLINE " ; return ; } if ( ( ( $ x > 0 ) && ( $ y < 0 ) ) || ( ( $ x < 0 ) && ( $ y > 0 ) ) ) { echo " - " $ x = $ x > 0 ? $ x : - $ x ; $ y = $ y > 0 ? $ y : - $ y ; } $ d = $ x / $ y ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) { echo $ d ; $ x = $ x - ( $ y * $ d ) ; if ( $ x == 0 ) break ; $ x = $ x * 10 ; $ d = $ x / $ y ; if ( $ i == 0 ) echo " . " ; } } $ x = 22 ; $ y = 7 ; $ n = 15 ; precisionCompute ( $ x , $ y , $ n ) ; ? >
Program to determine the quadrant of the cartesian plane | Function to check quadrant ; Driver code ; Function call
< ? php function quadrant ( $ x , $ y ) { if ( $ x > 0 and $ y > 0 ) echo ( " lies ▁ in ▁ First ▁ quadrant " ) ; else if ( $ x < 0 and $ y > 0 ) echo ( " lies ▁ in ▁ Second ▁ quadrant " ) ; else if ( $ x < 0 and $ y < 0 ) echo ( " lies ▁ in ▁ Third ▁ quadrant " ) ; else if ( $ x > 0 and $ y < 0 ) echo ( " lies ▁ in ▁ Fourth ▁ quadrant " ) ; else if ( $ x == 0 and $ y > 0 ) echo ( " lies ▁ at ▁ positive ▁ y ▁ axis " ) ; else if ( $ x == 0 and $ y < 0 ) echo ( " lies ▁ at ▁ negative ▁ y ▁ axis " ) ; else if ( $ y == 0 and $ x < 0 ) echo ( " lies ▁ at ▁ negative ▁ x ▁ axis " ) ; else if ( $ y == 0 and $ x > 0 ) echo ( " lies ▁ at ▁ positive ▁ x ▁ axis " ) ; else echo ( " lies ▁ at ▁ origin " ) ; } $ x = 1 ; $ y = 1 ; quadrant ( $ x , $ y ) ; ? >
Check if a number is Full Prime | function to check digits ; check all digits are prime or not ; check if digits are prime or not ; To check if n is prime or not ; check for all factors ; To check if n is Full Prime ; The order is important here for efficiency . ; Driver Code
< ? php function checkDigits ( $ n ) { while ( $ n ) { $ dig = $ n % 10 ; if ( $ dig != 2 && $ dig != 3 && $ dig != 5 && $ dig != 7 ) return false ; $ n = ( int ) ( $ n / 10 ) ; } return true ; } function prime ( $ n ) { if ( $ n == 1 ) return false ; for ( $ i = 2 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) return false ; } return true ; } function isFullPrime ( $ n ) { return ( checkDigits ( $ n ) && prime ( $ n ) ) ; } $ n = 53 ; if ( isFullPrime ( $ n ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >
SchrÃ ¶ derâ €“ Hipparchus number | A memoization based optimized PHP program to find n - th SchrAderaHipparchus number ; Driver Code
< ? php $ MAX = 500 ; function nthSHN ( $ n , $ dp ) { if ( $ n == 1 $ n == 2 ) return $ dp [ $ n ] = 1 ; if ( $ dp [ $ n ] != -1 ) return $ dp [ $ n ] ; return $ dp [ $ n ] = ( ( 6 * $ n - 9 ) * nthSHN ( $ n - 1 , $ dp ) - ( $ n - 3 ) * nthSHN ( $ n - 2 , $ dp ) ) / $ n ; } $ n = 6 ; $ dp = array_fill ( 0 , $ MAX , true ) ; echo nthSHN ( $ n , $ dp ) , " STRNEWLINE " ; ? >
Sum of first n even numbers | function to find sum of first n even numbers ; sum of first n even numbers ; next even number ; required sum ; Driver program to test above
< ? php function evenSum ( $ n ) { $ curr = 2 ; $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ sum += $ curr ; $ curr += 2 ; } return $ sum ; } $ n = 20 ; echo " Sum ▁ of ▁ first ▁ " . $ n . " ▁ Even ▁ numbers ▁ is : ▁ " . evenSum ( $ n ) ; ? >
Sum of first n even numbers | function to find sum of first n even numbers ; required sum ; Driver Code
< ? php function evenSum ( $ n ) { return ( $ n * ( $ n + 1 ) ) ; } $ n = 20 ; echo " Sum ▁ of ▁ first ▁ " , $ n , " ▁ Even ▁ numbers ▁ is : ▁ " , evenSum ( $ n ) ; ? >
Program to Convert Km / hr to miles / hr and vice versa | Function to convert kmph to mph ; Function to convert mph to kmph ; Driver Code
< ? php function kmphTOmph ( $ kmph ) { return 0.6214 * $ kmph ; } function mphTOkmph ( $ mph ) { return $ mph * 1.60934 ; } $ kmph = 150 ; $ mph = 100 ; echo " speed ▁ in ▁ mph ▁ is ▁ " , kmphTOmph ( $ kmph ) , " STRNEWLINE " ; echo " speed ▁ in ▁ kmph ▁ is ▁ " , mphTOkmph ( $ mph ) ; ? >