codeparrot/xlcost-text-to-code huggingface dataset:
Multiplication of two complex numbers given as strings | PHP program to multiply two complex numbers given as strings . ; Spiting the real and imaginary parts of the given complex strings based on ' + ' and ' i ' symbols . ; Storing the real part of complex string a ; Storing the imaginary part of complex string a ; Storing the real part of complex string b ; Storing the imaginary part of complex string b ; Returns the product . ; Driver Code
< ? php function complexNumberMultiply ( $ a , $ b ) { $ x = preg_split ( " / [ \s + ] + ▁ i / " , $ a ) ; $ y = preg_split ( " / [ \s + ] + ▁ i / " , $ b ) ; $ a_real = intval ( $ x [ 0 ] ) ; $ a_img = intval ( $ x [ 1 ] ) ; $ b_real = intval ( $ y [ 0 ] ) ; $ b_img = intval ( $ y [ 1 ] ) ; return ( $ a_real * $ b_real - $ a_img * $ b_img ) . " + " ( $ a_real * $ b_img + $ a_img * $ b_real ) . " i " ; } $ str1 = "1 + 1i " ; $ str2 = "1 + 1i " ; echo complexNumberMultiply ( $ str1 , $ str2 ) ; ? >
Average of Squares of Natural Numbers | Function to get the average ; Driver Code
< ? php function AvgofSquareN ( $ n ) { return ( ( $ n + 1 ) * ( 2 * $ n + 1 ) ) / 6 ; } $ n = 2 ; echo ( AvgofSquareN ( $ n ) ) ; ? >
Program to implement Simpson 's 3/8 rule | Given function to be integrated ; Function to perform calculations ; Calculates value till integral limit ; Driver Code
< ? php function func ( $ x ) { return ( 1 / ( 1 + $ x * $ x ) ) ; } function calculate ( $ lower_limit , $ upper_limit , $ interval_limit ) { $ interval_size = ( $ upper_limit - $ lower_limit ) / $ interval_limit ; $ sum = func ( $ lower_limit ) + func ( $ upper_limit ) ; for ( $ i = 1 ; $ i < $ interval_limit ; $ i ++ ) { if ( $ i % 3 == 0 ) $ sum = $ sum + 2 * func ( $ lower_limit + $ i * $ interval_size ) ; else $ sum = $ sum + 3 * func ( $ lower_limit + $ i * $ interval_size ) ; } return ( 3 * $ interval_size / 8 ) * $ sum ; } $ interval_limit = 10 ; $ lower_limit = 1 ; $ upper_limit = 10 ; $ integral_res = calculate ( $ lower_limit , $ upper_limit , $ interval_limit ) ; echo $ integral_res ; ? >
Program for focal length of a lens | Function to determine the focal length of a lens ; variable to store the distance between the lens and the image ; variable to store the distance between the lens and the object
< ? php function focal_length ( $ image_distance , $ object_distance ) { return 1 / ( ( 1 / $ image_distance ) + ( 1 / $ object_distance ) ) ; } $ image_distance = 2 ; $ object_distance = 50 ; echo " Focal ▁ length ▁ of ▁ a ▁ lens ▁ is ▁ " , focal_length ( $ image_distance , $ object_distance ) , " ▁ units ▁ . " ; ? >
Count numbers in range L | check if the number is divisible by the digits . ; function to calculate the number of numbers ; Driver function
< ? php function check ( $ n ) { $ m = $ n ; while ( $ n ) { $ r = $ n % 10 ; if ( $ r > 0 ) if ( ( $ m % $ r ) != 0 ) return false ; $ n /= 10 ; } return true ; } function countIn ( $ l , $ r ) { $ ans = 0 ; for ( $ i = $ l ; $ i <= $ r ; $ i ++ ) if ( check ( $ i ) ) $ ans += 1 ; return $ ans ; } $ l = 10 ; $ r = 20 ; echo countIn ( $ l , $ r ) ;
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
< ? 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 series ( n / 1 ) + ( n / 2 ) + ( n / 3 ) + ( n / 4 ) + ... ... . + ( n / n ) | function to find sum of series ; Driver code
< ? 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 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
< ? 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 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
< ? php function sumofsquare ( $ n ) { $ i ; $ j ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= min ( $ i , $ n ) ; $ j ++ ) { if ( $ j == 0 $ j == $ i ) $ C [ $ i ] [ $ j ] = 1 ; else $ C [ $ i ] [ $ j ] = $ C [ $ i - 1 ] [ $ j - 1 ] + $ C [ $ i - 1 ] [ $ j ] ; } } $ sum = 0 ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ sum += ( $ C [ $ n ] [ $ i ] * $ C [ $ n ] [ $ i ] ) ; return $ sum ; } $ n = 4 ; echo sumofsquare ( $ n ) , " STRNEWLINE " ; ? >
Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n | Function that find sum of series . ; Driver Code ; Function call
< ? php function sumOfSeries ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) for ( $ j = 1 ; $ j <= $ i ; $ j ++ ) $ sum = $ sum + $ i ; return $ sum ; } $ n = 10 ; echo ( sumOfSeries ( $ n ) ) ; ? >
Find sum of even index binomial coefficients | Return the sum of even index term ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; finding sum of even index term . ; Driver Code
< ? php function evenSum ( $ n ) { $ C = array ( array ( ) ) ; $ i ; $ j ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= min ( $ i , $ n ) ; $ j ++ ) { if ( $ j == 0 or $ j == $ i ) $ C [ $ i ] [ $ j ] = 1 ; else $ C [ $ i ] [ $ j ] = $ C [ $ i - 1 ] [ $ j - 1 ] + $ C [ $ i - 1 ] [ $ j ] ; } } $ sum = 0 ; for ( $ i = 0 ; $ i <= $ n ; $ i += 2 ) $ sum += $ C [ $ n ] [ $ i ] ; return $ sum ; } $ n = 4 ; echo evenSum ( $ n ) ; ? >
Program to print triangular number series till n | Function to find triangular number ; Driver Code
< ? php function triangular_series ( $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) echo ( " ▁ " . $ i * ( $ i + 1 ) / 2 . " ▁ " ) ; } $ n = 5 ; triangular_series ( $ n ) ; ? >
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 integers . ; if n is multiple of 3 ; else print " - 1" . ; Driver Program
< ? php function checksum ( $ n ) { if ( $ n % 3 == 0 ) echo $ n / 3 - 1 , " ▁ " , $ n / 3 , " ▁ " , $ n / 3 + 1 ; else 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 ' ; Driver code
< ? php function divisorSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; ++ $ i ) $ sum += floor ( $ n / $ i ) * $ i ; return $ sum ; } $ n = 4 ; echo divisorSum ( $ n ) , " STRNEWLINE " ; $ n = 5 ; echo divisorSum ( $ n ) , " STRNEWLINE " ; ? >
N | function to evaluate Nth polite number ; Driver code
< ? php function polite ( $ n ) { $ n += 1 ; $ base = 2 ; return $ n + ( log ( ( $ n + ( log ( $ n ) / log ( $ base ) ) ) ) ) / log ( $ base ) ; } $ n = 7 ; echo ( ( int ) polite ( $ n ) ) ; ? >
Find the number of stair steps | Modified Binary search function to solve the equation ; if mid is solution to equation ; if our solution to equation lies between mid and mid - 1 ; if solution to equation is greater than mid ; if solution to equation is less than mid ; Driver Code ; call binary search method to solve for limits 1 to T ; Because our pattern starts from 2 , 3 , 4 , 5. . . so , we subtract 1 from ans
< ? php function solve ( $ low , $ high , $ T ) { while ( $ low <= $ high ) { $ mid = ( $ low + $ high ) / 2 ; if ( ( $ mid * ( $ mid + 1 ) ) == $ T ) return $ mid ; if ( $ mid > 0 && ( $ mid * ( $ mid + 1 ) ) > $ T && ( $ mid * ( $ mid - 1 ) ) <= $ T ) return $ mid - 1 ; if ( ( $ mid * ( $ mid + 1 ) ) > $ T ) $ high = $ mid - 1 ; else $ low = $ mid + 1 ; } return -1 ; } $ T = 15 ; $ ans = solve ( 1 , $ T , 2 * $ T ) ; if ( $ ans != -1 ) $ ans -- ; echo " Number ▁ of ▁ stair ▁ steps ▁ = ▁ " , $ ans , " STRNEWLINE " ; ? >
Check for integer overflow on multiplication | Function to check whether there is overflow in a * b or not . It returns true if there is overflow . ; Check if either of them is zero ; Driver code
< ? php function isOverflow ( $ a , $ b ) { if ( $ a == 0 $ b == 0 ) return false ; $ result = $ a * $ b ; if ( $ a == ( int ) $ result / $ b ) return false ; else return true ; } $ a = 10000000000 ; $ b = -10000000000 ; if ( isOverflow ( $ a , $ b ) ) echo " Yes " ; else echo " No " ; ? >
Sum of first n odd numbers in O ( 1 ) Complexity | Returns the sum of first n odd numbers ; Driver Code
< ? 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 ) ; ? >
Sum of first n odd numbers in O ( 1 ) Complexity | Returns the sum of first n odd numbers ; Driver Code
< ? php function oddSum ( $ n ) { return ( $ n * $ n ) ; } $ n = 20 ; echo " ▁ Sum ▁ of ▁ first ▁ " , $ n , " ▁ Odd ▁ Numbers ▁ is : ▁ " , oddSum ( $ n ) ; ? >
K | Returns the sum of first n odd numbers ; Count prime factors of all numbers till B . ; Print all numbers with k prime factors ; Driver code
< ? php function printKPFNums ( $ A , $ B , $ K ) { $ prime = array_fill ( true , $ B + 1 , NULL ) ; $ p_factors = array_fill ( 0 , $ B + 1 , NULL ) ; for ( $ p = 2 ; $ p <= $ B ; $ p ++ ) if ( $ p_factors [ $ p ] == 0 ) for ( $ i = $ p ; $ i <= $ B ; $ i += $ p ) $ p_factors [ $ i ] ++ ; for ( $ i = $ A ; $ i <= $ B ; $ i ++ ) if ( $ p_factors [ $ i ] == $ K ) echo $ i . " " ; } $ A = 14 ; $ B = 18 ; $ K = 2 ; printKPFNums ( $ A , $ B , $ K ) ; ? >
Queries for maximum difference between prime numbers in given ranges | PHP program to find maximum differences between two prime numbers in given ranges ; Precompute Sieve , Prefix array , Suffix array ; Sieve of Eratosthenes ; Precomputing Prefix array . ; Precompute Suffix array . ; Function to solve each query ; Driver Code
< ? php $ MAX = 100005 ; function precompute ( & $ prefix , & $ suffix ) { global $ MAX ; $ prime = array_fill ( 0 , $ MAX , true ) ; for ( $ i = 2 ; $ i * $ i < $ MAX ; $ i ++ ) { if ( $ prime [ $ i ] ) { for ( $ j = $ i * $ i ; $ j < $ MAX ; $ j += $ i ) $ prime [ $ j ] = false ; } } $ prefix [ 1 ] = 1 ; $ suffix [ $ MAX - 1 ] = 1e9 + 7 ; for ( $ i = 2 ; $ i < $ MAX ; $ i ++ ) { if ( $ prime [ $ i ] ) $ prefix [ $ i ] = $ i ; else $ prefix [ $ i ] = $ prefix [ $ i - 1 ] ; } for ( $ i = $ MAX - 1 ; $ i > 1 ; $ i -- ) { if ( $ prime [ $ i ] ) $ suffix [ $ i ] = $ i ; else $ suffix [ $ i ] = $ suffix [ $ i + 1 ] ; } } function query ( $ prefix , $ suffix , $ L , $ R ) { if ( $ prefix [ $ R ] < $ L $ suffix [ $ L ] > $ R ) return 0 ; else return $ prefix [ $ R ] - $ suffix [ $ L ] ; } $ q = 3 ; $ L = array ( 2 , 2 , 24 ) ; $ R = array ( 5 , 2 , 28 ) ; $ prefix = array_fill ( 0 , $ MAX + 1 , 0 ) ; $ suffix = array_fill ( 0 , $ MAX + 1 , 0 ) ; precompute ( $ prefix , $ suffix ) ; for ( $ i = 0 ; $ i < $ q ; $ i ++ ) echo query ( $ prefix , $ suffix , $ L [ $ i ] , $ R [ $ i ] ) . " STRNEWLINE " ; ? >
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 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ total = $ total + ( pow ( $ x , $ i ) / $ i ) ; return $ total ; } $ x = 2 ; $ n = 5 ; echo ( sum ( $ x , $ n ) ) ; ? >
Find if a number is part of AP whose first element and difference are given | returns yes if exist else no . ; If difference is 0 , then x must be same as a ; Else difference between x and a must be divisible by d . ; Driver code .
< ? php function isMember ( $ a , $ d , $ x ) { if ( $ d == 0 ) return ( $ x == $ a ) ; return ( ( $ x - $ a ) % $ d == 0 && ( $ x - $ a ) / $ d >= 0 ) ; } $ a = 1 ; $ x = 7 ; $ d = 3 ; if ( isMember ( $ a , $ d , $ x ) ) echo " Yes " ; else echo " No " ; ? >
Check whether a number can be represented by sum of two squares | function to check if there exist two numbers sum of whose squares is n . ; Driver Code
< ? php function sumSquare ( int $ n ) { for ( $ i = 1 ; $ i * $ i <= $ n ; $ i ++ ) for ( $ j = 1 ; $ j * $ j <= $ n ; $ j ++ ) if ( $ i * $ i + $ j * $ j == $ n ) { echo $ i , " ^ 2 ▁ + ▁ " , $ j , " ^ 2" ; return true ; } return false ; } $ n = 25 ; if ( sumSquare ( $ n ) ) echo " ▁ STRNEWLINE " , " Yes " ; else echo " No " ; ? >
Smallest root of the equation x ^ 2 + s ( x ) * x | function to check if the sum of digits is equal to the summation assumed ; calculate the sum of digit ; function to find the largest root possible . ; iterate for all possible sum of digits . ; check if discriminent is a perfect square . ; check if discriminent is a perfect square and if it as perefect root of the equation ; function returns answer ; Driver Code
< ? php function check ( $ a , $ b ) { $ c = 0 ; while ( $ a != 0 ) { $ c = $ c + $ a % 10 ; $ a = ( int ) ( $ a / 10 ) ; } return ( $ c == $ b ) ? true : false ; } function root ( $ n ) { $ found = false ; $ mx = 1000000000000000001 ; for ( $ i = 0 ; $ i <= 90 ; $ i ++ ) { $ s = $ i * $ i + 4 * $ n ; $ sq = ( int ) ( sqrt ( $ s ) ) ; if ( $ sq * $ sq == $ s && check ( ( int ) ( ( $ sq - $ i ) / 2 ) , $ i ) ) { $ found = true ; $ mx = min ( $ mx , ( int ) ( ( $ sq - $ i ) / 2 ) ) ; } } if ( $ found ) return $ mx ; else return -1 ; } $ n = 110 ; echo root ( $ n ) ; ? >
Sum of digits of a given number to a given power | Function to calculate sum ; Driver Code
< ? 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 ) ) ; ? >
Co | Utility function ; function to check if pair is co - prime or not ; function to find and print co - prime pair ; Driver code
< ? php function gcd ( $ num1 , $ num2 ) { while ( $ num2 != 0 ) { $ t = $ num1 % $ num2 ; $ num1 = $ num2 ; $ num2 = $ t ; } return $ num1 ; } function coprime ( $ a , $ b ) { if ( gcd ( $ a , $ b ) == 1 ) return 1 ; else return 0 ; } function pairSum ( $ n ) { $ mid = ( int ) ( ( $ n / 2 ) ) ; for ( $ i = $ mid ; $ i >= 1 ; $ i -- ) { if ( coprime ( $ i , $ n - $ i ) == 1 ) { echo $ i . " ▁ " . ( $ n - $ i ) ; break ; } } } $ n = 11 ; pairSum ( $ n ) ; ? >
Program for quotient and remainder of big number | Function to calculate the modulus ; Store the modulus of big number ; Do step by step division ; Update modulo by concatenating current digit . ; Update quotient ; Update mod for next iteration . ; Flag used to remove starting zeros ; Driver Code
< ? php function modBigNumber ( $ num , $ m ) { $ vec ; $ x = 0 ; $ mod = 0 ; for ( $ i = 0 ; $ i < strlen ( $ num ) ; $ i ++ ) { $ digit = $ num [ $ i ] - '0' ; $ mod = $ mod * 10 + $ digit ; $ quo = ( int ) ( $ mod / $ m ) ; $ vec [ $ x ++ ] = $ quo ; $ mod = $ mod % $ m ; } echo " Remainder ▁ : ▁ " . $ mod . " STRNEWLINE " ; echo " Quotient ▁ : ▁ " ; $ zeroflag = 0 ; for ( $ i = 0 ; $ i < $ x ; $ i ++ ) { if ( $ vec [ $ i ] == 0 && $ zeroflag == 0 ) continue ; $ zeroflag = 1 ; echo $ vec [ $ i ] ; } return ; } $ num = "14598499948265358486" ; $ m = 487 ; modBigNumber ( $ num , $ m ) ; ? >
Queries to find whether a number has exactly four distinct factors or not | Initialize global variable according to given condition so that it can be accessible to all function ; Function to calculate all number having four distinct factors ; 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 ; Initialize prime [ ] array which will contains all the primes from 1 - N ; Iterate over all the prime numbers ; Mark cube root of prime numbers ; Mark product of prime numbers ; Driver Code
< ? php $ N = 1000001 ; $ fourDiv = array_fill ( 0 , $ N + 1 , false ) ; function fourDistinctFactors ( ) { global $ N ; global $ fourDiv ; $ primeAll = array_fill ( 0 , $ N + 1 , true ) ; for ( $ p = 2 ; $ p * $ p <= $ N ; $ p ++ ) { if ( $ primeAll [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ N ; $ i += $ p ) $ primeAll [ $ i ] = false ; } } $ prime ; $ x = 0 ; for ( $ p = 2 ; $ p <= $ N ; $ p ++ ) if ( $ primeAll [ $ p ] ) $ prime [ $ x ++ ] = $ p ; for ( $ i = 0 ; $ i < $ x ; ++ $ i ) { $ p = $ prime [ $ i ] ; if ( 1 * $ p * $ p * $ p <= $ N ) $ fourDiv [ $ p * $ p * $ p ] = true ; for ( $ j = $ i + 1 ; $ j < $ x ; ++ $ j ) { $ q = $ prime [ $ j ] ; if ( 1 * $ p * $ q > $ N ) break ; $ fourDiv [ $ p * $ q ] = true ; } } } fourDistinctFactors ( ) ; $ num = 10 ; if ( $ fourDiv [ $ num ] ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; $ num = 12 ; if ( $ fourDiv [ $ num ] ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; ? >
Leonardo Number | A simple recursive program to find n - th leonardo number . ; Driver code
< ? php function leonardo ( $ n ) { $ dp [ 0 ] = $ dp [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ dp [ $ i ] = $ dp [ $ i - 1 ] + $ dp [ $ i - 2 ] + 1 ; return $ dp [ $ n ] ; } echo leonardo ( 3 ) ; ? >
Cholesky Decomposition : Matrix Decomposition | PHP program to decompose a matrix using Cholesky Decomposition ; Decomposing a matrix into Lower Triangular ; summation for diagonals ; Evaluating L ( i , j ) using L ( j , j ) ; Displaying Lower Triangular and its Transpose ; Lower Triangular ; Transpose of Lower Triangular ; Driver Code
< ? php $ MAX = 100 ; function Cholesky_Decomposition ( $ matrix , $ n ) { $ lower ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) for ( $ j = 0 ; $ j <= $ n ; $ j ++ ) $ lower [ $ i ] [ $ j ] = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ i ; $ j ++ ) { $ sum = 0 ; if ( $ j == $ i ) { for ( $ k = 0 ; $ k < $ j ; $ k ++ ) $ sum += pow ( $ lower [ $ j ] [ $ k ] , 2 ) ; $ lower [ $ j ] [ $ j ] = sqrt ( $ matrix [ $ j ] [ $ j ] - $ sum ) ; } else { for ( $ k = 0 ; $ k < $ j ; $ k ++ ) $ sum += ( $ lower [ $ i ] [ $ k ] * $ lower [ $ j ] [ $ k ] ) ; $ lower [ $ i ] [ $ j ] = ( $ matrix [ $ i ] [ $ j ] - $ sum ) / $ lower [ $ j ] [ $ j ] ; } } } echo " TABSYMBOL Lower ▁ Triangular " . str_pad ( " Transpose " , 30 , " ▁ " , STR_PAD_BOTH ) . " STRNEWLINE " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) echo str_pad ( $ lower [ $ i ] [ $ j ] , 6 , " ▁ " , STR_PAD_BOTH ) . " TABSYMBOL " ; echo " TABSYMBOL " ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) echo str_pad ( $ lower [ $ j ] [ $ i ] , 6 , " ▁ " , STR_PAD_BOTH ) . " TABSYMBOL " ; echo " STRNEWLINE " ; } } $ n = 3 ; $ matrix = array ( array ( 4 , 12 , -16 ) , array ( 12 , 37 , -43 ) , array ( -16 , -43 , 98 ) ) ; Cholesky_Decomposition ( $ matrix , $ n ) ; ? >
Program for sum of arithmetic series | Efficient PHP code to find sum of arithmetic series . ; Driver code
< ? php function sumOfAP ( $ a , $ d , $ n ) { $ sum = ( $ n / 2 ) * ( 2 * $ a + ( $ n - 1 ) * $ d ) ; return $ sum ; } $ n = 20 ; $ a = 2.5 ; $ d = 1.5 ; echo ( sumOfAP ( $ a , $ d , $ n ) ) ; ? >
Program for cube sum of first n natural numbers | Returns sum of first n natural numbers ; Driver code
< ? php function sumOfSeries ( $ n ) { $ x ; if ( $ n % 2 == 0 ) $ x = ( $ n / 2 ) * ( $ n + 1 ) ; else $ x = ( ( $ n + 1 ) / 2 ) * $ n ; return $ x * $ x ; } $ n = 5 ; echo sumOfSeries ( $ n ) ; ? >
Maximum value of | arr [ i ] | PHP program to find maximum value of | arr [ i ] - arr [ j ] | + | i - j | ; Return maximum value of | arr [ i ] - arr [ j ] | + | i - j | ; Iterating two for loop , one for i and another for j . ; Evaluating | arr [ i ] - arr [ j ] | + | i - j | and compare with previous maximum . ; Driver Code
< ? php $ MAX = 10 ; function findValue ( $ arr , $ n ) { $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ ans = max ( $ ans , abs ( $ arr [ $ i ] - $ arr [ $ j ] ) + abs ( $ i - $ j ) ) ; return $ ans ; } $ arr = array ( 1 , 2 , 3 , 1 ) ; $ n = count ( $ arr ) ; echo findValue ( $ arr , $ n ) ; ? >
Maximum value of | arr [ i ] | Return maximum | arr [ i ] - arr [ j ] | + | i - j | ; Calculating first_array and second_array ; Finding maximum and minimum value in first_array ; Storing the difference between maximum and minimum value in first_array ; Finding maximum and minimum value in second_array ; Storing the difference between maximum and minimum value in second_array ; Driver Code
< ? php function findValue ( $ arr , $ n ) { $ a [ ] = array ( ) ; $ b = array ( ) ; $ tmp ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ a [ $ i ] = ( $ arr [ $ i ] + $ i ) ; $ b [ $ i ] = ( $ arr [ $ i ] - $ i ) ; } $ x = $ a [ 0 ] ; $ y = $ a [ 0 ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] > $ x ) $ x = $ a [ $ i ] ; if ( $ a [ $ i ] < $ y ) $ y = $ a [ $ i ] ; } $ ans1 = ( $ x - $ y ) ; $ x = $ b [ 0 ] ; $ y = $ b [ 0 ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ b [ $ i ] > $ x ) $ x = $ b [ $ i ] ; if ( $ b [ $ i ] < $ y ) $ y = $ b [ $ i ] ; } $ ans2 = ( $ x - $ y ) ; return max ( $ ans1 , $ ans2 ) ; } $ arr = array ( 1 , 2 , 3 , 1 ) ; $ n = count ( $ arr ) ; echo findValue ( $ arr , $ n ) ; ? >
Number of subarrays having product less than K | PHP program to count subarrays having product less than k . ; Counter for single element ; Multiple subarray ; If this multiple is less than k , then increment ; Driver Code
< ? php function countsubarray ( $ array , $ n , $ k ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ array [ $ i ] < $ k ) $ count ++ ; $ mul = $ array [ $ i ] ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { $ mul = $ mul * $ array [ $ j ] ; if ( $ mul < $ k ) $ count ++ ; else break ; } } return $ count ; } $ array = array ( 1 , 2 , 3 , 4 ) ; $ k = 10 ; $ size = sizeof ( $ array ) ; $ count = countsubarray ( $ array , $ size , $ k ) ; echo ( $ count . " " ) ; ? >