codeparrot/xlcost-text-to-code huggingface dataset:
Find the number of consecutive zero at the end after multiplying n numbers | Function to count two 's factor ; Count number of 2 s present in n ; Function to count five 's factor ; Function to count number of zeros ; Count the two 's  factor of n number ; Count the five 's  factor of n number ; Return the minimum ; Driver Code
< ? php function two_factor ( $ n ) { $ twocount = 0 ; while ( $ n % 2 == 0 ) { $ twocount ++ ; $ n = ( int ) ( $ n / 2 ) ; } return $ twocount ; } function five_factor ( $ n ) { $ fivecount = 0 ; while ( $ n % 5 == 0 ) { $ fivecount ++ ; $ n = ( int ) ( $ n / 5 ) ; } return $ fivecount ; } function find_con_zero ( $ arr , $ n ) { $ twocount = 0 ; $ fivecount = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ twocount += two_factor ( $ arr [ $ i ] ) ; $ fivecount += five_factor ( $ arr [ $ i ] ) ; } if ( $ twocount < $ fivecount ) return $ twocount ; else return $ fivecount ; } $ arr = array ( 100 , 10 , 5 , 25 , 35 , 14 ) ; $ n = 6 ; echo find_con_zero ( $ arr , $ n ) ; ? >
First occurrence of a digit in a given fraction | function to print the first digit ; reduce the number to its mod ; traverse for every decimal places ; get every fraction places when ( a * 10 / b ) / c ; check if it is equal to the required integer ; mod the number ; Driver Code
< ? php function first ( $ a , $ b , $ c ) { $ a %= $ b ; for ( $ i = 1 ; $ i <= $ b ; $ i ++ ) { $ a = $ a * 10 ; if ( $ a / $ b == $ c ) return $ i ; $ a %= $ b ; } return -1 ; } $ a = 1 ; $ b = 4 ; $ c = 5 ; echo first ( $ a , $ b , $ c ) ; ? >
Minimize the absolute difference of sum of two subsets | function to print difference ; summation of n elements ; if divisible by 4 ; if remainder 1 or 2. In case of remainder 2 , we divide elements from 3 to n in groups of size 4 and put 1 in one group and 2 in group . This also makes difference 1. ; We put elements from 4 to n in groups of size 4. Remaining elements 1 , 2 and 3 can be divided as ( 1 , 2 ) and ( 3 ) . ; Driver code
< ? php function subsetDifference ( $ n ) { $ s = $ n * ( $ n + 1 ) / 2 ; if ( $ n % 4 == 0 ) { echo " First ▁ subset ▁ sum ▁ = ▁ " , floor ( $ s / 2 ) ; echo " Second subset sum = " , floor ( $ s / 2 ) ; echo " Difference = " } else { if ( $ n % 4 == 1 $ n % 4 == 2 ) { echo " First ▁ subset ▁ sum ▁ = ▁ " , floor ( $ s / 2 ) ; echo " Second subset sum = " , floor ( $ s / 2 + 1 ) ; echo " Difference = " } else { echo " First ▁ subset ▁ sum ▁ = ▁ " , floor ( $ s / 2 ) ; echo " Second subset sum = " , floor ( $ s / 2 ) ; echo " Difference = " ▁ , ▁ 0 ; } } } $ n = 6 ; subsetDifference ( $ n ) ; ? >
Time required to meet in equilateral triangle | function to calculate time to meet ; Driver Code
< ? php function timeToMeet ( $ s , $ v ) { $ V = 3 * $ v / 2 ; $ time = $ s / $ V ; echo $ time ; } $ s = 25 ; $ v = 56 ; timeToMeet ( $ s , $ v ) ; ? >
Check if a number can be written as sum of three consecutive integers | function to check if a number can be written as sum of three consecutive integer . ; if n is 0 ; if n is positive , increment loop by 1. ; if n is negative , decrement loop by 1. ; Running loop from 0 to n - 2 ; check if sum of three consecutive integer is equal to n . ; Driver Code
< ? php function checksum ( $ n ) { if ( $ n == 0 ) { echo " - 1 ▁ 0 ▁ 1" ; return ; } $ inc ; if ( $ n > 0 ) $ inc = 1 ; else $ inc = -1 ; for ( $ i = 0 ; $ i <= $ n - 2 ; $ i += $ inc ) { if ( $ i + $ i + 1 + $ i + 2 == $ n ) { echo $ i , " ▁ " , $ i + 1 , " ▁ " , $ i + 2 ; return ; } } echo " - 1" ; } $ n = 6 ; checksum ( $ n ) ; ? >
Sum of all divisors from 1 to n | Utility function to find sum of all divisor of number up to ' n ' ; Find all divisors of i and add them ; Driver code
< ? php function divisorSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; ++ $ i ) { for ( $ j = 1 ; $ j * $ j <= $ i ; ++ $ j ) { if ( $ i % $ j == 0 ) { if ( $ i / $ j == $ j ) $ sum += $ j ; else $ sum += $ j + $ i / $ j ; } } } return $ sum ; } $ n = 4 ; echo " " , ▁ divisorSum ( $ n ) , ▁ " " $ n = 5 ; echo divisorSum ( $ n ) , " STRNEWLINE " ; ? >
Program for Binomial Coefficients table | Function to print binomial table ; B ( m , x ) is 1 if either m or x is 0. ; Otherwise using recursive formula B ( m , x ) = B ( m , x - 1 ) * ( m - x + 1 ) / x ; Driver Code
< ? php function printbinomial ( $ max ) { for ( $ m = 0 ; $ m <= $ max ; $ m ++ ) { echo $ m ; $ binom = 1 ; for ( $ x = 0 ; $ x <= $ m ; $ x ++ ) { if ( $ m != 0 && $ x != 0 ) $ binom = $ binom * ( $ m - $ x + 1 ) / $ x ; echo " ▁ " , $ binom , " ▁ " ; } echo " STRNEWLINE " ; } } $ max = 10 ; printbinomial ( $ max ) ; ? >
Find largest prime factor of a number | A function to find largest prime factor ; Initialize the maximum prime factor variable with the lowest one ; Print the number of 2 s that divide n ; equivalent to n /= 2 ; n must be odd at this point ; now we have to iterate only for integers who does not have prime factor 2 and 3 ; This condition is to handle the case when n is a prime number greater than 4 ; Driver Code
< ? php function maxPrimeFactors ( $ n ) { $ maxPrime = -1 ; while ( $ n % 2 == 0 ) { $ maxPrime = 2 ; $ n >>= 1 ; } while ( $ n % 3 == 0 ) { $ maxPrime = 3 ; $ n = $ n / 3 ; } for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i += 2 ) { while ( $ n % $ i == 0 ) { $ maxPrime = $ i ; $ n = $ n / $ i ; } while ( $ n % ( $ i + 2 ) == 0 ) { $ maxPrime = $ i + 2 ; $ n = $ n / ( $ i + 2 ) ; } } if ( $ n > 4 ) $ maxPrime = $ n ; return $ maxPrime ; } $ n = 15 ; echo maxPrimeFactors ( $ n ) , " STRNEWLINE " ; $ n = 25698751364526 ; echo maxPrimeFactors ( $ n ) , " STRNEWLINE " ; ? >
Count unset bits in a range | Function to get no of set bits in the binary representation of ' n ' ; function to count unset 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 ; returns number of unset bits in the range ' l ' to ' r ' in ' n ' ; Driver code
< ? php function countSetBits ( $ n ) { $ count = 0 ; while ( $ n ) { $ n &= ( $ n - 1 ) ; $ count ++ ; } return $ count ; } function countUnsetBitsInGivenRange ( $ n , $ l , $ r ) { $ num = ( ( 1 << $ r ) - 1 ) ^ ( ( 1 << ( $ l - 1 ) ) - 1 ) ; return ( $ r - $ l + 1 ) - countSetBits ( $ n & $ num ) ; } $ n = 80 ; $ l = 1 ; $ r = 4 ; echo countUnsetBitsInGivenRange ( $ n , $ l , $ r ) ; ? >
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
< ? 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 power of first n even natural numbers | calculate the sum of fourth power of first n even natural numbers ; Driver Code
< ? php function evenPowerSum ( $ n ) { return ( 8 * $ n * ( $ n + 1 ) * ( 2 * $ n + 1 ) * ( 3 * $ n * $ n + 3 * $ n - 1 ) ) / 15 ; } $ n = 4 ; echo ( evenPowerSum ( $ n ) ) ; ? >
Balanced Prime | PHP Program to find Nth Balanced Prime ; Return the Nth balanced prime . ; Sieve of Eratosthenes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; storing all primes ; Finding the Nth balanced Prime ; Driven Program
< ? php $ MAX = 501 ; function balancedprime ( $ n ) { global $ MAX ; $ prime = array_fill ( 0 , $ MAX + 1 , true ) ; for ( $ p = 2 ; $ p * $ p <= $ MAX ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ MAX ; $ i += $ p ) $ prime [ $ i ] = false ; } } $ v = array ( ) ; for ( $ p = 3 ; $ p <= $ MAX ; $ p += 2 ) if ( $ prime [ $ p ] ) array_push ( $ v , $ p ) ; $ count = 0 ; for ( $ i = 1 ; $ i < count ( $ v ) ; $ i ++ ) { if ( $ v [ $ i ] == ( $ v [ $ i + 1 ] + $ v [ $ i - 1 ] ) / 2 ) $ count ++ ; if ( $ count == $ n ) return $ v [ $ i ] ; } } $ n = 4 ; echo balancedprime ( $ n ) ; ? >
Smallest integer which has n factors or more | PHP program to print the smallest integer with n factors or more ; array to store prime factors ; function to generate all prime factors of numbers from 1 to 10 ^ 6 ; Initializes all the positions with their value . ; Initializes all multiples of 2 with 2 ; A modified version of Sieve of Eratosthenes to store the smallest prime factor that divides every number . ; check if it has no prime factor . ; Initializes of j starting from i * i ; if it has no prime factor before , then stores the smallest prime divisor ; function to calculate number of factors ; stores the smallest prime number that divides n ; stores the count of number of times a prime number divides n . ; reduces to the next number after prime factorization of n ; false when prime factorization is done ; if the same prime number is dividing n , then we increase the count ; if its a new prime factor that is factorizing n , then we again set c = 1 and change dup to the new prime factor , and apply the formula explained above . ; prime factorizes a number ; for the last prime factor ; function to find the smallest integer with n factors or more . ; check if no of factors is more than n or not ; generate prime factors of number upto 10 ^ 6
< ? php $ MAX = 100001 ; $ factor = array_fill ( 0 , $ MAX , 0 ) ; function generatePrimeFactors ( ) { global $ MAX ; global $ factor ; $ factor [ 1 ] = 1 ; for ( $ i = 2 ; $ i < $ MAX ; $ i ++ ) $ factor [ $ i ] = $ i ; for ( $ i = 4 ; $ i < $ MAX ; $ i += 2 ) $ factor [ $ i ] = 2 ; for ( $ i = 3 ; $ i * $ i < $ MAX ; $ i ++ ) { if ( $ factor [ $ i ] == $ i ) { for ( $ j = $ i * $ i ; $ j < $ MAX ; $ j += $ i ) { if ( $ factor [ $ j ] == $ j ) $ factor [ $ j ] = $ i ; } } } } function calculateNoOFactors ( $ n ) { global $ factor ; if ( $ n == 1 ) return 1 ; $ ans = 1 ; $ dup = $ factor [ $ n ] ; $ c = 1 ; $ j = ( int ) ( $ n / $ factor [ $ n ] ) ; while ( $ j != 1 ) { if ( $ factor [ $ j ] == $ dup ) $ c += 1 ; else { $ dup = $ factor [ $ j ] ; $ ans = $ ans * ( $ c + 1 ) ; $ c = 1 ; } $ j = ( int ) ( $ j / $ factor [ $ j ] ) ; } $ ans = $ ans * ( $ c + 1 ) ; return $ ans ; } function smallest ( $ n ) { for ( $ i = 1 ; ; $ i ++ ) if ( calculateNoOFactors ( $ i ) >= $ n ) return $ i ; } generatePrimeFactors ( ) ; $ n = 4 ; echo smallest ( $ 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
< ? php function squaresum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum += ( $ i * $ i ) ; return $ sum ; } $ n = 4 ; echo ( squaresum ( $ n ) ) ; ? >
Break a number such that sum of maximum divisors of all parts is minimum | Function to check if a number is prime or not . ; If n is an even number ( we can write it as sum of two primes ) ; If n is odd and n - 2 is prime . ; If n is odd , n - 3 must be even . ; Driver code
< ? php function isPrime ( $ n ) { $ i = 2 ; while ( $ i * $ i <= $ n ) { if ( $ n % $ i == 0 ) return false ; $ i ++ ; } return true ; } function minimumSum ( $ n ) { if ( isPrime ( $ n ) ) return 1 ; if ( $ n % 2 == 0 ) return 2 ; if ( isPrime ( $ n - 2 ) ) return 2 ; return 3 ; } $ n = 27 ; echo minimumSum ( $ n ) ; ? >
Find first and last digits of a number | Find the first digit ; Remove last digit from number till only one digit is left ; return the first digit ; Find the last digit ; return the last digit ; Driver Code
< ? php function firstDigit ( $ n ) { while ( $ n >= 10 ) $ n /= 10 ; return ( int ) $ n ; } function lastDigit ( $ n ) { return ( ( int ) $ n % 10 ) ; } $ n = 98562 ; echo firstDigit ( $ n ) . " ▁ " . lastDigit ( $ n ) . " STRNEWLINE " ;
Find first and last digits of a number | Find the first digit ; Find total number of digits - 1 ; Find first digit ; Return first digit ; Find the last digit ; return the last digit ; Driver Code
< ? php function firstDigit ( $ n ) { $ digits = ( int ) log10 ( $ n ) ; $ n = ( int ) ( $ n / pow ( 10 , $ digits ) ) ; return $ n ; } function lastDigit ( $ n ) { return ( $ n % 10 ) ; } $ n = 98562 ; echo firstDigit ( $ n ) , " " , lastDigit ( $ n ) , " " ; ? >
Express an odd number as sum of prime numbers | Function to check if a number is prime or not . ; Prints at most three prime numbers whose sum is n . ; CASE - I ; CASE - II ; CASE - III ; Driver code
< ? php function isPrime ( $ x ) { if ( $ x == 0 $ x == 1 ) return false ; for ( $ i = 2 ; $ i * $ i <= $ x ; ++ $ i ) if ( $ x % $ i == 0 ) return false ; return true ; } function findPrimes ( $ n ) { if ( isPrime ( $ n ) ) echo ( $ n ) ; else if ( isPrime ( $ n - 2 ) ) echo ( 2 . " ▁ " . ( $ n - 2 ) ) ; else { echo ( 3 . " ▁ " ) ; $ n = $ n - 3 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( isPrime ( $ i ) && isPrime ( $ n - $ i ) ) { echo ( $ i . " " . ( $ n - $ i ) ) ; break ; } } } } $ n = 27 ; findPrimes ( $ n ) ; ? >
AKS Primality Test | array used to store coefficients . ; function to calculate the coefficients of ( x - 1 ) ^ n - ( x ^ n - 1 ) with the help of Pascal 's triangle . ; function to check whether the number is prime or not ; Calculating all the coefficients by the function coef and storing all the coefficients in c array . ; checking all the coefficients whether they are divisible by n or not . if n is not prime , then loop breaks and ( i > 0 ) . ; Return true if all coefficients are divisible by n . ; Driver Code
< ? php global $ c ; function coef ( $ n ) { $ c [ 0 ] = 1 ; for ( $ i = 0 ; $ i < $ n ; $ c [ 0 ] = - $ c [ 0 ] , $ i ++ ) { $ c [ 1 + $ i ] = 1 ; for ( $ j = $ i ; $ j > 0 ; $ j -- ) $ c [ $ j ] = $ c [ $ j - 1 ] - $ c [ $ j ] ; } } function isPrime ( $ n ) { global $ c ; coef ( $ n ) ; $ i = $ n ; while ( $ i -- && $ c [ $ i ] % $ n == 0 ) return $ i < 0 ; } $ n = 37 ; if ( isPrime ( $ n ) ) echo " Not ▁ Prime " , " STRNEWLINE " ; else echo " Prime " , " STRNEWLINE " ; ? >
Motzkin number | Return the nth Motzkin Number . ; Base Case ; Recursive step ; Driven Code
< ? php function motzkin ( $ n ) { if ( $ n == 0 $ n == 1 ) return 1 ; return ( ( 2 * $ n + 1 ) * motzkin ( $ n - 1 ) + ( 3 * $ n - 3 ) * motzkin ( $ n - 2 ) ) / ( $ n + 2 ) ; } $ n = 8 ; echo ( motzkin ( $ 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
< ? php function sumOfSeries ( $ n ) { return ( 0.666 ) * ( 1 - 1 / pow ( 10 , $ n ) ) ; } $ n = 2 ; echo ( sumOfSeries ( $ n ) ) ; ? >
Narcissistic number | Function to count digits ; Returns true if n is Narcissistic number ; count the number of digits ; calculates the sum of digits raised to power ; Driver Code
< ? php function countDigit ( $ n ) { if ( $ n == 0 ) return 0 ; return ( 1 + countDigit ( $ n / 10 ) ) ; } function check ( $ n ) { $ l = countDigit ( $ n ) ; $ dup = $ n ; $ sum = 0 ; while ( $ dup ) { $ sum += pow ( $ dup % 10 , $ l ) ; $ dup = ( int ) $ dup / 10 ; } return ( $ n == $ sum ) ; } $ n = 1634 ; if ( check ( ! $ n ) ) echo " yes " ; else echo " no " ; ? >
Sum of squares of first n natural numbers | Function to calculate sum ; Driver code
< ? php function summation ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum += ( $ i * $ i ) ; return $ sum ; } $ n = 2 ; echo summation ( $ n ) ; ? >
Leyland Number | PHP program to print first N Leyland Numbers . ; Print first n Leyland Number . ; Outer loop for x from 2 to n . ; Inner loop for y from 2 to x . ; Calculating x ^ y + y ^ x ; Sorting the all Leyland Number . ; Printing first n Leyland number . ; Driver Code
< ? php $ MAX = 100 ; function leyland ( $ n ) { $ ans ; $ index = 0 ; for ( $ x = 2 ; $ x <= $ n ; $ x ++ ) { for ( $ y = 2 ; $ y <= $ x ; $ y ++ ) { $ temp = pow ( $ x , $ y ) + pow ( $ y , $ x ) ; $ ans [ $ index ] = $ temp ; $ index ++ ; } } sort ( $ ans ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ ans [ $ i ] . " ▁ " ; } $ n = 6 ; leyland ( $ n ) ; ? >
Nicomachusâ €™ s Theorem ( Sum of k | Return the sum of k - th group of positive odd integers . ; Finding first element of kth group . ; Finding the sum . ; Driver code
< ? php function kthgroupsum ( $ k ) { $ cur = ( $ k * ( $ k - 1 ) ) + 1 ; $ sum = 0 ; while ( $ k -- ) { $ sum += $ cur ; $ cur += 2 ; } return $ sum ; } $ k = 3 ; echo kthgroupsum ( $ k ) ; ? >
n | Returns n - th term of the series 2 , 12 , 36 , 80 , 150 ; driver code
< ? php function nthTerm ( $ n ) { return ( $ n * $ n ) + ( $ n * $ n * $ n ) ; } $ n = 4 ; echo ( nthTerm ( $ n ) ) ; ? >
Sum of the series 1 , 3 , 6 , 10. . . ( Triangular Numbers ) | Function to find the sum of series ; Driver code
< ? php function seriesSum ( $ n ) { return ( $ n * ( $ n + 1 ) * ( $ n + 2 ) ) / 6 ; } $ n = 4 ; echo ( seriesSum ( $ n ) ) ; ? >
Calculate speed , distance and time | Function to calculate speed ; Function to calculate distance traveled ; Function to calculate time taken ; Calling function cal_speed ( ) ; Calling function cal_dis ( ) ; Calling function cal_time ( )
< ? php function cal_speed ( $ dist , $ time ) { echo " Distance ( km ) : " echo " Time ( hr ) : " return $ dist / $ time ; } function cal_dis ( $ speed , $ time ) { echo " Time ( hr ) : " echo " Speed ( km / hr ) : " return $ speed * $ time ; } function cal_time ( $ dist , $ speed ) { echo " Distance ( km ) : " echo " Speed ( km / hr ) : " return $ speed * $ dist ; } echo " ▁ The ▁ calculated ▁ Speed ( km ▁ / ▁ hr ) ▁ is ▁ : ▁ " . cal_speed ( 45.9 , 2.0 ) . " STRNEWLINE " ; echo " The calculated Distance ( km ) : " . cal_dis ( 62.9 , 2.5 ) . " " ; echo " The calculated Time ( hr ) : " . cal_time ( 48.0 , 4.5 ) . " " ; ? >
Print factorials of a range in right aligned format | PHP Program to print format of factorial ; vector for store the result ; variable for store the each number factorial ; copy of first number ; found first number factorial ; push the first number in result vector ; incerement the first number ; found the all reaming number factorial loop is working until all required number factorial founded ; store the result of factorial ; incerement the first number ; return the result ; function for print the result ; setw ( ) is used for fill the blank right is used for right justification of data ; number which found the factorial of between range ; store the result of factorial ; function for found factorial ; function for print format
< ? php function find_factorial ( $ num1 , $ num2 ) { $ vec ; $ t = 0 ; $ fac = 1 ; $ temp = $ num1 ; while ( 1 ) { if ( $ temp == 1 ) break ; $ fac *= $ temp ; $ temp -- ; } $ vec [ $ t ++ ] = $ fac ; $ num1 ++ ; while ( $ num1 <= $ num2 ) { $ fac *= $ num1 ; $ vec [ $ t ++ ] = $ fac ; $ num1 ++ ; } return ( $ vec ) ; } function print_format ( $ result ) { $ x = count ( $ result ) ; $ digits = strlen ( ( string ) $ result [ $ x - 1 ] ) ; for ( $ i = 0 ; $ i < $ x ; $ i ++ ) { echo str_pad ( $ result [ $ i ] , ( $ digits + 1 ) , " " , ▁ STR _ PAD _ LEFT ) ▁ . ▁ " " } } $ m = 10 ; $ n = 20 ; $ result_fac ; $ result_fac = find_factorial ( $ m , $ n ) ; print_format ( $ result_fac ) ; ? >
Find n | Function to find the nth term of series ; Loop to add numbers ; Driver code
< ? php function term ( $ n ) { $ ans = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ ans += $ i ; return $ ans ; } $ n = 4 ; echo ( term ( $ n ) ) ; ? >
Find the average of first N natural numbers | Return the average of first n natural numbers ; Driver Code
< ? php function avgOfFirstN ( $ n ) { return ( float ) ( 1 + $ n ) / 2 ; } $ n = 20 ; echo ( avgOfFirstN ( $ n ) ) ; ? >
Find the sum of the series 1 + 11 + 111 + 1111 + ... . . upto n terms | Function for finding summation ; Driver Code
< ? php function summation ( $ n ) { $ sum ; $ sum = ( pow ( 10 , $ n + 1 ) - 10 - ( 9 * $ n ) ) / 81 ; return $ sum ; } $ n = 5 ; echo summation ( $ 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
< ? 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 ) ) ; ? >
Find n | function to solve the quadratic equation ; calculating the Nth term ; Driver Code
< ? php function term ( $ n ) { $ x = ( ( ( 1 ) + ( double ) sqrt ( 1 + ( 8 * $ n ) ) ) / 2 ) ; return $ x ; } $ n = 5 ; echo ( ( int ) term ( $ n ) ) ; ? >
Deserium Number | Returns count of digits in n . ; Returns true if x is Diserium ; Compute powers of digits from right to left . ; If sum of powers is same as given number . ; Driver Code
< ? php function countDigits ( $ n ) { $ c = 0 ; do { $ c ++ ; $ n = $ n / 10 ; } while ( $ n != 0 ) ; return $ c ; } function isDeserium ( $ x ) { $ temp = $ x ; $ p = countDigits ( $ x ) ; $ sum = 0 ; while ( $ x != 0 ) { $ digit = $ x % 10 ; $ sum += pow ( $ digit , $ p ) ; $ p -- ; $ x = $ x / 10 ; } return ( $ sum == $ temp ) ; } $ x = 135 ; if ( isDeserium ( $ x ) ) echo " No " ; else echo " Yes " ; ? >
Largest number by which given 3 numbers should be divided such that they leaves same remainder | __gcd function ; function return number which divides these three number and leaves same remainder . ; We find the differences of all three pairs ; Return GCD of three differences . ; Driver Code
< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function sameRemainder ( $ a , $ b , $ c ) { $ a1 = ( $ b - $ a ) ; $ b1 = ( $ c - $ b ) ; $ c1 = ( $ c - $ a ) ; return gcd ( $ a1 , gcd ( $ b1 , $ c1 ) ) ; } $ a = 62 ; $ b = 132 ; $ c = 237 ; echo sameRemainder ( $ a , $ b , $ c ) ; ? >
Find combined mean and variance of two series | Function to find mean of series . ; Function to find the standard deviation of series . ; Function to find combined variance of two different series . ; mean1 and mean2 are the mean of two arrays . ; sd1 and sd2 are the standard deviation of two array . ; combinedMean is variable to store the combined mean of both array . ; d1_square and d2_square are the combined mean deviation . ; combinedVar is variable to store combined variance of both array . ; Driver Code ; Function call to combined mean .
< ? php function mean ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum = $ sum + $ arr [ $ i ] ; $ mean = ( float ) ( $ sum / $ n ) ; return $ mean ; } function sd ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum = $ sum + ( $ arr [ $ i ] - mean ( $ arr , $ n ) ) * ( $ arr [ $ i ] - mean ( $ arr , $ n ) ) ; $ sdd = $ sum / $ n ; return $ sdd ; } function combinedVariance ( $ arr1 , $ arr2 , $ n , $ m ) { $ mean1 = mean ( $ arr1 , $ n ) ; $ mean2 = mean ( $ arr2 , $ m ) ; echo ( " Mean1 : ▁ " . round ( $ mean1 , 2 ) . " " ▁ . STRNEWLINE TABSYMBOL TABSYMBOL " mean2 : " $ sd1 = sd ( $ arr1 , $ n ) ; $ sd2 = sd ( $ arr2 , $ m ) ; echo ( " StandardDeviation1 : " ▁ . ▁ round ( $ sd1 , ▁ 2 ) ▁ . ▁ " " STRNEWLINE TABSYMBOL TABSYMBOL . ▁ " StandardDeviation2 : " $ combinedMean = ( float ) ( $ n * $ mean1 + $ m * $ mean2 ) / ( $ n + $ m ) ; echo ( " Combined Mean : " round ( $ combinedMean , 2 ) ) ; $ d1_square = ( $ mean1 - $ combinedMean ) * ( $ mean1 - $ combinedMean ) ; $ d2_square = ( $ mean2 - $ combinedMean ) * ( $ mean2 - $ combinedMean ) ; echo ( " d1 square : " ▁ . ▁ round ( $ d1 _ square , ▁ 2 ) ▁ . ▁ " " STRNEWLINE TABSYMBOL TABSYMBOL . ▁ " d2_square : " $ combinedVar = ( $ n * ( $ sd1 + $ d1_square ) + $ m * ( $ sd2 + $ d2_square ) ) / ( $ n + $ m ) ; return $ combinedVar ; } $ arr1 = array ( 23 , 45 , 34 , 78 , 12 , 76 , 34 ) ; $ arr2 = array ( 65 , 67 , 34 , 23 , 45 ) ; $ n = sizeof ( $ arr1 ) ; $ m = sizeof ( $ arr2 ) ; echo ( " Combined Variance : " . round ( combinedVariance ( $ arr1 , $ arr2 , $ n , $ m ) , 2 ) ) ; ? >