codeparrot/xlcost-text-to-code huggingface dataset:
Solve the Linear Equation of Single Variable | Function to solve the given equation ; Traverse the equation ; For cases such as : x , - x , + x ; Flip sign once ' = ' is seen ; There may be a number left in the end ; For infinite solutions ; For no solution ; x = total sum / coeff of x ' - ' sign indicates moving numeric value to right hand side ; Driver code
< ? php function solveEquation ( $ equation ) { $ n = strlen ( $ equation ) ; $ sign = 1 ; $ coeff = 0 ; $ total = 0 ; $ i = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ equation [ $ j ] == ' + ' $ equation [ $ j ] == ' - ' ) { if ( $ j > $ i ) $ total += $ sign * intval ( substr ( $ equation , $ i , $ j - $ i ) ) ; $ i = $ j ; } else if ( $ equation [ $ j ] == ' x ' ) { if ( ( $ i == $ j ) $ equation [ $ j - 1 ] == ' + ' ) $ coeff += $ sign ; else if ( $ equation [ $ j - 1 ] == ' - ' ) $ coeff -= $ sign ; else $ coeff += $ sign * intval ( substr ( $ equation , $ i , $ j - $ i ) ) ; $ i = $ j + 1 ; } else if ( $ equation [ $ j ] == ' = ' ) { if ( $ j > $ i ) $ total += $ sign * intval ( substr ( $ equation , $ i , $ j - $ i ) ) ; $ sign = -1 ; $ i = $ j + 1 ; } } if ( $ i < $ n ) $ total += $ sign * intval ( substr ( $ equation , $ i ) ) ; if ( $ coeff == 0 && $ total == 0 ) return " Infinite ▁ solutions " ; if ( $ coeff == 0 && $ total ) return " No ▁ solution " ; $ ans = - $ total / $ coeff ; return " x ▁ = ▁ " . $ ans ; } $ equation = " x + 5-3 + x = 6 + x - 2" ; echo ( solveEquation ( $ equation ) ) ; ? >
Check if a given number is Pronic | Efficient Approach | function to check Pronic Number ; Checking Pronic Number by multiplying consecutive numbers ; Driver Code
< ? php function pronic_check ( $ n ) { $ x = floor ( sqrt ( $ n ) ) ; if ( $ x * ( $ x + 1 ) == $ n ) return true ; else return false ; } $ n = 56 ; if ( pronic_check ( $ n ) == true ) echo " YES " ; else echo " NO " ; ? >
Writing power function for large numbers | This function multiplies x with the number represented by res [ ] . res_size is size of res [ ] or number of digits in the number represented by res [ ] . This function uses simple school mathematics for multiplication . This function may value of res_size and returns the new value of res_size ; Initialize carry ; One by one multiply n with individual digits of res [ ] ; Store last digit of ' prod ' in res [ ] ; Put rest in carry ; Put carry in res and increase result size ; This function finds power of a number x ; printing value "1" for power = 0 ; Initialize result ; Multiply x n times ( x ^ n = x * x * x ... . n times ) ; Driver Code
< ? php function multiply ( $ x , $ res ) { $ carry = 0 ; $ res_size = count ( $ res ) ; for ( $ i = 0 ; $ i < $ res_size ; $ i ++ ) { $ prod = $ res [ $ i ] * $ x + $ carry ; $ res [ $ i ] = $ prod % 10 ; $ carry = ( int ) ( $ prod / 10 ) ; } while ( $ carry ) { if ( $ carry % 10 ) $ res [ $ res_size ++ ] = $ carry % 10 ; $ carry = ( int ) ( $ carry / 10 ) ; } return $ res ; } function power ( $ x , $ n ) { if ( $ n == 0 ) { echo "1" ; return ; } $ res_size = 0 ; $ res = array ( ) ; $ temp = $ x ; while ( $ temp != 0 ) { $ res [ $ res_size ++ ] = $ temp % 10 ; $ temp = $ temp / 10 ; } for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res = multiply ( $ x , $ res ) ; echo $ x . " ^ " . $ n . " ▁ = ▁ " ; $ O = 0 ; for ( $ i = count ( $ res ) - 1 ; $ i >= 0 ; $ i -- , $ O ++ ) if ( $ res [ $ i ] ) break ; for ( $ i = count ( $ res ) - $ O - 1 ; $ i >= 0 ; $ i -- ) echo $ res [ $ i ] ; } $ exponent = 100 ; $ base = 2 ; power ( $ base , $ exponent ) ; ? >
P | function to check if number n is a P - smooth number or not ; prime factorise it by 2 ; if the number is divisible by 2 ; check for all the possible numbers that can divide it ; prime factorize it by i ; stores the maximum if maximum and i , if i divides the number ; if n at the end is a prime number , then it a divisor itself ; Driver Code
< ? php function check ( $ n , $ p ) { $ maximum = -1 ; while ( ! ( $ n % 2 ) ) { $ maximum = max ( $ maximum , 2 ) ; $ n = $ n / 2 ; } for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i += 2 ) { while ( $ n % $ i == 0 ) { $ maximum = max ( $ maximum , $ i ) ; $ n = $ n / $ i ; } } if ( $ n > 2 ) $ maximum = max ( $ maximum , $ n ) ; return ( $ maximum <= $ p ) ; } $ n = 24 ; $ p = 7 ; if ( check ( $ n , $ p ) ) echo ( " yes " ) ; else echo ( " no " ) ; ? >
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
< ? php function find_time ( $ h1 ) { $ theta = 30 * $ h1 ; echo ( " ( " . ( $ theta * 2 ) . " / " ▁ . ▁ " 11 " ▁ . ▁ " ) " ▁ . ▁ " minutes " } $ h1 = 3 ; find_time ( $ h1 ) ; ? >
Sum of Series ( n ^ 2 | function that calculate the sum of the nth series ; using formula of the nth term ; Driver Code
< ? php function sum_series ( $ n ) { $ nSquare = $ n * $ n ; return $ nSquare * ( $ nSquare - 1 ) / 4 ; } $ n = 2 ; echo ( sum_series ( $ n ) ) ; ? >
Check if a number is sandwiched between primes | returns true if number n is prime ; 0 and 1 both are non - primes ; finding square root of n ; checking if n has any factor upto square root of n if yes its not prime ; Driver Code
< ? php function isPrime ( $ n ) { if ( $ n == 0 $ n == 1 ) return false ; $ root = sqrt ( $ n ) ; for ( $ i = 2 ; $ i <= $ root ; $ i ++ ) if ( $ n % $ i == 0 ) return false ; return true ; } function isSandwitched ( $ n ) { return ( isPrime ( $ n - 1 ) && isPrime ( $ n + 1 ) ) ; } $ n = 642 ; echo $ n , " ▁ : ▁ " ; if ( isSandwitched ( $ n ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; $ n = 9 ; echo $ n , " ▁ : ▁ " ; if ( isSandwitched ( $ n ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; ? >
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
< ? php function day_of_the_week ( $ y , $ m , $ d ) { $ t = array ( 0 , 3 , 2 , 5 , 0 , 3 , 5 , 1 , 4 , 6 , 2 , 4 ) ; if ( $ m < 3 ) $ y -= 1 ; return ( ( $ y + $ y / 4 - $ y / 100 + $ y / 400 + $ t [ $ m - 1 ] + $ d ) % 7 ) ; } $ day = 13 ; $ month = 7 ; $ year = 2017 ; echo day_of_the_week ( $ year , $ month , $ day ) ; ? >
Recursive program for prime number | Returns true if n is prime , else return false . i is current divisor to check . ; Base cases ; Check for next divisor ; Driver Code
< ? php function isPrime ( $ n , $ i = 2 ) { if ( $ n <= 2 ) return ( $ n == 2 ) ? true : false ; if ( $ n % $ i == 0 ) return false ; if ( $ i * $ i > $ n ) return true ; return isPrime ( $ n , $ i + 1 ) ; } $ n = 15 ; if ( isPrime ( $ n ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >
Square Free Number | Returns true if n is a square free number , else returns false . ; If 2 again divides n , then n is not a square free number . ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; Check if i is a prime factor ; If i again divides , then n is not square free ; Driver Code
< ? php function isSquareFree ( $ n ) { if ( $ n % 2 == 0 ) $ n = $ n / 2 ; if ( $ n % 2 == 0 ) return false ; for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i = $ i + 2 ) { if ( $ n % $ i == 0 ) { $ n = $ n / $ i ; if ( $ n % $ i == 0 ) return false ; } } return true ; } $ n = 10 ; if ( isSquareFree ( $ n ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >
Area of a square from diagonal length | Returns area of square from given diagonal ; Driver Code
< ? php function findArea ( $ d ) { return ( $ d * $ d ) / 2 ; } $ d = 10 ; echo ( findArea ( $ d ) ) ; ? >
Sum of series 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + . . . + ( 2 * n | Function to find sum of series . ; Driver code
< ? 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 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + . . . + ( 2 * n | Function that find sum of series . ; Formula to find sum of series . ; Driver code
< ? php function sumOfSeries ( $ n ) { return ( $ n * ( 2 * $ n - 1 ) * ( 2 * $ n + 1 ) ) / 3 ; } $ n = 10 ; echo ( sumOfSeries ( $ n ) ) ; ? >
Program to implement standard error of mean | Function to find sample mean . ; loop to calculate sum of array elements . ; Function to calculate sample standard deviation . ; Function to calculate sample error . ; Formula to find sample error . ; Driver Code
< ? php function mean ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum = $ sum + $ arr [ $ i ] ; return $ sum / $ n ; } function SSD ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum = $ sum + ( $ arr [ $ i ] - mean ( $ arr , $ n ) ) * ( $ arr [ $ i ] - mean ( $ arr , $ n ) ) ; return sqrt ( $ sum / ( $ n - 1 ) ) ; } function sampleError ( $ arr , $ n ) { return SSD ( $ arr , $ n ) / sqrt ( $ n ) ; } { $ arr = array ( 78.53 , 79.62 , 80.25 , 81.05 , 83.21 , 83.46 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo sampleError ( $ arr , $ n ) ; return 0 ; } ? >
Minimum moves to reach target on a infinite line | Set 2 | Function to find minimum steps to reach target ; Handling negatives$ by symmetry$ ; Keep moving while sum is smaller i . e calculating n ; case 1 : d is even ; d is odd ; Driver code ; Function call
< ? php function StepstoReachTarget ( $ target ) { $ target = abs ( $ target ) ; $ n = ceil ( ( -1.0 + sqrt ( 1 + 8.0 * $ target ) ) / 2 ) ; $ sum = $ n * ( $ n + 1 ) / 2 ; if ( $ sum == $ target ) return $ n ; $ d = $ sum - $ target ; if ( ( $ d & 1 ) == 0 ) return n ; else return $ n + ( ( $ n & 1 ) ? 2 : 1 ) ; } $ target = 5 ; echo StepstoReachTarget ( $ target ) ; ? >
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
< ? 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 ) ) ; ? >
Number of Symmetric Relations on a Set | function find the square of n ; Base case ; Return 2 ^ ( n ( n + 1 ) / 2 ) ; Driver code
< ? php function countSymmetric ( $ n ) { if ( $ n == 0 ) return 1 ; return 1 << ( ( $ n * ( $ n + 1 ) ) / 2 ) ; } $ n = 3 ; echo ( countSymmetric ( $ n ) ) ; ? >
Program for centered nonagonal number | Function to find nth centered nonagonal number . ; Formula to find nth centered nonagonal number . ; Driver Code
< ? php function centeredNonagonal ( $ n ) { return ( 3 * $ n - 2 ) * ( 3 * $ n - 1 ) / 2 ; } $ n = 10 ; echo centeredNonagonal ( $ n ) ; ? >
Program for Mean Absolute Deviation | Function to find mean of the array elements . ; Calculate sum of all elements . ; Function to find mean absolute deviation of given elements . ; Calculate the sum of absolute deviation about mean . ; Return mean absolute deviation about mean . ; Driver Code
< ? php function Mean ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum = $ sum + $ arr [ $ i ] ; return $ sum / $ n ; } function meanAbsoluteDeviation ( $ arr , $ n ) { $ absSum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ absSum = $ absSum + abs ( $ arr [ $ i ] - Mean ( $ arr , $ n ) ) ; return $ absSum / $ n ; } $ arr = array ( 10 , 15 , 15 , 17 , 18 , 21 ) ; $ n = sizeof ( $ arr ) ; echo meanAbsoluteDeviation ( $ arr , $ n ) ; ? >
Find if it is possible to get a ratio from given ranges of costs and quantities | Returns true if it is possible to get ratio r from given cost and quantity ranges . ; Calculating cost corresponding to value of i ; Driver Code
< ? php function isRatioPossible ( $ lowCost , $ upCost , $ lowQuant , $ upQuant , $ r ) { for ( $ i = $ lowQuant ; $ i <= $ upQuant ; $ i ++ ) { $ ans = $ i * $ r ; if ( $ lowCost <= $ ans && $ ans <= $ upCost ) return true ; } return false ; } $ lowCost = 14 ; $ upCost = 30 ; $ lowQuant = 5 ; $ upQuant = 12 ; $ r = 9 ; if ( isRatioPossible ( $ lowCost , $ upCost , $ lowQuant , $ upQuant , $ r ) ) echo " Yes " ; else echo " No " ; # This  code is contributed by ajit NEW_LINE ? >
Find N integers with given difference between product and sum | Function to implement calculation ; Driver Code
< ? php function findNumbers ( $ n , $ d ) { for ( $ i = 0 ; $ i < $ n - 2 ; $ i ++ ) echo "1" , " ▁ " ; echo "2" , " ▁ " ; echo $ n + $ d , " STRNEWLINE " ; } $ N = 3 ; $ D = 5 ; findNumbers ( $ N , $ D ) ; ? >
Sum of fourth powers of first n odd natural numbers | calculate the sum of fourth power of first n odd natural numbers ; Driver Code
< ? php function oddNumSum ( $ n ) { return ( $ n * ( 2 * $ n + 1 ) * ( 24 * $ n * $ n * $ n - 12 * $ n * $ n - 14 * $ n + 7 ) ) / 15 ; } $ n = 4 ; echo ( oddNumSum ( $ n ) ) ; ? >
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
< ? 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 ) ) ; ? >
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
< ? 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
< ? 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 ) ; ? >
Find minimum moves to reach target on an infinite line | PHP program to find minimum moves to reach target if we can move i steps in i - th move . ; Handling negatives by symmetry ; Keep moving while sum is smaller or difference is odd . ; Driver code
< ? php function reachTarget ( $ target ) { $ target = abs ( $ target ) ; $ sum = 0 ; $ step = 0 ; while ( $ sum < $ target or ( $ sum - $ target ) % 2 != 0 ) { $ step ++ ; $ sum += $ step ; } return $ step ; } $ target = 5 ; echo reachTarget ( $ target ) ; ? >
Sum of fifth powers of the first n natural numbers | calculate the sum of fifth power of first n natural numbers ; Driver Code
< ? php function fifthPowerSum ( $ n ) { return ( ( 2 * $ n * $ n * $ n * $ n * $ n * $ n ) + ( 6 * $ n * $ n * $ n * $ n * $ n ) + ( 5 * $ n * $ n * $ n * $ n ) - ( $ n * $ n ) ) / 12 ; } $ n = 5 ; echo ( fifthPowerSum ( $ n ) ) ; ? >
Find unit digit of x raised to power y | Returns unit digit of x raised to power y ; Initialize result as 1 to handle case when y is 0. ; One by one multiply with x mod 10 to avoid overflow . ; Driver Code
< ? php function unitDigitXRaisedY ( $ x , $ y ) { $ res = 1 ; for ( $ i = 0 ; $ i < $ y ; $ i ++ ) $ res = ( $ res * $ x ) % 10 ; return $ res ; } echo ( unitDigitXRaisedY ( 4 , 2 ) ) ; ? >
Max occurring divisor in an interval | function to find max occurring divisor interval [ x , y ] ; if there is only one number in the in the interval , return that number ; otherwise , 2 is the max occurring divisor ; Driver code
< ? php function findDivisor ( $ x , $ y ) { if ( $ x == $ y ) return $ y ; return 2 ; } $ x = 3 ; $ y = 16 ; echo findDivisor ( $ x , $ y ) ; ? >
Average of Squares of Natural Numbers | Function to calculate average of square number ; Driver code
< ? php function AvgofSquareN ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum += ( $ i * $ i ) ; return $ sum / $ n ; } $ n = 2 ; echo ( AvgofSquareN ( $ n ) ) ; ? >
Find sum of even factors of a number | Returns sum of all factors of n . ; If n is odd , then there are no even factors . ; Traversing through all prime factors . ; While i divides n , print i and divide n ; here we remove the 2 ^ 0 that is 1. All other factors ; This condition is to handle the case when n is a prime number . ; Driver code
< ? php function sumofFactors ( $ n ) { if ( $ n % 2 != 0 ) return 0 ; $ res = 1 ; for ( $ i = 2 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { $ count = 0 ; $ curr_sum = 1 ; $ curr_term = 1 ; while ( $ n % $ i == 0 ) { $ count ++ ; $ n = floor ( $ n / $ i ) ; if ( $ i == 2 && $ count == 1 ) $ curr_sum = 0 ; $ curr_term *= $ i ; $ curr_sum += $ curr_term ; } $ res *= $ curr_sum ; } if ( $ n >= 2 ) $ res *= ( 1 + $ n ) ; return $ res ; } $ n = 18 ; echo sumofFactors ( $ n ) ; ? >
Program to determine focal length of a spherical mirror | Determines focal length of a spherical concave mirror ; Determines focal length of a spherical convex mirror ; Driver function
< ? php function focal_length_concave ( $ R ) { return $ R / 2 ; } function focal_length_convex ( $ R ) { return - ( $ R / 2 ) ; } $ R = 30 ; echo " Focal ▁ length ▁ of ▁ spherical " , " concave ▁ mirror ▁ is ▁ : ▁ " , focal_length_concave ( $ R ) , " ▁ units STRNEWLINE " ; echo " Focal ▁ length ▁ of ▁ spherical " , " ▁ convex ▁ mirror ▁ is ▁ : ▁ " , focal_length_convex ( $ R ) , " ▁ units " ; ? >
Find sum of odd factors of a number | Returns sum of all factors of n . ; Traversing through all prime factors . ; ignore even factors by removing all powers of 2 ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number . ; Driver code
< ? php function sumofoddFactors ( $ n ) { $ res = 1 ; while ( $ n % 2 == 0 ) $ n = $ n / 2 ; for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { $ count = 0 ; $ curr_sum = 1 ; $ curr_term = 1 ; while ( $ n % $ i == 0 ) { $ count ++ ; $ n = $ n / $ i ; $ curr_term *= $ i ; $ curr_sum += $ curr_term ; } $ res *= $ curr_sum ; } if ( $ n >= 2 ) $ res *= ( 1 + $ n ) ; return $ res ; } $ n = 30 ; echo sumofoddFactors ( $ n ) ; ? >
Number of non | return number of non negative integral solutions ; initialize total = 0 ; Base Case if n = 1 and val >= 0 then it should return 1 ; iterate the loop till equal the val ; total solution of equations and again call the recursive function Solutions ( variable , value ) ; return the total no possible solution ; Driver Code
< ? php function countSolutions ( $ n , $ val ) { $ total = 0 ; if ( $ n == 1 && $ val >= 0 ) return 1 ; for ( $ i = 0 ; $ i <= $ val ; $ i ++ ) { $ total += countSolutions ( $ n - 1 , $ val - $ i ) ; } return $ total ; } $ n = 5 ; $ val = 20 ; echo countSolutions ( $ n , $ val ) ; ? >
Fibonomial coefficient and Fibonomial triangle | PHP Program to print Fibonomial Triangle of height n . ; Function to produce Fibonacci Series . ; 0 th and 1 st number of the series are 0 and 1 ; Add the previous 2 numbers in the series and store it ; Function to produce fibonomial coefficient ; Function to print Fibonomial Triangle . ; Finding the fibonacci series . ; to store triangle value . ; initialising the 0 th element of each row and diagonal element equal to 0. ; for each row . ; for each column . ; finding each element using recurrence relation . ; printing the Fibonomial Triangle . ; Driven Program
< ? php $ N = 6 ; function fib ( & $ f , $ n ) { $ f [ 0 ] = 0 ; $ f [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ f [ $ i ] = $ f [ $ i - 1 ] + $ f [ $ i - 2 ] ; } function fibcoef ( $ fc , $ f , $ n ) { for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ fc [ $ i ] [ 0 ] = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ i ; $ j ++ ) { $ k = $ j ; while ( $ k -- ) $ fc [ $ i ] [ $ j ] *= $ f [ $ k ] ; $ k = 1 ; while ( ( $ j + 1 ) != $ k ) $ fc [ $ i ] [ $ j ] /= $ f [ $ k ++ ] ; } } } function printFibonomialTriangle ( $ n ) { global $ N ; $ f = array_fill ( 0 , $ N + 1 , 0 ) ; fib ( $ f , $ n ) ; $ dp = array_fill ( 0 , $ N + 1 , array_fill ( 0 , $ N + 1 , 0 ) ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ dp [ $ i ] [ 0 ] = $ dp [ $ i ] [ $ i ] = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 1 ; $ j < $ i ; $ j ++ ) $ dp [ $ i ] [ $ j ] = $ f [ $ i - $ j + 1 ] * $ dp [ $ i - 1 ] [ $ j - 1 ] + $ f [ $ j - 1 ] * $ dp [ $ i - 1 ] [ $ j ] ; } for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ i ; $ j ++ ) echo $ dp [ $ i ] [ $ j ] . " ▁ " ; echo " STRNEWLINE " ; } } $ n = 6 ; printFibonomialTriangle ( $ n ) ; ? >
Sum of Arithmetic Geometric Sequence | Return the sum of first n term of AGP ; finding the each term of AGP and adding it to sum . ; Driver Code
< ? php function sumofNterm ( $ a , $ d , $ b , $ r , $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum += ( ( $ a + ( $ i - 1 ) * $ d ) * ( $ b * pow ( $ r , $ i - 1 ) ) ) ; return $ sum ; } $ a = 1 ; $ d = 1 ; $ b = 2 ; $ r = 2 ; $ n = 3 ; echo ( sumofNterm ( $ a , $ d , $ b , $ r , $ 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
< ? php function sumOfTheSeries ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ k = 2 ; for ( $ j = 1 ; $ j <= $ i ; $ j ++ ) { $ sum += $ k ; $ k += 2 ; } } return $ sum ; } $ n = 5 ; echo " Sum = " ? >
Program to find sum of series 1 * 2 * 3 + 2 * 3 * 4 + 3 * 4 * 5 + . . . + n * ( n + 1 ) * ( n + 2 ) | Function to calculate sum of series . ; Driver Code
< ? php function sumOfSeries ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum = $ sum + $ i * ( $ i + 1 ) * ( $ i + 2 ) ; return $ sum ; } $ n = 10 ; echo sumOfSeries ( $ n ) ; ? >
Program to get the Sum of series : 1 | Function to get the series ; Sum of n - 1 terms starting from 2 nd term ; Driver Code
< ? php function Series ( $ x , $ n ) { $ sum = 1 ; $ term = 1 ; $ fct ; $ j ; $ y = 2 ; $ m ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ fct = 1 ; for ( $ j = 1 ; $ j <= $ y ; $ j ++ ) { $ fct = $ fct * $ j ; } $ term = $ term * ( -1 ) ; $ m = $ term * pow ( $ x , $ y ) / $ fct ; $ sum = $ sum + $ m ; $ y += 2 ; } return $ sum ; } $ x = 9 ; $ n = 10 ; $ precision = 4 ; echo substr ( number_format ( Series ( $ x , $ n ) , $ precision + 1 , ' . ' , ' ' ) , 0 , -1 ) ; ? >
Program to get the Sum of series : 1 | Function to get the series ; Computing sum of remaining n - 1 terms . ; Driver Code
< ? php function Series ( $ x , $ n ) { $ sum = 1 ; $ term = 1 ; $ fct = 1 ; $ p = 1 ; $ multi = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ fct = $ fct * $ multi * ( $ multi + 1 ) ; $ p = $ p * $ x * $ x ; $ term = ( -1 ) * $ term ; $ multi += 2 ; $ sum = $ sum + ( $ term * $ p ) / $ fct ; } return $ sum ; } $ x = 9 ; $ n = 10 ; $ precision = 4 ; echo substr ( number_format ( Series ( $ x , $ n ) , $ precision + 1 , ' . ' , ' ' ) , 0 , -1 ) ; ? >