"Print all multiplicative primes <= N | Function to return the digit product of n ; Function to print all multiplicative primes <= n ; Create a boolean array " prime [ 0 . . n + 1 ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; If i is prime and its digit sum is also prime i . e . i is a multiplicative prime ; Driver code"
Solution in php:

< ? php function digitProduct ( $ n ) { $ prod = 1 ; while ( $ n ) { $ prod = $ prod * ( $ n % 10 ) ; $ n = floor ( $ n / 10 ) ; } return $ prod ; } function printMultiplicativePrimes ( $ n ) { $ prime = array_fill ( 0 , $ n + 1 , true ) ; $ prime [ 0 ] = $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = false ; } } for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { if ( $ prime [ $ i ] && $ prime [ digitProduct ( $ i ) ] ) echo $ i , " ▁ " ; } } $ n = 10 ; printMultiplicativePrimes ( $ n ) ; ? >


"Print all n | n , sum -- > value of inputs out -- > output array index -- > index of next digit to be filled in output array ; Base case ; If number becomes N - digit ; if sum of its digits is equal to given sum , print it ; Traverse through every digit . Note that here we ' re ▁ considering ▁ leading ▁ ▁ 0' s as digits ; append current digit to number ; recurse for next digit with reduced sum ; This is mainly a wrapper over findNDigitNumsUtil . It explicitly handles leading digit ; output array to store N - digit numbers ; fill 1 st position by every digit from 1 to 9 and calls findNDigitNumsUtil ( ) for remaining positions ; Driver Code"
Solution in php:

< ? php function findNDigitNumsUtil ( $ n , $ sum , $ out , $ index ) { if ( $ index > $ n $ sum < 0 ) return ; if ( $ index == $ n ) { if ( $ sum == 0 ) { $ out [ $ index ] = ' ' ; foreach ( $ out as & $ value ) print ( $ value ) ; print ( " ▁ " ) ; } return ; } for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { $ out [ $ index ] = chr ( $ i + ord ( '0' ) ) ; findNDigitNumsUtil ( $ n , $ sum - $ i , $ out , $ index + 1 ) ; } } function findNDigitNums ( $ n , $ sum ) { $ out = array_fill ( 0 , $ n + 1 , false ) ; for ( $ i = 1 ; $ i <= 9 ; $ i ++ ) { $ out [ 0 ] = chr ( $ i + ord ( '0' ) ) ; findNDigitNumsUtil ( $ n , $ sum - $ i , $ out , 1 ) ; } } $ n = 2 ; $ sum = 3 ; findNDigitNums ( $ n , $ sum ) ; ? >


"Print all n | n -- > value of input out -- > output array index -- > index of next digit to be filled in output array evenSum , oddSum -- > sum of even and odd digits so far ; Base case ; If number becomes n - digit ; if absolute difference between sum of even and odd digits is 1 , print the number ; If current index is odd , then add it to odd sum and recurse ; else else add to even sum and recurse ; This is mainly a wrapper over findNDigitNumsUtil . It explicitly handles leading digit and calls findNDigitNumsUtil ( ) for remaining indexes . ; output array to store n - digit numbers ; Initialize number index considered so far ; Initialize even and odd sums ; Explicitly handle first digit and call recursive function findNDigitNumsUtil for remaining indexes . Note that the first digit is considered to be present in even position . ; Driver program"
Solution in php:

< ? php function findNDigitNumsUtil ( $ n , $ out , $ index , $ evenSum , $ oddSum ) { if ( $ index > $ n ) return ; if ( $ index == $ n ) { if ( abs ( $ evenSum - $ oddSum ) == 1 ) { echo implode ( " " , $ out ) . " " } return ; } if ( $ index & 1 ) { for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { $ out [ $ index ] = $ i + '0' ; findNDigitNumsUtil ( $ n , $ out , $ index + 1 , $ evenSum , $ oddSum + $ i ) ; } } { for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { $ out [ $ index ] = $ i + '0' ; findNDigitNumsUtil ( $ n , $ out , $ index + 1 , $ evenSum + $ i , $ oddSum ) ; } } } function findNDigitNums ( $ n ) { $ out = array_fill ( 0 , $ n + 1 , " " ) ; $ index = 0 ; $ evenSum = 0 ; $ oddSum = 0 ; for ( $ i = 1 ; $ i <= 9 ; $ i ++ ) { $ out [ $ index ] = $ i + '0' ; findNDigitNumsUtil ( $ n , $ out , $ index + 1 , $ evenSum + $ i , $ oddSum ) ; } } $ n = 3 ; findNDigitNums ( $ n ) ; ? >


"Print all non | function to print array arr [ 0. . n - 1 ] ; Recursive Function to generate all non - increasing sequences with sum x arr [ ] -- > Elements of current sequence curr_sum -- > Current Sum curr_idx -- > Current index in arr [ ] ; If current sum is equal to x , then we found a sequence ; Try placing all numbers from 1 to x - curr_sum at current index ; The placed number must also be smaller than previously placed numbers and it may be equal to the previous stored value , i . e . , arr [ curr_idx - 1 ] if there exists a previous number ; Place number at curr_idx ; Recur ; Try next number ; A wrapper over generateUtil ( ) ; Array to store sequences on by one ; Driver Code"
Solution in php:

 < ? php function printArr ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] , " ▁ " ; echo " ▁
 " ; } function generateUtil ( $ x , $ arr , $ curr_sum , $ curr_idx ) { if ( $ curr_sum == $ x ) { printArr ( $ arr , $ curr_idx ) ; return ; } $ num = 1 ; while ( $ num <= $ x - $ curr_sum and ( $ curr_idx == 0 or $ num <= $ arr [ $ curr_idx - 1 ] ) ) { $ arr [ $ curr_idx ] = $ num ; generateUtil ( $ x , $ arr , $ curr_sum + $ num , $ curr_idx + 1 ) ; $ num ++ ; } } function generate ( $ x ) { $ arr = array ( ) ; generateUtil ( $ x , $ arr , 0 , 0 ) ; } $ x = 5 ; generate ( $ x ) ; ? >


"Print all numbers whose set of prime factors is a subset of the set of the prime factors of X | Function to print all the numbers ; Iterate for every element in the array ; Find the gcd ; Iterate till gcd is 1 of number and x ; Divide the number by gcd ; Find the new gcdg ; If the number is 1 at the end then print the number ; If no numbers have been there ; Driver code"
Solution in php:

< ? php function printNumbers ( $ a , $ n , $ x ) { $ flag = false ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ num = $ a [ $ i ] ; $ g = __gcd ( $ num , $ x ) ; while ( $ g != 1 ) { $ num /= $ g ; $ g = __gcd ( $ num , $ x ) ; } if ( $ num == 1 ) { $ flag = true ; echo $ a [ $ i ] , " " ; } } if ( ! $ flag ) echo ( " There ▁ are ▁ no ▁ such ▁ numbers " ) ; } function __gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return __gcd ( $ b , $ a % $ b ) ; } $ x = 60 ; $ a = array ( 2 , 5 , 10 , 7 , 17 ) ; $ n = count ( $ a ) ; printNumbers ( $ a , $ n , $ x ) ; ? >


"Print all pairs with given sum | Returns number of pairs in arr [ 0. . n - 1 ] with sum equal to ' sum ' ; Initialize result ; Consider all possible pairs and check their sums ; Driver Code"
Solution in php:


"Print all possible strings that can be made by placing spaces | Function recursively prints the strings having space pattern . i and j are indices in ' str [ ] ' and ' buff [ ] ' respectively ; Either put the character ; Or put a space followed by next character ; This function creates buf [ ] to store individual output string and uses printPatternUtil ( ) to print all permutations . ; Buffer to hold the string containing spaces 2 n - 1 characters and 1 string terminator ; Copy the first character as it is , since it will be always at first position ; Driver code"
Solution in php:

 < ? php function printPatternUtil ( $ str , $ buff , $ i , $ j , $ n ) { if ( $ i == $ n ) { $ buff [ $ j ] = ' ' ; echo str_replace ( ' , ▁ ' , ' ' , implode ( ' , ▁ ' , $ buff ) ) . "
 " ; return ; } $ buff [ $ j ] = $ str [ $ i ] ; printPatternUtil ( $ str , $ buff , $ i + 1 , $ j + 1 , $ n ) ; $ buff [ $ j ] = ' ▁ ' ; $ buff [ $ j + 1 ] = $ str [ $ i ] ; printPatternUtil ( $ str , $ buff , $ i +1 , $ j + 2 , $ n ) ; } function printPattern ( $ str ) { $ n = strlen ( $ str ) ; $ buf = array_fill ( 0 , 2 * $ n , null ) ; $ buf [ 0 ] = $ str [ 0 ] ; printPatternUtil ( $ str , $ buf , 1 , 1 , $ n ) ; } $ str = " ABCD " ; printPattern ( $ str ) ; ? >


"Print all possible strings that can be made by placing spaces | Function to print all subsequences ; Driver code"
Solution in php:

 < ? php function printSubsequences ( $ str ) { $ n = strlen ( $ str ) ; $ opsize = pow ( 2 , $ n - 1 ) ; for ( $ counter = 0 ; $ counter < $ opsize ; $ counter ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { echo $ str [ $ j ] ; if ( $ counter & ( 1 << $ j ) ) echo " ▁ " ; } echo "
 " ; } } $ str = " ABC " ; printSubsequences ( $ str ) ; ? >


"Print all safe primes below N | Function to print first n safe primes ; Initialize all entries of integer array as 1. A value in prime [ i ] will finally be 0 if i is Not a prime , else 1 ; 0 and 1 are not primes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; If i is prime ; 2 p + 1 ; If 2 p + 1 is also a prime then set prime [ 2 p + 1 ] = 2 ; i is a safe prime ; Driver code"
Solution in php:


"Print all sequences of given length | A utility function that prints a given arr [ ] of length size ; The core function that recursively generates and prints all sequences of length k ; A function that uses printSequencesRecur ( ) to prints all sequences from 1 , 1 , . .1 to n , n , . . n ; Driver Code"
Solution in php:

 < ? php function printArray ( $ arr , $ size ) { for ( $ i = 0 ; $ i < $ size ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; echo "
 " ; return ; } function printSequencesRecur ( $ arr , $ n , $ k , $ index ) { if ( $ k == 0 ) { printArray ( $ arr , $ index ) ; } if ( $ k > 0 ) { for ( $ i = 1 ; $ i <= $ n ; ++ $ i ) { $ arr [ $ index ] = $ i ; printSequencesRecur ( $ arr , $ n , $ k - 1 , $ index + 1 ) ; } } } function printSequences ( $ n , $ k ) { $ arr = array ( ) ; printSequencesRecur ( $ arr , $ n , $ k , 0 ) ; return ; } $ n = 3 ; $ k = 2 ; printSequences ( $ n , $ k ) ; ? >


"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"
Solution in php:


"Print all the combinations of N elements by changing sign such that their sum is divisible by M | Function to print all the combinations ; Iterate for all combinations ; Initially 100 in binary if n is 3 as 1 << ( 3 - 1 ) = 100 in binary ; Iterate in the array and assign signs to the array elements ; If the j - th bit from left is set take ' + ' sign ; Right shift to check if jth bit is set or not ; re - initialize ; Iterate in the array elements ; If the jth from left is set ; right shift ; Driver Code"
Solution in php:


"Print all triplets in sorted array that form AP | Function to print all triplets in given sorted array that forms AP ; Search other two elements of AP with arr [ i ] as middle . ; if a triplet is found ; Since elements are distinct , arr [ k ] and arr [ j ] cannot form any more triplets with arr [ i ] ; If middle element is more move to higher side , else move lower side . ; Driver code"
Solution in php:


"Print all triplets in sorted array that form AP | Function to print all triplets in given sorted array that forms AP ; Use hash to find if there is a previous element with difference equal to arr [ j ] - arr [ i ] ; Driver code"
Solution in php:

< ? php function printAllAPTriplets ( $ arr , $ n ) { $ s = array ( ) ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { $ diff = $ arr [ $ j ] - $ arr [ $ i ] ; if ( in_array ( $ arr [ $ i ] - $ diff , $ arr ) ) echo ( ( $ arr [ $ i ] - $ diff ) . " " ▁ . ▁ $ arr [ $ i ] ▁ . ▁ " " ▁ . ▁ $ arr [ $ j ] ▁ . ▁ " " } array_push ( $ s , $ arr [ $ i ] ) ; } } $ arr = array ( 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 ) ; $ n = count ( $ arr ) ; printAllAPTriplets ( $ arr , $ n ) ; ? >


"Print all triplets with given sum | Prints all triplets in arr [ ] with given sum ; Driver code"
Solution in php:


"Print an N x M matrix such that each row and column has all the vowels in it | Function to print the required matrix ; Impossible to generate the required matrix ; Store all the vowels ; Print the matrix ; Print vowels for every index ; Shift the vowels by one ; Driver code"
Solution in php:


"Print array elements in alternatively increasing and decreasing order | Function to print array elements in alternative increasing and decreasing order ; First sort the array in increasing order ; start with 2 elements in increasing order ; till all the elements are not printed ; printing the elements in increasing order ; else printing the elements in decreasing order ; increasing the number of elements to printed in next iteration ; Driver Code"
Solution in php:


"Print digit 's position to be removed to make a number divisible by 6 | Function to print the number divisible by 6 after exactly removing a digit ; stores the sum of all elements ; traverses the string and converts string to number array and sums up ; if ( $a [ $n - 1 ] % 2 ) ODD CHECK ; if second last is odd or sum of n - 1 elements are not divisible by 3. ; second last is even and print n - 1 elements removing last digit ; last digit removed ; counter to check if any element after removing , its sum % 3 == 0 ; traverse till second last element ; to check if any element after removing , its sum % 3 == 0 ; the leftmost element ; break at the leftmost element ; stores the right most element ; if no element has been found as a [ i + 1 ] > a [ i ] ; if second last is even , then remove last if ( sum - last ) % 3 == 0 ; if no element which on removing gives sum % 3 == 0 ; Driver Code"
Solution in php:


"Print equal sum sets of array ( Partition problem ) | Set 1 | Function to print the equal sum sets of the array . ; Print set 1. ; Print set 2. ; Utility function to find the sets of the array which have equal sum . ; If entire array is traversed , compare both the sums . ; If sums are equal print both sets and return true to show sets are found . ; If sums are not equal then return sets are not found . ; Add current element to set 1. ; Recursive call after adding current element to set 1. ; If this inclusion results in equal sum sets partition then return true to show desired sets are found . ; If not then backtrack by removing current element from set1 and include it in set 2. ; Recursive call after including current element to set 2. ; Return true if array arr can be partitioned into two equal sum sets or not . ; Calculate sum of elements in array . ; If sum is odd then array cannot be partitioned . ; Declare vectors to store both the sets . ; Find both the sets . ; Driver code"
Solution in php:

 < ? php function printSets ( $ set1 , $ set2 ) { $ i = 0 ; for ( $ i = 0 ; $ i < count ( $ set1 ) ; $ i ++ ) { echo ( $ set1 [ $ i ] . " " ) ; } echo ( "
 " ) ; for ( $ i = 0 ; $ i < count ( $ set2 ) ; $ i ++ ) { echo ( $ set2 [ $ i ] . " " ) ; } } function findSets ( $ arr , $ n , & $ set1 , & $ set2 , $ sum1 , $ sum2 , $ pos ) { if ( $ pos == $ n ) { if ( $ sum1 == $ sum2 ) { printSets ( $ set1 , $ set2 ) ; return true ; } else return false ; } array_push ( $ set1 , $ arr [ $ pos ] ) ; $ res = findSets ( $ arr , $ n , $ set1 , $ set2 , $ sum1 + $ arr [ $ pos ] , $ sum2 , $ pos + 1 ) ; if ( $ res ) return $ res ; array_pop ( $ set1 ) ; array_push ( $ set2 , $ arr [ $ pos ] ) ; return findSets ( $ arr , $ n , $ set1 , $ set2 , $ sum1 , $ sum2 + $ arr [ $ pos ] , $ pos + 1 ) ; } function isPartitionPoss ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] ; if ( $ sum % 2 != 0 ) return false ; $ set1 = array ( ) ; $ set2 = array ( ) ; return findSets ( $ arr , $ n , $ set1 , $ set2 , 0 , 0 , 0 ) ; } $ arr = array ( 5 , 5 , 1 , 11 ) ; $ n = count ( $ arr ) ; if ( isPartitionPoss ( $ arr , $ n ) == false ) echo ( " - 1" ) ; ? >


"Print factorials of a range in right aligned format | PHP Program to print format of factorial ; vector for store the result ; variable for store the each number factorial ; copy of first number ; found first number factorial ; push the first number in result vector ; incerement the first number ; found the all reaming number factorial loop is working until all required number factorial founded ; store the result of factorial ; incerement the first number ; return the result ; function for print the result ; setw ( ) is used for fill the blank right is used for right justification of data ; number which found the factorial of between range ; store the result of factorial ; function for found factorial ; function for print format"
Solution in php:

< ? php function find_factorial ( $ num1 , $ num2 ) { $ vec ; $ t = 0 ; $ fac = 1 ; $ temp = $ num1 ; while ( 1 ) { if ( $ temp == 1 ) break ; $ fac *= $ temp ; $ temp -- ; } $ vec [ $ t ++ ] = $ fac ; $ num1 ++ ; while ( $ num1 <= $ num2 ) { $ fac *= $ num1 ; $ vec [ $ t ++ ] = $ fac ; $ num1 ++ ; } return ( $ vec ) ; } function print_format ( $ result ) { $ x = count ( $ result ) ; $ digits = strlen ( ( string ) $ result [ $ x - 1 ] ) ; for ( $ i = 0 ; $ i < $ x ; $ i ++ ) { echo str_pad ( $ result [ $ i ] , ( $ digits + 1 ) , " " , ▁ STR _ PAD _ LEFT ) ▁ . ▁ " " } } $ m = 10 ; $ n = 20 ; $ result_fac ; $ result_fac = find_factorial ( $ m , $ n ) ; print_format ( $ result_fac ) ; ? >


"Print first k digits of 1 / n where n is a positive integer | Function to print first k digits after dot in value of 1 / n . n is assumed to be a positive integer . ; Initialize remainder ; Run a loop k times to print k digits ; The next digit can always be obtained as doing ( 10 * rem ) / 10 ; Update remainder ; Driver Code"
Solution in php:


"Print first n Fibonacci Numbers using direct formula | Function to calculate fibonacci using recurrence relation formula ; Using direct formula ; Driver code"
Solution in php:


"Print first n numbers with exactly two set bits | Prints first n numbers with two set bits ; Initialize higher of two sets bits ; Keep reducing n for every number with two set bits . ; Consider all lower set bits for current higher set bit ; Print current number ; If we have found n numbers ; Consider next lower bit for current higher bit . ; Increment higher set bit ; Driver code"
Solution in php:

< ? php function printTwoSetBitNums ( $ n ) { $ x = 1 ; while ( $ n > 0 ) { $ y = 0 ; while ( $ y < $ x ) { echo ( 1 << $ x ) + ( 1 << $ y ) , " ▁ " ; $ n -- ; if ( $ n == 0 ) return ; $ y ++ ; } $ x ++ ; } } printTwoSetBitNums ( 4 ) ; ? >


"Print given sentence into its equivalent ASCII form | Function to compute the ASCII value of each character one by one ; Driver code"
Solution in php:

 < ? php function ASCIISentence ( $ str ) { for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) echo ord ( $ str [ $ i ] ) ; } $ str = " GeeksforGeeks " ; echo " ASCII ▁ Sentence : " . "
 " ; ASCIISentence ( $ str ) ; ? >


"Print k numbers where all pairs are divisible by m | function to generate k numbers whose difference is divisible by m ; Using an adjacency list like representation to store numbers that lead to same remainder . ; stores the modulus when divided by m ; If we found k elements which have same remainder . ; If we could not find k elements ; Driver Code"
Solution in php:


"Print last character of each word in a string | Function to print the last character of each word in the given string ; Now , last word is also followed by a space ; If current character is a space ; Then previous character must be the last character of some word ; Driver code"
Solution in php:

< ? php function printLastChar ( $ str ) { $ str = $ str . " ▁ " ; for ( $ i = 1 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( ! strcmp ( $ str [ $ i ] , ' ▁ ' ) ) echo ( $ str [ $ i - 1 ] . " ▁ " ) ; } } $ str = " Geeks ▁ for ▁ Geeks " ; printLastChar ( $ str ) ; ? >


"Print last k digits of a ^ b ( a raised to power b ) | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; $x = $x % $p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now $y = $y >> 1 ; y = y / 2 ; function to calculate number of digits in x ; function to print last k digits of a ^ b ; Generating 10 ^ k ; Calling modular exponentiation ; Printing leftmost zeros . Since ( a ^ b ) % k can have digits less then k . In that case we need to print zeros ; If temp is not zero then print temp If temp is zero then already printed ; Driver Code"
Solution in php:

< ? php function power ( $ x , $ y , $ p ) { $ res = 1 ; while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function numberOfDigits ( $ x ) { $ i = 0 ; while ( $ x ) { $ x = ( int ) $ x / 10 ; $ i ++ ; } return $ i ; } function printLastKDigits ( $ a , $ b , $ k ) { echo " Last ▁ " , $ k ; echo " ▁ digits ▁ of ▁ " , $ a ; echo " ^ " , $ b , " ▁ = ▁ " ; $ temp = 1 ; for ( $ i = 1 ; $ i <= $ k ; $ i ++ ) $ temp *= 10 ; $ temp = power ( $ a , $ b , $ temp ) ; for ( $ i = 0 ; $ i < $ k - numberOfDigits ( $ temp ) ; $ i ++ ) echo 0 ; if ( $ temp ) echo $ temp ; } $ a = 11 ; $ b = 3 ; $ k = 2 ; printLastKDigits ( $ a , $ b , $ k ) ; ? >


"Print left rotation of array in O ( n ) time and O ( 1 ) space | Function to leftRotate array multiple times ; To get the starting point of rotated array ; Prints the rotated array from start position ; Driver Code ; Function Call ; Function Call ; Function Call"
Solution in php:

 < ? php function leftRotate ( $ arr , $ n , $ k ) { $ mod = $ k % $ n ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo ( $ arr [ ( $ mod + $ i ) % $ n ] ) , " ▁ " ; echo "
 " ; } $ arr = array ( 1 , 3 , 5 , 7 , 9 ) ; $ n = sizeof ( $ arr ) ; $ k = 2 ; leftRotate ( $ arr , $ n , $ k ) ; $ k = 3 ; leftRotate ( $ arr , $ n , $ k ) ; $ k = 4 ; leftRotate ( $ arr , $ n , $ k ) ; ? >


"Print matrix after applying increment operations in M ranges | Function to update and print the matrix after performing queries ; Add 1 to the first element of the sub - matrix ; If there is an element after the last element of the sub - matrix then decrement it by 1 ; Calculate the running sum ; Print the updated element ; Next line ; Size of the matrix ; Queries"
Solution in php:


"Print matrix in diagonal pattern | php program to print matrix in diagonal order ; Initialize indexes of element to be printed next ; Direction is initially from down to up ; Traverse the matrix till all elements get traversed ; If isUp = true then traverse from downward to upward ; Set i and j according to direction ; If isUp = 0 then traverse up to down ; Set i and j according to direction ; Revert the isUp to change the direction ; Driver code"
Solution in php:


"Print n 0 s and m 1 s such that no two 0 s and no three 1 s are together | Function to print the required pattern ; When condition fails ; When m = n - 1 ; Driver code"
Solution in php:


"Print n numbers such that their sum is a perfect square | Function to prn numbers such that their sum is a perfect square ; Print ith odd number ; Driver code"
Solution in php:

< ? php function findNumbers ( $ n ) { $ i = 1 ; while ( $ i <= $ n ) { echo ( ( 2 * $ i ) - 1 ) . " ▁ " ; $ i ++ ; } } $ n = 3 ; findNumbers ( $ n ) ; ? >


"Print n terms of Newman | Function to find the n - th element ; Declare array to store sequence ; Driver Code"
Solution in php:


"Print n x n spiral matrix using O ( 1 ) extra space | Prints spiral matrix of size n x n containing numbers from 1 to n x n ; x stores the layer in which ( i , j ) th element lies ; Finds minimum of four inputs ; For upper right half ; for lower left half ; Driver code ; print a n x n spiral matrix in O ( 1 ) space"
Solution in php:


"Print numbers in descending order along with their frequencies | Function to print the elements in descending along with their frequencies ; Sorts the element in decreasing order ; traverse the array elements ; Prints the number and count ; Prints the last step ; Driver Code"
Solution in php:

 < ? php function printElements ( & $ a , $ n ) { rsort ( $ a ) ; $ cnt = 1 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ a [ $ i ] != $ a [ $ i + 1 ] ) { echo ( $ a [ $ i ] ) ; echo ( " ▁ occurs ▁ " ) ; echo $ cnt ; echo ( " ▁ times
 " ) ; $ cnt = 1 ; } else $ cnt += 1 ; } echo ( $ a [ $ n - 1 ] ) ; echo ( " ▁ occurs ▁ " ) ; echo $ cnt ; echo ( " ▁ times
 " ) ; } $ a = array ( 1 , 1 , 1 , 2 , 3 , 4 , 9 , 9 , 10 ) ; $ n = sizeof ( $ a ) ; printElements ( $ a , $ n ) ; ? >


"Print numbers in the range 1 to n having bits in alternate pattern | function to print numbers in the range 1 to n having bits in alternate pattern ; first number having bits in alternate pattern ; display ; loop until n < curr_num ; generate next number having alternate bit pattern ; if true then break ; display ; generate next number having alternate bit pattern ; if true then break ; display ; Driver code"
Solution in php:

< ? php function printNumHavingAltBitPatrn ( $ n ) { $ curr_num = 1 ; echo $ curr_num . " " ; while ( 1 ) { $ curr_num <<= 1 ; if ( $ n < $ curr_num ) break ; echo $ curr_num . " " ; $ curr_num = ( ( $ curr_num ) << 1 ) ^ 1 ; if ( $ n < $ curr_num ) break ; echo $ curr_num . " " ; } } $ n = 50 ; printNumHavingAltBitPatrn ( $ n ) ; ? >


"Print pair with maximum AND value in an array | Utility function to check number of elements having set msb as of pattern ; Function for finding maximum and value pair ; iterate over total of 30 bits from msb to lsb ; find the count of element having set msb ; if count >= 2 set particular bit in result ; Find the elements ; print the pair of elements ; inc count value after printing element ; return the result value ; Driver code"
Solution in php:

 < ? php function checkBit ( $ pattern , $ arr , $ n ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( ( $ pattern & $ arr [ $ i ] ) == $ pattern ) $ count ++ ; return $ count ; } function maxAND ( $ arr , $ n ) { $ res = 0 ; for ( $ bit = 31 ; $ bit >= 0 ; $ bit -- ) { $ count = checkBit ( $ res | ( 1 << $ bit ) , $ arr , $ n ) ; if ( $ count >= 2 ) $ res |= ( 1 << $ bit ) ; } if ( $ res == 0 ) echo " Not ▁ Possible
 " ; else { echo " Pair = " $ count = 0 ; for ( $ i = 0 ; $ i < $ n && $ count < 2 ; $ i ++ ) { if ( ( $ arr [ $ i ] & $ res ) == $ res ) { $ count ++ ; echo $ arr [ $ i ] . " " ; } } } return $ res ; } $ arr = array ( 4 , 8 , 6 , 2 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo " Maximum AND Value = " ? >


"Print prime numbers from 1 to N in reverse order | PHP program to print all primes between 1 to N in reverse order using Sieve of Eratosthenes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Print all prime numbers in reverse order ; static input ; to display ; Reverseorder ( $N ) ; calling the function"
Solution in php:

 < ? php function Reverseorder ( $ n ) { $ prime = array_fill ( 0 , $ n + 1 , true ) ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = false ; } } for ( $ p = $ n ; $ p >= 2 ; $ p -- ) if ( $ prime [ $ p ] ) echo $ p . " " ; } $ N = 25 ; echo " Prime ▁ number ▁ in ▁ reverse ▁ order
 " ; if ( $ N == 1 ) echo " No ▁ prime ▁ no ▁ exist ▁ in ▁ this ▁ range " ; else ? >


"Print prime numbers with prime sum of digits in an array | Function to store the primes ; Function to return the sum of digits ; Function to print additive primes ; If the number is prime ; Check if it 's digit sum is prime ; Driver code"
Solution in php:

< ? php function sieve ( $ maxEle , & $ prime ) { $ prime [ 0 ] = $ prime [ 1 ] = 1 ; for ( $ i = 2 ; $ i * $ i <= $ maxEle ; $ i ++ ) { if ( ! $ prime [ $ i ] ) { for ( $ j = 2 * $ i ; $ j <= $ maxEle ; $ j += $ i ) $ prime [ $ j ] = 1 ; } } } function digitSum ( $ n ) { $ sum = 0 ; while ( $ n ) { $ sum += $ n % 10 ; $ n = $ n / 10 ; } return $ sum ; } function printAdditivePrime ( $ arr , $ n ) { $ maxEle = max ( $ arr ) ; $ prime = array_fill ( 0 , $ maxEle + 1 , 0 ) ; sieve ( $ maxEle , $ prime ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ prime [ $ arr [ $ i ] ] == 0 ) { $ sum = digitSum ( $ arr [ $ i ] ) ; if ( $ prime [ $ sum ] == 0 ) print ( $ arr [ $ i ] . " ▁ " ) ; } } } $ a = array ( 2 , 4 , 6 , 11 , 12 , 18 , 7 ) ; $ n = count ( $ a ) ; printAdditivePrime ( $ a , $ n ) ; ? >


"Print steps to make a number in form of 2 ^ X | Function to find the leftmost unset bit in a number . ; Function that perform the step ; Find the leftmost unset bit ; If the number has no bit unset , it means it is in form 2 ^ x - 1 ; Count the steps ; Iterate till number is of form 2 ^ x - 1 ; At even step increase by 1 ; Odd step xor with any 2 ^ m - 1 ; Find the leftmost unset bit ; 2 ^ m - 1 ; Perform the step ; Increase the steps ; Driver code"
Solution in php:

 < ? php function find_leftmost_unsetbit ( $ n ) { $ ind = -1 ; $ i = 1 ; while ( $ n ) { if ( ! ( $ n & 1 ) ) $ ind = $ i ; $ i ++ ; $ n >>= 1 ; } return $ ind ; } function perform_steps ( $ n ) { $ left = find_leftmost_unsetbit ( $ n ) ; if ( $ left == -1 ) { echo " No ▁ steps ▁ required " ; return ; } $ step = 1 ; while ( find_leftmost_unsetbit ( $ n ) != -1 ) { if ( $ step % 2 == 0 ) { $ n += 1 ; echo " Step " , $ step , " : ▁ Increase ▁ by ▁ 1
 " ; } else { $ m = find_leftmost_unsetbit ( $ n ) ; $ num = pow ( 2 , $ m ) - 1 ; $ n = $ n ^ $ num ; echo " Step " , $ step , " : ▁ Xor ▁ with ▁ " , $ num , "
 " ; } $ step += 1 ; } } $ n = 39 ; perform_steps ( $ n ) ; ? >


"Print string of odd length in ' X ' format | Function to print given string in cross pattern ; i and j are the indexes of characters to be displayed in the ith iteration i = 0 initially and go upto length of string j = length of string initially in each iteration of i , we increment i and decrement j , we print character only of k == i or k == j ; Driver code"
Solution in php:

 < ? php function pattern ( $ str , $ len ) { for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { $ j = $ len - 1 - $ i ; for ( $ k = 0 ; $ k < $ len ; $ k ++ ) { if ( $ k == $ i $ k == $ j ) echo $ str [ $ k ] ; else echo " ▁ " ; } echo "
 " ; } } $ str = " geeksforgeeks " ; $ len = strlen ( $ str ) ; pattern ( $ str , $ len ) ; ? >


"Print the balanced bracket expression using given brackets | Function to print balanced bracket expression if it is possible ; If the condition is met ; Print brackets of type - 1 ; Print brackets of type - 3 ; Print brackets of type - 4 ; Print brackets of type - 2 ; If the condition is not met ; Driver code"
Solution in php:


"Print the kth common factor of two numbers | Returns k 'th common factor of x and y. ; Find smaller of two numbers ; Count common factors until we either reach small or count becomes k . ; If we reached small ; Driver code"
Solution in php:

< ? php function findKCF ( $ x , $ y , $ k ) { $ small = min ( $ x , $ y ) ; $ count = 1 ; for ( $ i = 2 ; $ i <= $ small ; $ i ++ ) { if ( $ x % $ i == 0 && $ y % $ i == 0 ) $ count ++ ; if ( $ count == $ k ) return $ i ; } return -1 ; } $ x = 4 ; $ y = 24 ; $ k = 3 ; echo findKCF ( $ x , $ y , $ k ) ; ? >


"Print triplets with sum less than k | A Simple PHP program to count triplets with sum smaller than a given value ; Fix the first element as A [ i ] ; Fix the second element as A [ j ] ; Now look for the third number ; Driver Code"
Solution in php:


"Print triplets with sum less than k | PHP program to print triplets with sum smaller than a given value ; Sort input array ; Every iteration of loop counts triplet with first element as arr [ i ] . ; Initialize other two elements as corner elements of subarray arr [ j + 1. . k ] ; Use Meet in the Middle concept ; If sum of current triplet is more or equal , move right corner to look for smaller values ; Else move left corner ; This is important . For current i and j , there are total k - j third elements . ; Driver Code"
Solution in php:


"Print uncommon elements from two sorted arrays | PHP program to find uncommon elements of two sorted arrays ; If not common , prsmaller ; Skip common element ; printing remaining elements ; Driver code"
Solution in php:


"Print values of ' a ' in equation ( a + b ) <= n and a + b is divisible by x | function to Find values of a , in equation ( a + b ) <= n and a + b is divisible by x . ; least possible which is divisible by x ; run a loop to get required answer ; increase value by x ; answer is possible ; Driver code ; function call"
Solution in php:

< ? php function PossibleValues ( $ b , $ x , $ n ) { $ leastdivisible = ( intval ( $ b / $ x ) + 1 ) * $ x ; $ flag = 1 ; while ( $ leastdivisible <= $ n ) { if ( $ leastdivisible - $ b >= 1 ) { echo $ leastdivisible - $ b . " " ; $ leastdivisible += $ x ; $ flag = 0 ; } else break ; } if ( $ flag ) echo " - 1" ; } $ b = 10 ; $ x = 6 ; $ n = 40 ; PossibleValues ( $ b , $ x , $ n ) ; ? >


"Printing Items in 0 / 1 Knapsack | Prints the items which are kept in a knapsack of capacity W ; Build table K [ ] [ ] in bottom up manner ; stores the result of Knapsack ; either the result comes from the top ( K [ i - 1 ] [ w ] ) or from ( val [ i - 1 ] + K [ i - 1 ] [ w - wt [ i - 1 ] ] ) as in Knapsack table . If it comes from the latter one / it means the item is included . ; This item is included . ; Since this weight is included its value is deducted ; Driver code"
Solution in php:


"Printing string in plus ‘ + ’ pattern in the matrix | Function to make a cross in the matrix ; As , it is not possible to make the cross exactly in the middle of the matrix with an even length string . ; declaring a 2D array i . e a matrix ; Now , we will fill all the elements of the array with ' X ' ; Now , we will place the characters of the string in the matrix , such that a cross is formed in it . ; here the characters of the string will be added in the middle column of our array . ; here the characters of the string will be added in the middle row of our array . ; Now finally , we will print the array ; Driver Code"
Solution in php:

 < ? php function carveCross ( $ str ) { $ n = strlen ( $ str ) ; if ( $ n % 2 == 0 ) { echo ( " Not ▁ possible . ▁ Please ▁ enter ▁ " ) ; echo ( " odd ▁ length ▁ string .
 " ) ; } else { $ arr = array ( ) ; $ m = $ n / 2 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ arr [ $ i ] [ $ j ] = ' X ' ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ arr [ $ i ] [ $ m ] = $ str [ $ i ] ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ arr [ $ m ] [ $ i ] = $ str [ $ i ] ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { echo ( $ arr [ $ i ] [ $ j ] . " " ) ; } echo ( "
 " ) ; } } } $ str = " PICTURE " ; carveCross ( $ str ) ; ? >


"Probability for three randomly chosen numbers to be in AP | function to calculate probability ; Driver code"
Solution in php:


"Probability of Knight to remain in the chessboard | size of the chessboard ; direction vector for the Knight ; returns true if the knight is inside the chessboard ; Bottom up approach for finding the probability to go out of chessboard . ; dp array ; for 0 number of steps , each position will have probability 1 ; for every number of steps s ; for every position ( x , y ) after s number of steps ; for every position reachable from ( x , y ) ; if this position lie inside the board ; store the result ; return the result ; number of steps ; Function Call"
Solution in php:


"Probability of a key K present in array | Function to find the probability ; find probability ; Driver Code"
Solution in php:

< ? php function kPresentProbability ( & $ a , $ n , $ k ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ a [ $ i ] == $ k ) $ count ++ ; return $ count / $ n ; } $ A = array ( 4 , 7 , 2 , 0 , 8 , 7 , 5 ) ; $ K = 2 ; $ N = sizeof ( $ A ) ; echo round ( kPresentProbability ( $ A , $ N , $ K ) , 2 ) ; ? >


"Probability of a random pair being the maximum weighted pair | Function to return probability ; Count occurrences of maximum element in A [ ] ; Count occurrences of maximum element in B [ ] ; Returning probability ; Driver code"
Solution in php:

< ? php function probability ( $ a , $ b , $ size1 , $ size2 ) { $ max1 = PHP_INT_MIN ; $ count1 = 0 ; for ( $ i = 0 ; $ i < $ size1 ; $ i ++ ) { if ( $ a [ $ i ] > $ max1 ) { $ max1 = $ a [ $ i ] ; $ count1 = 1 ; } else if ( $ a [ $ i ] == $ max1 ) { $ count1 ++ ; } } $ max2 = PHP_INT_MIN ; $ count2 = 0 ; for ( $ i = 0 ; $ i < $ size2 ; $ i ++ ) { if ( $ b [ $ i ] > $ max2 ) { $ max2 = $ b [ $ i ] ; $ count2 = 1 ; } else if ( $ b [ $ i ] == $ max2 ) { $ count2 ++ ; } } return ( double ) ( $ count1 * $ count2 ) / ( $ size1 * $ size2 ) ; } $ a = array ( 1 , 2 , 3 ) ; $ b = array ( 1 , 3 , 3 ) ; $ size1 = sizeof ( $ a ) ; $ size2 = sizeof ( $ b ) ; echo probability ( $ a , $ b , $ size1 , $ size2 ) ; ? >


"Probability of choosing a random pair with maximum sum in an array | Function to get max first and second ; If current element is smaller than first , then update both first and second ; If arr [ i ] is in between first and second then update second ; frequency of first maximum ; frequency of second maximum ; Returns probability of choosing a pair with maximum sum . ; Driver Code"
Solution in php:

< ? php function countMaxSumPairs ( $ a , $ n ) { $ first = PHP_INT_MIN ; $ second = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] > $ first ) { $ second = $ first ; $ first = $ a [ $ i ] ; } else if ( $ a [ $ i ] > $ second && $ a [ $ i ] != $ first ) $ second = $ a [ $ i ] ; } $ cnt1 = 0 ; $ cnt2 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] == $ first ) $ cnt1 ++ ; if ( $ a [ $ i ] == $ second ) $ cnt2 ++ ; } if ( $ cnt1 == 1 ) return $ cnt2 ; if ( $ cnt1 > 1 ) return $ cnt1 * ( $ cnt1 - 1 ) / 2 ; } function findMaxSumProbability ( $ a , $ n ) { $ total = $ n * ( $ n - 1 ) / 2 ; $ max_sum_pairs = countMaxSumPairs ( $ a , $ n ) ; return ( float ) $ max_sum_pairs / ( float ) $ total ; } $ a = array ( 1 , 2 , 2 , 3 ) ; $ n = sizeof ( $ a ) ; echo findMaxSumProbability ( $ a , $ n ) ; ? >


"Probability of getting at least K heads in N tosses of Coins | Dynamic and Logarithm approach find probability of at least k heads ; dp [ i ] is going to store Log ( i ! ) in base 2 ; Initialize result ; Iterate from k heads to n heads ; Preprocess all the logarithm value on base 2 ; Driver code ; Probability of getting 2 head out of 3 coins ; Probability of getting 3 head out of 6 coins ; Probability of getting 500 head out of 10000 coins"
Solution in php:

 < ? php $ MAX = 100001 ; $ dp = array_fill ( 0 , $ MAX , 0 ) ; function probability ( $ k , $ n ) { global $ MAX , $ dp ; $ ans = 0 ; for ( $ i = $ k ; $ i <= $ n ; ++ $ i ) { $ res = $ dp [ $ n ] - $ dp [ $ i ] - $ dp [ $ n - $ i ] - $ n ; $ ans += pow ( 2.0 , $ res ) ; } return $ ans ; } function precompute ( ) { global $ MAX , $ dp ; for ( $ i = 2 ; $ i < $ MAX ; ++ $ i ) precompute ( ) ; echo probability ( 2 , 3 ) . "
 " ; echo probability ( 3 , 6 ) . "
 " ; echo probability ( 500 , 1000 ) ; ? >


"Probability of getting at least K heads in N tosses of Coins | Naive approach in PHP to find probability of at least k heads ; Returns probability of getting at least k heads in n tosses . ; Probability of getting exactly i heads out of n heads ; Note : 1 << n = pow ( 2 , n ) ; Preprocess all factorial only upto 19 , as after that it will overflow ; Driver code ; Probability of getting 2 head out of 3 coins ; Probability of getting 3 head out of 6 coins ; Probability of getting 12 head out of 18 coins"
Solution in php:

 < ? php $ MAX = 21 ; $ fact = array_fill ( 0 , $ MAX , 0 ) ; function probability ( $ k , $ n ) { global $ fact ; $ ans = 0 ; for ( $ i = $ k ; $ i <= $ n ; ++ $ i ) $ ans += $ fact [ $ n ] / ( $ fact [ $ i ] * $ fact [ $ n - $ i ] ) ; $ ans = $ ans / ( 1 << $ n ) ; return $ ans ; } function precompute ( ) { global $ fact ; $ fact [ 0 ] = $ fact [ 1 ] = 1 ; for ( $ i = 2 ; $ i < 20 ; ++ $ i ) $ fact [ $ i ] = $ fact [ $ i - 1 ] * $ i ; } precompute ( ) ; echo number_format ( probability ( 2 , 3 ) , 6 ) . "
 " ; echo number_format ( probability ( 3 , 6 ) , 6 ) . "
 " ; echo number_format ( probability ( 12 , 18 ) , 6 ) ; ? >


"Probability of getting two consecutive heads after choosing a random coin among two different types of coins | Function to return the probability of getting two consecutive heads ; Formula derived from Bayes 's theorem ; given the probability of getting a head for both the coins"
Solution in php:

< ? php function getProbability ( $ p , $ q ) { $ p /= 100 ; $ q /= 100 ; $ probability = ( $ p * $ p + $ q * $ q ) / ( $ p + $ q ) ; return $ probability ; } $ p = 80 ; $ q = 40 ; echo getProbability ( $ p , $ q ) ; ? >


"Probability of reaching a point with 2 or 3 steps at a time | Returns probability to reach N ; Driver code"
Solution in php:


"Problem of 8 Neighbours of an element in a 2 | Dimension of Array ; Count of 1 s ; Counting all neighbouring 1 s ; Comparing the number of neighbouring 1 s with given ranges ; Copying changes to the main matrix ; Driver code ; Function call to calculate the resultant matrix after ' K ' iterations . ; Printing Result"
Solution in php:


"Problems not solved at the end of Nth day | Function to find problems not solved at the end of Nth day ; Driver Code"
Solution in php:

< ? php function problemsLeft ( $ K , $ P , $ N ) { if ( $ K <= $ P ) return 0 ; else return ( $ K - $ P ) * $ N ; } $ K = 4 ; $ P = 1 ; $ N = 10 ; echo problemsLeft ( $ K , $ P , $ N ) ; ? >


"Product of N with its largest odd digit | Function to return the largest odd digit in n ; If all digits are even then - 1 will be returned ; Last digit from n ; If current digit is odd and > maxOdd ; Remove last digit ; Return the maximum odd digit ; Function to return the product of n with its largest odd digit ; If there are no odd digits in n ; Product of n with its largest odd digit ; Driver code"
Solution in php:

< ? php function largestOddDigit ( $ n ) { $ maxOdd = -1 ; while ( $ n > 0 ) { $ digit = $ n % 10 ; if ( $ digit % 2 == 1 && $ digit > $ maxOdd ) $ maxOdd = $ digit ; $ n = $ n / 10 ; } return $ maxOdd ; } function getProduct ( $ n ) { $ maxOdd = largestOddDigit ( $ n ) ; if ( $ maxOdd == -1 ) return -1 ; return ( $ n * $ maxOdd ) ; } $ n = 12345 ; echo getProduct ( $ n ) ; ? >


"Product of all the Composite Numbers in an array | Function that returns the the product of all composite numbers ; Find maximum value in the array ; Use sieve to find all prime numbers less than or equal to max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Set 0 and 1 as primes as they don 't need to be  counted as composite numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Find the product of all composite numbers in the arr [ ] ; Driver code"
Solution in php:

< ? php function compositeProduct ( $ arr , $ n ) { $ max_val = max ( $ arr ) ; $ prime = array_fill ( 0 , $ max_val + 1 , true ) ; $ prime [ 0 ] = true ; $ prime [ 1 ] = true ; for ( $ p = 2 ; $ p * $ p <= $ max_val ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ max_val ; $ i += $ p ) $ prime [ $ i ] = false ; } } $ product = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( ! $ prime [ $ arr [ $ i ] ] ) { $ product *= $ arr [ $ i ] ; } return $ product ; } $ arr = array ( 2 , 3 , 4 , 5 , 6 , 7 ) ; $ n = count ( $ arr ) ; echo compositeProduct ( $ arr , $ n ) ; ? >


"Product of factors of number | PHP program to calculate product of factors of number ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; function to count the factors ; If factors are equal , count only once ; Otherwise count both ; Calculate product of factors ; If numFactor is odd return power ( n , numFactor / 2 ) * sqrt ( n ) ; Driver code"
Solution in php:

< ? php $ M = 1000000007 ; function power ( $ x , $ y ) { global $ M ; $ res = 1 ; while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ M ; $ y = ( $ y >> 1 ) % $ M ; $ x = ( $ x * $ x ) % $ M ; } return $ res ; } function countFactors ( $ n ) { $ count = 0 ; for ( $ i = 1 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) $ count ++ ; else $ count += 2 ; } } return $ count ; } function multiplyFactors ( $ n ) { $ numFactor = countFactors ( $ n ) ; $ product = power ( $ n , $ numFactor / 2 ) ; if ( $ numFactor & 1 ) $ product = ( $ product * sqrt ( $ n ) ) % $ M ; return $ product ; } $ n = 12 ; echo multiplyFactors ( $ n ) ; ? >


"Product of factors of number | PHP program to calculate product of factors of number ; function to product the factors ; If factors are equal , multiply only once ; Otherwise multiply both ; Driver Code"
Solution in php:

< ? php $ M = 1000000007 ; function multiplyFactors ( $ n ) { global $ M ; $ prod = 1 ; for ( $ i = 1 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) $ prod = ( $ prod * $ i ) % $ M ; else { $ prod = ( $ prod * $ i ) % $ M ; $ prod = ( $ prod * $ n / $ i ) % $ M ; } } } return $ prod ; } $ n = 12 ; echo multiplyFactors ( $ 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"
Solution in php:

 < ? 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 ) , "
 " ; $ N = 5 ; echo findProduct ( $ N ) , "
 " ; ? >


"Product of given N fractions in reduced form | Function to return gcd of a and b ; Print the Product of N fraction in Reduced Form . ; finding the product of all N numerators and denominators . ; Finding GCD of new numerator and denominator ; Converting into reduced form . ; Driver Code"
Solution in php:

 < ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function productReduce ( $ n , $ num , $ den ) { $ new_num = 1 ; $ new_den = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ new_num *= $ num [ $ i ] ; $ new_den *= $ den [ $ i ] ; } $ GCD = gcd ( $ new_num , $ new_den ) ; $ new_num /= $ GCD ; $ new_den /= $ GCD ; echo $ new_num , " / " , $ new_den , "
 " ; } $ n = 3 ; $ num = array ( 1 , 2 , 5 ) ; $ den = array ( 2 , 1 , 6 ) ; productReduce ( $ n , $ num , $ den ) ; ? >


"Product of maximum in first array and minimum in second | Function to calculate the product ; Initialize max of first array ; initialize min of second array ; To find the maximum element in first array ; To find the minimum element in second array ; Process remaining elements ; Driven code"
Solution in php:


"Product of maximum in first array and minimum in second | Function to calculate the product ; Sort the arrays to find the maximum and minimum elements in given arrays ; Return product of maximum and minimum . ; Driver code"
Solution in php:

< ? php function minMaxProduct ( $ arr1 , $ arr2 , $ n1 , $ n2 ) { sort ( $ arr1 ) ; sort ( $ arr2 ) ; return $ arr1 [ $ n1 - 1 ] * $ arr2 [ 0 ] ; } $ arr1 = array ( 10 , 2 , 3 , 6 , 4 , 1 ) ; $ arr2 = array ( 5 , 1 , 4 , 2 , 6 , 9 ) ; $ n1 = count ( $ arr1 ) ; $ n2 = count ( $ arr2 ) ; echo minMaxProduct ( $ arr1 , $ arr2 , $ n1 , $ n2 ) ; ? >


"Product of unique prime factors of a number | A function to print all prime factors of a given number n ; Handle prime factor 2 explicitly so that can optimally handle other prime factors . ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Driver Code"
Solution in php:

< ? php function productPrimeFactors ( $ n ) { $ product = 1 ; if ( $ n % 2 == 0 ) { $ product *= 2 ; while ( $ n % 2 == 0 ) $ n = $ n / 2 ; } for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i = $ i + 2 ) { if ( $ n % $ i == 0 ) { $ product = $ product * $ i ; while ( $ n % $ i == 0 ) $ n = $ n / $ i ; } } if ( $ n > 2 ) $ product = $ product * $ n ; return $ product ; } $ n = 44 ; echo productPrimeFactors ( $ n ) ; ? >


"Products of ranges in an array | Function to calculate Product in the given range . ; As our array is 0 based as and L and R are given as 1 based index . ; Driver code"
Solution in php:

< ? php function calculateProduct ( $ A , $ L , $ R , $ P ) { $ L = $ L - 1 ; $ R = $ R - 1 ; $ ans = 1 ; for ( $ i = $ L ; $ i <= $ R ; $ i ++ ) { $ ans = $ ans * $ A [ $ i ] ; $ ans = $ ans % $ P ; } return $ ans ; } $ A = array ( 1 , 2 , 3 , 4 , 5 , 6 ) ; $ P = 229 ; $ L = 2 ; $ R = 5 ; echo calculateProduct ( $ A , $ L , $ R , $ P ) , " " ; $ L = 1 ; $ R = 3 ; echo calculateProduct ( $ A , $ L , $ R , $ P ) , " " ; ? >


"Program for Area And Perimeter Of Rectangle | Utility function ; Driver program"
Solution in php:

 < ? php function areaRectangle ( $ a , $ b ) { $ area = $ a * $ b ; return $ area ; } function perimeterRectangle ( $ a , $ b ) { $ perimeter = 2 * ( $ a + $ b ) ; return $ perimeter ; } $ a = 5 ; $ b = 6 ; echo ( " Area ▁ = ▁ " ) ; echo ( areaRectangle ( $ a , $ b ) ) ; echo ( "
 " ) ; echo ( " Perimeter ▁ = ▁ " ) ; echo ( perimeterRectangle ( $ a , $ b ) ) ; ? >


"Program for Area Of Square after N | Function to calculate area of square after given number of folds ; Driver Code"
Solution in php:

< ? php function areaSquare ( $ side , $ fold ) { $ area = $ side * $ side ; return $ area * 1.0 / pow ( 2 , $ fold ) ; } $ side = 4 ; $ fold = 2 ; echo areaSquare ( $ side , $ fold ) ; ? >


"Program for Area Of Square | PHP program to find the aria of the square ; Driver Code"
Solution in php:

< ? php function areaSquare ( $ side ) { $ area = $ side * $ side ; return $ area ; } $ side = 4 ; echo ( areaSquare ( $ side ) ) ; ? >


"Program for Binary To Decimal Conversion | Function to convert binary to decimal ; Initializing base value to 1 , i . e 2 ^ 0 ; Driver Code"
Solution in php:

 < ? php function binaryToDecimal ( $ n ) { $ num = $ n ; $ dec_value = 0 ; $ base = 1 ; $ temp = $ num ; while ( $ temp ) { $ last_digit = $ temp % 10 ; $ temp = $ temp / 10 ; $ dec_value += $ last_digit * $ base ; $ base = $ base * 2 ; } return $ dec_value ; } $ num = 10101001 ; echo binaryToDecimal ( $ num ) , "
 " ; ? >


"Program for Binomial Coefficients table | Function to print binomial table ; B ( m , x ) is 1 if either m or x is 0. ; Otherwise using recursive formula B ( m , x ) = B ( m , x - 1 ) * ( m - x + 1 ) / x ; Driver Code"
Solution in php:

 < ? php function printbinomial ( $ max ) { for ( $ m = 0 ; $ m <= $ max ; $ m ++ ) { echo $ m ; $ binom = 1 ; for ( $ x = 0 ; $ x <= $ m ; $ x ++ ) { if ( $ m != 0 && $ x != 0 ) $ binom = $ binom * ( $ m - $ x + 1 ) / $ x ; echo " ▁ " , $ binom , " ▁ " ; } echo "
 " ; } } $ max = 10 ; printbinomial ( $ max ) ; ? >


"Program for Celsius To Fahrenheit conversion | function to convert Celsius scale to Fahrenheit scale ; Driver Code"
Solution in php:


"Program for Centered Icosahedral Number | Function to find Centered icosahedral number ; Formula to calculate nth Centered icosahedral number and return it into main function . ; Driver Code"
Solution in php:

 < ? php function centeredIcosahedralNum ( $ n ) { return ( 2 * $ n + 1 ) * ( 5 * $ n * $ n + 5 * $ n + 3 ) / 3 ; } $ n = 10 ; echo centeredIcosahedralNum ( $ n ) , "
 " ; $ n = 12 ; echo centeredIcosahedralNum ( $ n ) , "
 " ; ? >


"Program for Chocolate and Wrapper Puzzle | Returns maximum number of chocolates we can eat with given money , price of chocolate and number of wrapprices required to get a chocolate . ; Corner case ; First find number of chocolates that can be purchased with the given amount ; Now just add number of chocolates with the chocolates gained by wrapprices ; total money ; cost of each candy ; no of wrappers needs to be ; exchanged for one chocolate ."
Solution in php:

< ? php function countMaxChoco ( $ money , $ price , $ wrap ) { if ( $ money < $ price ) return 0 ; $ choc = $ money / $ price ; $ choc = $ choc + ( $ choc - 1 ) / ( $ wrap - 1 ) ; return $ choc ; } $ money = 15 ; $ price = 1 ; $ wrap = 3 ; echo countMaxChoco ( $ money , $ price , $ wrap ) ; ? >


"Program for Decimal to Binary Conversion | function to convert decimal to binary ; array to store binary number ; counter for binary array ; storing remainder in binary array ; printing binary array in reverse order ; Driver Code"
Solution in php:

< ? php function decToBinary ( $ n ) { $ binaryNum ; $ i = 0 ; while ( $ n > 0 ) { $ binaryNum [ $ i ] = $ n % 2 ; $ n = ( int ) ( $ n / 2 ) ; $ i ++ ; } for ( $ j = $ i - 1 ; $ j >= 0 ; $ j -- ) echo $ binaryNum [ $ j ] ; } $ n = 17 ; decToBinary ( $ n ) ; ? >


"Program for Identity Matrix | PHP program to check if a given matrix is identity $MAX = 100 ; ; Driver Code"
Solution in php:

< ? php function isIdentity ( $ mat , $ N ) { for ( $ row = 0 ; $ row < $ N ; $ row ++ ) { for ( $ col = 0 ; $ col < $ N ; $ col ++ ) { if ( $ row == $ col and $ mat [ $ row ] [ $ col ] != 1 ) return false ; else if ( $ row != $ col && $ mat [ $ row ] [ $ col ] != 0 ) return false ; } } return true ; } $ N = 4 ; $ mat = array ( array ( 1 , 0 , 0 , 0 ) , array ( 0 , 1 , 0 , 0 ) , array ( 0 , 0 , 1 , 0 ) , array ( 0 , 0 , 0 , 1 ) ) ; if ( isIdentity ( $ mat , $ N ) ) echo " Yes ▁ " ; else echo " No ▁ " ; ? >


"Program for Identity Matrix | PHP program to print Identity Matrix ; Checking if row is equal to column ; Driver Code"
Solution in php:

 < ? php function Identity ( $ num ) { $ row ; $ col ; for ( $ row = 0 ; $ row < $ num ; $ row ++ ) { for ( $ col = 0 ; $ col < $ num ; $ col ++ ) { if ( $ row == $ col ) echo 1 , " ▁ " ; else echo 0 , " ▁ " ; } echo "
 " ; } return 0 ; } $ size = 5 ; identity ( $ size ) ; ? >


"Program for Markov matrix | PHP code to check Markov Matrix ; outer loop to access rows and inner to access columns ; Find sum of current row ; Matrix to check ; calls the function check ( )"
Solution in php:

< ? php function checkMarkov ( $ m ) { $ n = 3 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ sum = $ sum + $ m [ $ i ] [ $ j ] ; if ( $ sum != 1 ) return false ; } return true ; } $ m = array ( array ( 0 , 0 , 1 ) , array ( 0.5 , 0 , 0.5 ) , array ( 1 , 0 , 0 ) ) ; if ( checkMarkov ( $ m ) ) echo " ▁ yes ▁ " ; else echo " ▁ no ▁ " ; ? >


"Program for Mean Absolute Deviation | Function to find mean of the array elements . ; Calculate sum of all elements . ; Function to find mean absolute deviation of given elements . ; Calculate the sum of absolute deviation about mean . ; Return mean absolute deviation about mean . ; Driver Code"
Solution in php:

< ? php function Mean ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum = $ sum + $ arr [ $ i ] ; return $ sum / $ n ; } function meanAbsoluteDeviation ( $ arr , $ n ) { $ absSum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ absSum = $ absSum + abs ( $ arr [ $ i ] - Mean ( $ arr , $ n ) ) ; return $ absSum / $ n ; } $ arr = array ( 10 , 15 , 15 , 17 , 18 , 21 ) ; $ n = sizeof ( $ arr ) ; echo meanAbsoluteDeviation ( $ arr , $ n ) ; ? >


"Program for Mean and median of an unsorted array | Function for calculating mean ; Function for calculating median ; First we sort the array ; check for even case ; Driver Code ; Function call"
Solution in php:

 < ? php function findMean ( & $ a , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ a [ $ i ] ; return ( double ) $ sum / ( double ) $ n ; } function findMedian ( & $ a , $ n ) { sort ( $ a ) ; if ( $ n % 2 != 0 ) return ( double ) $ a [ $ n / 2 ] ; return ( double ) ( $ a [ ( $ n - 1 ) / 2 ] + $ a [ $ n / 2 ] ) / 2.0 ; } $ a = array ( 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 ) ; $ n = sizeof ( $ a ) ; echo " Mean ▁ = ▁ " . findMean ( $ a , $ n ) . "
 " ; echo " Median ▁ = ▁ " . findMedian ( $ a , $ n ) ; ? >


"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"
Solution in php:

 < ? 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 ) , "
 " ; echo " Mobius ▁ Functions ▁ M ( N ) ▁ at ▁ N ▁ = ▁ " , 25 , " ▁ is : ▁ " , mobius ( 25 ) , "
 " ; echo " Mobius ▁ Functions ▁ M ( N ) ▁ at ▁ N ▁ = ▁ " , 6 , " ▁ is : ▁ " , mobius ( 6 ) ; ? >


"Program for Muller Method | PHP Program to find root of a function , f ( x ) ; Function to calculate f ( x ) ; Taking f ( x ) = x ^ 3 + 2 x ^ 2 + 10 x - 20 ; Calculating various constants required to calculate x3 ; Taking the root which is closer to x2 ; checking for resemblance of x3 with x2 till two decimal places ; Driver main function"
Solution in php:


"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"
Solution in php:

< ? 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 ) ) ; ? >


"Program for Newton Raphson Method | PHP program for implementation of Newton Raphson Method for solving equations ; An example function whose solution is determined using Bisection Method . The function is x ^ 3 - x ^ 2 + 2 ; Derivative of the above function which is 3 * x ^ x - 2 * x ; Function to find the root ; x ( i + 1 ) = x ( i ) - f ( x ) / f '(x) ; Initial values assumed"
Solution in php:

< ? php $ EPSILON = 0.001 ; function func ( $ x ) { return $ x * $ x * $ x - $ x * $ x + 2 ; } function derivFunc ( $ x ) { return 3 * $ x * $ x - 2 * $ x ; } function newtonRaphson ( $ x ) { global $ EPSILON ; $ h = func ( $ x ) / derivFunc ( $ x ) ; while ( abs ( $ h ) >= $ EPSILON ) { $ h = func ( $ x ) / derivFunc ( $ x ) ; $ x = $ x - $ h ; } echo " The ▁ value ▁ of ▁ the ▁ " . " root ▁ is ▁ : ▁ " , $ x ; } $ x0 = -20 ; newtonRaphson ( $ x0 ) ; ? >


"Program for Next Fit algorithm in Memory Management | Function to allocate memory to blocks as per Next fit algorithm ; Stores block id of the block allocated to a process ; pick each process and find suitable blocks according to its size ad assign to it ; Do not start from beginning ; allocate block j to p [ i ] process ; Reduce available memory in this block . ; mod m will help in traversing the blocks from starting block after we reach the end . ; Driver program"
Solution in php:

 < ? php function NextFit ( $ blockSize , $ m , $ processSize , $ n ) { $ allocation = array_fill ( 0 , $ n , -1 ) ; $ j = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { while ( $ j < $ m ) { if ( $ blockSize [ $ j ] >= $ processSize [ $ i ] ) { $ allocation [ $ i ] = $ j ; $ blockSize [ $ j ] -= $ processSize [ $ i ] ; break ; } $ j = ( $ j + 1 ) % $ m ; } } echo " Process No . Process Size Block no . " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { echo " " . ( $ i ▁ + ▁ 1 ) . " " . $ processSize [ $ i ] . " " if ( $ allocation [ $ i ] != -1 ) echo ( $ allocation [ $ i ] + 1 ) ; else echo " Not ▁ Allocated " ; echo "
 " ; } } $ blockSize = array ( 5 , 10 , 20 ) ; $ processSize = array ( 10 , 20 , 5 ) ; $ m = count ( $ blockSize ) ; $ n = count ( $ processSize ) ; NextFit ( $ blockSize , $ m , $ processSize , $ n ) ; ? >


"Program for Perrin numbers | Optimized PHP program for n 'th perrin number ; Driver code"
Solution in php:


"Program for Rank of Matrix | PHP program to find rank of a matrix ; function for exchanging two rows of a matrix ; function for finding rank of matrix ; Before we visit current row ' row ' , we make sure that mat [ row ] [ 0 ] , ... . mat [ row ] [ row - 1 ] are 0. Diagonal element is not zero ; This makes all entries of current column as 0 except entry ' mat [ row ] [ row ] ' ; Diagonal element is already zero . Two cases arise : 1 ) If there is a row below it with non - zero entry , then swap this row with that row and process that row 2 ) If all elements in current column below mat [ r ] [ row ] are 0 , then remvoe this column by swapping it with last column and reducing number of columns by 1. ; Find the non - zero element in current column ; Swap the row with non - zero element with this row . ; If we did not find any row with non - zero element in current columnm , then all values in this column are 0. ; Reduce number of columns ; Copy the last column here ; Process this row again ; Uncomment these lines to see intermediate results display ( mat , R , C ) ; printf ( " \n " ) ; ; function for displaying the matrix ; Driver code"
Solution in php:

 < ? php $ R = 3 ; $ C = 3 ; function swap ( & $ mat , $ row1 , $ row2 , $ col ) { for ( $ i = 0 ; $ i < $ col ; $ i ++ ) { $ temp = $ mat [ $ row1 ] [ $ i ] ; $ mat [ $ row1 ] [ $ i ] = $ mat [ $ row2 ] [ $ i ] ; $ mat [ $ row2 ] [ $ i ] = $ temp ; } } function rankOfMatrix ( $ mat ) { global $ R , $ C ; $ rank = $ C ; for ( $ row = 0 ; $ row < $ rank ; $ row ++ ) { if ( $ mat [ $ row ] [ $ row ] ) { for ( $ col = 0 ; $ col < $ R ; $ col ++ ) { if ( $ col != $ row ) { $ mult = $ mat [ $ col ] [ $ row ] / $ mat [ $ row ] [ $ row ] ; for ( $ i = 0 ; $ i < $ rank ; $ i ++ ) $ mat [ $ col ] [ $ i ] -= $ mult * $ mat [ $ row ] [ $ i ] ; } } } else { $ reduce = true ; for ( $ i = $ row + 1 ; $ i < $ R ; $ i ++ ) { if ( $ mat [ $ i ] [ $ row ] ) { swap ( $ mat , $ row , $ i , $ rank ) ; $ reduce = false ; break ; } } if ( $ reduce ) { $ rank -- ; for ( $ i = 0 ; $ i < $ R ; $ i ++ ) $ mat [ $ i ] [ $ row ] = $ mat [ $ i ] [ $ rank ] ; } $ row -- ; } } return $ rank ; } function display ( $ mat , $ row , $ col ) { for ( $ i = 0 ; $ i < $ row ; $ i ++ ) { for ( $ j = 0 ; $ j < $ col ; $ j ++ ) print ( " ▁ $ mat [ $ i ] [ $ j ] " ) ; print ( "
 " ) ; } } $ mat = array ( array ( 10 , 20 , 10 ) , array ( -20 , -30 , 10 ) , array ( 30 , 50 , 0 ) ) ; print ( " Rank ▁ of ▁ the ▁ matrix ▁ is ▁ : ▁ " . rankOfMatrix ( $ mat ) ) ; ? >


"Program for Surface Area of Octahedron | utility Function ; Driver Code"
Solution in php:

< ? php function surface_area_octahedron ( $ side ) { return ( 2 * ( sqrt ( 3 ) ) * ( $ side * $ side ) ) ; } $ side = 7 ; echo ( " Surface ▁ area ▁ of ▁ octahedron ▁ = " ) ; echo ( surface_area_octahedron ( $ side ) ) ; ? >


"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"
Solution in php:

 < ? 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 ▁
 " ; 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 "

 " ; } $ numdisks = 4 ; initPoles ( $ numdisks ) ; echo " Tower ▁ of ▁ Hanoi ▁ Solution ▁ for ▁ $ numdisks ▁ disks : ▁

 " ; dispPoles ( ) ; TOH ( $ numdisks ) ; ? >


"Program for Volume and Surface Area of Cube | utility function ; driver function"
Solution in php:

 < ? php function areaCube ( $ a ) { return ( $ a * $ a * $ a ) ; } function surfaceCube ( $ a ) { return ( 6 * $ a * $ a ) ; } $ a = 5 ; echo ( " Area ▁ = ▁ " ) ; echo ( areaCube ( $ a ) ) ; echo ( "
 " ) ; echo ( " Total ▁ surface ▁ area ▁ = ▁ " ) ; echo ( surfaceCube ( $ a ) ) ; ? >


"Program for Volume and Surface Area of Cuboid | utility function ; Driver Code"
Solution in php:

 < ? php function areaCuboid ( $ l , $ h , $ w ) { return ( $ l * $ h * $ w ) ; } function surfaceAreaCuboid ( $ l , $ h , $ w ) { return ( 2 * $ l * $ w + 2 * $ w * $ h + 2 * $ l * $ h ) ; } $ l = 1 ; $ h = 5 ; $ w = 7 ; echo " Area ▁ = ▁ " , areaCuboid ( $ l , $ h , $ w ) , "
 " ; echo " Total ▁ Surface ▁ Area ▁ = ▁ " , surfaceAreaCuboid ( $ l , $ h , $ w ) ; ? >


"Program for Volume and Surface area of Frustum of Cone | Function to calculate Volume of frustum of cone ; Function to calculate Curved Surface area of frustum of cone ; Function to calculate Total Surface area of frustum of cone ; Driver Code ; Printing value of volume and surface area"
Solution in php:

 < ? php function volume ( $ r , $ R , $ h ) { $ pi = 3.14159 ; return ( 1 / ( 3 ) ) * $ pi * $ h * ( $ r * $ r + $ R * $ R + $ r * $ R ) ; } function curved_surface_area ( $ r , $ R , $ l ) { $ pi = 3.14159 ; return $ pi * $ l * ( $ R + $ r ) ; } function total_surface_area ( $ r , $ R , $ l , $ h ) { $ pi = 3.14159 ; return ( $ pi * $ l * ( $ R + $ r ) + $ pi * ( $ r * $ r + $ R * $ R ) ) ; } $ small_radius = 3 ; $ big_radius = 8 ; $ slant_height = 13 ; $ height = 12 ; echo ( " Volume ▁ Of ▁ Frustum ▁ of ▁ Cone ▁ : ▁ " ) ; echo ( volume ( $ small_radius , $ big_radius , $ height ) ) ; echo ( "
 " ) ; echo ( " Curved ▁ Surface ▁ Area ▁ Of ▁ Frustum ▁ of ▁ Cone ▁ : ▁ " ) ; echo ( curved_surface_area ( $ small_radius , $ big_radius , $ slant_height ) ) ; echo ( "
 " ) ; echo ( " Total ▁ Surface ▁ Area ▁ Of ▁ Frustum ▁ of ▁ Cone ▁ : ▁ " ) ; echo ( total_surface_area ( $ small_radius , $ big_radius , $ slant_height , $ height ) ) ; ? >


"Program for centered nonagonal number | Function to find nth centered nonagonal number . ; Formula to find nth centered nonagonal number . ; Driver Code"
Solution in php:

< ? php function centeredNonagonal ( $ n ) { return ( 3 * $ n - 2 ) * ( 3 * $ n - 1 ) / 2 ; } $ n = 10 ; echo centeredNonagonal ( $ n ) ; ? >


"Program for cube sum of first n natural numbers | Returns sum of first n natural numbers ; Driver code"
Solution in php:

< ? php function sumOfSeries ( $ n ) { $ x ; if ( $ n % 2 == 0 ) $ x = ( $ n / 2 ) * ( $ n + 1 ) ; else $ x = ( ( $ n + 1 ) / 2 ) * $ n ; return $ x * $ x ; } $ n = 5 ; echo sumOfSeries ( $ n ) ; ? >


"Program for decimal to hexadecimal conversion | function to convert decimal to hexadecimal ; char array to store hexadecimal number ; counter for hexadecimal number array ; temporary variable to store remainder ; storing remainder in temp variable . ; check if temp < 10 ; printing hexadecimal number array in reverse order ; Driver Code"
Solution in php:

< ? php function decToHexa ( $ n ) { $ hexaDeciNum ; $ i = 0 ; while ( $ n != 0 ) { $ temp = 0 ; $ temp = $ n % 16 ; if ( $ temp < 10 ) { $ hexaDeciNum [ $ i ] = chr ( $ temp + 48 ) ; $ i ++ ; } else { $ hexaDeciNum [ $ i ] = chr ( $ temp + 55 ) ; $ i ++ ; } $ n = ( int ) ( $ n / 16 ) ; } for ( $ j = $ i - 1 ; $ j >= 0 ; $ j -- ) echo $ hexaDeciNum [ $ j ] ; } $ n = 2545 ; decToHexa ( $ n ) ; ? >


"Program for dot product and cross product of two vectors | PHP implementation for dot product and cross product of two vector . ; Function that return dot product of two vector array . ; Loop for calculate cot product ; Function to find cross product of two vector array . ; Driver Code ; dotproduct function call ; crossproduct function call ; Loop that print cross product of two vector array ."
Solution in php:

< ? php $ n = 3 ; function dotproduct ( $ vect_A , $ vect_B ) { global $ n ; $ product = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ product = $ product + $ vect_A [ $ i ] * $ vect_B [ $ i ] ; return $ product ; } function crossproduct ( $ vect_A , $ vect_B , $ cross_P ) { $ cross_P [ 0 ] = $ vect_A [ 1 ] * $ vect_B [ 2 ] - $ vect_A [ 2 ] * $ vect_B [ 1 ] ; $ cross_P [ 1 ] = $ vect_A [ 2 ] * $ vect_B [ 0 ] - $ vect_A [ 0 ] * $ vect_B [ 2 ] ; $ cross_P [ 2 ] = $ vect_A [ 0 ] * $ vect_B [ 1 ] - $ vect_A [ 1 ] * $ vect_B [ 0 ] ; return $ cross_P ; } $ vect_A = array ( 3 , -5 , 4 ) ; $ vect_B = array ( 2 , 6 , 5 ) ; $ cross_P = array_fill ( 0 , $ n , 0 ) ; echo " Dot ▁ product : " ; echo dotproduct ( $ vect_A , $ vect_B ) ; echo " Cross product : " $ cross_P = crossproduct ( $ vect_A , $ vect_B , $ cross_P ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ cross_P [ $ i ] . " ▁ " ; ? >


"Program for focal length of a lens | Function to determine the focal length of a lens ; variable to store the distance between the lens and the image ; variable to store the distance between the lens and the object"
Solution in php:

< ? php function focal_length ( $ image_distance , $ object_distance ) { return 1 / ( ( 1 / $ image_distance ) + ( 1 / $ object_distance ) ) ; } $ image_distance = 2 ; $ object_distance = 50 ; echo " Focal ▁ length ▁ of ▁ a ▁ lens ▁ is ▁ " , focal_length ( $ image_distance , $ object_distance ) , " ▁ units ▁ . " ; ? >


"Program for harmonic mean of numbers | Function that returns harmonic mean . ; Declare sum variables and initialize with zero ; Driver code"
Solution in php:

< ? php function harmonicMean ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum = $ sum + ( float ) ( 1 / $ arr [ $ i ] ) ; return ( float ) ( $ n / $ sum ) ; } $ arr = array ( 13.5 , 14.5 , 14.8 , 15.2 , 16.1 ) ; $ n = sizeof ( $ arr ) ; echo ( harmonicMean ( $ arr , $ n ) ) ; ? >


"Program for harmonic mean of numbers | Function that returns harmonic mean . ; Driver code"
Solution in php:

< ? php function harmonicMean ( $ arr , $ freq , $ n ) { $ sum = 0 ; $ frequency_sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum = $ sum + ( float ) ( $ freq [ $ i ] / $ arr [ $ i ] ) ; $ frequency_sum = $ frequency_sum + $ freq [ $ i ] ; } return ( $ frequency_sum / $ sum ) ; } $ num = array ( 13 , 14 , 15 , 16 , 17 ) ; $ freq = array ( 2 , 5 , 13 , 7 , 3 ) ; $ n = sizeof ( $ num ) ; echo ( harmonicMean ( $ num , $ freq , $ n ) ) ; ? >


"Program for length of a string using recursion | Function to calculate length ; if we reach at the end of the string ; Driver Code"
Solution in php:

< ? php function recLen ( & $ str , $ i ) { if ( $ i == strlen ( $ str ) ) return 0 ; else return 1 + recLen ( $ str , $ i + 1 ) ; } $ str = " GeeksforGeeks " ; echo ( recLen ( $ str , 0 ) ) ; ? >


"Program for n | Function to find the Nth odd number ; Driver code"
Solution in php:

< ? php function nthOdd ( $ n ) { return ( 2 * $ n - 1 ) ; } $ n = 10 ; echo nthOdd ( $ n ) ; ? >


"Program for n | Function to find the nth even number ; Driver code"
Solution in php:

< ? php function nthEven ( $ n ) { return ( 2 * $ n ) ; } $ n = 10 ; echo nthEven ( $ n ) ; ? >


"Program for nth Catalan Number | A recursive function to find nth catalan number ; Base case ; catalan ( n ) is sum of catalan ( i ) * catalan ( n - i - 1 ) ; Driver Code"
Solution in php:

< ? php function catalan ( $ n ) { if ( $ n <= 1 ) return 1 ; $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ res += catalan ( $ i ) * catalan ( $ n - $ i - 1 ) ; return $ res ; } for ( $ i = 0 ; $ i < 10 ; $ i ++ ) echo catalan ( $ i ) , " ▁ " ; ? >


"Program for nth Catalan Number | PHP program for nth Catalan Number A dynamic programming based function to find nth Catalan number ; Table to store results of subproblems ; Initialize first two values in table ; Fill entries in catalan [ ] using recursive formula ; Return last entry ; Driver Code"
Solution in php:

< ? php function catalanDP ( $ n ) { $ catalan = array ( ) ; $ catalan [ 0 ] = $ catalan [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ catalan [ $ i ] = 0 ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) $ catalan [ $ i ] += $ catalan [ $ j ] * $ catalan [ $ i - $ j - 1 ] ; } return $ catalan [ $ n ] ; } for ( $ i = 0 ; $ i < 10 ; $ i ++ ) echo catalanDP ( $ i ) , " ▁ " ;


"Program for nth Catalan Number | Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Driver code"
Solution in php:

< ? php function binomialCoeff ( $ n , $ k ) { $ res = 1 ; if ( $ k > $ n - $ k ) $ k = $ n - $ k ; for ( $ i = 0 ; $ i < $ k ; ++ $ i ) { $ res *= ( $ n - $ i ) ; $ res = floor ( $ res / ( $ i + 1 ) ) ; } return $ res ; } function catalan ( $ n ) { $ c = binomialCoeff ( 2 * ( $ n ) , $ n ) ; return floor ( $ c / ( $ n + 1 ) ) ; } for ( $ i = 0 ; $ i < 10 ; $ i ++ ) echo catalan ( $ i ) , " ▁ " ; ? >


"Program for quotient and remainder of big number | Function to calculate the modulus ; Store the modulus of big number ; Do step by step division ; Update modulo by concatenating current digit . ; Update quotient ; Update mod for next iteration . ; Flag used to remove starting zeros ; Driver Code"
Solution in php:

 < ? php function modBigNumber ( $ num , $ m ) { $ vec ; $ x = 0 ; $ mod = 0 ; for ( $ i = 0 ; $ i < strlen ( $ num ) ; $ i ++ ) { $ digit = $ num [ $ i ] - '0' ; $ mod = $ mod * 10 + $ digit ; $ quo = ( int ) ( $ mod / $ m ) ; $ vec [ $ x ++ ] = $ quo ; $ mod = $ mod % $ m ; } echo " Remainder ▁ : ▁ " . $ mod . "
 " ; echo " Quotient ▁ : ▁ " ; $ zeroflag = 0 ; for ( $ i = 0 ; $ i < $ x ; $ i ++ ) { if ( $ vec [ $ i ] == 0 && $ zeroflag == 0 ) continue ; $ zeroflag = 1 ; echo $ vec [ $ i ] ; } return ; } $ num = "14598499948265358486" ; $ m = 487 ; modBigNumber ( $ num , $ m ) ; ? >


"Program for sum of arithmetic series | Efficient PHP code to find sum of arithmetic series . ; Driver code"
Solution in php:


"Program for sum of arithmetic series | Function to find sum of series . ; Driver Code"
Solution in php:


"Program for sum of cos ( x ) series | PHP program to find the sum of cos ( x ) series ; here x is in degree . we have to convert it to radian for using it with series formula , as in series expansion angle is in radian ; Driver Code"
Solution in php:

< ? php $ PI = 3.142 ; function cosXSertiesSum ( $ x , $ n ) { global $ PI ; $ x = $ x * ( $ PI / 180.0 ) ; $ res = 1 ; $ sign = 1 ; $ fact = 1 ; $ pow = 1 ; for ( $ i = 1 ; $ i < 5 ; $ i ++ ) { $ sign = $ sign * -1 ; $ fact = $ fact * ( 2 * $ i - 1 ) * ( 2 * $ i ) ; $ pow = $ pow * $ x * $ x ; $ res = $ res + $ sign * $ pow / $ fact ; } return $ res ; } $ x = 50 ; $ n = 5 ; echo cosXSertiesSum ( $ x , 5 ) ; ? >


"Program for weighted mean of natural numbers . | Function to calculate weighted mean . ; Take num array and corresponding weight array and initialize it . ; Calculate the size of array . ; Check the size of both array is equal or not ."
Solution in php:

< ? php function weightedMean ( $ X , $ W , $ n ) { $ sum = 0 ; $ numWeight = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ numWeight = $ numWeight + $ X [ $ i ] * $ W [ $ i ] ; $ sum = $ sum + $ W [ $ i ] ; } return ( float ) ( $ numWeight / $ sum ) ; } $ X = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ) ; $ W = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ) ; $ n = sizeof ( $ X ) ; $ m = sizeof ( $ W ) ; if ( $ n == $ m ) echo ( weightedMean ( $ X , $ W , $ n ) ) ; else echo ( " - 1" ) ; ? >


"Program for weighted mean of natural numbers . | Returns weighted mean assuming for numbers { 1 , 2 , . . n } and weights { 1 , 2 , . . n } ; Driver Code"
Solution in php:

< ? php function weightedMean ( $ n ) { return ( 2 * $ n + 1 ) / 3 ; } $ n = 10 ; echo ( weightedMean ( $ n ) ) ; ? >


"Program to Calculate the Perimeter of a Decagon | Function for finding the perimeter ; Driver Code"
Solution in php:

< ? php function CalPeri ( $ s ) { $ Perimeter = 10 * $ s ; echo " The ▁ Perimeter ▁ of ▁ Decagon ▁ is ▁ : ▁ $ Perimeter " ; } $ s = 5 ; CalPeri ( $ s ) ; ? >


"Program to Convert Hexadecimal Number to Binary | function to convert Hexadecimal to Binary Number ; Get the Hexadecimal number ; Convert HexaDecimal to Binary"
Solution in php:

< ? php function HexToBin ( $ hexdec ) { $ i = 0 ; while ( $ hexdec [ $ i ] ) { switch ( $ hexdec [ $ i ] ) { case '0' : echo "0000" ; break ; case '1' : echo "0001" ; break ; case '2' : echo "0010" ; break ; case '3' : echo "0011" ; break ; case '4' : echo "0100" ; break ; case '5' : echo "0101" ; break ; case '6' : echo "0110" ; break ; case '7' : echo "0111" ; break ; case '8' : echo "1000" ; break ; case '9' : echo "1001" ; break ; case ' A ' : case ' a ' : echo "1010" ; break ; case ' B ' : case ' b ' : echo "1011" ; break ; case ' C ' : case ' c ' : echo "1100" ; break ; case ' D ' : case ' d ' : echo "1101" ; break ; case ' E ' : case ' e ' : echo "1110" ; break ; case ' F ' : case ' f ' : echo "1111" ; break ; default : echo " Invalid hexadecimal digit " $ hexdec [ $ i ] ; } $ i ++ ; } } $ hexdec = "1AC5" ; echo " Equivalent Binary value is : " HexToBin ( $ hexdec ) ;


"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"
Solution in php:

 < ? 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 ) , "
 " ; echo " speed ▁ in ▁ kmph ▁ is ▁ " , mphTOkmph ( $ mph ) ; ? >


"Program to add two fractions | Function to return gcd of a and b ; Function to convert the obtained fraction into it 's simplest form ; Finding gcd of both terms ; Converting both terms into simpler terms by dividing them by common factor ; Function to add two fractions ; Finding gcd of den1 and den2 ; Denominator of final fraction obtained finding LCM of den1 and den2 LCM * GCD = a * b ; Changing the fractions to have same denominator Numerator of the final fraction obtained ; Calling function to convert final fraction into it 's  simplest form ; Driver Code"
Solution in php:

 < ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function lowest ( & $ den3 , & $ num3 ) { $ common_factor = gcd ( $ num3 , $ den3 ) ; $ den3 = ( int ) $ den3 / $ common_factor ; $ num3 = ( int ) $ num3 / $ common_factor ; } function addFraction ( $ num1 , $ den1 , $ num2 , $ den2 , & $ num3 , & $ den3 ) { $ den3 = gcd ( $ den1 , $ den2 ) ; $ den3 = ( $ den1 * $ den2 ) / $ den3 ; $ num3 = ( $ num1 ) * ( $ den3 / $ den1 ) + ( $ num2 ) * ( $ den3 / $ den2 ) ; lowest ( $ den3 , $ num3 ) ; } $ num1 = 1 ; $ den1 = 500 ; $ num2 = 2 ; $ den2 = 1500 ; $ den3 ; $ num3 ; addFraction ( $ num1 , $ den1 , $ num2 , $ den2 , $ num3 , $ den3 ) ; echo $ num1 , " / " , $ den1 , " ▁ + ▁ " , $ num2 , " / " , $ den2 , " ▁ is ▁ equal ▁ to ▁ " , $ num3 , " / " , $ den3 , "
 " ; ? >


"Program to calculate Area Of Octagon | Utility function ; Driver Code"
Solution in php:

< ? php function areaOctagon ( $ side ) { return ( 2 * ( 1 + sqrt ( 2 ) ) * $ side * $ side ) ; } $ side = 4 ; echo ( " Area ▁ of ▁ Regular ▁ Octagon ▁ = ▁ " ) ; echo ( areaOctagon ( $ side ) ) ; ? >


"Program to calculate GST from original and net prices | PHP Program to compute GST from original and net prices . ; return value after calculate GST % ; Driver Code"
Solution in php:

< ? php function Calculate_GST ( $ org_cost , $ N_price ) { return ( ( ( $ N_price - $ org_cost ) * 100 ) / $ org_cost ) ; } $ org_cost = 100 ; $ N_price = 120 ; echo ( " GST ▁ = ▁ " ) ; echo ( Calculate_GST ( $ org_cost , $ N_price ) ) ; echo ( " ▁ % ▁ " ) ; ? >


"Program to calculate Profit Or Loss | Function to calculate Profit . ; Function to calculate Loss . ; Driver Code ."
Solution in php:

< ? 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 ▁ " ; ? >


"Program to calculate Root Mean Square | Function that Calculate Root Mean Square ; Calculate square . ; Calculate Mean . ; Calculate Root . ; Driver code"
Solution in php:

< ? php function rmsValue ( $ arr , $ n ) { $ square = 0 ; $ mean = 0.0 ; $ root = 0.0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ square += pow ( $ arr [ $ i ] , 2 ) ; } $ mean = ( $ square / ( float ) ( $ n ) ) ; $ root = sqrt ( $ mean ) ; return $ root ; } $ arr = array ( 10 , 4 , 6 , 8 ) ; $ n = sizeof ( $ arr ) ; echo rmsValue ( $ arr , $ n ) ; ? >


"Program to calculate area and perimeter of a rhombus whose diagonals are given | calculate area and perimeter of a rhombus ; Driver code"
Solution in php:

 < ? php function rhombusAreaPeri ( $ d1 , $ d2 ) { $ area = ( $ d1 * $ d2 ) / 2 ; $ perimeter = 2 * sqrt ( pow ( $ d1 , 2 ) + pow ( $ d2 , 2 ) ) ; echo " The ▁ area ▁ of ▁ rhombus ▁ with ▁ diagonals ▁ " . $ d1 . " ▁ and ▁ " . $ d2 . " ▁ is ▁ " . $ area . " . " . "
 " ; echo " The ▁ perimeter ▁ of ▁ rhombus ▁ with ▁ diagonals ▁ " . $ d1 . " ▁ and ▁ " . $ d2 . " ▁ is ▁ " . $ perimeter . " . " . "
 " ; } $ d1 = 2 ; $ d2 = 4 ; rhombusAreaPeri ( $ d1 , $ d2 ) ; ? >


"Program to calculate area and perimeter of equilateral triangle | Function to calculate Area of equilateral triangle ; Function to calculate Perimeter of equilateral triangle ; Driver Code"
Solution in php:

 < ? php function area_equi_triangle ( $ side ) { return sqrt ( 3 ) / 4 * $ side * $ side ; } function peri_equi_triangle ( $ side ) { return 3 * $ side ; } $ side = 4 ; echo ( " Area ▁ of ▁ Equilateral ▁ Triangle : ▁ " ) ; echo ( area_equi_triangle ( $ side ) ) ; echo ( "
 " ) ; echo ( " Perimeter ▁ of ▁ Equilateral ▁ Triangle : ▁ " ) ; echo ( peri_equi_triangle ( $ side ) ) ; ? >


"Program to calculate area and volume of a Tetrahedron | Function to calculate volume ; Driver Code"
Solution in php:

< ? php function vol_tetra ( $ side ) { $ volume = ( pow ( $ side , 3 ) / ( 6 * sqrt ( 2 ) ) ) ; return $ volume ; } $ side = 3 ; $ vol = vol_tetra ( $ side ) ; echo $ vol ; ? >


"Program to calculate area and volume of a Tetrahedron | Utility Function ; Driver Code"
Solution in php:

< ? php function area_of_tetrahedron ( $ side ) { return ( sqrt ( 3 ) * ( $ side * $ side ) ) ; } $ side = 3 ; echo " Area ▁ of ▁ Tetrahedron ▁ = ▁ " , area_of_tetrahedron ( $ side ) ; ? >


"Program to calculate distance between two points in 3 D | function to print distance ; Driver Code ; function call for distance"
Solution in php:


"Program to calculate distance between two points | Function to calculate distance ; Calculating distance ; Driver Code"
Solution in php:


"Program to calculate the Area and Perimeter of Incircle of an Equilateral Triangle | PHP program to find the area of inscribed circle of equilateral triangle ; function to find area of inscribed circle ; function to find perimeter of inscribed circle ; Driver code"
Solution in php:

< ? php $ PI = 3.14159265 ; function area_inscribed ( $ a ) { global $ PI ; return ( $ a * $ a * ( $ PI / 12 ) ) ; } function perm_inscribed ( $ a ) { global $ PI ; return ( $ PI * ( $ a / sqrt ( 3 ) ) ) ; } $ a = 6 ; echo ( " Area ▁ of ▁ inscribed ▁ circle ▁ is ▁ : " ) ; echo ( area_inscribed ( $ a ) ) ; echo ( " Perimeter ▁ of ▁ inscribed ▁ circle ▁ is ▁ : " ) ; echo ( perm_inscribed ( $ a ) ) ; ? >


"Program to calculate the area between two Concentric Circles | Function to find area between the two given concentric circles ; Declare value of pi ; Calculate area of outer circle ; Calculate area of inner circle ; Difference in areas ; Driver Code"
Solution in php:

< ? php function calculateArea ( $ x , $ y ) { $ pi = 3.1415926536 ; $ arx = $ pi * $ x * $ x ; $ ary = $ pi * $ y * $ y ; return ( $ arx - $ ary ) ; } $ x = 2 ; $ y = 1 ; echo calculateArea ( $ x , $ y ) ; ? >


"Program to calculate the number of odd days in given number of years | Function to return the count of odd days ; Count of years divisible by 100 and 400 ; Every 4 th year is a leap year ; Every 100 th year is divisible by 4 but is not a leap year ; Every 400 th year is divisible by 100 but is a leap year ; Total number of extra days ; modulo ( 7 ) for final answer ; Number of days"
Solution in php:

< ? php function oddDays ( $ N ) { $ hund1 = floor ( $ N / 100 ) ; $ hund4 = floor ( $ N / 400 ) ; $ leap = $ N >> 2 ; $ ord = $ N - $ leap ; if ( $ hund1 ) { $ ord += $ hund1 ; $ leap -= $ hund1 ; } if ( $ hund4 ) { $ ord -= $ hund4 ; $ leap += $ hund4 ; } $ days = $ ord + $ leap * 2 ; $ odd = $ days % 7 ; return $ odd ; } $ N = 100 ; echo oddDays ( $ N ) ; ? >


"Program to calculate the value of sin ( x ) and cos ( x ) using Expansion | Function for calculation ; Converting degrees to radian ; maps the sum along the series ; holds the actual value of sin ( n ) ; Driver Code"
Solution in php:

< ? php function cal_cos ( $ n ) { $ accuracy = 0.0001 ; $ 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 <= abs ( $ cosval - $ cosx ) ) ; echo round ( $ cosx , 6 ) ; } $ n = 30 ; cal_cos ( $ n ) ; ? >


"Program to calculate value of nCr | PHP program To calculate the Value Of nCr ; Returns factorial of n ; Driver code"
Solution in php:

< ? php function nCr ( $ n , $ r ) { return fact ( $ n ) / ( fact ( $ r ) * fact ( $ n - $ r ) ) ; } function fact ( $ n ) { $ res = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res = $ res * $ i ; return $ res ; } $ n = 5 ; $ r = 3 ; echo nCr ( $ n , $ r ) ; ? >


"Program to calculate volume of Ellipsoid | Function to find the volume ; Driver Code"
Solution in php:

< ? php function volumeOfEllipsoid ( $ r1 , $ r2 , $ r3 ) { $ pi = 3.14 ; return 1.33 * $ pi * $ r1 * $ r2 * $ r3 ; } $ r1 = 2.3 ; $ r2 = 3.4 ; $ r3 = 5.7 ; echo ( " volume ▁ of ▁ ellipsoid ▁ is ▁ : ▁ " ) ; echo ( volumeOfEllipsoid ( $ r1 , $ r2 , $ r3 ) ) ; ? >


"Program to calculate volume of Octahedron | utility Function ; Driver Function"
Solution in php:

< ? php function vol_of_octahedron ( $ side ) { return ( ( $ side * $ side * $ side ) * ( sqrt ( 2 ) / 3 ) ) ; } $ side = 3 ; echo ( " Volume ▁ of ▁ octahedron ▁ = " ) ; echo ( vol_of_octahedron ( $ side ) ) ; ? >


"Program to check Involutory Matrix | Program to implement involutory matrix . ; Function for matrix multiplication . ; Function to check involutory matrix . ; multiply function call . ; Driver Code ; Function call . If function return true then if part will execute otherwise else part will execute ."
Solution in php:

< ? php $ N = 3 ; function multiply ( $ mat , $ res ) { global $ N ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = 0 ; $ j < $ N ; $ j ++ ) { $ res [ $ i ] [ $ j ] = 0 ; for ( $ k = 0 ; $ k < $ N ; $ k ++ ) $ res [ $ i ] [ $ j ] += $ mat [ $ i ] [ $ k ] * $ mat [ $ k ] [ $ j ] ; } } return $ res ; } function InvolutoryMatrix ( $ mat ) { global $ N ; $ res ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ N ; $ j ++ ) $ res [ $ i ] [ $ j ] = 0 ; $ res = multiply ( $ mat , $ res ) ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = 0 ; $ j < $ N ; $ j ++ ) { if ( $ i == $ j && $ res [ $ i ] [ $ j ] != 1 ) return false ; if ( $ i != $ j && $ res [ $ i ] [ $ j ] != 0 ) return false ; } } return true ; } $ mat = array ( array ( 1 , 0 , 0 ) , array ( 0 , -1 , 0 ) , array ( 0 , 0 , -1 ) ) ; if ( InvolutoryMatrix ( $ mat ) ) echo " Involutory ▁ Matrix " ; else echo " Not ▁ Involutory ▁ Matrix " ; ? >


"Program to check Plus Perfect Number | function to check plus perfect number ; calculating number of digits ; calculating plus perfect number ; checking whether number is plus perfect or not ; Driver Code"
Solution in php:

< ? php function checkplusperfect ( $ x ) { $ temp = $ x ; $ n = 0 ; while ( $ x != 0 ) { $ x /= 10 ; $ n ++ ; } $ x = $ temp ; $ sum = 0 ; while ( $ x != 0 ) { $ sum += pow ( $ x % 10 , $ n ) ; $ x /= 10 ; } return ( $ sum == $ temp ) ; } $ x = 9474 ; if ( checkplusperfect ( ! $ x ) ) echo " Yes " ; else echo " No " ; ? >


"Program to check diagonal matrix and scalar matrix | Program to check matrix is diagonal matrix or not . ; Function to check matrix is diagonal matrix or not . ; condition to check other elements except main diagonal are zero or not . ; Driver Code"
Solution in php:

 < ? php $ N = 4 ; function isDiagonalMatrix ( $ mat ) { global $ N ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ N ; $ j ++ ) if ( ( $ i != $ j ) && ( $ mat [ $ i ] [ $ j ] != 0 ) ) return false ; return true ; } $ mat = array ( array ( 4 , 0 , 0 , 0 ) , array ( 0 , 7 , 0 , 0 ) , array ( 0 , 0 , 5 , 0 ) , array ( 0 , 0 , 0 , 1 ) ) ; if ( isDiagonalMatrix ( $ mat ) ) echo " Yes " , "
 " ; else echo " No " , "
 " ; ? >


"Program to check diagonal matrix and scalar matrix | Program to check matrix is scalar matrix or not . ; Function to check matrix is scalar matrix or not . ; Check all elements except main diagonal are zero or not . ; Check all diagonal elements are same or not . ; Driver Code ; Function call"
Solution in php:

< ? php $ N = 4 ; function isScalarMatrix ( $ mat ) { global $ N ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ N ; $ j ++ ) if ( ( $ i != $ j ) && ( $ mat [ $ i ] [ $ j ] != 0 ) ) return false ; for ( $ i = 0 ; $ i < $ N - 1 ; $ i ++ ) if ( $ mat [ $ i ] [ $ i ] != $ mat [ $ i + 1 ] [ $ i + 1 ] ) return false ; return true ; } $ mat = array ( array ( 2 , 0 , 0 , 0 ) , array ( 0 , 2 , 0 , 0 ) , array ( 0 , 0 , 2 , 0 ) , array ( 0 , 0 , 0 , 2 ) ) ; if ( isScalarMatrix ( $ mat ) ) echo " Yes " ; else echo " No " ; ? >


"Program to check if N is a Pentagonal Number | Function to determine if N is pentagonal or not . ; Substitute values of i in the formula . ; Driver Code"
Solution in php:

< ? php function isPentagonal ( int $ N ) { $ i = 1 ; $ M ; do { $ M = ( 3 * $ i * $ i - $ i ) / 2 ; $ i += 1 ; } while ( $ M < $ N ) ; return ( $ M == $ N ) ; } $ N = 12 ; if ( isPentagonal ( $ N ) ) echo $ N , " ▁ is ▁ pentagonal ▁ " ; else echo $ N , " ▁ is ▁ not ▁ pentagonal " ; ? >


"Program to check if a number is divisible by any of its digits | Function to check if given number is divisible by any of its digits ; check if any of digit divides n ; check if K divides N ; Driver Code"
Solution in php:

< ? php function isDivisible ( $ n ) { $ temp = $ n ; while ( $ n ) { $ k = $ n % 10 ; if ( $ temp % $ k == 0 ) return " YES " ; $ n = floor ( $ n / 10 ) ; } return " NO " ; } $ n = 9876543 ; echo isDivisible ( $ n ) ; ? >


"Program to check if a number is divisible by sum of its digits | Function to check if the given number is divisible by sum of its digits ; Find sum of digits ; check if sum of digits divides n ; Driver Code"
Solution in php:

< ? php function isDivisible ( $ n ) { $ temp = $ n ; $ sum = 0 ; while ( $ n ) { $ k = $ n % 10 ; $ sum += $ k ; $ n = ( int ) ( $ n / 10 ) ; } if ( $ temp % $ sum == 0 ) return " YES " ; return " NO " ; } $ n = 123 ; print ( isDivisible ( $ n ) ) ; ? >


"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"
Solution in php:

< ? 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 check if the points are parallel to X axis or Y axis | To check for parallel line ; checking for parallel to X and Y axis condition ; To display the output ; Driver 's Code"
Solution in php:

< ? php function parallel ( $ n , $ a ) { $ x = true ; $ y = true ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ a [ $ i ] [ 0 ] != $ a [ $ i + 1 ] [ 0 ] ) $ x = false ; if ( $ a [ $ i ] [ 1 ] != $ a [ $ i + 1 ] [ 1 ] ) $ y = false ; } if ( $ x ) echo " parallel ▁ to ▁ Y ▁ Axis " ; else if ( y ) echo " parallel ▁ to ▁ X ▁ Axis " ; else echo " Not ▁ parallel ▁ to ▁ X " , " ▁ and ▁ Y ▁ Axis " ; } $ a = array ( array ( 1 , 2 ) , array ( 1 , 4 ) , array ( 1 , 6 ) , array ( 1 , 0 ) ) ; $ n = count ( $ a ) ; parallel ( $ n , $ a ) ; ? >


"Program to check if three points are collinear | function to check if point collinear or not ; Driver Code"
Solution in php:


"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"
Solution in php:

 < ? 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 " , "
 " ; } else { echo " Not ▁ in ▁ overflow ▁ state " , "
 " ; } } $ H = 10 ; $ r = 5 ; $ h = 5 ; $ N = 2 ; $ R = 2 ; overflow ( $ H , $ r , $ h , $ N , $ R ) ; ? >


"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"
Solution in php:


"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"
Solution in php:


"Program to check whether a number is Proth number or not | Utility function to check power of two ; Function to check if the Given number is Proth number or not ; check if k divides n or not ; Check if n / k is power of 2 or not ; update k to next odd number ; If we reach here means there exists no value of K Such that k is odd number and n / k is a power of 2 greater than k ; Get n ; Check n for Proth Number"
Solution in php:

< ? php function isPowerOfTwo ( $ n ) { return ( $ n && ! ( $ n & ( $ n - 1 ) ) ) ; } function isProthNumber ( $ n ) { $ k = 1 ; while ( $ k < ( $ n / $ k ) ) { if ( $ n % $ k == 0 ) { if ( isPowerOfTwo ( $ n / $ k ) ) return true ; } $ k = $ k + 2 ; } return false ; } $ n = 25 ; if ( isProthNumber ( $ n - 1 ) ) echo " YES " ; else echo " NO " ; ? >


"Program to compare m ^ n and n ^ m | function to compare m ^ n and n ^ m ; m ^ n ; n ^ m ; Driver Code ; function call to compare m ^ n and n ^ m"
Solution in php:

< ? php function check ( $ m , $ n ) { $ RHS = $ m * log ( $ n ) ; $ LHS = $ n * log ( $ m ) ; if ( $ LHS > $ RHS ) echo " m ^ n ▁ > ▁ n ^ m " ; else if ( $ LHS < $ RHS ) echo " m ^ n ▁ < ▁ n ^ m " ; else echo " m ^ n ▁ = ▁ n ^ m " ; } $ m = 987654321 ; $ n = 123456987 ; check ( $ m , $ n ) ; ? >


"Program to compare two fractions | Get max of the two fractions ; Declare nume1 and nume2 for get the value of first numerator and second numerator ; Compute ad - bc ; Driver Code"
Solution in php:

< ? php function maxFraction ( $ first , $ sec ) { $ a = $ first [ 0 ] ; $ b = $ first [ 1 ] ; $ c = $ sec [ 0 ] ; $ d = $ sec [ 1 ] ; $ Y = $ a * $ d - $ b * $ c ; return ( $ Y ) ? $ first : $ sec ; } $ first = array ( 3 , 2 ) ; $ sec = array ( 3 , 4 ) ; $ res = maxFraction ( $ first , $ sec ) ; echo $ res [ 0 ] . " / " . $ res [ 1 ] ; ? >


"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"
Solution in php:


"Program to convert KiloBytes to Bytes and Bits | Function to calculates the bits ; calculates Bits 1 kilobytes ( s ) = 8192 bits ; Function to calculates the bytes ; calculates Bytes 1 KB = 1024 bytes ; Driver code"
Solution in php:

< ? php function Bits ( $ kilobytes ) { $ Bits = 0 ; $ Bits = $ kilobytes * 8192 ; return $ Bits ; } function Bytes ( $ kilobytes ) { $ Bytes = 0 ; $ Bytes = $ kilobytes * 1024 ; return $ Bytes ; } $ kilobytes = 1 ; echo $ kilobytes ; echo ( " ▁ Kilobytes ▁ = ▁ " ) ; echo Bytes ( $ kilobytes ) ; echo ( " ▁ Bytes ▁ and ▁ " ) ; echo Bits ( $ kilobytes ) ; echo ( " ▁ Bits . " ) ; ? >