"Sum of all substrings of a string representing a number | Set 1 | Method to convert character digit to integer digit ; Returns sum of all substring of num ; allocate memory equal to length of string ; initialize first value with first digit ; loop over all digits of string ; update each sumofdigit from previous value ; add current value to the result ; Driver Code"
Solution in php:

< ? php function toDigit ( $ ch ) { return ( $ ch - '0' ) ; } function sumOfSubstrings ( $ num ) { $ n = strlen ( $ num ) ; $ sumofdigit [ $ n ] = 0 ; $ sumofdigit [ 0 ] = toDigit ( $ num [ 0 ] ) ; $ res = $ sumofdigit [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ numi = toDigit ( $ num [ $ i ] ) ; $ sumofdigit [ $ i ] = ( $ i + 1 ) * $ numi + 10 * $ sumofdigit [ $ i - 1 ] ; $ res += $ sumofdigit [ $ i ] ; } return $ res ; } $ num = "1234" ; echo sumOfSubstrings ( $ num ) ; ? >


"Sum of all substrings of a string representing a number | Set 2 ( Constant Extra Space ) | Returns sum of all substring of num ; Initialize result ; Here traversing the array in reverse order . Initializing loop from last element . mf is multiplying factor . ; Each time sum is added to its previous sum . Multiplying the three factors as explained above . s [ i ] - '0' is done to convert char to int . ; Making new multiplying factor as explained above . ; Driver Code"
Solution in php:

 < ? php function sumOfSubstrings ( $ num ) { $ sum = 0 ; $ mf = 1 ; for ( $ i = strlen ( $ num ) - 1 ; $ i >= 0 ; $ i -- ) { $ sum += ( $ num [ $ i ] - '0' ) * ( $ i + 1 ) * $ mf ; $ mf = $ mf * 10 + 1 ; } return $ sum ; } $ num = "6759" ; echo sumOfSubstrings ( $ num ) , "
 " ; ? >


"Sum of all the multiples of 3 and 7 below N | Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of 3 and 7 below N ; Since , we need the sum of multiples less than N ; Driver code"
Solution in php:

< ? php function sumAP ( $ n , $ d ) { $ n = ( int ) ( $ n / $ d ) ; return ( $ n ) * ( 1 + $ n ) * ( $ d / 2 ) ; } function sumMultiples ( $ n ) { $ n -- ; return sumAP ( $ n , 3 ) + sumAP ( $ n , 7 ) - sumAP ( $ n , 21 ) ; } $ n = 24 ; echo sumMultiples ( $ n ) ; ? >


"Sum of array elements that is first continuously increasing then decreasing | Efficient PHP method to find sum of the elements of array that is halfway increasing and then halfway decreassing ; Driver code"
Solution in php:


"Sum of average of all subsets | Returns value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; method returns sum of average of all subsets ; Initialize result ; Find sum of elements ; looping once for all subset of same size ; each element occurs nCr ( N - 1 , n - 1 ) times while considering subset of size n ; Driver Code"
Solution in php:


"Sum of bit differences among all pairs | PHP program to compute sum of pairwise bit differences ; Initialize result ; traverse over all bits ; count number of elements with i 'th bit set ; Add " count ▁ * ▁ ( n ▁ - ▁ count ) ▁ * ▁ 2" to the answer ; Driver Code"
Solution in php:

 < ? php function sumBitDifferences ( $ arr , $ n ) { $ ans = 0 ; for ( $ i = 0 ; $ i < 32 ; $ i ++ ) { $ count = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( ( $ arr [ $ j ] & ( 1 << $ i ) ) ) $ count ++ ; $ ans += ( $ count * ( $ n - $ count ) * 2 ) ; } return $ ans ; } $ arr = array ( 1 , 3 , 5 ) ; $ n = sizeof ( $ arr ) ; echo sumBitDifferences ( $ arr , $ n ) , "
 " ; ? >


"Sum of bitwise AND of all possible subsets of given set | PHP program to calculate sum of Bit - wise and sum of all subsets of an array ; assuming representation of each element is in 32 bit ; iterating array element ; Counting the set bit of array in ith position ; counting subset which produce sum when particular bit position is set . ; multiplying every position subset with 2 ^ i to count the sum . ; Driver code"
Solution in php:

< ? php $ BITS = 32 ; function andSum ( $ arr , $ n ) { global $ BITS ; $ ans = 0 ; for ( $ i = 0 ; $ i < $ BITS ; $ i ++ ) { $ countSetBits = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ arr [ $ j ] & ( 1 << $ i ) ) $ countSetBits ++ ; } $ subset = ( 1 << $ countSetBits ) - 1 ; $ subset = ( $ subset * ( 1 << $ i ) ) ; $ ans += $ subset ; } return $ ans ; } $ arr = array ( 1 , 2 , 3 ) ; $ size = count ( $ arr ) ; echo andSum ( $ arr , $ size ) ; ? >


"Sum of bitwise AND of all subarrays | Function to find the sum of bitwise AND of all subarrays ; variable to store the final sum ; multiplier ; variable to check if counting is on ; variable to store the length of the subarrays ; loop to find the contiguous segments ; updating the multiplier ; returning the sum ; Driver Code"
Solution in php:


"Sum of both diagonals of a spiral odd | function returns sum of diagonals ; as order should be only odd we should pass only odd - integers ; Driver Code"
Solution in php:

< ? php function spiralDiaSum ( $ n ) { if ( $ n == 1 ) return 1 ; return ( 4 * $ n * $ n - 6 * $ n + 6 + spiralDiaSum ( $ n - 2 ) ) ; } $ n = 7 ; echo spiralDiaSum ( $ n ) ; ? >


"Sum of common divisors of two numbers A and B | Function to calculate gcd of two numbers ; Function to calculate all common divisors of two given numbers a , b -- > input integer numbers ; find gcd of a , b ; Find the sum of divisors of n . ; if ' i ' is factor of n ; check if divisors are equal ; Driver program to run the case"
Solution in php:

< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function sumcommDiv ( $ a , $ b ) { $ n = gcd ( $ a , $ b ) ; $ sum = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) $ sum += $ i ; else $ sum += ( $ n / $ i ) + $ i ; } } return $ sum ; } $ a = 10 ; $ b = 15 ; echo " Sum = " ? >


"Sum of common divisors of two numbers A and B | print the sum of common factors ; sum of common factors ; iterate from 1 to minimum of a and b ; if i is the common factor of both the numbers ; Driver code ; print the sum of common factors"
Solution in php:


"Sum of digits of a given number to a given power | Function to calculate sum ; Driver Code"
Solution in php:

< ? php function calculate ( $ n , $ power ) { $ sum = 0 ; $ bp = ( int ) pow ( $ n , $ power ) ; while ( $ bp != 0 ) { $ d = $ bp % 10 ; $ sum += $ d ; $ bp /= 10 ; } return $ sum ; } $ n = 5 ; $ power = 4 ; echo ( calculate ( $ n , $ power ) ) ; ? >


"Sum of digits written in different bases from 2 to n | function to calculate sum of digit for a given base ; Sum of digits ; Calculating the number ( n ) by taking mod with the base and adding remainder to the result and parallelly reducing the num value . ; returning the result ; function calling for multiple bases ; Driver code"
Solution in php:

< ? php function solve ( $ n , $ base ) { $ result = 0 ; while ( $ n > 0 ) { $ remainder = $ n % $ base ; $ result = $ result + $ remainder ; $ n = $ n / $ base ; } return $ result ; } function printSumsOfDigits ( $ n ) { for ( $ base = 2 ; $ base < $ n ; ++ $ base ) { echo ( solve ( $ n , $ base ) ) ; echo ( " ▁ " ) ; } } $ n = 8 ; printSumsOfDigits ( $ n ) ; ? >


"Sum of each element raised to ( prime | Function to return the required sum ; Driver code"
Solution in php:

< ? php function getSum ( $ arr , $ p ) { return count ( $ arr ) ; } $ arr = array ( 5 , 6 , 8 ) ; $ p = 7 ; echo ( getSum ( $ arr , $ p ) ) ; ? >


"Sum of elements from an array having even parity | Function that returns true if x has even parity ; We basically count set bits https : www . geeksforgeeks . org / count - set - bits - in - an - integer / ; Function to return the sum of the elements from an array which have even parity ; If a [ i ] has even parity ; Driver code"
Solution in php:

< ? php function checkEvenParity ( $ x ) { $ parity = 0 ; while ( $ x != 0 ) { $ x = ( $ x & ( $ x - 1 ) ) ; $ parity ++ ; } if ( $ parity % 2 == 0 ) return true ; else return false ; } function sumlist ( $ a , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( checkEvenParity ( $ a [ $ i ] ) ) $ sum += $ a [ $ i ] ; } return $ sum ; } $ arr = array ( 2 , 4 , 3 , 5 , 9 ) ; $ n = sizeof ( $ arr ) ; echo sumlist ( $ arr , $ n ) ; ? >


"Sum of elements in 1 st array such that number of elements less than or equal to them in 2 nd array is maximum | PHP implementation of the approach ; Function to return the required sum ; Creating hash array initially filled with zero ; Calculate the frequency of elements of arr2 [ ] ; Running sum of hash array such that hash [ i ] will give count of elements less than or equal to i in arr2 [ ] ; To store the maximum value of the number of elements in arr2 [ ] which are smaller than or equal to some element of arr1 [ ] ; Calculate the sum of elements from arr1 [ ] corresponding to maximum frequency ; Return the required sum ; Driver code"
Solution in php:

< ? php $ MAX = 10000 ; function findSumofEle ( $ arr1 , $ m , $ arr2 , $ n ) { $ hash = array_fill ( 0 , $ GLOBALS [ ' MAX ' ] , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ hash [ $ arr2 [ $ i ] ] ++ ; for ( $ i = 1 ; $ i < $ GLOBALS [ ' MAX ' ] ; $ i ++ ) $ hash [ $ i ] = $ hash [ $ i ] + $ hash [ $ i - 1 ] ; $ maximumFreq = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) $ maximumFreq = max ( $ maximumFreq , $ hash [ $ arr1 [ $ i ] ] ) ; $ sumOfElements = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) $ sumOfElements += ( $ maximumFreq == $ hash [ $ arr1 [ $ i ] ] ) ? $ arr1 [ $ i ] : 0 ; return $ sumOfElements ; } $ arr1 = array ( 2 , 5 , 6 , 8 ) ; $ arr2 = array ( 4 , 10 ) ; $ m = count ( $ arr1 ) ; $ n = count ( $ arr2 ) ; echo findSumofEle ( $ arr1 , $ m , $ arr2 , $ n ) ; ? >


"Sum of elements in an array whose difference with the mean of another array is less than k | Function for finding sum of elements whose diff with mean is not more than k ; Find the mean of second array ; Find sum of elements from array1 whose difference with mean is not more than k ; Return result ; Driver code"
Solution in php:

< ? php function findSumofEle ( $ arr1 , $ m , $ arr2 , $ n , $ k ) { $ arraySum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arraySum += $ arr2 [ $ i ] ; $ mean = $ arraySum / $ n ; $ sumOfElements = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { $ difference = $ arr1 [ $ i ] - $ mean ; if ( ( $ difference < 0 ) && ( $ k > ( -1 ) * $ difference ) ) { $ sumOfElements += $ arr1 [ $ i ] ; } if ( ( $ difference >= 0 ) && ( $ k > $ difference ) ) { $ sumOfElements += $ arr1 [ $ i ] ; } } return $ sumOfElements ; } $ arr1 = array ( 1 , 2 , 3 , 4 , 7 , 9 ) ; $ arr2 = array ( 0 , 1 , 2 , 1 , 1 , 4 ) ; $ k = 2 ; $ m = count ( $ arr1 ) ; $ n = count ( $ arr2 ) ; print ( findSumofEle ( $ arr1 , $ m , $ arr2 , $ n , $ k ) ) ; ? >


"Sum of elements in range L | Function to find the sum between L and R ; array created ; fill the first half of array ; fill the second half of array ; find the sum between range ; Driver Code"
Solution in php:


"Sum of every Kâ €™ th prime number in an array | PHP implementation of the approach ; 0 and 1 are not prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; compute the answer ; count of primes ; sum of the primes ; traverse the array ; if the number is a prime ; increase the count ; if it is the K 'th prime ; create the sieve"
Solution in php:

 < ? php $ MAX = 100000 ; $ prime = array_fill ( 0 , $ MAX + 1 , true ) ; function SieveOfEratosthenes ( ) { global $ MAX , $ prime ; $ prime [ 1 ] = false ; $ prime [ 0 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ MAX ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ MAX ; $ i += $ p ) $ prime [ $ i ] = false ; } } } function SumOfKthPrimes ( $ arr , $ n , $ k ) { global $ MAX , $ prime ; $ c = 0 ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ prime [ $ arr [ $ i ] ] ) { $ c ++ ; if ( $ c % $ k == 0 ) { $ sum += $ arr [ $ i ] ; $ c = 0 ; } } } echo $ sum . "
 " ; } SieveOfEratosthenes ( ) ; $ arr = array ( 2 , 3 , 5 , 7 , 11 ) ; $ n = sizeof ( $ arr ) ; $ k = 2 ; SumOfKthPrimes ( $ arr , $ n , $ k ) ; ? >


"Sum of fifth powers of the first n natural numbers | calculate the sum of fifth power of first n natural numbers ; Driver Code"
Solution in php:


"Sum of first N natural numbers by taking powers of 2 as negative number | to store power of 2 ; to store presum of the power of 2 's ; function to find power of 2 ; to store power of 2 ; to store pre sum ; Function to find the sum ; first store sum of first n natural numbers . ; find the first greater number than given number then minus double of this from answer ; function call ; function call"
Solution in php:

< ? php $ power = array_fill ( 0 , 31 , 0 ) ; $ pre = array_fill ( 0 , 31 , 0 ) ; function PowerOfTwo ( ) { global $ power , $ pre ; $ x = 1 ; for ( $ i = 0 ; $ i < 31 ; $ i ++ ) { $ power [ $ i ] = $ x ; $ x *= 2 ; } $ pre [ 0 ] = 1 ; for ( $ i = 1 ; $ i < 31 ; $ i ++ ) $ pre [ $ i ] = $ pre [ $ i - 1 ] + $ power [ $ i ] ; } function Sum ( $ n ) { global $ power , $ pre ; $ ans = $ n * ( $ n + 1 ) / 2 ; for ( $ i = 0 ; $ i < 31 ; $ i ++ ) if ( $ power [ $ i ] > $ n ) { $ ans -= 2 * $ pre [ $ i - 1 ] ; break ; } return $ ans ; } PowerOfTwo ( ) ; $ n = 4 ; print ( Sum ( $ n ) ) ; ? >


"Sum of first N natural numbers which are divisible by 2 and 7 | Function to calculate the sum of numbers divisible by 2 or 7 ; Driver code"
Solution in php:


"Sum of first N natural numbers which are divisible by X or Y | Function to calculate the sum of numbers divisible by X or Y ; Driver code"
Solution in php:


"Sum of first N natural numbers which are not powers of K | Function to return the sum of first n natural numbers which are not positive powers of k ; sum of first n natural numbers ; subtract all positive powers of k which are less than n ; next power of k ; Driver code"
Solution in php:

< ? php function find_sum ( $ n , $ k ) { $ total_sum = ( $ n * ( $ n + 1 ) ) / 2 ; $ power = $ k ; while ( $ power <= $ n ) { $ total_sum -= $ power ; $ power *= $ k ; } return $ total_sum ; } $ n = 11 ; $ k = 2 ; echo find_sum ( $ n , $ k ) ; ? >


"Sum of first n even numbers | function to find sum of first n even numbers ; required sum ; Driver Code"
Solution in php:

< ? php function evenSum ( $ n ) { return ( $ n * ( $ n + 1 ) ) ; } $ n = 20 ; echo " Sum ▁ of ▁ first ▁ " , $ n , " ▁ Even ▁ numbers ▁ is : ▁ " , evenSum ( $ n ) ; ? >


"Sum of first n even numbers | function to find sum of first n even numbers ; sum of first n even numbers ; next even number ; required sum ; Driver program to test above"
Solution in php:

< ? php function evenSum ( $ n ) { $ curr = 2 ; $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ sum += $ curr ; $ curr += 2 ; } return $ sum ; } $ n = 20 ; echo " Sum ▁ of ▁ first ▁ " . $ n . " ▁ Even ▁ numbers ▁ is : ▁ " . evenSum ( $ n ) ; ? >


"Sum of first n natural numbers | Function to find the sum of series ; Driver code"
Solution in php:

< ? php function seriesSum ( $ n ) { return ( $ n * ( $ n + 1 ) * ( $ n + 2 ) ) / 6 ; } $ n = 4 ; echo ( seriesSum ( $ n ) ) ; ? >


< ? php function seriesSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum += $ i * ( $ i + 1 ) / 2 ; return $ sum ; } $ n = 4 ; echo ( seriesSum ( $ n ) ) ; ? >


"Sum of first n odd numbers in O ( 1 ) Complexity | Returns the sum of first n odd numbers ; Driver Code"
Solution in php:

< ? php function oddSum ( $ n ) { $ sum = 0 ; $ curr = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum += $ curr ; $ curr += 2 ; } return $ sum ; } $ n = 20 ; echo " ▁ Sum ▁ of ▁ first ▁ " , $ n , " ▁ Odd ▁ Numbers ▁ is : ▁ " , oddSum ( $ n ) ; ? >


< ? php function oddSum ( $ n ) { return ( $ n * $ n ) ; } $ n = 20 ; echo " ▁ Sum ▁ of ▁ first ▁ " , $ n , " ▁ Odd ▁ Numbers ▁ is : ▁ " , oddSum ( $ n ) ; ? >


"Sum of first n terms of a given series 3 , 6 , 11 , ... . . | Function to calculate the sum ; starting number ; Common Ratio ; Common difference ; Nth term to be find ; find the Sn"
Solution in php:


"Sum of fourth power of first n even natural numbers | calculate the sum of fourth power of first n even natural numbers ; Driver Code"
Solution in php:


"Sum of fourth power of first n even natural numbers | calculate the sum of fourth power of first n even natural numbers ; made even number ; Driver Code"
Solution in php:

< ? php function evenPowerSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ j = 2 * $ i ; $ sum = $ sum + ( $ j * $ j * $ j * $ j ) ; } return $ sum ; } $ n = 5 ; echo ( evenPowerSum ( $ n ) ) ; ? >


"Sum of fourth powers of first n odd natural numbers | calculate the sum of fourth power of first n odd natural numbers ; Driver Code"
Solution in php:


"Sum of fourth powers of the first n natural numbers | Return the sum of fourth power of first n natural numbers ; Driver Code"
Solution in php:

< ? php function fourthPowerSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum = $ sum + ( $ i * $ i * $ i * $ i ) ; return $ sum ; } $ n = 6 ; echo ( fourthPowerSum ( $ n ) ) ; ? >


< ? php function fourthPowerSum ( $ n ) { return ( ( 6 * $ n * $ n * $ n * $ n * $ n ) + ( 15 * $ n * $ n * $ n * $ n ) + ( 10 * $ n * $ n * $ n ) - $ n ) / 30 ; } $ n = 6 ; echo ( fourthPowerSum ( $ n ) ) ; ? >


"Sum of greatest odd divisor of numbers in given range | Function to return sum of first n odd numbers ; Recursive function to return sum of greatest odd divisor of numbers in range [ 1 , n ] ; { Odd n ; { Even n ; Function to return sum of greatest odd divisor of numbers in range [ a , b ] ; Driver code"
Solution in php:

< ? php function square ( $ n ) { return $ n * $ n ; } function sum ( $ n ) { if ( $ n == 0 ) return 0 ; if ( $ n % 2 == 1 ) return square ( ( int ) ( ( $ n + 1 ) / 2 ) ) + sum ( ( int ) ( $ n / 2 ) ) ; } else return square ( ( int ) ( $ n / 2 ) ) + sum ( ( int ) ( $ n / 2 ) ) ; } } function oddDivSum ( $ a , $ b ) { return sum ( $ b ) - sum ( $ a - 1 ) ; } $ a = 3 ; $ b = 9 ; echo oddDivSum ( $ a , $ b ) ; ? >


"Sum of i * countDigits ( i ) ^ 2 for all i in range [ L , R ] | PHP implementation of the approach ; Function to return the required sum ; If range is valid ; Sum of AP ; Driver code"
Solution in php:


"Sum of integers upto N with given unit digit ( Set 2 ) | Function to return the required sum ; Decrement N ; Driver code"
Solution in php:


"Sum of integers upto N with given unit digit | Function to return the required sum ; Driver code"
Solution in php:

< ? php function getSum ( $ n , $ d ) { $ sum = 0 ; while ( $ d <= $ n ) { $ sum += $ d ; $ d += 10 ; } return $ sum ; } $ n = 30 ; $ d = 3 ; echo ( getSum ( $ n , $ d ) ) ; ? >


"Sum of integers upto N with given unit digit | Function to return the required sum ; If the unit digit is d ; Driver code"
Solution in php:


"Sum of internal angles of a Polygon | Function to return the sum of internal angles of an n - sided polygon ; Driver code"
Solution in php:

< ? php function sumOfInternalAngles ( $ n ) { if ( $ n < 3 ) return 0 ; return ( ( $ n - 2 ) * 180 ) ; } $ n = 5 ; echo ( sumOfInternalAngles ( $ n ) ) ; ? >


"Sum of kth powers of first n natural numbers | A global array to store factorial ; Function to calculate the factorials of all the numbers upto k ; Function to return the binomial coefficient ; nCr = ( n ! * ( n - r ) ! ) / r ! ; Function to return the sum of kth powers of n natural numbers ; When j is unity ; Calculating sum ( n ^ 1 ) of unity powers of n ; storing sum ( n ^ 1 ) for sum ( n ^ 2 ) ; If k = 1 then temp is the result ; For finding sum ( n ^ k ) removing 1 and n * kCk from ( n + 1 ) ^ k ; Removing all kC2 * sum ( n ^ ( k - 2 ) ) + ... + kCk - 1 * ( sum ( n ^ ( k - ( k - 1 ) ) ; Storing the result for next sum of next powers of k ; Driver code"
Solution in php:


"Sum of lengths of all 12 edges of any rectangular parallelepiped | function to find the sum of all the edges of parallelepiped ; to calculate the length of one edge ; sum of all the edges of one side ; net sum will be equal to the summation of edges of all the sides ; initialize the area of three faces which has a common vertex"
Solution in php:


"Sum of matrix element where each elements is integer division of row and column | Return sum of matrix element where each element is division of its corresponding row and column . ; For each column . ; count the number of elements of each column . Initialize to i - 1 because number of zeroes are i - 1. ; For multiply ; Driver Code"
Solution in php:

< ? php function findSum ( $ n ) { $ ans = 0 ; $ temp = 0 ; $ num ; for ( $ i = 1 ; $ i <= $ n and $ temp < $ n ; $ i ++ ) { $ temp = $ i - 1 ; $ num = 1 ; while ( $ temp < $ n ) { if ( $ temp + $ i <= $ n ) $ ans += ( $ i * $ num ) ; else $ ans += ( ( $ n - $ temp ) * $ num ) ; $ temp += $ i ; $ num ++ ; } } return $ ans ; } $ N = 2 ; echo findSum ( $ N ) ; ? >


"Sum of matrix in which each element is absolute difference of its row and column numbers | Retuen the sum of matrix in which each element is absolute difference of its corresponding row and column number row ; Driver Code"
Solution in php:


"Sum of matrix in which each element is absolute difference of its row and column numbers | Retuen the sum of matrix in which each element is absolute difference of its corresponding row and column number row ; Generate matrix ; Compute sum ; Driver Code"
Solution in php:


"Sum of matrix in which each element is absolute difference of its row and column numbers | Return the sum of matrix in which each element is absolute difference of its corresponding row and column number row ; Driver Code"
Solution in php:

< ? php function findSum ( $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ i * ( $ n - $ i ) ; return 2 * $ sum ; } $ n = 3 ; echo findSum ( $ n ) ; ? >


"Sum of middle row and column in Matrix | PHP program to find sum of middle row and column in matrix ; loop for sum of row ; loop for sum of column ; Driver function"
Solution in php:

 < ? php function middlesum ( $ mat , $ n ) { $ row_sum = 0 ; $ col_sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ row_sum += $ mat [ $ n / 2 ] [ $ i ] ; echo " Sum ▁ of ▁ middle ▁ row ▁ = ▁ " , $ row_sum , "
 " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ col_sum += $ mat [ $ i ] [ $ n / 2 ] ; echo " Sum ▁ of ▁ middle ▁ column ▁ = ▁ " , $ col_sum ; } $ mat = array ( array ( 2 , 5 , 7 ) , array ( 3 , 7 , 2 ) , array ( 5 , 6 , 9 ) ) ; middlesum ( $ mat , 3 ) ; ? >


"Sum of minimum absolute difference of each array element | function to find the sum of minimum absolute difference ; sort the given array ; initialize sum ; min absolute difference for the 1 st array element ; min absolute difference for the last array element ; find min absolute difference for rest of the array elements and add them to sum ; required sum ; Driver Code"
Solution in php:

< ? php function sumOfMinAbsDifferences ( $ arr , $ n ) { sort ( $ arr ) ; sort ( $ arr , $ n ) ; $ sum = 0 ; $ sum += abs ( $ arr [ 0 ] - $ arr [ 1 ] ) ; $ sum += abs ( $ arr [ $ n - 1 ] - $ arr [ $ n - 2 ] ) ; for ( $ i = 1 ; $ i < $ n - 1 ; $ i ++ ) $ sum += min ( abs ( $ arr [ $ i ] - $ arr [ $ i - 1 ] ) , abs ( $ arr [ $ i ] - $ arr [ $ i + 1 ] ) ) ; return $ sum ; } $ arr = array ( 5 , 10 , 1 , 4 , 8 , 7 ) ; $ n = sizeof ( $ arr ) ; echo " Sum = " ? >


"Sum of minimum difference between consecutive elements of an array | function to find minimum sum of difference of consecutive element ; ul to store upper limit ll to store lower limit ; storethe lower range in ll and upper range in ul ; initialize the answer with 0 ; iterate for all ranges ; case 1 , in this case the difference will be 0 ; change upper limit and lower limit ; case 2 ; store the difference ; case 3 ; store the difference ; array of range"
Solution in php:

< ? php function solve ( $ v , $ n ) { $ ans ; $ ul ; $ ll ; $ first = 0 ; $ second = 1 ; $ ll = $ v [ 0 ] [ $ first ] ; $ ul = $ v [ 0 ] [ $ second ] ; $ ans = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( ( $ v [ $ i ] [ $ first ] <= $ ul and $ v [ $ i ] [ $ first ] >= $ ll ) or ( $ v [ $ i ] [ $ second ] >= $ ll and $ v [ $ i ] [ $ second ] <= $ ul ) ) { if ( $ v [ $ i ] [ $ first ] > $ ll ) { $ ll = $ v [ $ i ] [ $ first ] ; } if ( $ v [ $ i ] [ $ second ] < $ ul ) { $ ul = $ v [ $ i ] [ $ second ] ; } } else if ( $ v [ $ i ] [ $ first ] > $ ul ) { $ ans += abs ( $ ul - $ v [ $ i ] [ $ first ] ) ; $ ul = $ v [ $ i ] [ $ first ] ; $ ll = $ v [ $ i ] [ $ first ] ; } else if ( $ v [ $ i ] [ $ second ] < $ ll ) { $ ans += abs ( $ ll - $ v [ $ i ] [ $ second ] ) ; $ ul = $ v [ $ i ] [ $ second ] ; $ ll = $ v [ $ i ] [ $ second ] ; } } return $ ans ; } $ v = array ( array ( 1 , 3 ) , array ( 2 , 5 ) , array ( 6 , 8 ) , array ( 1 , 2 ) , array ( 2 , 3 ) ) ; $ n = 5 ; echo ( solve ( $ v , $ n ) ) ; ? >


"Sum of minimum element of all sub | Function to find the sum of minimum of all subsequence ; Driver code"
Solution in php:

< ? php function findMinSum ( $ arr , $ n ) { $ occ1 = ( $ n ) ; $ occ = $ occ1 - 1 ; $ Sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ Sum += $ arr [ $ i ] * pow ( 2 , $ occ ) ; $ occ -= 1 ; } return $ Sum ; } $ arr = array ( 1 , 2 , 4 , 5 ) ; $ n = count ( $ arr ) ; echo findMinSum ( $ arr , $ n ) ; ? >


"Sum of minimum element of all subarrays of a sorted array | Function to find the sum of minimum of all subarrays ; Driver code"
Solution in php:

< ? php function findMinSum ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] * ( $ n - $ i ) ; return $ sum ; } $ arr = array ( 3 , 5 , 7 , 8 ) ; $ n = count ( $ arr ) ; echo findMinSum ( $ arr , $ n ) ; ? >


"Sum of multiples of A and B less than N | PHP program to find the sum of all multiples of A and B below N ; Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of A and B below N ; Since , we need the sum of multiples less than N ; common factors of A and B ; Driver code"
Solution in php:

< ? php function __gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return __gcd ( $ b , $ a % $ b ) ; } function sumAP ( $ n , $ d ) { $ n = ( int ) ( $ n / $ d ) ; return ( $ n ) * ( 1 + $ n ) * $ d / 2 ; } function sumMultiples ( $ A , $ B , $ n ) { $ n -- ; $ common = ( int ) ( ( $ A * $ B ) / __gcd ( $ A , $ B ) ) ; return sumAP ( $ n , $ A ) + sumAP ( $ n , $ B ) - sumAP ( $ n , $ common ) ; } $ n = 100 ; $ A = 5 ; $ B = 10 ; echo " Sum = " ? >


"Sum of multiplication of triplet of divisors of a number | PHP implementation of the approach ; Global array declaration ; Function to find the sum of multiplication of every triplet in the divisors of a number ; sum1 [ x ] represents the sum of all the divisors of x ; Adding i to sum1 [ j ] because i is a divisor of j ; sum2 [ x ] represents the sum of all the divisors of x ; Here i is divisor of j and sum1 [ j ] - i represents sum of all divisors of j which do not include i so we add i * ( sum1 [ j ] - i ) to sum2 [ j ] ; In the above implementation we have considered every pair two times so we have to divide every sum2 array element by 2 ; Here i is the divisor of j and we are trying to add the sum of multiplication of all triplets of divisors of j such that one of the divisors is i ; In the above implementation we have considered every triplet three times so we have to divide every sum3 array element by 3 ; Print the results ; Driver code ; Precomputing"
Solution in php:

< ? php $ max_Element = 1005 ; $ sum1 = array_fill ( 0 , $ max_Element , 0 ) ; $ sum2 = array_fill ( 0 , $ max_Element , 0 ) ; $ sum3 = array_fill ( 0 , $ max_Element , 0 ) ; function precomputation ( $ arr , $ n ) { global $ max_Element , $ sum3 , $ sum2 , $ sum1 ; for ( $ i = 1 ; $ i < $ max_Element ; $ i ++ ) for ( $ j = $ i ; $ j < $ max_Element ; $ j += $ i ) $ sum1 [ $ j ] += $ i ; for ( $ i = 1 ; $ i < $ max_Element ; $ i ++ ) for ( $ j = $ i ; $ j < $ max_Element ; $ j += $ i ) $ sum2 [ $ j ] += ( $ sum1 [ $ j ] - $ i ) * $ i ; for ( $ i = 1 ; $ i < $ max_Element ; $ i ++ ) $ sum2 [ $ i ] = ( int ) ( $ sum2 [ $ i ] / 2 ) ; for ( $ i = 1 ; $ i < $ max_Element ; $ i ++ ) for ( $ j = $ i ; $ j < $ max_Element ; $ j += $ i ) $ sum3 [ $ j ] += $ i * ( $ sum2 [ $ j ] - $ i * ( $ sum1 [ $ j ] - $ i ) ) ; for ( $ i = 1 ; $ i < $ max_Element ; $ i ++ ) $ sum3 [ $ i ] = ( int ) ( $ sum3 [ $ i ] / 3 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ sum3 [ $ arr [ $ i ] ] . " ▁ " ; } $ arr = array ( 9 , 5 , 6 ) ; $ n = count ( $ arr ) ; precomputation ( $ arr , $ n ) ; ? >


"Sum of n digit numbers divisible by a given number | Returns sum of n digit numbers divisible by ' number ' ; compute the first and last term ; sum of number which having n digit and divisible by number ; Driver code"
Solution in php:

< ? php function totalSumDivisibleByNum ( $ n , $ number ) { $ firstnum = pow ( 10 , $ n - 1 ) ; $ lastnum = pow ( 10 , $ n ) ; $ sum = 0 ; for ( $ i = $ firstnum ; $ i < $ lastnum ; $ i ++ ) if ( $ i % $ number == 0 ) $ sum += $ i ; return $ sum ; } $ n = 3 ; $ num = 7 ; echo totalSumDivisibleByNum ( $ n , $ num ) , " " ; ? >


"Sum of n digit numbers divisible by a given number | find the Sum of having n digit and divisible by the number ; compute the first and last term ; first number which is divisible by given number ; last number which is divisible by given number ; total divisible number ; return the total sum ; Driver Code"
Solution in php:

< ? php function totalSumDivisibleByNum ( $ digit , $ number ) { $ firstnum = pow ( 10 , $ digit - 1 ) ; $ lastnum = pow ( 10 , $ digit ) ; $ firstnum = ( $ firstnum - $ firstnum % $ number ) + $ number ; $ lastnum = ( $ lastnum - $ lastnum % $ number ) ; $ count = ( ( $ lastnum - $ firstnum ) / $ number + 1 ) ; return ( ( $ lastnum + $ firstnum ) * $ count ) / 2 ; } $ n = 3 ; $ number = 7 ; echo totalSumDivisibleByNum ( $ n , $ number ) ; ? >


"Sum of nth terms of Modified Fibonacci series made by every pair of two arrays | PHP program to find sum of n - th terms of a Fibonacci like series formed using first two terms of two arrays . ; if sum of first term is required ; if sum of second term is required ; fibonacci series used to find the nth term of every series ; as every b [ i ] term appears m times and every a [ i ] term also appears m times ; m is the size of the array"
Solution in php:


"Sum of numbers from 1 to N which are in Lucas Sequence | Function to return the required sum ; Generate lucas number and keep on adding them ; Driver code"
Solution in php:

< ? php function LucasSum ( $ N ) { $ sum = 0 ; $ a = 2 ; $ b = 1 ; $ c ; $ sum += $ a ; while ( $ b <= $ N ) { $ sum += $ b ; $ c = $ a + $ b ; $ a = $ b ; $ b = $ c ; } return $ sum ; } $ N = 20 ; echo ( LucasSum ( $ N ) ) ; ? >


"Sum of numbers with exactly 2 bits set | To calculate sum of numbers ; Find numbers whose 2 bits are set ; If number is greater then n we don 't include this in sum ; Return sum of numbers ; Driver Code"
Solution in php:


"Sum of numbers with exactly 2 bits set | To count number of set bits ; To calculate sum of numbers ; To count sum of number whose 2 bit are set ; Driver Code"
Solution in php:

< ? php function countSetBits ( $ n ) { $ count = 0 ; while ( $ n ) { $ n &= ( $ n - 1 ) ; $ count ++ ; } return $ count ; } function findSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) if ( countSetBits ( $ i ) == 2 ) $ sum += $ i ; return $ sum ; } $ n = 10 ; echo findSum ( $ n ) ; ? >


"Sum of pairwise products | Efficient PHP program to find sum of given series . ; Sum of multiples of 1 is 1 * ( 1 + 2 + . . ) ; Adding sum of multiples of numbers other than 1 , starting from 2. ; Subtract previous number from current multiple . ; For example , for 2 , we get sum as ( 2 + 3 + 4 + ... . ) * 2 ; Driver code"
Solution in php:

< ? php function findSum ( $ n ) { $ multiTerms = ( int ) ( $ n * ( $ n + 1 ) / 2 ) ; $ sum = $ multiTerms ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ multiTerms = $ multiTerms - ( $ i - 1 ) ; $ sum = $ sum + $ multiTerms * $ i ; } return $ sum ; } $ n = 5 ; echo findSum ( $ n ) ; ? >


"Sum of pairwise products | Simple PHP program to find sum of given series . ; Driver Code"
Solution in php:

< ? php function findSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) for ( $ j = $ i ; $ j <= $ n ; $ j ++ ) $ sum = $ sum + $ i * $ j ; return $ sum ; } $ n = 5 ; echo findSum ( $ n ) ; ? >


"Sum of product of consecutive Binomial Coefficients | PHP Program to find sum of product of consecutive Binomial Coefficient . ; Find the binomial coefficient up to nth term ; $C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return the sum of the product of consecutive binomial coefficient . ; Driver Code"
Solution in php:

< ? php $ MAX = 100 ; function binomialCoeff ( $ n , $ k ) { $ C = array_fill ( 0 , ( $ k + 1 ) , 0 ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = min ( $ i , $ k ) ; $ j > 0 ; $ j -- ) $ C [ $ j ] = $ C [ $ j ] + $ C [ $ j - 1 ] ; } return $ C [ $ k ] ; } function sumOfproduct ( $ n ) { return binomialCoeff ( 2 * $ n , $ n - 1 ) ; } $ n = 3 ; echo sumOfproduct ( $ n ) ; ? >


"Sum of product of consecutive Binomial Coefficients | PHP Program to find sum of product of consecutive Binomial Coefficient . ; Find the binomial coefficient upto nth term ; $C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return the sum of the product of consecutive binomial coefficient . ; finding the sum of product of consecutive coefficient . ; Driver Code"
Solution in php:

< ? php $ MAX = 100 ; function binomialCoeff ( $ C , $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = min ( $ i , $ n ) ; $ j > 0 ; $ j -- ) $ C [ $ j ] = $ C [ $ j ] + $ C [ $ j - 1 ] ; } return $ C ; } function sumOfproduct ( $ n ) { global $ MAX ; $ sum = 0 ; $ C = array_fill ( 0 , $ MAX , 0 ) ; $ C = binomialCoeff ( $ C , $ n ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ sum += $ C [ $ i ] * $ C [ $ i + 1 ] ; return $ sum ; } $ n = 3 ; echo sumOfproduct ( $ n ) ; ? >


"Sum of product of r and rth Binomial Coefficient ( r * nCr ) | PHP Program to find sum of product of r and rth Binomial Coefficient i . e summation r * nCr ; Return the first n term of binomial coefficient . ; $C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return summation of r * nCr ; finding the first n term of binomial coefficient ; Iterate a loop to find the sum . ; Driver Code"
Solution in php:

< ? php $ MAX = 100 ; function binomialCoeff ( $ n , & $ C ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = min ( $ i , $ n ) ; $ j > 0 ; $ j -- ) $ C [ $ j ] = $ C [ $ j ] + $ C [ $ j - 1 ] ; } } function summation ( $ n ) { global $ MAX ; $ C = array_fill ( 0 , $ MAX , 0 ) ; binomialCoeff ( $ n , $ C ) ; $ sum = 0 ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ sum += ( $ i * $ C [ $ i ] ) ; return $ sum ; } $ n = 2 ; echo summation ( $ n ) ; ? >


"Sum of product of r and rth Binomial Coefficient ( r * nCr ) | Return summation of r * nCr ; Driver Code"
Solution in php:

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


"Sum of product of x and y such that floor ( n / x ) = y | Return the sum of natural number in a range . ; n * ( n + 1 ) / 2. ; Return the sum of product x * y . ; Iterating i from 1 to sqrt ( n ) ; Finding the upper limit . ; Finding the lower limit . ; Driven Code"
Solution in php:

 < ? php function sumOfRange ( $ a , $ b ) { $ i = ( $ a * ( $ a + 1 ) ) >> 1 ; $ j = ( $ b * ( $ b + 1 ) ) >> 1 ; return ( $ i - $ j ) ; } function sumofproduct ( $ n ) { $ sum = 0 ; $ root = sqrt ( $ n ) ; for ( $ i = 1 ; $ i <= $ root ; $ i ++ ) { $ up = ( int ) ( $ n / $ i ) ; $ low = max ( ( int ) ( $ n / ( $ i + 1 ) ) , $ root ) ; $ sum += ( $ i * sumOfRange ( $ up , $ low ) ) ; $ sum += ( $ i * ( int ) ( $ n / $ i ) ) ; } return $ sum ; } $ n = 10 ; echo sumofproduct ( $ n ) . "
 " ; ? >


"Sum of product of x and y such that floor ( n / x ) = y | Return the sum of product x * y . ; Iterating x from 1 to n ; Finding y = n / x . ; Adding product of x and y to answer . ; Driver Code"
Solution in php:

< ? php function sumofproduct ( $ n ) { $ ans = 0 ; for ( $ x = 1 ; $ x <= $ n ; $ x ++ ) { $ y = ( int ) ( $ n / $ x ) ; $ ans += ( $ y * $ x ) ; } return $ ans ; } $ n = 10 ; echo sumofproduct ( $ n ) ; ? >


"Sum of range in a series of first odd then even natural numbers | Function that returns sum in the range 1 to x in the sequence 1 3 5 7. ... . N 2 4 6. . . N - 1 ; number of odd numbers ; number of extra even numbers required ; Driver code"
Solution in php:

< ? php function sumTillX ( $ x , $ n ) { $ odd = ceil ( $ n / 2.0 ) ; if ( $ x <= $ odd ) return $ x * $ x ; $ even = $ x - $ odd ; return ( ( $ odd * $ odd ) + ( $ even * $ even ) + $ even ) ; } function rangeSum ( $ N , $ L , $ R ) { return sumTillX ( $ R , $ N ) - sumTillX ( $ L - 1 , $ N ) ; } $ N = 10 ; $ L = 1 ; $ R = 6 ; echo ( rangeSum ( $ N , $ L , $ R ) ) ; ? >


"Sum of series ( n / 1 ) + ( n / 2 ) + ( n / 3 ) + ( n / 4 ) + ... ... . + ( n / n ) | function to find sum of series ; Driver code"
Solution in php:

< ? php function sum ( $ n ) { $ root = intval ( sqrt ( $ n ) ) ; $ ans = 0 ; for ( $ i = 1 ; $ i <= $ root ; $ i ++ ) $ ans += intval ( $ n / $ i ) ; $ ans = ( 2 * $ ans ) - ( $ root * $ root ) ; return $ ans ; } $ n = 35 ; echo ( sum ( $ n ) ) ; ? >


"Sum of series 1 * 1 ! + 2 * 2 ! + …… . . + n * n ! | PHP program to find sum of the series . ; Function to calculate required series ; Driver code"
Solution in php:

< ? php function factorial ( $ n ) { $ res = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res = $ res * $ i ; return $ res ; } function calculateSeries ( $ n ) { return factorial ( $ n + 1 ) - 1 ; } $ n = 3 ; echo calculateSeries ( $ n ) ; ? >


"Sum of series 1 * 1 * 2 ! + 2 * 2 * 3 ! + …… . . + n * n * ( n + 1 ) ! | PHP program to find sum of the series . ; Function to calculate required series ; Driver code"
Solution in php:

< ? php function factorial ( $ n ) { $ res = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res = $ res * $ i ; return $ res ; } function calculateSeries ( $ n ) { return 2 + ( $ n * $ n + $ n - 2 ) * factorial ( $ n + 1 ) ; } $ n = 3 ; echo calculateSeries ( $ n ) ; ? >


"Sum of series 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + . . . + ( 2 * n | Function that find sum of series . ; Formula to find sum of series . ; Driver code"
Solution in php:

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


"Sum of series 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + . . . + ( 2 * n | Function to find sum of series . ; Driver code"
Solution in php:

< ? php function sumOfSeries ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum = $ sum + ( 2 * $ i - 1 ) * ( 2 * $ i - 1 ) ; return $ sum ; } $ n = 10 ; echo ( sumOfSeries ( $ n ) ) ; ? >


"Sum of series 2 / 3 | Function to find sum of series up - to n terms ; initializing counter by 1 ; variable to calculate result ; while loop until nth term is not reached ; boolean type variable for checking validation ; Driver Code"
Solution in php:

< ? php function seriesSum ( $ n ) { $ i = 1 ; $ res = 0.0 ; $ sign = true ; while ( $ n > 0 ) { $ n -- ; if ( $ sign ) { $ sign = ! $ sign ; $ res = $ res + ( double ) ++ $ i / ++ $ i ; } else { $ sign = ! $ sign ; $ res = $ res - ( double ) ++ $ i / ++ $ i ; } } return $ res ; } $ n = 5 ; echo ( seriesSum ( $ n ) ) ; ? >


"Sum of square of first n even numbers | Efficient PHP method to find sum of square of first n even numbers . ; Driver code"
Solution in php:

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


"Sum of square of first n odd numbers | Efficient PHP method to find sum of square of first n odd numbers . ; driver code"
Solution in php:

< ? php < ? php function squareSum ( $ n ) { return $ n * ( 4 * $ n * $ n - 1 ) / 3 ; } echo squareSum ( 8 ) ; ? >


"Sum of square | Function to find sum of sum of square of first n natural number ; Driver Code"
Solution in php:


"Sum of squares of Fibonacci numbers | Function to calculate sum of squares of Fibonacci numbers ; Initialize result ; Add remaining terms ; Driver Code"
Solution in php:

< ? php function calculateSquareSum ( $ n ) { if ( $ n <= 0 ) return 0 ; $ fibo [ 0 ] = 0 ; $ fibo [ 1 ] = 1 ; $ sum = ( $ fibo [ 0 ] * $ fibo [ 0 ] ) + ( $ fibo [ 1 ] * $ fibo [ 1 ] ) ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ fibo [ $ i ] = $ fibo [ $ i - 1 ] + $ fibo [ $ i - 2 ] ; $ sum += ( $ fibo [ $ i ] * $ fibo [ $ i ] ) ; } return $ sum ; } $ n = 6 ; echo " Sum ▁ of ▁ squares ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : ▁ " , calculateSquareSum ( $ n ) ; ? >


"Sum of squares of Fibonacci numbers | PHP Program to find sum of squares of Fibonacci numbers in O ( Log n ) time . ; Create an array for memoization ; Returns n 'th Fibonacci number using table f[] ; Base cases ; If fib ( n ) is already computed ; Applying above formula [ Note value n & 1 is 1 if n is odd , else 0 ] . ; Function to calculate sum of squares of Fibonacci numbers ; Driver Code"
Solution in php:

< ? php $ MAX = 1000 ; global $ f ; $ f = array_fill ( 0 , $ MAX , 0 ) ; function fib ( $ n ) { if ( $ n == 0 ) return 0 ; if ( $ n == 1 $ n == 2 ) return ( $ f [ $ n ] = 1 ) ; $ k = ( $ n & 1 ) ? ( $ n + 1 ) / 2 : $ n / 2 ; $ f [ $ n ] = ( $ n & 1 ) ? ( fib ( $ k ) * fib ( $ k ) + fib ( $ k - 1 ) * fib ( $ k - 1 ) ) : ( 2 * fib ( $ k - 1 ) + fib ( $ k ) ) * fib ( $ k ) ; return $ f [ $ n ] ; } function calculateSumOfSquares ( $ n ) { return fib ( $ n ) * fib ( $ n + 1 ) ; } $ n = 6 ; echo " Sum ▁ of ▁ Squares ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : ▁ " ; echo calculateSumOfSquares ( $ n ) ; ? >


"Sum of squares of binomial coefficients | Return the sum of square of binomial coefficient ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Finding the sum of square of binomial coefficient . ; Driven Program"
Solution in php:


"Sum of squares of binomial coefficients | function to return product of number from start to end . ; Return the sum of square of binomial coefficient ; Driver Code"
Solution in php:

 < ? php function factorial ( $ start , $ end ) { $ res = 1 ; for ( $ i = $ start ; $ i <= $ end ; $ i ++ ) $ res *= $ i ; return $ res ; } function sumofsquare ( $ n ) { return factorial ( $ n + 1 , 2 * $ n ) / factorial ( 1 , $ n ) ; } $ n = 4 ; echo sumofsquare ( $ n ) , "
 " ; ? >


"Sum of squares of first n natural numbers | Function to calculate sum ; Driver code"
Solution in php:

< ? php function summation ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum += ( $ i * $ i ) ; return $ sum ; } $ n = 2 ; echo summation ( $ n ) ; ? >


"Sum of squares of first n natural numbers | Return the sum of square of first n natural numbers ; Driver Code"
Solution in php:

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


"Sum of squares of first n natural numbers | Return the sum of square of first n natural numbers ; Iterate i from 1 and n finding square of i and add to sum . ; Driven Code"
Solution in php:

< ? php function squaresum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum += ( $ i * $ i ) ; return $ sum ; } $ n = 4 ; echo ( squaresum ( $ n ) ) ; ? >


"Sum of sum of all subsets of a set formed by first N natural numbers | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; $x = $x % $p ; Update x if it is more than or equal to p ; If y is odd , multiply x with the result ; y must be even now $y = $y >> 1 ; y = y / 2 ; function to find ff ( n ) ; In formula n is starting from zero ; calculate answer using formula 2 ^ n * ( n ^ 2 + n + 2 ) - 1 ; whenever answer is greater than or equals to mod then modulo it . ; adding modulo while subtraction is very necessary otherwise it will cause wrong answer ; Driver code ; function call"
Solution in php:

 < ? php function power ( $ x , $ y , $ p ) { while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function check ( $ n ) { $ mod = 1e9 + 7 ; $ n -- ; $ ans = $ n * $ n ; if ( $ ans >= $ mod ) $ ans %= $ mod ; $ ans += $ n + 2 ; if ( $ ans >= $ mod ) $ ans %= $ mod ; $ ans = ( power ( 2 , $ n , $ mod ) % $ mod * $ ans % $ mod ) % $ mod ; $ ans = ( $ ans - 1 + $ mod ) % $ mod ; return $ ans ; } $ n = 4 ; echo check ( $ n ) . "
 " ; ? >


"Sum of the Series 1 + x / 1 + x ^ 2 / 2 + x ^ 3 / 3 + . . + x ^ n / n | Code to print the sum of the series ; Driver code"
Solution in php:

< ? php function sum ( $ x , $ n ) { $ i ; $ total = 1.0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ total = $ total + ( pow ( $ x , $ i ) / $ i ) ; return $ total ; } $ x = 2 ; $ n = 5 ; echo ( sum ( $ x , $ n ) ) ; ? >


"Sum of the Series 1 + x / 1 + x ^ 2 / 2 + x ^ 3 / 3 + . . + x ^ n / n | code to print the sum of the series ; Driver code"
Solution in php:

< ? php function sum ( $ x , $ n ) { $ i ; $ total = 1.0 ; $ multi = $ x ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ total = $ total + $ multi / $ i ; $ multi = $ multi * $ x ; } return $ total ; } $ x = 2 ; $ n = 5 ; echo ( sum ( $ x , $ n ) ) ; ? >


"Sum of the Series 1 / ( 1 * 2 ) + 1 / ( 2 * 3 ) + 1 / ( 3 * 4 ) + 1 / ( 4 * 5 ) + . . . . . | function to find the sum of given series ; Computing sum term by term ; Driver Code"
Solution in php:

< ? php function sumOfTheSeries ( $ n ) { $ sum = 0.0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum += 1.0 / ( $ i * ( $ i + 1 ) ) ; return $ sum ; } $ n = 10 ; echo sumOfTheSeries ( $ n ) ; ? >


"Sum of the alphabetical values of the characters of a string | Function to find string score ; Driver code"
Solution in php:

 < ? php function strScore ( $ str , $ s , $ n ) { $ score = 0 ; $ index ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] == $ s ) { for ( $ j = 0 ; $ j < strlen ( $ s ) ; $ j ++ ) $ score += ( ord ( $ s [ $ j ] ) - ord ( ' a ' ) ) + 1 ; $ index = ( $ i + 1 ) ; break ; } } $ score = $ score * $ index ; return $ score ; } $ str = array ( " sahil " , " shashanak " , " sanjit " , " abhinav " , " mohit " ) ; $ s = " abhinav " ; $ n = sizeof ( $ str ) ; $ score = strScore ( $ str , $ s , $ n ) ; echo $ score , "
 " ; ? >


"Sum of the digits of a number N written in all bases from 2 to N / 2 | Function to calculate the sum of the digits of n in the given base ; Sum of digits ; Digit of n in the given base ; Add the digit ; Function to calculate the sum of digits of n in bases from 2 to n / 2 ; to store digit sum in all bases ; function call for multiple bases ; Driver Code"
Solution in php:

< ? php function solve ( $ n , $ base ) { $ sum = 0 ; while ( $ n > 0 ) { $ remainder = $ n % $ base ; $ sum += $ remainder ; $ n = $ n / $ base ; } return $ sum ; } function SumsOfDigits ( $ n ) { $ sum = 0 ; for ( $ base = 2 ; $ base <= $ n / 2 ; ++ $ base ) $ sum += solve ( $ n , $ base ) ; echo $ sum ; } $ n = 8 ; SumsOfDigits ( $ n ) ; ? >


"Sum of the first N terms of the series 2 , 6 , 12 , 20 , 30. ... | Function to calculate the sum ; number of terms to be included in the sum ; find the Sn"
Solution in php:


"Sum of the first N terms of the series 5 , 12 , 23 , 38. ... | Function to calculate the sum ; number of terms to be included in sum ; find the Sn"
Solution in php:


"Sum of the natural numbers ( up to N ) whose modulo with K yield R | Function to return the sum ; If current number gives R as the remainder on dividing by K ; Update the sum ; Return the sum ; Driver code"
Solution in php:


"Sum of the numbers upto N that are divisible by 2 or 5 | Function to find the sum ; sum2 is sum of numbers divisible by 2 ; sum5 is sum of number divisible by 5 ; sum10 of numbers divisible by 2 and 5 ; Driver Code"
Solution in php:


"Sum of the sequence 2 , 22 , 222 , ... ... ... | function which return the the sum of series ; Driver Code"
Solution in php:

< ? php function sumOfSeries ( $ n ) { return 0.0246 * ( pow ( 10 , $ n ) - 1 - ( 9 * $ n ) ) ; } $ n = 3 ; echo ( sumOfSeries ( $ n ) ) ; ? >


"Sum of the series ( 1 * 2 ) + ( 2 * 3 ) + ( 3 * 4 ) + ... ... upto n terms | Function to return sum ; Driver code"
Solution in php:

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


"Sum of the series 0.6 , 0.06 , 0.006 , 0.0006 , ... to n terms | function which return the the sum of series ; Driver code"
Solution in php:

< ? php function sumOfSeries ( $ n ) { return ( 0.666 ) * ( 1 - 1 / pow ( 10 , $ n ) ) ; } $ n = 2 ; echo ( sumOfSeries ( $ n ) ) ; ? >


"Sum of the series 1 + ( 1 + 2 ) + ( 1 + 2 + 3 ) + ( 1 + 2 + 3 + 4 ) + ... ... + ( 1 + 2 + 3 + 4 + ... + n ) | Function to find sum of given series ; Driver Code"
Solution in php:

< ? php function sumOfSeries ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) for ( $ j = 1 ; $ j <= $ i ; $ j ++ ) $ sum += $ j ; return $ sum ; } $ n = 10 ; echo ( sumOfSeries ( $ n ) ) ; ? >


"Sum of the series 1 + ( 1 + 3 ) + ( 1 + 3 + 5 ) + ( 1 + 3 + 5 + 7 ) + Ã ¢ â ‚¬¦ Ã ¢ â ‚¬¦ + ( 1 + 3 + 5 + 7 + Ã ¢ â ‚¬¦ + ( 2 n | functionn to find the sum of the given series ; required sum ; Driver Code"
Solution in php:

< ? php function sumOfTheSeries ( $ n ) { return ( $ n * ( $ n + 1 ) / 2 ) * ( 2 * $ n + 1 ) / 3 ; } $ n = 5 ; echo " Sum ▁ = ▁ " . sumOfTheSeries ( $ n ) ; ? >


"Sum of the series 1 , 2 , 4 , 3 , 5 , 7 , 9 , 6 , 8 , 10 , 11 , 13. . till N | Function to find the sum of first N odd numbers ; Function to find the sum of first N even numbers ; Function to overall find the sum of series ; Initial odd numbers ; Initial even numbers ; First power of 2 ; Check for parity for odd / even ; Counts the sum ; Get the minimum out of remaining num or power of 2 ; Decrease that much numbers from num ; If the segment has odd numbers ; Summate the odd numbers By exclusion ; Increase number of odd numbers ; If the segment has even numbers ; Summate the even numbers By exclusion ; Increase number of even numbers ; Next set of numbers ; Change parity for odd / even ; Driver code"
Solution in php:

< ? php function sumodd ( $ n ) { return ( $ n * $ n ) ; } function sumeven ( $ n ) { return ( $ n * ( $ n + 1 ) ) ; } function findSum ( $ num ) { $ sumo = 0 ; $ sume = 0 ; $ x = 1 ; $ cur = 0 ; $ ans = 0 ; while ( $ num > 0 ) { $ inc = min ( $ x , $ num ) ; $ num -= $ inc ; if ( $ cur == 0 ) { $ ans = $ ans + sumodd ( $ sumo + $ inc ) - sumodd ( $ sumo ) ; $ sumo += $ inc ; } else { $ ans = $ ans + sumeven ( $ sume + $ inc ) - sumeven ( $ sume ) ; $ sume += $ inc ; } $ x *= 2 ; $ cur ^= 1 ; } return $ ans ; } $ n = 4 ; echo findSum ( $ n ) ; ? >


"Sum of the series 1 , 3 , 6 , 10. . . ( Triangular Numbers ) | Function to find the sum of series ; Driver code"
Solution in php:

< ? php function seriesSum ( $ n ) { return ( $ n * ( $ n + 1 ) * ( $ n + 2 ) ) / 6 ; } $ n = 4 ; echo ( seriesSum ( $ n ) ) ; ? >


"Sum of the series 1 ^ 1 + 2 ^ 2 + 3 ^ 3 + ... . . + n ^ n using recursion | Recursive function to return the sum of the given series ; 1 ^ 1 = 1 ; Recursive call ; Driver code"
Solution in php:

< ? php function sum ( $ n ) { if ( $ n == 1 ) return 1 ; else return ( pow ( $ n , $ n ) + sum ( $ n - 1 ) ) ; } $ n = 2 ; echo ( sum ( $ n ) ) ; ? >


"Sum of the series 1.2 . 3 + 2.3 . 4 + ... + n ( n + 1 ) ( n + 2 ) | PHP program to find sum of the series 1.2 . 3 + 2.3 . 4 + 3.4 . 5 + ... ; Driver Code"
Solution in php:

< ? php function sumofseries ( $ n ) { $ res = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ res += ( $ i ) * ( $ i + 1 ) * ( $ i + 2 ) ; return $ res ; } echo sumofseries ( 3 ) ; ? >


"Sum of the series 1.2 . 3 + 2.3 . 4 + ... + n ( n + 1 ) ( n + 2 ) | function to calculate sum of series ; Driver Code"
Solution in php:

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


"Sum of the series 2 + ( 2 + 4 ) + ( 2 + 4 + 6 ) + ( 2 + 4 + 6 + 8 ) + â €¦ â €¦ + ( 2 + 4 + 6 + 8 + â €¦ . + 2 n ) | function to find the sum of the given series ; sum of 1 st n natural numbers ; sum of squares of 1 st n natural numbers ; required sum ; Driver Code"
Solution in php:

< ? php function sumOfTheSeries ( $ n ) { $ sum_n = ( $ n * ( $ n + 1 ) / 2 ) ; $ sum_sq_n = ( $ n * ( $ n + 1 ) / 2 ) * ( 2 * $ n + 1 ) / 3 ; return ( $ sum_n + $ sum_sq_n ) ; } $ n = 5 ; echo ( " Sum ▁ = ▁ " . sumOfTheSeries ( $ n ) ) ; ? >


"Sum of the series 2 + ( 2 + 4 ) + ( 2 + 4 + 6 ) + ( 2 + 4 + 6 + 8 ) + …… + ( 2 + 4 + 6 + 8 + … . + 2 n ) | function to find the sum of the given series ; first term of each i - th term ; next term ; required sum ; Driver program to test above"
Solution in php:


"Sum of the series 2 ^ 0 + 2 ^ 1 + 2 ^ 2 + ... . . + 2 ^ n | function to calculate sum of series ; initialize sum as 0 ; loop to calculate sum of series ; calculate 2 ^ i and add it to sum ; Driver code"
Solution in php:

< ? php function calculateSum ( $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum = $ sum + ( 1 << $ i ) ; } return $ sum ; } $ n = 10 ; echo " Sum ▁ of ▁ the ▁ series ▁ of ▁ " . " power ▁ 2 ▁ is ▁ : ▁ " , calculateSum ( $ n ) ; ? >


"Sum of the series 5 + 55 + 555 + . . up to n terms | function which return the the sum of series ; Driver code"
Solution in php:

< ? php function sumOfSeries ( $ n ) { return ( int ) ( 0.6172 * ( pow ( 10 , $ n ) - 1 ) - 0.55 * $ n ) ; } $ n = 2 ; echo ( sumOfSeries ( $ n ) ) ; ? >


"Sum of the series Kn + ( K ( n | Function to return sum ; Driver code"
Solution in php:

< ? php function sum ( $ k , $ n ) { $ sum = pow ( $ k , $ n + 1 ) - pow ( $ k - 1 , $ n + 1 ) ; return $ sum ; } $ n = 3 ; $ K = 3 ; echo sum ( $ K , $ n ) ;


"Sum of the series Kn + ( K ( n | Recursive C program to compute modular power ; Base cases ; If B is even ; If B is odd ; Function to return sum ; Driver code"
Solution in php:

< ? php function exponent ( $ A , $ B ) { if ( $ A == 0 ) return 0 ; if ( $ B == 0 ) return 1 ; if ( $ B % 2 == 0 ) { $ y = exponent ( $ A , $ B / 2 ) ; $ y = ( $ y * $ y ) ; } else { $ y = $ A ; $ y = ( $ y * exponent ( $ A , $ B - 1 ) ) ; } return $ y ; } function sum ( $ k , $ n ) { $ sum = exponent ( $ k , $ n + 1 ) - exponent ( $ k - 1 , $ n + 1 ) ; return $ sum ; } $ n = 3 ; $ K = 3 ; echo sum ( $ K , $ n ) ; ? >


"Sum of two large numbers | Function for finding sum of larger numbers ; Before proceeding further , make sure length of str2 is larger . ; Take an empty string for storing result ; Calculate length of both string ; Initially take carry zero ; Traverse from end of both strings ; Do school mathematics , compute sum of current digits and carry ; Add remaining digits of str2 [ ] ; Add remaining carry ; reverse resultant string ; Driver code"
Solution in php:


"Sum of two large numbers | Function for finding sum of larger numbers ; Before proceeding further , make sure length of str2 is larger . ; Take an empty string for storing result ; Calculate length of both string ; Reverse both of strings ; Do school mathematics , compute sum of current digits and carry ; Calculate carry for next step ; Add remaining digits of larger number ; Add remaining carry ; reverse resultant string ; Driver code"
Solution in php:


"Sum of width ( max and min diff ) of all Subsequences | PHP implementation of above approach ; Function to return sum of width of all subsets ; Sort the array ; Driver Code"
Solution in php:


"Summation of GCD of all the pairs up to N | PHP approach of finding sum of GCD of all pairs ; phi [ i ] stores euler totient function for i result [ j ] stores result for value j ; Precomputation of phi [ ] numbers . Refer link for details : https : goo . gl / LUqdtY ; Refer https : goo . gl / LUqdtY ; Precomputes result for all numbers till MAX ; Precompute all phi value ; Iterate throght all the divisors of i . ; Add summation of previous calculated sum ; Function to calculate sum of all the GCD pairs"
Solution in php:

 < ? php $ MAX = 100001 ; $ phi = array_fill ( 0 , $ MAX , 0 ) ; $ result = array_fill ( 0 , $ MAX , 0 ) ; function computeTotient ( ) { global $ MAX , $ phi ; $ phi [ 1 ] = 1 ; for ( $ i = 2 ; $ i < $ MAX ; $ i ++ ) { if ( ! $ phi [ $ i ] ) { $ phi [ $ i ] = $ i - 1 ; for ( $ j = ( $ i << 1 ) ; $ j < $ MAX ; $ j += $ i ) { if ( ! $ phi [ $ j ] ) $ phi [ $ j ] = $ j ; $ phi [ $ j ] = ( $ phi [ $ j ] / $ i ) * ( $ i - 1 ) ; } } } } function sumOfGcdPairs ( ) { global $ MAX , $ phi , $ result ; computeTotient ( ) ; for ( $ i = 1 ; $ i < $ MAX ; ++ $ i ) { for ( $ j = 2 ; $ i * $ j < $ MAX ; ++ $ j ) $ result [ $ i * $ j ] += $ i * $ phi [ $ j ] ; } for ( $ i = 2 ; $ i < $ MAX ; $ i ++ ) $ result [ $ i ] += $ result [ $ i - 1 ] ; } sumOfGcdPairs ( ) ; $ N = 4 ; echo " Summation ▁ of ▁ " . $ N . " ▁ = ▁ " . $ result [ $ N ] . "
 " ; $ N = 12 ; echo " Summation ▁ of ▁ " . $ N . " ▁ = ▁ " . $ result [ $ N ] . "
 " ; $ N = 5000 ; echo " Summation ▁ of ▁ " . $ N . " ▁ = ▁ " . $ result [ $ N ] . "
 " ; ? >


"Summing the sum series | Function to calculate twice of sum of first N natural numbers ; Function to calculate the terms of summing of sum series ; Driver Code"
Solution in php:


"Superperfect Number | Function to calculate sum of all divisors ; Final result of summation of divisors ; find all divisors which divides ' num ' ; if ' i ' is divisor of ' num ' ; if both divisors are same then add it only once else add both ; Returns true if n is Super Perfect else false . ; Find the sum of all divisors of number n ; Again find the sum of all divisors of n1 and check if sum is equal to n1 ; Driver code"
Solution in php:

 < ? php function divSum ( $ num ) { $ result = 0 ; for ( $ i = 1 ; $ i * $ i <= $ num ; ++ $ i ) { if ( $ num % $ i == 0 ) { if ( $ i == ( $ num / $ i ) ) $ result += $ i ; else $ result += ( $ i + $ num / $ i ) ; } } return $ result ; } function isSuperPerfect ( $ n ) { $ n1 = divSum ( $ n ) ; return ( 2 * $ n == divSum ( $ n1 ) ) ; } $ n = 16 ; $ hh = ( isSuperPerfect ( $ n ) ? " Yes
 " : " No
 " ) ; echo ( $ hh ) ; $ n = 6 ; $ hh = ( isSuperPerfect ( $ n ) ? " Yes
 " : " No
 " ) ; echo ( $ hh ) ; ? >


"Surd number | Returns true if x is Surd number ; Try all powers of i ; Driver code"
Solution in php:

< ? php function isSurd ( $ n ) { for ( $ i = 2 ; $ i * $ i <= $ n ; $ i ++ ) { $ j = $ i ; while ( $ j < $ n ) $ j = $ j * $ i ; if ( $ j == $ n ) return false ; } return true ; } $ n = 15 ; if ( isSurd ( $ n ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >


"Surface Area and Volume of Hexagonal Prism | Function to calculate Surface area ; Formula to calculate surface area ; Display surface area ; Function to calculate Volume ; formula to calculate Volume ; Display Volume ; Driver Code ; surface area function call ; volume function call"
Solution in php:

 < ? php function findSurfaceArea ( $ a , $ h ) { $ Area ; $ Area = 6 * $ a * $ h + 3 * sqrt ( 3 ) * $ a * $ a ; echo " Surface ▁ Area : ▁ " , $ Area , "
 " ; } function findVolume ( $ a , $ h ) { $ Volume ; $ Volume = 3 * sqrt ( 3 ) * $ a * $ a * $ h / 2 ; echo " Volume : ▁ " , $ Volume ; } $ a = 5 ; $ h = 10 ; findSurfaceArea ( $ a , $ h ) ; findVolume ( $ a , $ h ) ; ? >


"Swap all odd and even bits | Function to swap even and odd bits ; Get all even bits of x ; Get all odd bits of x ; Right shift even bits ; Left shift odd bits ; Combine even and odd bits ; 00010111 ; Output is 43 ( 00101011 )"
Solution in php:

< ? php function swapBits ( $ x ) { $ even_bits = $ x & 0xAAAAAAAA ; $ odd_bits = $ x & 0x55555555 ; $ even_bits >>= 1 ; $ odd_bits <<= 1 ; return ( $ even_bits $ odd_bits ) ; } $ x = 23 ; echo swapBits ( $ x ) ; ? >


"Swap bits in a given number | PHP Program to swap bits in a given number function returns the swapped bits ; Move all bits of first set to rightmost side ; Move all bits of second set to rightmost side ; XOR the two sets ; Put the xor bits back to their original positions ; XOR the ' xor ' with the original number so that the two sets are swapped ; Driver Code"
Solution in php:


"Swap every two bits in bytes | PHP program to swap every two bits in a byte . ; Extracting the high bit shift it to lowbit Extracting the low bit shift it to highbit ; Driver Code"
Solution in php:

< ? php function swapBitsInPair ( $ x ) { return ( ( $ x & 0b10101010 ) >> 1 ) | ( ( $ x & 0b01010101 ) << 1 ) ; } $ x = 4 ; echo swapBitsInPair ( $ x ) ; ? >


"Swap three variables without using temporary variable | Assign c ' s ▁ value ▁ to ▁ a , ▁ a ' s value to b and b 's value to c. ; Store XOR of all in a ; After this , b has value of a ; After this , c has value of b ; After this , a has value of c ; Driver code ; Calling Function"
Solution in php:

 < ? php function swapThree ( & $ a , & $ b , & $ c ) { $ a = $ a ^ $ b ^ $ c ; $ b = $ a ^ $ b ^ $ c ; $ c = $ a ^ $ b ^ $ c ; $ a = $ a ^ $ b ^ $ c ; } $ a = 10 ; $ b = 20 ; $ c = 30 ; echo " Before ▁ swapping ▁ a ▁ = ▁ " , $ a , " , ▁ b ▁ = ▁ " , $ b , " , ▁ c ▁ = ▁ " , $ c , "
 " ; swapThree ( $ a , $ b , $ c ) ; echo " After ▁ swapping ▁ a ▁ = ▁ " , $ a , " , ▁ b ▁ = ▁ " , $ b , " , ▁ c ▁ = ▁ " , $ c , "
 " ; ? >


"Swapping four variables without temporary variable | PHP program to swap 4 variables without using temporary variable ; swapping a and b variables ; swapping b and c variables ; swapping c and d variables ; initialising variables ; Function call"
Solution in php:

 < ? php function swap ( $ a , $ b , $ c , $ d ) { $ a = $ a + $ b ; $ b = $ a - $ b ; $ a = $ a - $ b ; $ b = $ b + $ c ; $ c = $ b - $ c ; $ b = $ b - $ c ; $ c = $ c + $ d ; $ d = $ c - $ d ; $ c = $ c - $ d ; echo " values ▁ after ▁ swapping ▁ are ▁ : ▁ " , "
 " ; echo " a = " ▁ , ▁ $ a ▁ , " " ;
 	 echo ▁ " b = " ▁ , ▁ $ b ▁ , " " ;
 	 echo ▁ " c = " ▁ , $ c ▁ , " " ;
 	 echo ▁ " d = " ▁ , ▁ $ d ▁ , " " } $ a = 1 ; $ b = 2 ; $ c = 3 ; $ d = 4 ; echo " Values ▁ before ▁ swapping ▁ are ▁ : " , "
 " ; echo " a = " ▁ , ▁ $ a ▁ , " " ;
 	 echo ▁ " b = " ▁ , $ b , " " ;
 	 echo ▁ " c = " ▁ , ▁ $ c ▁ , " " ;
 	 echo ▁ " d = " ▁ , ▁ $ d ▁ , " " , " " swap ( $ a , $ b , $ c , $ d ) ; ? >


"Sylvester 's sequence | PHP program to print terms of Sylvester 's sequence ; To store the product . ; To store the current number . ; Loop till n . ; Driver Code"
Solution in php:


"Tail Recursion | A NON - tail - recursive function . The function is not tail recursive because the value returned by fact ( n - 1 ) is used in fact ( n ) and call to fact ( n - 1 ) is not the last thing done by fact ( n ) ; Driver Code"
Solution in php:

< ? php function fact ( $ n ) { if ( $ n == 0 ) return 1 ; return $ n * fact ( $ n - 1 ) ; } echo fact ( 5 ) ; ? >


"Tail Recursion | A tail recursive function to calculate factorial ; A wrapper over factTR ; Driver program to test above function"
Solution in php:

< ? php function factTR ( $ n , $ a ) { if ( $ n == 0 ) return $ a ; return factTR ( $ n - 1 , $ n * $ a ) ; } function fact ( $ n ) { return factTR ( $ n , 1 ) ; } echo fact ( 5 ) ; ? >


"Temple Offerings | Returns minimum offerings required ; Go through all templs one by one ; Go to left while height keeps increasing ; Go to right while height keeps increasing ; This temple should offer maximum of two values to follow the rule . ; Driver code"
Solution in php:

 < ? php function offeringNumber ( $ n , $ templeHeight ) { for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ left = 0 ; $ right = 0 ; for ( $ j = $ i - 1 ; $ j >= 0 ; -- $ j ) { if ( $ templeHeight [ $ j ] < $ templeHeight [ $ j + 1 ] ) ++ $ left ; else break ; } for ( $ j = $ i + 1 ; $ j < $ n ; ++ $ j ) { if ( $ templeHeight [ $ j ] < $ templeHeight [ $ j - 1 ] ) ++ $ right ; else break ; } $ sum += max ( $ right , $ left ) + 1 ; } return $ sum ; } $ arr1 = array ( 1 , 2 , 2 ) ; echo offeringNumber ( 3 , $ arr1 ) , "
 " ; $ arr2 = array ( 1 , 4 , 3 , 6 , 2 , 1 ) ; echo offeringNumber ( 6 , $ arr2 ) , "
 " ; ? >


"Tetrahedral Numbers | Function to find Tetrahedral Number ; Driver Code"
Solution in php:

< ? php function tetrahedralNumber ( $ n ) { return ( $ n * ( $ n + 1 ) * ( $ n + 2 ) ) / 6 ; } $ n = 5 ; echo tetrahedralNumber ( $ n ) ; ? >


"Thabit number | Utility function to check power of two ; function to check if the given number is Thabit Number ; Add 1 to the number ; Divide the number by 3 ; Check if the given number is power of 2 ; Driver Code ; Check if number is thabit number"
Solution in php:

< ? php function isPowerOfTwo ( $ n ) { return ( $ n && ! ( $ n & ( $ n - 1 ) ) ) ; } function isThabitNumber ( $ n ) { $ n = $ n + 1 ; if ( $ n % 3 == 0 ) $ n = $ n / 3 ; else return false ; if ( isPowerOfTwo ( $ n ) ) return true ; else return false ; } $ n = 47 ; if ( isThabitNumber ( $ n ) ) echo " YES " ; else echo " NO " ; ? >


"The Lazy Caterer 's Problem | This function receives an integer n and returns the maximum number of pieces that can be made form pancake using n cuts ; Use the formula ; Driver Code"
Solution in php:

 < ? php function findPieces ( $ n ) { return ( $ n * ( $ n + 1 ) ) / 2 + 1 ; } echo findPieces ( 1 ) , "
 " ; echo findPieces ( 2 ) , "
 " ; echo findPieces ( 3 ) , "
 " ; echo findPieces ( 50 ) , "
 " ; ? >


"The Stock Span Problem | Fills array S [ ] with span values ; Span value of first day is always 1 ; Calculate span value of remaining days by linearly checking previous days ; Initialize span value ; Traverse left while the next element on left is smaller than price [ i ] ; print the calculated span values ; Driver Code ; Fill the span values in array S [ ]"
Solution in php:


"The biggest possible circle that can be inscribed in a rectangle | Function to find the area of the biggest circle ; the length and breadth cannot be negative ; area of the circle ; Driver code"
Solution in php:

 < ? php function circlearea ( $ l , $ b ) { if ( $ l < 0 $ b < 0 ) return -1 ; if ( $ l < $ b ) return 3.14 * pow ( $ l / 2 , 2 ) ; else return 3.14 * pow ( $ b / 2 , 2 ) ; } $ l = 4 ; $ b = 8 ; echo circlearea ( $ l , $ b ) . "
 " ; ? >


"The painter 's partition problem | function to calculate sum between two indices in array ; bottom up tabular dp ; initialize table ; base cases k = 1 ; n = 1 ; 2 to k partitions ; track minimum ; i - 1 th separator before position arr [ p = 1. . j ] ; required ; Driver Code ; Calculate size of array ."
Solution in php:


"Third largest element in an array of distinct elements | PHP program to find third Largest element in an array ; There should be atleast three elements ; Initialize first , second and third Largest element ; Traverse array elements to find the third Largest ; If current element is greater than first , then update first , second and third ; If arr [ i ] is in between first and second ; If arr [ i ] is in between second and third ; Driver Code"
Solution in php:

< ? php function thirdLargest ( $ arr , $ arr_size ) { if ( $ arr_size < 3 ) { echo " ▁ Invalid ▁ Input ▁ " ; return ; } $ first = $ arr [ 0 ] ; $ second = PHP_INT_MIN ; $ third = PHP_INT_MIN ; for ( $ i = 1 ; $ i < $ arr_size ; $ i ++ ) { if ( $ arr [ $ i ] > $ first ) { $ third = $ second ; $ second = $ first ; $ first = $ arr [ $ i ] ; } else if ( $ arr [ $ i ] > $ second ) { $ third = $ second ; $ second = $ arr [ $ i ] ; } else if ( $ arr [ $ i ] > $ third ) $ third = $ arr [ $ i ] ; } echo " The ▁ third ▁ Largest ▁ element ▁ is ▁ " , $ third ; } $ arr = array ( 12 , 13 , 1 , 10 , 34 , 16 ) ; $ n = sizeof ( $ arr ) ; thirdLargest ( $ arr , $ n ) ; ? >


"Third largest element in an array of distinct elements | PHP program to find third Largest element in an array of distinct elements ; There should be atleast three elements ; Find first largest element ; Find second largest element ; Find third largest element ; Driver Code"
Solution in php:

 < ? php function thirdLargest ( $ arr , $ arr_size ) { if ( $ arr_size < 3 ) { echo " ▁ Invalid ▁ Input ▁ " ; return ; } $ first = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ arr_size ; $ i ++ ) if ( $ arr [ $ i ] > $ first ) $ first = $ arr [ $ i ] ; $ second = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ arr_size ; $ i ++ ) if ( $ arr [ $ i ] > $ second && $ arr [ $ i ] < $ first ) $ second = $ arr [ $ i ] ; $ third = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ arr_size ; $ i ++ ) if ( $ arr [ $ i ] > $ third && $ arr [ $ i ] < $ second ) $ third = $ arr [ $ i ] ; echo " The ▁ third ▁ Largest ▁ element ▁ is ▁ " , $ third , "
 " ; } $ arr = array ( 12 , 13 , 1 , 10 , 34 , 16 ) ; $ n = sizeof ( $ arr ) ; thirdLargest ( $ arr , $ n ) ; ? >


"Tidy Number ( Digits in non | Returns true if num is Tidy ; To store previous digit ( Assigning initial value which is more than any digit ) ; Traverse all digits from right to left and check if any digit is smaller than previous . ; Driver code"
Solution in php:

< ? php function isTidy ( $ num ) { $ prev = 10 ; while ( $ num ) { $ rem = $ num % 10 ; $ num = ( int ) $ num / 10 ; if ( $ rem > $ prev ) return false ; $ prev = $ rem ; } return true ; } $ num = 1556 ; if ( isTidy ( $ num ) == true ) echo " Yes " ; else echo " No " ; ? >


"Tiling with Dominoes | PHP program to find no . of ways to fill a 3 xn board with 2 x1 dominoes . ; Driver Code"
Solution in php:


"Time required to meet in equilateral triangle | function to calculate time to meet ; Driver Code"
Solution in php:

< ? php function timeToMeet ( $ s , $ v ) { $ V = 3 * $ v / 2 ; $ time = $ s / $ V ; echo $ time ; } $ s = 25 ; $ v = 56 ; timeToMeet ( $ s , $ v ) ; ? >


"Time taken by two persons to meet on a circular track | PHP implementation of above approach ; Function to return the time when both the persons will meet at the starting point ; Time to cover 1 round by both ; Finding LCM to get the meeting point ; Function to return the time when both the persons will meet for the first time ; Driver Code ; Calling function"
Solution in php:

 < ? php function gcd ( $ a , $ b ) { return $ b ? gcd ( $ b , $ a % $ b ) : $ a ; } function startingPoint ( $ Length , $ Speed1 , $ Speed2 ) { $ result1 = 0 ; $ result2 = 0 ; $ time1 = $ Length / $ Speed1 ; $ time2 = $ Length / $ Speed2 ; $ result1 = gcd ( $ time1 , $ time2 ) ; $ result2 = $ time1 * $ time2 / ( $ result1 ) ; return $ result2 ; } function firstTime ( $ Length , $ Speed1 , $ Speed2 ) { $ result = 0 ; $ relativeSpeed = abs ( $ Speed1 - $ Speed2 ) ; $ result = ( ( float ) $ Length / $ relativeSpeed ) ; return $ result ; } $ L = 30 ; $ S1 = 5 ; $ S2 = 2 ; $ first_Time = firstTime ( $ L , $ S1 , $ S2 ) ; $ starting_Point = startingPoint ( $ L , $ S1 , $ S2 ) ; echo " Met ▁ first ▁ time ▁ after ▁ " . $ first_Time . " ▁ hrs " . "
 " ; echo " Met ▁ at ▁ starting ▁ point ▁ after ▁ " . $ starting_Point . " ▁ hrs " . "
 " ; ? >


"Time until distance gets equal to X between two objects moving in opposite direction | Function to return the time for which the two policemen can communicate ; time = distance / speed ; Driver code"
Solution in php:

< ? php function getTime ( $ u , $ v , $ x ) { $ speed = $ u + $ v ; $ time = $ x / $ speed ; return $ time ; } $ u = 3 ; $ v = 3 ; $ x = 3 ; echo getTime ( $ u , $ v , $ x ) ; ? >


"Time when minute hand and hour hand coincide | function to find the minute ; finding the angle between minute hand and the first hour hand ; Driver code"
Solution in php:

< ? php function find_time ( $ h1 ) { $ theta = 30 * $ h1 ; echo ( " ( " . ( $ theta * 2 ) . " / " ▁ . ▁ " 11 " ▁ . ▁ " ) " ▁ . ▁ " minutes " } $ h1 = 3 ; find_time ( $ h1 ) ; ? >


"Times required by Simple interest for the Principal to become Y times itself | Function to return the no . of years ; Driver code"
Solution in php:


"To check a number is palindrome or not without using any extra space | Function to check if given number is palindrome or not without using the extra space ; Find the appropriate divisor to extract the leading digit ; If first and last digit not same return false ; Removing the leading and trailing digit from number ; Reducing divisor by a factor of 2 as 2 digits are dropped ; Driver code"
Solution in php:

< ? php function isPalindrome ( $ n ) { $ divisor = 1 ; while ( $ n / $ divisor >= 10 ) $ divisor *= 10 ; while ( $ n != 0 ) { $ leading = floor ( $ n / $ divisor ) ; $ trailing = $ n % 10 ; if ( $ leading != $ trailing ) return false ; $ n = ( $ n % $ divisor ) / 10 ; $ divisor = $ divisor / 100 ; } return true ; } if ( isPalindrome ( 1001 ) == true ) echo " Yes , ▁ it ▁ is ▁ Palindrome " ; else echo " No , ▁ not ▁ Palindrome " ; ? >


"To check divisibility of any large number by 999 | function to check divisibility ; Append required 0 s at the beginning . ; add digits in group of three in gSum ; group saves 3 - digit group ; calculate result till 3 digit sum ; Driver Code"
Solution in php:

< ? php function isDivisible999 ( $ num ) { $ n = strlen ( $ num ) ; if ( $ n == 0 && $ num [ 0 ] == '0' ) return true ; if ( $ n % 3 == 1 ) $ num = "00" . $ num ; if ( $ n % 3 == 2 ) $ num = "0" . $ num ; $ gSum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i += 3 ) { $ group = 0 ; $ group += ( ord ( $ num [ $ i ] ) - 48 ) * 100 ; $ group += ( ord ( $ num [ $ i + 1 ] ) - 48 ) * 10 ; $ group += ( ord ( $ num [ $ i + 2 ] ) - 48 ) ; $ gSum += $ group ; } if ( $ gSum > 1000 ) { $ num = strval ( $ gSum ) ; $ n = strlen ( $ num ) ; $ gSum = isDivisible999 ( $ num ) ; } return ( $ gSum == 999 ) ; } $ num = "1998" ; if ( isDivisible999 ( $ num ) ) echo " Divisible " ; else echo " Not ▁ divisible " ; ? >


"Toggle all bits after most significant bit | Function to toggle bits starting from MSB ; temporary variable to use XOR with one of a n ; Run loop until the only set bit in temp crosses MST of n . ; Toggle bit of n corresponding to current set bit in temp . ; Move set bit to next higher position . ; Driver code"
Solution in php:

< ? php function toggle ( & $ n ) { $ temp = 1 ; while ( $ temp <= $ n ) { $ n = $ n ^ $ temp ; $ temp = $ temp << 1 ; } } $ n = 10 ; toggle ( $ n ) ; echo $ n ; ? >


"Toggle all bits after most significant bit | Returns a number which has all set bits starting from MSB of n ; This makes sure two bits ( From MSB and including MSB ) are set ; This makes sure 4 bits ( From MSB and including MSB ) are set ; Driver Code"
Solution in php:

< ? php function setAllBitsAfterMSB ( $ n ) { $ n |= $ n >> 1 ; $ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ; return $ n ; } function toggle ( & $ n ) { $ n = $ n ^ setAllBitsAfterMSB ( $ n ) ; } $ n = 10 ; toggle ( $ n ) ; echo $ n ; ? >


"Toggle all even bits of a number | Returns a number which has all even bits of n toggled . ; Generate number form of 101010 . . till of same order as n ; if bit is even then generate number and or with res ; return toggled number ; Driver code"
Solution in php:

< ? php function evenbittogglenumber ( $ n ) { $ res = 0 ; $ count = 0 ; for ( $ temp = $ n ; $ temp > 0 ; $ temp >>= 1 ) { if ( $ count % 2 == 1 ) $ res |= ( 1 << $ count ) ; $ count ++ ; } return $ n ^ $ res ; } $ n = 11 ; echo evenbittogglenumber ( $ n ) ; ? >


"Toggle all odd bits of a number | Returns a number which has all odd bits of n toggled . ; Generate number form of 101010. . . . . till of same order as n ; if bit is odd , then generate number and or with res ; return toggled number ; Driver code"
Solution in php:

< ? php function evenbittogglenumber ( $ n ) { $ res = 0 ; $ count = 0 ; for ( $ temp = $ n ; $ temp > 0 ; $ temp >>= 1 ) { if ( $ count % 2 == 0 ) $ res |= ( 1 << $ count ) ; $ count ++ ; } return $ n ^ $ res ; } $ n = 11 ; echo evenbittogglenumber ( $ n ) ; ? >


"Toggle bits in the given range | function to toggle bits in the given range ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; toggle bits in the range l to r in ' n ' Besides this , we can calculate num as : $num = ( 1 << $r ) - $l . and return the number ; Driver Code"
Solution in php:

< ? php function toggleBitsFromLToR ( $ n , $ l , $ r ) { $ num = ( ( 1 << $ r ) - 1 ) ^ ( ( 1 << ( $ l - 1 ) ) - 1 ) ; return ( $ n ^ $ num ) ; } $ n = 50 ; $ l = 2 ; $ r = 5 ; echo toggleBitsFromLToR ( $ n , $ l , $ r ) ; ? >


"Toggle first and last bits of a number | Returns a number which has same bit count as n and has only first and last bits as set . ; set all the bit of the number ; Adding one to n now unsets all bits and moves MSB to one place . Now we shift the number by 1 and add 1. ; if number is 1 ; take XOR with first and last set bit number ; Driver code"
Solution in php:

< ? php function takeLandFsetbits ( $ n ) { $ n |= $ n >> 1 ; $ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ; return ( ( $ n + 1 ) >> 1 ) + 1 ; } function toggleFandLbits ( int $ n ) { if ( $ n == 1 ) return 0 ; return $ n ^ takeLandFsetbits ( $ n ) ; } $ n = 10 ; echo toggleFandLbits ( $ n ) ; ? >


"Toggle the last m bits | function to toggle the last m bits ; calculating a number ' num ' having ' m ' bits and all are set . ; toggle the last m bits and return the number ; Driver code"
Solution in php:

< ? php function toggleLastMBits ( $ n , $ m ) { $ num = ( 1 << $ m ) - 1 ; return ( $ n ^ $ num ) ; } { $ n = 107 ; $ m = 4 ; echo toggleLastMBits ( $ n , $ m ) ; return 0 ; } ? >


"Tomohiko Sakamoto 's Algorithm | function to implement tomohiko sakamoto algorithm ; array with leading number of days values ; if month is less than 3 reduce year by 1 ; Driver Code"
Solution in php:


"Total money to be paid after traveling the given number of hours | PHP implementation of the above approach ; calculating hours travelled"
Solution in php:


"Total no of 1 's in numbers | PHP code to count the frequency of 1 in numbers less than or equal to the given number . ; Driver Code"
Solution in php:

 < ? php function countDigitOne ( $ n ) { $ countr = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ str = strval ( $ i ) ; $ countr += substr_count ( $ str , '1' ) ; } return $ countr ; } $ n = 13 ; echo countDigitOne ( $ n ) . "
 " ; $ n = 131 ; echo countDigitOne ( $ n ) . "
 " ; $ n = 159 ; echo countDigitOne ( $ n ) . "
 " ; ? >


"Total no of 1 's in numbers | function to count the frequency of 1. ; Driver Code"
Solution in php:

 < ? php function countDigitOne ( $ n ) { $ countr = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i *= 10 ) { $ divider = $ i * 10 ; $ countr += ( int ) ( $ n / $ divider ) * $ i + min ( max ( $ n % $ divider - $ i + 1 , 0 ) , $ i ) ; } return $ countr ; } $ n = 13 ; echo countDigitOne ( $ n ) , "
 " ; $ n = 113 ; echo countDigitOne ( $ n ) , "
 " ; $ n = 205 ; echo countDigitOne ( $ n ) , "
 " ; ? >


"Total number of different staircase that can made from N boxes | Function to find the total number of different staircase that can made from N boxes ; Initialize all the elements of the table to zero ; Base case ; When step is equal to 2 ; When step is greater than 2 ; Count the total staircase from all the steps ; Driver Code"
Solution in php:


"Total number of divisors for a given number | program for finding no . of divisors ; sieve method for prime calculation ; Traversing through all prime numbers ; calculate number of divisor with formula total div = ( p1 + 1 ) * ( p2 + 1 ) * ... . . * ( pn + 1 ) where n = ( a1 ^ p1 ) * ( a2 ^ p2 ) . ... * ( an ^ pn ) ai being prime divisor for n and pi are their respective power in factorization ; Driver Code"
Solution in php:


"Total number of non | PHP program to count non - decreasing number with n digits ; dp [ i ] [ j ] contains total count of non decreasing numbers ending with digit i and of length j ; Fill table for non decreasing numbers of length 1 Base cases 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ; Fill the table in bottom - up manner ; Compute total numbers of non decreasing numbers of length ' len ' ; sum of all numbers of length of len - 1 in which last digit x is <= ' digit ' ; There total nondecreasing numbers of length n wiint be dp [ 0 ] [ n ] + dp [ 1 ] [ n ] . . + dp [ 9 ] [ n ] ; Driver program"
Solution in php:

< ? php function countNonDecreasing ( $ n ) { $ dp = array_fill ( 0 , 10 , array_fill ( 0 , $ n + 1 , NULL ) ) ; for ( $ i = 0 ; $ i < 10 ; $ i ++ ) $ dp [ $ i ] [ 1 ] = 1 ; for ( $ digit = 0 ; $ digit <= 9 ; $ digit ++ ) { for ( $ len = 2 ; $ len <= $ n ; $ len ++ ) { for ( $ x = 0 ; $ x <= $ digit ; $ x ++ ) $ dp [ $ digit ] [ $ len ] += $ dp [ $ x ] [ $ len - 1 ] ; } } $ count = 0 ; for ( $ i = 0 ; $ i < 10 ; $ i ++ ) $ count += $ dp [ $ i ] [ $ n ] ; return $ count ; } $ n = 3 ; echo countNonDecreasing ( $ n ) ; return 0 ; ? >


"Total number of non | PHP program to count non - decreasing numner with n digits ; Compute value of N * ( N + 1 ) / 2 * ( N + 2 ) / 3 * ... ... . * ( N + n - 1 ) / n ; Driver Code"
Solution in php:

< ? php function countNonDecreasing ( $ n ) { $ N = 10 ; $ count = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ count *= ( $ N + $ i - 1 ) ; $ count /= $ i ; } return $ count ; } $ n = 3 ; echo countNonDecreasing ( $ n ) ; ? >


"Total number of possible Binary Search Trees and Binary Trees with n keys | A function to find factorial of a given number ; Calculate value of [ 1 * ( 2 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; 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 ) ; A function to count number of BST with n nodes using catalan ; find nth catalan number ; return nth catalan number ; A function to count number of binary trees with n nodes ; find count of BST with n numbers ; return count * n ! ; Driver Code ; find count of BST and binary trees with n nodes ; print count of BST and binary trees with n nodes"
Solution in php:

 < ? php function factorial ( $ n ) { $ res = 1 ; for ( $ i = 1 ; $ i <= $ n ; ++ $ i ) { $ res *= $ i ; } return $ res ; } function binomialCoeff ( $ n , $ k ) { $ res = 1 ; if ( $ k > $ n - $ k ) $ k = $ n - $ k ; for ( $ i = 0 ; $ i < $ k ; ++ $ i ) { $ res *= ( $ n - $ i ) ; $ res = ( int ) $ res / ( $ i + 1 ) ; } return $ res ; } function catalan ( $ n ) { $ c = binomialCoeff ( 2 * $ n , $ n ) ; return ( int ) $ c / ( $ n + 1 ) ; } function countBST ( $ n ) { $ count = catalan ( $ n ) ; return $ count ; } function countBT ( $ n ) { $ count = catalan ( $ n ) ; return $ count * factorial ( $ n ) ; } $ count1 ; $ count2 ; $ n = 5 ; $ count1 = countBST ( $ n ) ; $ count2 = countBT ( $ n ) ; echo " Count ▁ of ▁ BST ▁ with ▁ " , $ n , " ▁ nodes ▁ is ▁ " , $ count1 , "
 " ; echo " Count ▁ of ▁ binary ▁ trees ▁ with ▁ " , $ n , " ▁ nodes ▁ is ▁ " , $ count2 ; ? >


"Total number of triangles formed when there are H horizontal and V vertical lines | Function to return total triangles ; Only possible triangle is the given triangle ; If only vertical lines are present ; If only horizontal lines are present ; Return total triangles ; Driver code"
Solution in php:

< ? php function totalTriangles ( $ h , $ v ) { if ( $ h == 0 && $ v == 0 ) return 1 ; if ( $ h == 0 ) return ( ( $ v + 1 ) * ( $ v + 2 ) / 2 ) ; if ( $ v == 0 ) return ( $ h + 1 ) ; $ Total = ( $ h + 1 ) * ( ( $ v + 1 ) * ( $ v + 2 ) / 2 ) ; return $ Total ; } $ h = 2 ; $ v = 2 ; echo totalTriangles ( $ h , $ v ) ; ? >


"Total number of ways to place X and Y at n places such that no two X are together | Function to return number of ways ; for n = 1 ; for n = 2 ; iterate to find Fibonacci term ; total number of places"
Solution in php:

< ? php function ways ( $ n ) { $ first = 2 ; $ second = 3 ; $ res = 0 ; for ( $ i = 3 ; $ i <= $ n ; $ i ++ ) { $ res = $ first + $ second ; $ first = $ second ; $ second = $ res ; } return $ res ; } $ n = 7 ; echo " Total ▁ ways ▁ are : ▁ " , ways ( $ n ) ; ? >


"Total numbers with no repeated digits in a range | Function to check if the given number has repeated digit or not ; Traversing through each digit ; if the digit is present more than once in the number ; return 0 if the number has repeated digit ; return 1 if the number has no repeated digit ; Function to find total number in the given range which has no repeated digit ; Traversing through the range ; Add 1 to the answer if i has no repeated digit else 0 ; Driver Code ; Calling the calculate"
Solution in php:

< ? php function repeated_digit ( $ n ) { $ c = 10 ; $ a = array_fill ( 0 , $ c , 0 ) ; while ( $ n > 0 ) { $ d = $ n % 10 ; if ( $ a [ $ d ] > 0 ) { return 0 ; } $ a [ $ d ] ++ ; $ n = ( int ) ( $ n / 10 ) ; } return 1 ; } function calculate ( $ L , $ R ) { $ answer = 0 ; for ( $ i = $ L ; $ i <= $ R ; $ i ++ ) { $ answer += repeated_digit ( $ i ) ; } return $ answer ; } $ L = 1 ; $ R = 100 ; echo calculate ( $ L , $ R ) ; ? >


"Total pairs in an array such that the bitwise AND , bitwise OR and bitwise XOR of LSB is 1 | Function to find the count of required pairs ; To store the count of elements which give remainder 0 i . e . even values ; To store the count of elements which give remainder 1 i . e . odd values ; Driver code"
Solution in php:

 < ? php function CalculatePairs ( $ a , $ n ) { $ cnt_zero = 0 ; $ cnt_one = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] % 2 == 0 ) $ cnt_zero += 1 ; else $ cnt_one += 1 ; } $ total_XOR_pairs = $ cnt_zero * $ cnt_one ; $ total_AND_pairs = ( $ cnt_one ) * ( $ cnt_one - 1 ) / 2 ; $ total_OR_pairs = $ cnt_zero * $ cnt_one + ( $ cnt_one ) * ( $ cnt_one - 1 ) / 2 ; echo ( " cntXOR ▁ = ▁ $ total _ XOR _ pairs
 " ) ; echo ( " cntAND ▁ = ▁ $ total _ AND _ pairs
 " ) ; echo ( " cntOR ▁ = ▁ $ total _ OR _ pairs
 " ) ; } $ a = array ( 1 , 3 , 4 , 2 ) ; $ n = count ( $ a ) ; CalculatePairs ( $ a , $ n ) ; ? >


"Total position where king can reach on a chessboard in exactly M moves | Function to return the number of squares that the king can reach in the given number of moves ; Calculate initial and final coordinates ; Since chessboard is of size 8 X8 so if any coordinate is less than 1 or greater than 8 make it 1 or 8. ; Calculate total positions ; Driver code"
Solution in php:


"Trailing number of 0 s in product of two factorials | Returns number of zeros in factorial n ; dividing x by powers of 5 and update count ; Returns count of trailing zeros in M ! x N ! ; Driver Code"
Solution in php:

< ? php function trailingZero ( $ x ) { $ i = 5 ; $ count = 0 ; while ( $ x > $ i ) { $ count = $ count + ( int ) ( $ x / $ i ) ; $ i = $ i * 5 ; } return $ count ; } function countProductTrailing ( $ M , $ N ) { return trailingZero ( $ N ) + trailingZero ( $ M ) ; } $ N = 67 ; $ M = 98 ; echo ( countProductTrailing ( $ N , $ M ) ) ; ? >


"Transform a string such that it has abcd . . z as a subsequence | function to transform string with string passed as reference ; initializing the variable ch to ' a ' ; if the length of string is less than 26 , we can 't obtain the required  subsequence ; if ch has reached ' z ' , it means we have transformed our string such that required subsequence can be obtained ; current character is not greater than ch , then replace it with ch and increment ch ; Driver Code"
Solution in php:

< ? php function transformString ( & $ s ) { $ ch = " a " ; if ( strlen ( $ s ) < 26 ) return false ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( ord ( $ ch ) > ord ( " z " ) ) break ; if ( $ s [ $ i ] <= $ ch ) { $ s [ $ i ] = $ ch ; $ ch = chr ( ord ( $ ch ) + 1 ) ; } } if ( $ ch <= " z " ) return false ; return true ; } $ str = " aaaaaaaaaaaaaaaaaaaaaaaaaa " ; if ( transformString ( $ str ) ) echo $ str ; else echo " Not ▁ Possible " ; ? >


"Trapezoidal Rule for Approximate Value of Definite Integral | A sample function whose definite integral 's approximate value is computed using Trapezoidal rule ; Declaring the function f ( x ) = 1 / ( 1 + x * x ) ; Function to evaluate the value of integral ; Grid spacing ; Computing sum of first and last terms in above formula ; Adding middle terms in above formula ; h / 2 indicates ( b - a ) / 2 n . Multiplying h / 2 with s . ; Range of definite integral ; Number of grids . Higher value means more accuracy"
Solution in php:

< ? php function y ( $ x ) { return 1 / ( 1 + $ x * $ x ) ; } function trapezoidal ( $ a , $ b , $ n ) { $ h = ( $ b - $ a ) / $ n ; $ s = y ( $ a ) + y ( $ b ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ s += 2 * Y ( $ a + $ i * $ h ) ; return ( $ h / 2 ) * $ s ; } $ x0 = 0 ; $ xn = 1 ; $ n = 6 ; echo ( " Value ▁ of ▁ integral ▁ is ▁ " ) ; echo ( trapezoidal ( $ x0 , $ xn , $ n ) ) ; ? >


"Trapping Rain Water | Method to find maximum amount of water that can be trapped within given set of bars . ; initialize output ; maximum element on left and right ; indices to traverse the array ; update max in left ; water on curr element = max - curr ; update right maximum ; Driver Code"
Solution in php:

< ? php function findWater ( $ arr , $ n ) { $ result = 0 ; $ left_max = 0 ; $ right_max = 0 ; $ lo = 0 ; $ hi = $ n - 1 ; while ( $ lo <= $ hi ) { if ( $ arr [ $ lo ] < $ arr [ $ hi ] ) { if ( $ arr [ $ lo ] > $ left_max ) $ left_max = $ arr [ $ lo ] ; else $ result += $ left_max - $ arr [ $ lo ] ; $ lo ++ ; } else { if ( $ arr [ $ hi ] > $ right_max ) $ right_max = $ arr [ $ hi ] ; else $ result += $ right_max - $ arr [ $ hi ] ; $ hi -- ; } } return $ result ; } $ arr = array ( 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 ) ; $ n = count ( $ arr ) ; echo " Maximum ▁ water ▁ that ▁ can ▁ be ▁ accumulated ▁ is ▁ " , findWater ( $ arr , $ n ) ; ? >


"Trapping Rain Water | PHP program to find maximum amount of water that can be trapped within given set of bars . ; Initialize result ; Fill left array ; Fill right array ; Calculate the accumulated water element by element consider the amount of water on i 'th bar, the  amount of water accumulated  on this particular  bar will be equal to min(left[i], right[i]) - arr[i] . ; Driver program"
Solution in php:

< ? php function findWater ( $ arr , $ n ) { $ water = 0 ; $ left [ 0 ] = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ left [ $ i ] = max ( $ left [ $ i - 1 ] , $ arr [ $ i ] ) ; $ right [ $ n - 1 ] = $ arr [ $ n - 1 ] ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) $ right [ $ i ] = max ( $ right [ $ i + 1 ] , $ arr [ $ i ] ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ water += min ( $ left [ $ i ] , $ right [ $ i ] ) - $ arr [ $ i ] ; return $ water ; } $ arr = array ( 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 ) ; $ n = sizeof ( $ arr ) ; echo " Maximum ▁ water ▁ that ▁ can ▁ be ▁ accumulated ▁ is ▁ " , findWater ( $ arr , $ n ) ; ? >


"Triangular Matchstick Number | PHP program to find X - th triangular matchstick number ; Driver code"
Solution in php:

< ? php function numberOfSticks ( $ x ) { return ( 3 * $ x * ( $ x + 1 ) ) / 2 ; } echo ( numberOfSticks ( 7 ) ) ; ? >


"Triangular Numbers | Returns true if ' num ' is triangular , else false ; Base case ; A Triangular number must be sum of first n natural numbers ; Driver code"
Solution in php:

< ? php function isTriangular ( $ num ) { if ( $ num < 0 ) return false ; $ sum = 0 ; for ( $ n = 1 ; $ sum <= $ num ; $ n ++ ) { $ sum = $ sum + $ n ; if ( $ sum == $ num ) return true ; } return false ; } $ n = 55 ; if ( isTriangular ( $ n ) ) echo " The ▁ number ▁ is ▁ a ▁ triangular ▁ number " ; else echo " The ▁ number ▁ is ▁ NOT ▁ a ▁ triangular ▁ number " ; ? >


"Triangular Numbers | Returns true if num is triangular ; Considering the equation n * ( n + 1 ) / 2 = num The equation is : a ( n ^ 2 ) + bn + c = 0 "; ; Find roots of equation ; checking if root1 is natural ; checking if root2 is natural ; Driver code"
Solution in php:

< ? php function isTriangular ( $ num ) { if ( $ num < 0 ) return false ; $ c = ( -2 * $ num ) ; $ b = 1 ; $ a = 1 ; $ d = ( $ b * $ b ) - ( 4 * $ a * $ c ) ; if ( $ d < 0 ) return false ; $ root1 = ( - $ b + ( float ) sqrt ( $ d ) ) / ( 2 * $ a ) ; $ root2 = ( - $ b - ( float ) sqrt ( $ d ) ) / ( 2 * $ a ) ; if ( $ root1 > 0 && floor ( $ root1 ) == $ root1 ) return true ; if ( $ root2 > 0 && floor ( $ root2 ) == $ root2 ) return true ; return false ; } $ num = 55 ; if ( isTriangular ( $ num ) ) echo ( " The ▁ number ▁ is " . " ▁ a ▁ triangular ▁ number " ) ; else echo ( " The ▁ number ▁ " . " is ▁ NOT ▁ a ▁ triangular ▁ number " ) ; ? >


"Tribonacci Numbers | A DP based PHP program to print first n Tribonacci numbers . ; Driver code"
Solution in php:


"Tribonacci Numbers | A space optimized based PHP program to print first n Tribinocci numbers . | ; Initialize first three numbers ; Loop to add previous three numbers for each number starting from 3 and then assign first , second , third to second , third , and curr to third respectively ; Driver code"
Solution in php:

< ? php function printTrib ( $ n ) { if ( $ n < 1 ) return ; $ first = 0 ; $ second = 0 ; $ third = 1 ; echo $ first , " " ; if ( $ n > 1 ) echo $ second , " ▁ " ; if ( $ n > 2 ) echo $ second , " ▁ " ; for ( $ i = 3 ; $ i < $ n ; $ i ++ ) { $ curr = $ first + $ second + $ third ; $ first = $ second ; $ second = $ third ; $ third = $ curr ; echo $ curr , " " ; } } $ n = 10 ; printTrib ( $ n ) ; ? >


"Tribonacci Numbers | Program to print first n tribonacci numbers Matrix Multiplication function for 3 * 3 matrix ; Recursive function to raise the matrix T to the power n ; base condition . ; recursively call to square the matrix ; calculating square of the matrix T ; if n is odd multiply it one time with M ; base condition ; $T [ 0 ] [ 0 ] contains the tribonacci number so return it ; Driver Code"
Solution in php:


"Trimorphic Number | Function to check Trimorphic number ; Store the cube ; Start Comparing digits ; Return false , if any digit of N doesn ' t ▁ match ▁ with ▁ ▁ its ▁ cube ' s digits from last ; Reduce N and cube ; Driver code"
Solution in php:

< ? php function isTrimorphic ( $ N ) { $ cube = $ N * $ N * $ N ; while ( $ N > 0 ) { if ( $ N % 10 != $ cube % 10 ) return -1 ; $ N /= 10 ; $ cube /= 10 ; } return 1 ; } $ N = 24 ; $ r = isTrimorphic ( $ N ) ? " trimorphic " : " not ▁ trimporphic " ; echo $ r ; ? >


"Trimorphic Number | Functions to find nth Trimorphic number ; Comparing the digits ; Return false , if any digit of num doesn ' t ▁ match ▁ with ▁ ▁ its ▁ cube ' s digits from last ; Reduce num and cube ; Check in max int size ; check number is Trimorphic or not ; if counter is equal to the n then return nth number ; Driver code"
Solution in php:

< ? php function checkTrimorphic ( $ num ) { $ cube = $ num * $ num * $ num ; while ( $ num > 0 ) { if ( $ num % 10 != $ cube % 10 ) return false ; $ num = ( int ) ( $ num / 10 ) ; $ cube = ( int ) ( $ cube / 10 ) ; } return true ; } function nthTrimorphic ( $ n ) { $ count = 0 ; for ( $ i = 0 ; $ i < PHP_INT_MAX ; $ i ++ ) { if ( checkTrimorphic ( $ i ) ) $ count ++ ; if ( $ count == $ n ) return $ i ; } } $ n = 9 ; echo nthTrimorphic ( $ n ) ; ? >


"Triplet with no element divisible by 3 and sum N | Function to print a , b and c ; check if n - 2 is divisible by 3 or not ; Driver code"
Solution in php:

< ? php function printCombination ( $ n ) { echo "1 ▁ " ; if ( ( $ n - 2 ) % 3 == 0 ) echo "2 ▁ " . ( $ n - 3 ) ; else echo "1 ▁ " . ( $ n - 2 ) ; } $ n = 233 ; printCombination ( $ n ) ; ? >


"Triplet with no element divisible by 3 and sum N | Function to print a , b and c ; first loop ; check for 1 st number ; second loop ; check for 2 nd number ; third loop ; Check for 3 rd number ; Driver Code"
Solution in php:


"Tug of War | solution by calling itself recursively ; checks whether the it is going out of bound ; checks that the numbers of elements left are not less than the number of elements required to form the solution ; consider the cases when current element is not included in the solution ; add the current element to the solution ; checks if a solution is formed ; checks if the solution formed is better than the best solution so far ; consider the cases where current element is included in the solution ; removes current element before returning to the caller of this function ; main function that generate an arr ; the boolean array that contains the inclusion and exclusion of an element in current set . The number excluded automatically form the other set ; The inclusion / exclusion array for final solution ; Find the solution using recursive function TOWUtil ( ) ; Print the solution ; Driver Code"
Solution in php:

< ? php function TOWUtil ( & $ arr , $ n , & $ curr_elements , $ no_of_selected_elements , & $ soln , & $ min_diff , $ sum , $ curr_sum , $ curr_position ) { if ( $ curr_position == $ n ) return ; if ( ( intval ( $ n / 2 ) - $ no_of_selected_elements ) > ( $ n - $ curr_position ) ) return ; TOWUtil ( $ arr , $ n , $ curr_elements , $ no_of_selected_elements , $ soln , $ min_diff , $ sum , $ curr_sum , $ curr_position + 1 ) ; $ no_of_selected_elements ++ ; $ curr_sum = ( $ curr_sum + $ arr [ $ curr_position ] ) ; $ curr_elements [ $ curr_position ] = true ; if ( $ no_of_selected_elements == intval ( $ n / 2 ) ) { if ( abs ( intval ( $ sum / 2 ) - $ curr_sum ) < $ min_diff ) { $ min_diff = abs ( intval ( $ sum / 2 ) - $ curr_sum ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ soln [ $ i ] = $ curr_elements [ $ i ] ; } } else { TOWUtil ( $ arr , $ n , $ curr_elements , $ no_of_selected_elements , $ soln , $ min_diff , $ sum , $ curr_sum , $ curr_position + 1 ) ; } $ curr_elements [ $ curr_position ] = false ; } function tugOfWar ( & $ arr , $ n ) { $ curr_elements = array_fill ( 0 , $ n , 0 ) ; $ soln = array_fill ( 0 , $ n , 0 ) ; $ min_diff = PHP_INT_MAX ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum += $ arr [ $ i ] ; $ curr_elements [ $ i ] = $ soln [ $ i ] = false ; } TOWUtil ( $ arr , $ n , $ curr_elements , 0 , $ soln , $ min_diff , $ sum , 0 , 0 ) ; echo " The ▁ first ▁ subset ▁ is : ▁ " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ soln [ $ i ] == true ) echo $ arr [ $ i ] . " ▁ " ; } echo " The second subset is : " for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ soln [ $ i ] == false ) echo $ arr [ $ i ] . " ▁ " ; } } $ arr = array ( 23 , 45 , -34 , 12 , 0 , 98 , -99 , 4 , 189 , -1 , 4 ) ; $ n = count ( $ arr ) ; tugOfWar ( $ arr , $ n ) ; ? >


"Turn off the rightmost set bit | unsets the rightmost set bit of n and returns the result ; Driver Code"
Solution in php:

< ? php function fun ( $ n ) { return $ n & ( $ n - 1 ) ; } $ n = 7 ; echo " The ▁ number ▁ after ▁ unsetting ▁ the " . " ▁ rightmost ▁ set ▁ bit ▁ " , fun ( $ n ) ; ? >


"Twin Prime Numbers between 1 and n | PHP program to print all twin primes 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 ; to check for twin prime numbers display the twin primes ; Driver Code ; Calling the function"
Solution in php:


"Twin Prime Numbers | Please refer below post for details of this function https : goo . gl / Wv3fGv ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Returns true if n1 and n2 are twin primes ; Driver code"
Solution in php:

 < ? php function isPrime ( $ n ) { if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return false ; return true ; } function twinPrime ( $ n1 , $ n2 ) { return ( isPrime ( $ n1 ) && isPrime ( $ n2 ) && abs ( $ n1 - $ n2 ) == 2 ) ; } $ n1 = 11 ; $ n2 = 13 ; if ( twinPrime ( $ n1 , $ n2 ) ) echo " Twin ▁ Prime " , "
 " ; else echo "
 " , " Not ▁ Twin ▁ Prime " , "
 " ; ? >