codeparrot/xlcost-text-to-code huggingface dataset:
Regular polygon using only 1 s in a binary numbered circle | method returns true if polygon is possible with ' midpoints ' number of midpoints ; loop for getting first vertex of polygon ; loop over array values at ' midpoints ' distance ; and ( & ) all those values , if even one of them is 0 , val will be 0 ; if val is still 1 and ( N / midpoints ) or ( number of vertices ) are more than two ( for a polygon minimum ) print result and return true ; method prints sides in the polygon or print not possible in case of no possible polygon ; limit for iterating over divisors ; If i divides N then i and ( N / i ) will be divisors ; check polygon for both divisors ; Driver Code
< ? php function checkPolygonWithMidpoints ( $ arr , $ N , $ midpoints ) { for ( $ j = 0 ; $ j < $ midpoints ; $ j ++ ) { $ val = 1 ; for ( $ k = $ j ; $ k < $ N ; $ k += $ midpoints ) { $ val &= $ arr [ $ k ] ; } if ( $ val && $ N / $ midpoints > 2 ) { echo " Polygon ▁ possible ▁ with ▁ side ▁ length ▁ " , ( $ N / $ midpoints ) , " STRNEWLINE " ; return true ; } } return false ; } function isPolygonPossible ( $ arr , $ N ) { $ limit = sqrt ( $ N ) ; for ( $ i = 1 ; $ i <= $ limit ; $ i ++ ) { if ( $ N % $ i == 0 ) { if ( checkPolygonWithMidpoints ( $ arr , $ N , $ i ) || checkPolygonWithMidpoints ( $ arr , $ N , ( $ N / $ i ) ) ) return ; } } echo " Not ▁ possiblen " ; } $ arr = array ( 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 1 ) ; $ N = sizeof ( $ arr ) ; isPolygonPossible ( $ arr , $ N ) ; ? >
Maximum height when coins are arranged in a triangle | Returns the square root of n . Note that the function ; We are using n itself as initial approximation This can definitely be improved ; e decides the accuracy level ; Method to find maximum height of arrangement of coins ; calculating portion inside the square root ; Driver code to test above method
< ? php function squareRoot ( $ n ) { $ x = $ n ; $ y = 1 ; $ e = 0.000001 ; while ( $ x - $ y > $ e ) { $ x = ( $ x + $ y ) / 2 ; $ y = $ n / $ x ; } return $ x ; } function findMaximumHeight ( $ N ) { $ n = 1 + 8 * $ N ; $ maxH = ( -1 + squareRoot ( $ n ) ) / 2 ; return floor ( $ maxH ) ; } $ N = 12 ; echo findMaximumHeight ( $ N ) ; ? >
The Stock Span Problem | Fills array S [ ] with span values ; Span value of first day is always 1 ; Calculate span value of remaining days by linearly checking previous days ; Initialize span value ; Traverse left while the next element on left is smaller than price [ i ] ; print the calculated span values ; Driver Code ; Fill the span values in array S [ ]
< ? php function calculateSpan ( $ price , $ n , $ S ) { $ S [ 0 ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ S [ $ i ] = 1 ; for ( $ j = $ i - 1 ; ( $ j >= 0 ) && ( $ price [ $ i ] >= $ price [ $ j ] ) ; $ j -- ) $ S [ $ i ] ++ ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ S [ $ i ] . " ▁ " ; ; } $ price = array ( 10 , 4 , 5 , 90 , 120 , 80 ) ; $ n = count ( $ price ) ; $ S = array ( $ n ) ; calculateSpan ( $ price , $ n , $ S ) ; ? >
Next Greater Element | prints element and NGE pair for all elements of arr [ ] of size n ; Driver Code
< ? php function printNGE ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ next = -1 ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { if ( $ arr [ $ i ] < $ arr [ $ j ] ) { $ next = $ arr [ $ j ] ; break ; } } echo $ arr [ $ i ] . " -- " . ▁ $ next . " " } } $ arr = array ( 11 , 13 , 21 , 3 ) ; $ n = count ( $ arr ) ; printNGE ( $ arr , $ n ) ; ? >
Program for Tower of Hanoi | Tower of Hanoi ( n - disk ) algorithm in PHP with Display of Pole / rod Contents the 3 poles representation ; Number of disks ; A , B and C are names of rods
< ? php $ poles = array ( array ( ) , array ( ) , array ( ) ) ; function TOH ( $ n , $ A = " A " , $ B = " B " , $ C = " C " ) { if ( $ n > 0 ) { TOH ( $ n - 1 , $ A , $ C , $ B ) ; echo " Move ▁ disk ▁ from ▁ rod ▁ $ A ▁ to ▁ rod ▁ $ C ▁ STRNEWLINE " ; move ( $ A , $ C ) ; dispPoles ( ) ; TOH ( $ n - 1 , $ B , $ A , $ C ) ; } else { return ; } } function initPoles ( $ n ) { global $ poles ; for ( $ i = $ n ; $ i >= 1 ; -- $ i ) { $ poles [ 0 ] [ ] = $ i ; } } function move ( $ source , $ destination ) { global $ poles ; if ( $ source == " A " ) $ ptr1 = 0 ; elseif ( $ source == " B " ) $ ptr1 = 1 ; else $ ptr1 = 2 ; if ( $ destination == " A " ) $ ptr2 = 0 ; elseif ( $ destination == " B " ) $ ptr2 = 1 ; else $ ptr2 = 2 ; $ top = array_pop ( $ poles [ $ ptr1 ] ) ; array_push ( $ poles [ $ ptr2 ] , $ top ) ; } function dispPoles ( ) { global $ poles ; echo " A : ▁ [ " . implode ( " , ▁ " , $ poles [ 0 ] ) . " ] ▁ " ; echo " B : ▁ [ " . implode ( " , ▁ " , $ poles [ 1 ] ) . " ] ▁ " ; echo " C : ▁ [ " . implode ( " , ▁ " , $ poles [ 2 ] ) . " ] ▁ " ; echo " STRNEWLINE STRNEWLINE " ; } $ numdisks = 4 ; initPoles ( $ numdisks ) ; echo " Tower ▁ of ▁ Hanoi ▁ Solution ▁ for ▁ $ numdisks ▁ disks : ▁ STRNEWLINE STRNEWLINE " ; dispPoles ( ) ; TOH ( $ numdisks ) ; ? >
Find maximum of minimum for every window size in a given array | PHP program to find maximum of minimum of all windows of different sizes Method to find maximum of minimum of all windows of different sizes ; Consider all windows of different sizes starting from size 1 ; Initialize max of min for current window size k ; Traverse through all windows of current size k ; Find minimum of current window ; Update maxOfMin if required ; Print max of min for current window size ; Driver Code
< ? php function printMaxOfMin ( $ arr , $ n ) { for ( $ k = 1 ; $ k <= $ n ; $ k ++ ) { $ maxOfMin = PHP_INT_MIN ; for ( $ i = 0 ; $ i <= $ n - $ k ; $ i ++ ) { $ min = $ arr [ $ i ] ; for ( $ j = 1 ; $ j < $ k ; $ j ++ ) { if ( $ arr [ $ i + $ j ] < $ min ) $ min = $ arr [ $ i + $ j ] ; } if ( $ min > $ maxOfMin ) $ maxOfMin = $ min ; } echo $ maxOfMin , " " ; } } $ arr = array ( 10 , 20 , 30 , 50 , 10 , 70 , 30 ) ; $ n = sizeof ( $ arr ) ; printMaxOfMin ( $ arr , $ n ) ; ? >
Reverse a number using stack | Stack to maintain order of digits ; Function to push digits into stack ; Function to reverse the number ; Function call to push number 's   digits to stack ; Popping the digits and forming the reversed number ; Return the reversed number formed ; Driver Code ; Function call to reverse number
< ? php $ st = array ( ) ; function push_digits ( $ number ) { global $ st ; while ( $ number != 0 ) { array_push ( $ st , $ number % 10 ) ; $ number = ( int ) ( $ number / 10 ) ; } } function reverse_number ( $ number ) { global $ st ; push_digits ( $ number ) ; $ reverse = 0 ; $ i = 1 ; while ( ! empty ( $ st ) ) { $ reverse = $ reverse + ( $ st [ count ( $ st ) - 1 ] * $ i ) ; array_pop ( $ st ) ; $ i = $ i * 10 ; } return $ reverse ; } $ number = 39997 ; echo reverse_number ( $ number ) ; ? >
HeapSort | To heapify a subtree rooted with node i which is an index in arr [ ] . n is size of heap ; Initialize largest as root ; left = 2 * i + 1 ; right = 2 * i + 2 ; If left child is larger than root ; If right child is larger than largest so far ; If largest is not root ; Recursively heapify the affected sub - tree ; main function to do heap sort ; Build heap ( rearrange array ) ; One by one extract an element from heap ; Move current root to end ; call max heapify on the reduced heap ; A utility function to print array of size n ; Driver program
< ? php function heapify ( & $ arr , $ n , $ i ) { $ largest = $ i ; $ l = 2 * $ i + 1 ; $ r = 2 * $ i + 2 ; if ( $ l < $ n && $ arr [ $ l ] > $ arr [ $ largest ] ) $ largest = $ l ; if ( $ r < $ n && $ arr [ $ r ] > $ arr [ $ largest ] ) $ largest = $ r ; if ( $ largest != $ i ) { $ swap = $ arr [ $ i ] ; $ arr [ $ i ] = $ arr [ $ largest ] ; $ arr [ $ largest ] = $ swap ; heapify ( $ arr , $ n , $ largest ) ; } } function heapSort ( & $ arr , $ n ) { for ( $ i = $ n / 2 - 1 ; $ i >= 0 ; $ i -- ) heapify ( $ arr , $ n , $ i ) ; for ( $ i = $ n - 1 ; $ i > 0 ; $ i -- ) { $ temp = $ arr [ 0 ] ; $ arr [ 0 ] = $ arr [ $ i ] ; $ arr [ $ i ] = $ temp ; heapify ( $ arr , $ i , 0 ) ; } } function printArray ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; ++ $ i ) echo ( $ arr [ $ i ] . " ▁ " ) ; } $ arr = array ( 12 , 11 , 13 , 5 , 6 , 7 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; heapSort ( $ arr , $ n ) ; echo ' Sorted array is ' . " STRNEWLINE " ; printArray ( $ arr , $ n ) ; ? >
How to check if a given array represents a Binary Heap ? | Returns true if arr [ i . . n - 1 ] represents a max - heap ; If a leaf node ; If an internal node and is greater than its children , and same is recursively true for the children ; Driver Code
< ? php function isHeap ( $ arr , $ i , $ n ) { if ( $ i >= ( $ n - 2 ) / 2 ) return true ; if ( $ arr [ $ i ] >= $ arr [ 2 * $ i + 1 ] && $ arr [ $ i ] >= $ arr [ 2 * $ i + 2 ] && isHeap ( $ arr , 2 * $ i + 1 , $ n ) && isHeap ( $ arr , 2 * $ i + 2 , $ n ) ) return true ; return false ; } $ arr = array ( 90 , 15 , 10 , 7 , 12 , 2 , 7 , 3 ) ; $ n = sizeof ( $ arr ) ; if ( isHeap ( $ arr , 0 , $ n ) ) echo " Yes " ; else echo " No " ; ? >
How to check if a given array represents a Binary Heap ? | Returns true if arr [ i . . n - 1 ] represents a max - heap ; Start from root and go till the last internal node ; If left child is greater , return false ; If right child is greater , return false ; Driver Code
< ? php function isHeap ( $ arr , $ i , $ n ) { for ( $ i = 0 ; $ i < ( ( $ n - 2 ) / 2 ) + 1 ; $ i ++ ) { if ( $ arr [ 2 * $ i + 1 ] > $ arr [ $ i ] ) return False ; if ( 2 * $ i + 2 < $ n && $ arr [ 2 * $ i + 2 ] > $ arr [ $ i ] ) return False ; return True ; } } $ arr = array ( 90 , 15 , 10 , 7 , 12 , 2 , 7 , 3 ) ; $ n = sizeof ( $ arr ) ; if ( isHeap ( $ arr , 0 , $ n ) ) echo " Yes " ; else echo " No " ; ? >
Smallest Derangement of Sequence | Efficient PHP program to find smallest derangement . ; Generate Sequence S ; Generate Derangement ; Only if i is odd Swap S [ N - 1 ] and S [ N ] ; Print Derangement ; Driver Program
< ? php function generate_derangement ( $ N ) { $ S = array ( ) ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) $ S [ $ i ] = $ i ; $ D = array ( ) ; for ( $ i = 1 ; $ i <= $ N ; $ i += 2 ) { if ( $ i == $ N ) { $ D [ $ N ] = $ S [ $ N - 1 ] ; $ D [ $ N - 1 ] = $ S [ $ N ] ; } else { $ D [ $ i ] = $ i + 1 ; $ D [ $ i + 1 ] = $ i ; } } for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) echo $ D [ $ i ] . " ▁ " ; echo " STRNEWLINE " ; } generate_derangement ( 10 ) ; ? >
Program to calculate Profit Or Loss | Function to calculate Profit . ; Function to calculate Loss . ; Driver Code .
< ? php function Profit ( $ costPrice , $ sellingPrice ) { $ profit = ( $ sellingPrice - $ costPrice ) ; return $ profit ; } function Loss ( $ costPrice , $ sellingPrice ) { $ Loss = ( $ costPrice - $ sellingPrice ) ; return $ Loss ; } $ costPrice = 1500 ; $ sellingPrice = 2000 ; if ( $ sellingPrice == $ costPrice ) echo " No ▁ profit ▁ nor ▁ Loss " ; else if ( $ sellingPrice > $ costPrice ) echo Profit ( $ costPrice , $ sellingPrice ) . " ▁ Profit ▁ " ; else echo Loss ( $ costPrice , $ sellingPrice ) . " ▁ Loss ▁ " ; ? >
Find the Next perfect square greater than a given number | Function to find the next perfect square ; Driver Code
< ? php function nextPerfectSquare ( $ N ) { $ nextN = floor ( sqrt ( $ N ) ) + 1 ; return $ nextN * $ nextN ; } $ n = 35 ; echo nextPerfectSquare ( $ n ) ; ? >
Print all substring of a number without any conversion | Function to print the substrings of a number ; Calculate the total number of digits ; 0.5 has been added because of it will return double value like 99.556 ; Print all the numbers from starting position ; Update the no . ; Update the no . of digits ; Driver code
< ? php function printSubstrings ( $ n ) { $ s = ( int ) log10 ( $ n ) ; $ d = ( int ) ( pow ( 10 , $ s ) + 0.5 ) ; $ k = $ d ; while ( $ n ) { while ( $ d ) { echo ( int ) ( $ n / $ d ) . " STRNEWLINE " ; $ d = ( int ) ( $ d / 10 ) ; } $ n = $ n % $ k ; $ k = ( int ) ( $ k / 10 ) ; $ d = $ k ; } } $ n = 123 ; printSubstrings ( $ n ) ; ? >
Modulo power for large numbers represented as strings | PHP program to find ( a ^ b ) % MOD where a and b may be very large and represented as strings . ; Returns modulo exponentiation for two numbers represented as long long int . It is used by powerStrings ( ) . Its complexity is log ( n ) ; Returns modulo exponentiation for two numbers represented as strings . It is used by powerStrings ( ) ; We convert strings to number ; calculating a % MOD ; calculating b % ( MOD - 1 ) ; Now a and b are long long int . We calculate a ^ b using modulo exponentiation ; As numbers are very large that is it may contains upto 10 ^ 6 digits . So , we use string .
< ? php $ MOD = 1000000007 ; function powerLL ( $ x , $ n ) { global $ MOD ; $ result = 1 ; while ( $ n ) { if ( $ n & 1 ) $ result = $ result * $ x % $ MOD ; $ n = ( int ) $ n / 2 ; $ x = $ x * $ x % $ MOD ; } return $ result ; } function powerStrings ( $ sa , $ sb ) { global $ MOD ; $ a = 0 ; $ b = 0 ; for ( $ i = 0 ; $ i < strlen ( $ sa ) ; $ i ++ ) $ a = ( $ a * 10 + ( $ sa [ $ i ] - '0' ) ) % $ MOD ; for ( $ i = 0 ; $ i < strlen ( $ sb ) ; $ i ++ ) $ b = ( $ b * 10 + ( $ sb [ $ i ] - '0' ) ) % ( $ MOD - 1 ) ; return powerLL ( $ a , $ b ) ; } $ sa = "2" ; $ sb = "3" ; echo powerStrings ( $ sa , $ sb ) ; ? >
Check if a number can be expressed as 2 ^ x + 2 ^ y | Utility function to check if a number is power of 2 or not ; Utility function to determine the value of previous power of 2 ; function to check if n can be expressed as 2 ^ x + 2 ^ y or not ; if value of n is 0 or 1 it can not be expressed as 2 ^ x + 2 ^ y ; if a number is power of 2 then it can be expressed as 2 ^ x + 2 ^ y ; if the remainder after subtracting previous power of 2 is also a power of 2 then it can be expressed as 2 ^ x + 2 ^ y ; Driver code
< ? php function isPowerOfTwo ( $ n ) { return ( $ n and ! ( $ n & ( $ n - 1 ) ) ) ; } function previousPowerOfTwo ( $ n ) { while ( $ n & $ n - 1 ) { $ n = $ n & $ n - 1 ; } return $ n ; } function checkSum ( $ n ) { if ( $ n == 0 or $ n == 1 ) return false ; else if ( isPowerOfTwo ( $ n ) ) { echo " " ▁ , ▁ $ n ▁ / ▁ 2 ▁ , ▁ " " return true ; } else { $ x = previousPowerOfTwo ( $ n ) ; $ y = $ n - $ x ; if ( isPowerOfTwo ( $ y ) ) { echo $ x , " " , $ y ; return true ; } } return false ; } $ n1 = 20 ; if ( checkSum ( $ n1 ) == false ) echo " No " ; echo " STRNEWLINE " ; $ n2 = 11 ; if ( checkSum ( $ n2 ) == false ) echo " No " ; ? >
10 's Complement of a decimal number | Function to find 10 's complement ; Calculating total digits in num ; restore num ; calculate 10 's complement ; Driver code
< ? php function complement ( $ num ) { $ i ; $ len = 0 ; $ comp ; $ temp = $ num ; while ( 1 ) { $ len ++ ; $ num = ( int ) ( $ num / 10 ) ; if ( abs ( $ num ) == 0 ) break ; } $ num = $ temp ; $ comp = pow ( 10 , $ len ) - $ num ; return $ comp ; } echo complement ( 25 ) . " STRNEWLINE " ; echo complement ( 456 ) ; ? >
Program to find HCF ( Highest Common Factor ) of 2 Numbers | Recursive function to return gcd of a and b ; Everything divides 0 ; base case ; a is greater ; Driver code
< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 && $ b == 0 ) return 0 ; if ( $ a == 0 ) return $ b ; if ( $ b == 0 ) return $ a ; if ( $ a == $ b ) return $ a ; if ( $ a > $ b ) return gcd ( $ a - $ b , $ b ) ; return gcd ( $ a , $ b - $ a ) ; } $ a = 98 ; $ b = 56 ; echo " GCD ▁ of ▁ $ a ▁ and ▁ $ b ▁ is ▁ " , gcd ( $ a , $ b ) ; ? >
Sum of all elements up to Nth row in a Pascal triangle | Function to find sum of all elements upto nth row . ; Initialize sum with 0 ; Loop to calculate power of 2 upto n and add them ; Driver Code
< ? php function calculateSum ( $ n ) { $ sum = 0 ; for ( $ row = 0 ; $ row < $ n ; $ row ++ ) { $ sum = $ sum + ( 1 << $ row ) ; } return $ sum ; } $ n = 10 ; echo " ▁ Sum ▁ of ▁ all ▁ elements ▁ : ▁ " . calculateSum ( $ n ) ; ? >
Number of sequences which has HEAD at alternate positions to the right of the first HEAD | function to calculate total sequences possible ; Value of N is even ; Value of N is odd ; Driver code
< ? php function findAllSequence ( $ N ) { if ( $ N % 2 == 0 ) { return pow ( 2 , $ N / 2 + 1 ) + pow ( 2 , $ N / 2 ) - 2 ; } else { return pow ( 2 , ( $ N + 1 ) / 2 ) + pow ( 2 , ( $ N + 1 ) / 2 ) - 2 ; } } $ N = 2 ; echo findAllSequence ( $ N ) ; ? >
Number of ones in the smallest repunit | Function to find number of 1 s in smallest repunit multiple of the number ; to store number of 1 s in smallest repunit multiple of the number . ; initialize rem with 1 ; run loop until rem becomes zero ; rem * 10 + 1 here represents the repunit modulo n ; when remainder becomes 0 return count ; Driver Code ; Calling function
< ? php function countOnes ( $ n ) { $ count = 1 ; $ rem = 1 ; while ( $ rem != 0 ) { $ rem = ( $ rem * 10 + 1 ) % $ n ; $ count ++ ; } return $ count ; } $ n = 13 ; echo countOnes ( $ n ) ; ? >
Largest of two distinct numbers without using any conditional statements or operators | Function to find the largest number ; Drivers code
< ? php function largestNum ( $ a , $ b ) { return ( $ a * ( boolean ) floor ( ( $ a / $ b ) ) ) + ( $ b * ( boolean ) floor ( ( $ b / $ a ) ) ) ; } $ a = 22 ; $ b = 1231 ; echo ( largestNum ( $ a , $ b ) ) ;
Number of Distinct Meeting Points on a Circular Road | Returns the GCD of two number . ; Returns the number of distinct meeting points . ; Find the relative speed . ; convert the negative value to positive . ; Driver Code
< ? php function gcd ( $ a , $ b ) { $ c = $ a % $ b ; while ( $ c != 0 ) { $ a = $ b ; $ b = $ c ; $ c = $ a % $ b ; } return $ b ; } function numberOfmeet ( $ a , $ b ) { $ ans ; if ( $ a > $ b ) $ ans = $ a - $ b ; else $ ans = $ b - $ a ; if ( $ a < 0 ) $ a = $ a * ( -1 ) ; if ( $ b < 0 ) $ b = $ b * ( -1 ) ; return $ ans / gcd ( $ a , $ b ) ; } $ a = 1 ; $ b = -1 ; echo numberOfmeet ( $ a , $ b ) . " STRNEWLINE " ; ? >
Find larger of x ^ y and y ^ x | PHP program to print greater of x ^ y and y ^ x ; Driver Code
< ? php function printGreater ( $ x , $ y ) { $ X = $ y * log ( $ x ) ; $ Y = $ x * log ( $ y ) ; if ( abs ( $ X - $ Y ) < 1e-9 ) { echo " Equal " ; } else if ( $ X > $ Y ) { echo $ x . " ^ " . $ y ; } else { echo $ y . " ^ " . $ x ; } } $ x = 5 ; $ y = 8 ; printGreater ( $ x , $ y ) ; ? >
n | Function to find nth term ; Driver code
< ? php function sumOfSeries ( $ n ) { return $ n * ( $ n + 1 ) * ( 6 * $ n * $ n * $ n + 9 * $ n * $ n + $ n - 1 ) / 30 ; } $ n = 4 ; echo sumOfSeries ( $ n ) ; ? >
Product of first N factorials | To compute ( a * b ) % MOD ; $res = 0 ; Initialize result ; If b is odd , add ' a ' to result ; Multiply ' a ' with 2 ; Divide b by 2 ; Return result ; This function computes factorials and product by using above function i . e . modular multiplication ; Initialize product and fact with 1 ; ith factorial ; product of first i factorials ; If at any iteration , product becomes divisible by MOD , simply return 0 ; ; Driver Code
< ? php function mulmod ( $ a , $ b , $ mod ) { $ a = $ a % $ mod ; while ( $ b > 0 ) { if ( $ b % 2 == 1 ) $ res = ( $ res + $ a ) % $ mod ; $ a = ( $ a * 2 ) % $ mod ; $ b /= 2 ; } return $ res % $ mod ; } function findProduct ( $ N ) { $ product = 1 ; $ fact = 1 ; $ MOD = 1000000000 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) { $ fact = mulmod ( $ fact , $ i , $ MOD ) ; $ product = mulmod ( $ product , $ fact , $ MOD ) ; if ( $ product == 0 ) return 0 ; } return $ product ; } $ N = 3 ; echo findProduct ( $ N ) , " STRNEWLINE " ; $ N = 5 ; echo findProduct ( $ N ) , " STRNEWLINE " ; ? >
Check if sum of divisors of two numbers are same | Function to calculate sum of all proper divisors num -- > given natural number ; To store sum of divisors ; Find all divisors and add them ; Function to check if both numbers are equivalent or not ; Driver code
< ? php function divSum ( $ n ) { $ sum = 1 ; for ( $ i = 2 ; $ i * $ i <= $ n ; $ i ++ ) if ( $ n % $ i == 0 ) $ sum = $ sum + $ i + floor ( $ n / $ i ) ; return $ sum ; } function areEquivalent ( $ num1 , $ num2 ) { return divSum ( $ num1 ) == divSum ( $ num2 ) ; } $ num1 = 559 ; $ num2 = 703 ; if ( areEquivalent ( $ num1 , $ num2 ) == true ) echo " Equivalent " ; else echo " Not ▁ Equivalent " ; ? >
Dodecahedral number | Function to find dodecahedral number ; Formula to calculate nth dodecahedral number and return it into main function . ; Drivers Code ; print result
< ? php function dodecahedral_num ( $ n ) { return $ n * ( 3 * $ n - 1 ) * ( 3 * $ n - 2 ) / 2 ; } $ n = 5 ; echo $ n , " th ▁ Dodecahedral ▁ number : ▁ " ; echo dodecahedral_num ( $ n ) ; ? >
Count of divisors having more set bits than quotient on dividing N | Return the count of set bit . ; check if q and d have same number of set bit . ; Binary Search to find the point at which number of set in q is less than or equal to d . ; while left index is less than right index ; finding the middle . ; check if q and d have same number of set it or not . ; Driver Code
< ? php function bit ( $ x ) { $ ans = 0 ; while ( $ x ) { $ x /= 2 ; $ ans ++ ; } return $ ans ; } function check ( $ d , $ x ) { if ( bit ( $ x / $ d ) <= bit ( $ d ) ) return true ; return false ; } function bs ( int $ n ) { $ l = 1 ; $ r = sqrt ( $ n ) ; while ( $ l < $ r ) { $ m = ( $ l + $ r ) / 2 ; if ( check ( $ m , $ n ) ) $ r = $ m ; else $ l = $ m + 1 ; } if ( ! check ( $ l , $ n ) ) return floor ( $ l + 1 ) ; else return floor ( $ l ) ; } function countDivisor ( $ n ) { return $ n - bs ( $ n ) + 1 ; } $ n = 5 ; echo countDivisor ( $ n ) ; ? >
Check if two people starting from different points ever meet | PHP program to find if two people starting from different positions ever meet or not . ; If speed of a person at a position before other person is smaller , then return false . ; Making sure that x1 is greater ; Until one person crosses other ; first person taking one jump in each iteration ; second person taking one jump in each iteration ; Driver code
< ? php function everMeet ( $ x1 , $ x2 , $ v1 , $ v2 ) { if ( $ x1 < $ x2 && $ v1 <= $ v2 ) return false ; if ( $ x1 > $ x2 && $ v1 >= $ v2 ) return false ; if ( $ x1 < $ x2 ) { list ( $ x1 , $ x2 ) = array ( $ x2 , $ x1 ) ; list ( $ v1 , $ v2 ) = array ( $ v2 , $ v1 ) ; } while ( $ x1 >= $ x2 ) { if ( $ x1 == $ x2 ) return true ; $ x1 = $ x1 + $ v1 ; $ x2 = $ x2 + $ v2 ; } return false ; } $ x1 = 5 ; $ v1 = 8 ; $ x2 = 4 ; $ v2 = 7 ; if ( everMeet ( $ x1 , $ x2 , $ v1 , $ v2 ) ) echo " Yes " ; else echo " No " ; ? >
Divisibility by 3 where each digit is the sum of all prefix digits modulo 10 | Function to check the divisibility ; Cycle ; no of residual terms ; sum of residual terms ; if no of residue term = 0 ; if no of residue term = 1 ; if no of residue term = 2 ; if no of residue term = 3 ; sum of all digits ; divisibility check ; Driver code
< ? php function check ( $ k , $ d0 , $ d1 ) { $ s = ( 2 * ( $ d0 + $ d1 ) ) % 10 + ( 4 * ( $ d0 + $ d1 ) ) % 10 + ( 8 * ( $ d0 + $ d1 ) ) % 10 + ( 6 * ( $ d0 + $ d1 ) ) % 10 ; $ a = ( $ k - 3 ) % 4 ; $ x ; switch ( $ a ) { case 0 : $ x = 0 ; break ; case 1 : $ x = ( 2 * ( $ d0 + $ d1 ) ) % 10 ; break ; case 2 : $ x = ( 2 * ( $ d0 + $ d1 ) ) % 10 + ( 4 * ( $ d0 + $ d1 ) ) % 10 ; break ; case 3 : $ x = ( 2 * ( $ d0 + $ d1 ) ) % 10 + ( 4 * ( $ d0 + $ d1 ) ) % 10 + ( 8 * ( $ d0 + $ d1 ) ) % 10 ; break ; } $ sum = $ d0 + $ d1 + ( int ) ( ( $ k - 3 ) / 4 ) * $ s + $ x ; if ( $ sum % 3 == 0 ) return " YES " ; return " NO " ; } $ k ; $ d0 ; $ d1 ; $ k = 13 ; $ d0 = 8 ; $ d1 = 1 ; echo check ( $ k , $ d0 , $ d1 ) , " STRNEWLINE " ; $ k = 5 ; $ d0 = 3 ; $ d1 = 4 ; echo check ( $ k , $ d0 , $ d1 ) , " STRNEWLINE " ; ? >
Find ceil of a / b without using ceil ( ) function | taking input 1 ; example of perfect division taking input 2
< ? php $ a = 4 ; $ b = 3 ; $ val = ( $ a / $ b ) + ( ( $ a % $ b ) != 0 ) ; echo " The ▁ ceiling ▁ value ▁ of ▁ 4/3 ▁ is ▁ " , floor ( $ val ) , " STRNEWLINE " ; $ a = 6 ; $ b = 3 ; $ val = ( $ a / $ b ) + ( ( $ a % $ b ) != 0 ) ; echo " The ▁ ceiling ▁ value ▁ of ▁ 6/3 ▁ is ▁ " , $ val ; ? >
Program to print Collatz Sequence | PHP program to print Collatz sequence ; We simply follow steps while we do not reach 1 ; If $n is odd ; If even ; Print 1 at the end ; Driver code
< ? php function printCollatz ( $ n ) { while ( $ n != 1 ) { echo $ n . " " ; if ( $ n & 1 ) $ n = 3 * $ n + 1 ; else $ n = $ n / 2 ; } echo $ n ; } printCollatz ( 6 ) ; ? >