codeparrot/xlcost-text-to-code huggingface dataset:
Sum of all the prime divisors of a number | PHP program to find sum of prime divisors of N ; Function to check if the number is prime or not . ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; function to find sum of prime divisors of N ; Driver code
< ? php $ N = 1000005 ; function isPrime ( $ n ) { global $ 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 SumOfPrimeDivisors ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( isPrime ( $ i ) ) $ sum += $ i ; } } return $ sum ; } $ n = 60 ; echo " Sum ▁ of ▁ prime ▁ divisors ▁ of ▁ 60 ▁ is ▁ " . SumOfPrimeDivisors ( $ n ) ; ? >
Sum of all the prime divisors of a number | function to find prime divisors of all numbers from 1 to n ; if the number is prime ; add this prime to all it 's multiples ; Driver code
< ? php function Sum ( $ N ) { for ( $ i = 0 ; $ i <= $ N ; $ i ++ ) $ SumOfPrimeDivisors [ $ i ] = 0 ; for ( $ i = 2 ; $ i <= $ N ; ++ $ i ) { if ( ! $ SumOfPrimeDivisors [ $ i ] ) { for ( $ j = $ i ; $ j <= $ N ; $ j += $ i ) { $ SumOfPrimeDivisors [ $ j ] += $ i ; } } } return $ SumOfPrimeDivisors [ $ N ] ; } $ N = 60 ; echo " Sum ▁ of ▁ prime ▁ divisors ▁ of ▁ 60 ▁ is ▁ " . Sum ( $ N ) ; ? >
Find ( a ^ b ) % m where ' b ' is very large | Function to find power ; 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 ; Driver Code ; String input as b is very large ; Reduce the number B to a small number using Fermat Little
< ? php function power ( $ x , $ y , $ p ) { $ x = $ x % $ p ; while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } $ a = 3 ; $ b = "100000000000000000000000000" ; $ remainderB = 0 ; $ MOD = 1000000007 ; for ( $ i = 0 ; $ i < strlen ( $ b ) ; $ i ++ ) $ remainderB = ( $ remainderB * 10 + $ b [ $ i ] - '0' ) % ( $ MOD - 1 ) ; echo power ( $ a , $ remainderB , $ MOD ) ; ? >
Squares of numbers with repeated single digits | Set 1 ( 3 , 6 and 9 ) | Function to find the square of 333. . .333 , 666. . .666 and 999. . .999 ; if the number is 333. . .333 ; if the number is 666. . .666 ; if the number is 999. . .999 ; variable for hold result ; find the no of digit ; add size - 1 time a in result ; add one time b in result ; add size - 1 time c in result ; add one time d in result ; return result ; Drivers code ; find square of 33. .33 ; find square of 66. .66 ; find square of 66. .66
< ? php function find_Square_369 ( $ num ) { if ( $ num [ 0 ] == '3' ) { $ a = '1' ; $ b = '0' ; $ c = '8' ; $ d = '9' ; } else if ( $ num [ 0 ] == '6' ) { $ a = '4' ; $ b = '3' ; $ c = '5' ; $ d = '6' ; } else { $ a = '9' ; $ b = '8' ; $ c = '0' ; $ d = '1' ; } $ result = " " ; $ size = strlen ( $ num ) ; for ( $ i = 1 ; $ i < $ size ; $ i ++ ) $ result = $ result . $ a ; $ result = $ result . $ b ; for ( $ i = 1 ; $ i < $ size ; $ i ++ ) $ result = $ result . $ c ; $ result = $ result . $ d ; return $ result ; } $ num_3 = "3333" ; $ num_6 = "6666" ; $ num_9 = "9999" ; $ result = " " ; $ result = find_Square_369 ( $ num_3 ) ; echo " Square ▁ of ▁ " . $ num_3 . " ▁ is ▁ : ▁ " . $ result . " STRNEWLINE " ; $ result = find_Square_369 ( $ num_6 ) ; echo " Square ▁ of ▁ " . $ num_6 . " ▁ is ▁ : ▁ " . $ result . " STRNEWLINE " ; $ result = find_Square_369 ( $ num_9 ) ; echo " Square ▁ of ▁ " . $ num_9 . " ▁ is ▁ : ▁ " . $ result . " STRNEWLINE " ; return 0 ; ? >
Trick for modular division ( ( x1 * x2 ... . xn ) / b ) mod ( m ) |
< ? php < ? php $ ans = 1 ; $ mod = 1000000007 * 120 ; for ( $ i = 0 ; $ i < 5 ; $ i ++ ) $ ans = ( $ ans * ( 55555 - $ i ) ) % $ mod ; $ ans = $ ans / 120 ; echo " Answer ▁ using ▁ shortcut : ▁ " , $ ans ; ? >
Ways to multiply n elements with an associative operation | Function to find the required factorial ; Function to find nCr ; function to find the number of ways ; Driver code
< ? php function fact ( $ n ) { if ( $ n == 0 $ n == 1 ) return 1 ; $ ans = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ ans = $ ans * $ i ; return $ ans ; } function nCr ( $ n , $ r ) { $ Nr = $ n ; $ Dr = 1 ; $ ans = 1 ; for ( $ i = 1 ; $ i <= $ r ; $ i ++ ) { $ ans = ( $ ans * $ Nr ) / ( $ Dr ) ; $ Nr -- ; $ Dr ++ ; } return $ ans ; } function solve ( $ n ) { $ N = 2 * $ n - 2 ; $ R = $ n - 1 ; return nCr ( $ N , $ R ) * fact ( $ n - 1 ) ; } $ n = 6 ; echo solve ( $ n ) ; ? >
Pythagorean Triplet with given sum | PHP program to find Pythagorean Triplet of given sum . ; Considering triplets in sorted order . The value of first element in sorted triplet can be at - most n / 3. ; The value of second element must be less than equal to n / 2 ; Driver Code
< ? php function pythagoreanTriplet ( $ n ) { for ( $ i = 1 ; $ i <= $ n / 3 ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j <= $ n / 2 ; $ j ++ ) { $ k = $ n - $ i - $ j ; if ( $ i * $ i + $ j * $ j == $ k * $ k ) { echo $ i , " , ▁ " , $ j , " , ▁ " , $ k ; return ; } } } echo " No ▁ Triplet " ; } $ n = 12 ; pythagoreanTriplet ( $ n ) ; ? >
Program to print binomial expansion series | function to calculate factorial of a number ; function to print the series ; calculating the value of n ! ; loop to display the series ; For calculating the value of nCr ; calculating the value of A to the power k and X to the power k ; display the series ; Driver Code
< ? php function factorial ( $ n ) { $ f = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ f *= $ i ; return $ f ; } function series ( $ A , $ X , $ n ) { $ nFact = factorial ( $ n ) ; for ( $ i = 0 ; $ i < $ n + 1 ; $ i ++ ) { $ niFact = factorial ( $ n - $ i ) ; $ iFact = factorial ( $ i ) ; $ aPow = pow ( $ A , $ n - $ i ) ; $ xPow = pow ( $ X , $ i ) ; echo ( $ nFact * $ aPow * $ xPow ) / ( $ niFact * $ iFact ) , " " ; } } $ A = 3 ; $ X = 4 ; $ n = 5 ; series ( $ A , $ X , $ n ) ; ? >
Sum of series with alternate signed squares of AP | function to calculate series sum ; Driver Code
< ? php function seiresSum ( $ n , $ a ) { $ res = 0 ; for ( $ i = 0 ; $ i < 2 * $ n ; $ i ++ ) { if ( $ i % 2 == 0 ) $ res += $ a [ $ i ] * $ a [ $ i ] ; else $ res -= $ a [ $ i ] * $ a [ $ i ] ; } return $ res ; } $ n = 2 ; $ a = array ( 1 , 2 , 3 , 4 ) ; echo seiresSum ( $ n , $ a ) ; ? >
Power of a prime number ‘ r ’ in n ! | Function to return power of a no . ' r ' in factorial of n ; Keep dividing n by powers of ' r ' and update count ; Driver Code
< ? php function power ( $ n , $ r ) { $ count = 0 ; for ( $ i = $ r ; ( $ n / $ i ) >= 1 ; $ i = $ i * $ r ) $ count += $ n / $ i ; return $ count ; } $ n = 6 ; $ r = 3 ; echo power ( $ n , $ r ) ; ? >
Average of first n odd naturals numbers | Returns the Avg of first n odd numbers ; sum of first n odd number ; Average of first n odd numbers ; Driver Code
< ? php function avg_of_odd_num ( $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += ( 2 * $ i + 1 ) ; return $ sum / $ n ; } $ n = 20 ; echo ( avg_of_odd_num ( $ n ) ) ; ? >
Average of first n odd naturals numbers | Return the average of sum of first n odd numbers ; Driver Code
< ? php function avg_of_odd_num ( $ n ) { return $ n ; } $ n = 8 ; echo ( avg_of_odd_num ( $ n ) ) ; ? >
Program to print Fibonacci Triangle | function to fill Fibonacci Numbers in f [ ] ; 1 st and 2 nd number of the series are 1 and 1 ; Add the previous 2 numbers in the series and store it ; Fill Fibonacci numbers in f [ ] using fib ( ) . We need N = n * ( n + 1 ) / 2 Fibonacci numbers to make a triangle of height n ; To store next Fibonacci Number to print ; for loop to keep track of number of lines ; For loop to keep track of numbers in each line ; Driver code
< ? php function fib ( & $ f , $ N ) { $ f [ 1 ] = 1 ; $ f [ 2 ] = 1 ; for ( $ i = 3 ; $ i <= $ N ; $ i ++ ) $ f [ $ i ] = $ f [ $ i - 1 ] + $ f [ $ i - 2 ] ; } function fiboTriangle ( $ n ) { $ N = $ n * ( $ n + 1 ) / 2 ; $ f = array ( ) ; fib ( $ f , $ N ) ; $ fiboNum = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ i ; $ j ++ ) echo ( $ f [ $ fiboNum ++ ] . " ▁ " ) ; echo ( " STRNEWLINE " ) ; } } $ n = 5 ; fiboTriangle ( $ n ) ; ? >
Average of odd numbers till a given odd number | Function to calculate the average of odd numbers ; count odd numbers ; store the sum of odd numbers ; Driver Code
< ? php function averageOdd ( $ n ) { if ( $ n % 2 == 0 ) { echo ( " Invalid ▁ Input " ) ; return -1 ; } $ sum = 0 ; $ count = 0 ; while ( $ n >= 1 ) { $ count ++ ; $ sum += $ n ; $ n = $ n - 2 ; } return $ sum / $ count ; } $ n = 15 ; echo ( averageOdd ( $ n ) ) ; ? >
Average of odd numbers till a given odd number | Function to calculate the average of odd numbers ; Driver Code
< ? php function averageOdd ( $ n ) { if ( $ n % 2 == 0 ) { echo ( " Invalid ▁ Input " ) ; return -1 ; } return ( $ n + 1 ) / 2 ; } $ n = 15 ; echo ( averageOdd ( $ n ) ) ; ? >
Trinomial Triangle | Function to find the trinomial triangle value . ; base case ; base case ; recursive step . ; Function to print Trinomial Triangle of height n . ; printing n rows . ; printing first half of triangle ; printing second half of triangle . ; Driver Code
< ? php function TrinomialValue ( $ n , $ k ) { if ( $ n == 0 && $ k == 0 ) return 1 ; if ( $ k < - $ n $ k > $ n ) return 0 ; return TrinomialValue ( $ n - 1 , $ k - 1 ) + TrinomialValue ( $ n - 1 , $ k ) + TrinomialValue ( $ n - 1 , $ k + 1 ) ; } function printTrinomial ( $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = - $ i ; $ j <= 0 ; $ j ++ ) echo TrinomialValue ( $ i , $ j ) , " ▁ " ; for ( $ j = 1 ; $ j <= $ i ; $ j ++ ) echo TrinomialValue ( $ i , $ j ) , " ▁ " ; echo " STRNEWLINE " ; } } $ n = 4 ; printTrinomial ( $ n ) ; ? >
Trinomial Triangle | PHP Program to print trinomial triangle . ; Function to find the trinomial triangle value . ; Using property of trinomial triangle . ; If value already calculated , return that . ; base case ; base case ; recursive step and storing the value . ; Function to print Trinomial Triangle of height n . ; printing n rows . ; printing first half of triangle ; printing second half of triangle . ; Driven Code
< ? php $ MAX = 10 ; function TrinomialValue ( $ dp , $ n , $ k ) { if ( $ k < 0 ) $ k = - $ k ; if ( $ dp [ $ n ] [ $ k ] != 0 ) return $ dp [ $ n ] [ $ k ] ; if ( $ n == 0 && $ k == 0 ) return 1 ; if ( $ k < - $ n $ k > $ n ) return 0 ; return ( $ dp [ $ n ] [ $ k ] = TrinomialValue ( $ dp , $ n - 1 , $ k - 1 ) + TrinomialValue ( $ dp , $ n - 1 , $ k ) + TrinomialValue ( $ dp , $ n - 1 , $ k + 1 ) ) ; } function printTrinomial ( $ n ) { global $ MAX ; $ dp ; for ( $ i = 0 ; $ i < $ MAX ; $ i ++ ) for ( $ j = 0 ; $ j < $ MAX ; $ j ++ ) $ dp [ $ i ] [ $ j ] = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = - $ i ; $ j <= 0 ; $ j ++ ) echo TrinomialValue ( $ dp , $ i , $ j ) . " ▁ " ; for ( $ j = 1 ; $ j <= $ i ; $ j ++ ) echo TrinomialValue ( $ dp , $ i , $ j ) . " ▁ " ; echo " STRNEWLINE " ; } } $ n = 4 ; printTrinomial ( $ n ) ; ? >
Sum of largest prime factor of each number less than equal to n | function to find sum of largest prime factor of each number less than equal to n ; Create an integer array " prime [ 0 . . n ] " and initialize all entries of it as 0. A value in prime [ i ] will finally be 0 if ' i ' is a prime , else it will contain the largest prime factor of ' i ' . ; If prime [ p ] is '0' , then it is a prime number ; Update all multiples of p ; Sum up the largest prime factor of all the numbers ; if ' p ' is a non - prime number then prime [ p ] gives its largesr prime factor ; ' p ' is a prime number ; required sum ; Driver program to test above
< ? php function sumOfLargePrimeFactor ( $ n ) { $ prime = array_fill ( 0 , $ n + 1 , 0 ) ; $ sum = 0 ; $ max = ( int ) ( $ n / 2 ) ; for ( $ p = 2 ; $ p <= $ max ; $ p ++ ) { if ( $ prime [ $ p ] == 0 ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = $ p ; } } for ( $ p = 2 ; $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] ) $ sum += $ prime [ $ p ] ; else $ sum += $ p ; } return $ sum ; } $ n = 12 ; echo " Sum = " ? >
Sum of multiples of a number up to N | Function for calculating sum of multiples of a upto N ; Number of multiples ; sum of first m natural numbers ; sum of multiples ; Driver code
< ? php function calculate_sum ( $ a , $ N ) { $ m = $ N / $ a ; $ sum = $ m * ( $ m + 1 ) / 2 ; $ ans = $ a * $ sum ; return $ ans ; } $ a = 7 ; $ N = 49 ; echo " Sum ▁ of ▁ multiples ▁ of ▁ " . $ a , " ▁ up ▁ to ▁ " . $ N . " ▁ = ▁ " . calculate_sum ( $ a , $ N ) ; ? >
Given a HUGE number check if it 's a power of two. | returns 1 when str is power of 2 return 0 when str is not a power of 2 ; sum stores the intermediate dividend while dividing . ; if the input is "1" then return 0 because 2 ^ k = 1 where k >= 1 and here k = 0 ; Divide the number until it gets reduced to 1 if we are successfully able to reduce the number to 1 it means input string is power of two if in between an odd number appears at the end it means string is not divisible by two hence not a power of 2. ; if the last digit is odd then string is not divisible by 2 hence not a power of two return 0. ; divide the whole string by 2. i is used to track index in current number . j is used to track index for next iteration . ; if num < 2 then we have to take another digit to the right of A [ i ] to make it bigger than A [ i ] . E . g . 214 / 2 -- > 107 ; if it 's not the first index. E.g 214  then we have to include 0. ; for eg . "124" we will not write 064 so if it is the first index just ignore ; After every division by 2 the length of string is changed . ; if the string reaches to 1 then the str is a power of 2. ; Driver code .
< ? php function isPowerOf2 ( $ str ) { $ len_str = strlen ( $ str ) ; $ num = 0 ; if ( $ len_str == 1 && $ str [ $ len_str - 1 ] == '1' ) return 0 ; while ( $ len_str != 1 $ str [ $ len_str - 1 ] != '1' ) { if ( ord ( $ str [ $ len_str - 1 ] - '0' ) % 2 == 1 ) return 0 ; $ j = 0 ; for ( $ i = 0 ; $ i < $ len_str ; $ i ++ ) { $ num = $ num * 10 + ( ord ( $ str [ $ i ] ) - ord ( '0' ) ) ; if ( $ num < 2 ) { if ( $ i != 0 ) $ str [ $ j ++ ] = '0' ; continue ; } $ str [ $ j ++ ] = chr ( ( int ) ( $ num / 2 ) + ord ( '0' ) ) ; $ num = ( $ num ) - ( int ) ( $ num / 2 ) * 2 ; } $ len_str = $ j ; } return 1 ; } $ str1 = "124684622466842024680246842024662202000002" ; $ str2 = "1" ; $ str3 = "128" ; print ( isPowerOf2 ( $ str1 ) . " " . isPowerOf2 ( $ str2 ) . " " ? > ? >
Given a HUGE number check if it 's a power of two. | Function to check whether a number is power of 2 or not ; Driver Code
< ? php function ispowerof2 ( $ num ) { if ( ( $ num & ( $ num - 1 ) ) == 0 ) return 1 ; return 0 ; } $ num = 549755813888 ; echo ispowerof2 ( $ num ) ; ? >
Count divisors of array multiplication | To count number of factors in a number ; Initialize count with 0 ; Increment count for every factor of the given number X . ; Return number of factors ; Returns number of divisors in array multiplication ; Multipliying all elements of the given array . ; Calling function which count number of factors of the number ; Driver code
< ? php function counDivisors ( $ X ) { $ count = 0 ; for ( $ i = 1 ; $ i <= $ X ; ++ $ i ) { if ( $ X % $ i == 0 ) { $ count ++ ; } } return $ count ; } function countDivisorsMult ( $ arr , $ n ) { $ mul = 1 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) $ mul *= $ arr [ $ i ] ; return counDivisors ( $ mul ) ; } $ arr = array ( 2 , 4 , 6 ) ; $ n = sizeof ( $ arr ) ; echo countDivisorsMult ( $ arr , $ n ) ; ? >
Count the number of pairs ( i , j ) such that either arr [ i ] is divisible by arr [ j ] or arr [ j ] is divisible by arr [ i ] | Function to find number of unordered pairs ; Maximum element from the array ; Array to store the frequency of each element ; Stores the number of unordered pairs ; Store the frequency of each element ; Find the number of unordered pairs ; If the number j divisible by ith element is present in the array ; If the ith element of the array has frequency more than one ; Driver code
< ? php function freqPairs ( $ arr , $ n ) { $ max = max ( $ arr ) ; $ freq = array_fill ( 0 , $ max + 1 , 0 ) ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ freq [ $ arr [ $ i ] ] ++ ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 2 * $ arr [ $ i ] ; $ j <= $ max ; $ j += $ arr [ $ i ] ) { if ( $ freq [ $ j ] >= 1 ) $ count += $ freq [ $ j ] ; } if ( $ freq [ $ arr [ $ i ] ] > 1 ) { $ count += $ freq [ $ arr [ $ i ] ] - 1 ; $ freq [ $ arr [ $ i ] ] -- ; } } return $ count ; } $ arr = array ( 3 , 2 , 4 , 2 , 6 ) ; $ n = count ( $ arr ) ; echo freqPairs ( $ arr , $ n ) ; ? >
Find the Nth term of the series 1 + 2 + 6 + 15 + 31 + 56 + ... | calculate Nth term of given series ; Driver code
< ? php function Nth_Term ( $ n ) { return ( 2 * pow ( $ n , 3 ) - 3 * pow ( $ n , 2 ) + $ n + 6 ) / 6 ; } $ N = 8 ; echo Nth_Term ( $ N ) ; ? >
Program to find N | Function to find n - th number in series made of 3 and 5 ; create an array of size ( n + 1 ) ; If i is odd ; Driver code
< ? php function printNthElement ( $ n ) { $ arr = array_fill ( 0 , ( $ n + 1 ) , NULL ) ; $ arr [ 1 ] = 3 ; $ arr [ 2 ] = 5 ; for ( $ i = 3 ; $ i <= $ n ; $ i ++ ) { if ( $ i % 2 != 0 ) $ arr [ $ i ] = $ arr [ $ i / 2 ] * 10 + 3 ; else $ arr [ $ i ] = $ arr [ ( $ i / 2 ) - 1 ] * 10 + 5 ; } return $ arr [ $ n ] ; } $ n = 6 ; echo printNthElement ( $ n ) ; ? >
Program to find Nth term of the series 3 , 6 , 18 , 24 , ... | Function to calculate Nth term of series ; By using abeove formula ; get the value of nthTerm ; Calculate and print the Nth term
< ? php function nthTerm ( $ N ) { return ( $ N * ( ( int ) ( $ N / 2 ) + ( ( $ N % 2 ) * 2 ) + $ N ) ) ; } $ N = 5 ; echo " Nth ▁ term ▁ for ▁ N ▁ = ▁ " , $ N , " ▁ : ▁ " , nthTerm ( $ N ) ; ? >
Program to print binomial expansion series | function to print the series ; Calculating and printing first term ; Computing and printing remaining terms ; Find current term using previous terms We increment power of X by 1 , decrement power of A by 1 and compute nCi using previous term by multiplying previous term with ( n - i + 1 ) / i ; Driver Code
< ? php function series ( $ A , $ X , $ n ) { $ term = pow ( $ A , $ n ) ; echo $ term , " " ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ term = $ term * $ X * ( $ n - $ i + 1 ) / ( $ i * $ A ) ; echo $ term , " " ; } } $ A = 3 ; $ X = 4 ; $ n = 5 ; series ( $ A , $ X , $ n ) ; ? >
Check if a number is divisible by 8 using bitwise operators | function to check number is div by 8 or not using bitwise operator ; Driver program
< ? php function Div_by_8 ( $ n ) { return ( ( ( $ n >> 3 ) << 3 ) == $ n ) ; } $ n = 16 ; if ( Div_by_8 ( $ n ) ) echo " YES " ; else echo " NO " ; ? >
Average of even numbers till a given even number | Function to calculate the average of even numbers ; count even numbers ; store the sum of even numbers ; Driver Code
< ? php function averageEven ( $ n ) { if ( $ n % 2 != 0 ) { echo ( " Invalid ▁ Input " ) ; return -1 ; } $ sum = 0 ; $ count = 0 ; while ( $ n >= 2 ) { $ count ++ ; $ sum += $ n ; $ n = $ n - 2 ; } return $ sum / $ count ; } $ n = 16 ; echo ( averageEven ( $ n ) ) ; ? >
Average of even numbers till a given even number | Function to calculate the average of even numbers ; Driver Code
< ? php function averageEven ( $ n ) { if ( $ n % 2 != 0 ) { echo ( " Invalid ▁ Input " ) ; return -1 ; } return ( $ n + 2 ) / 2 ; } $ n = 16 ; echo ( averageEven ( $ n ) ) ; return 0 ; ? >
Largest number that divides x and is co | Recursive function to return gcd of a and b ; Everything divides 0 ; base case ; a is greater ; function to find largest coprime divisor ; Driver Code
< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 $ b == 0 ) return 0 ; if ( $ a == $ b ) return $ a ; if ( $ a > $ b ) return gcd ( $ a - $ b , $ b ) ; return gcd ( $ a , $ b - $ a ) ; } function cpFact ( $ x , $ y ) { while ( gcd ( $ x , $ y ) != 1 ) { $ x = $ x / gcd ( $ x , $ y ) ; } return $ x ; } $ x = 15 ; $ y = 3 ; echo cpFact ( $ x , $ y ) , " STRNEWLINE " ; $ x = 14 ; $ y = 28 ; echo cpFact ( $ x , $ y ) , " STRNEWLINE " ; $ x = 7 ; $ y = 3 ; echo cpFact ( $ x , $ y ) ; ? >
Count numbers with unit digit k in given range | Returns count of numbers with k as last digit . ; Driver Code
< ? php function counLastDigitK ( $ low , $ high , $ k ) { $ count = 0 ; for ( $ i = $ low ; $ i <= $ high ; $ i ++ ) if ( $ i % 10 == $ k ) $ count ++ ; return $ count ; } $ low = 3 ; $ high = 35 ; $ k = 3 ; echo counLastDigitK ( $ low , $ high , $ k ) ; ? >
Taxicab Numbers | PHP implementation to print first N Taxicab ( 2 ) numbers : ; Starting from 1 , check every number if it is Taxicab until count reaches N . ; Try all possible pairs ( j , k ) whose cube sums can be i . ; Taxicab ( 2 ) found ; Driver code
< ? php function printTaxicab2 ( $ N ) { $ i = 1 ; $ count = 0 ; while ( $ count < $ N ) { $ int_count = 0 ; for ( $ j = 1 ; $ j <= pow ( $ i , 1.0 / 3 ) ; $ j ++ ) for ( $ k = $ j + 1 ; $ k <= pow ( $ i , 1.0 / 3 ) ; $ k ++ ) if ( $ j * $ j * $ j + $ k * $ k * $ k == $ i ) $ int_count ++ ; if ( $ int_count == 2 ) { $ count ++ ; echo $ count , " " , ▁ $ i , ▁ " " } $ i ++ ; } } $ N = 5 ; printTaxicab2 ( $ N ) ; ? >
Composite Number | A optimized school method based PHP program to check if a number is composite . ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver Code
< ? php function isComposite ( $ n ) { if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return false ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return true ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return true ; return false ; } if ( isComposite ( 11 ) ) echo " true " ; else echo " false " ; echo " STRNEWLINE " ; if ( isComposite ( 15 ) ) echo " true " ; else echo " false " ; echo " STRNEWLINE " ; ? >
Insert minimum number in array so that sum of array becomes prime | function to check if a number is prime or not ; Corner case ; Check from 2 to n - 1 ; Find prime number greater than a number ; find prime greater than n ; check if num is prime ; increment num ; To find number to be added so sum of array is prime ; To find sum of array elements ; if sum is already prime return 0 ; To find prime number greater than sum ; Return difference of sum and num ; Driver Code
< ? php function isPrime ( $ n ) { if ( $ n <= 1 ) return false ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) if ( $ n % $ i == 0 ) return false ; return true ; } function findPrime ( $ n ) { $ num = $ n + 1 ; while ( $ num ) { if ( isPrime ( $ num ) ) return $ num ; $ num = $ num + 1 ; } return 0 ; } function minNumber ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] ; if ( isPrime ( $ sum ) ) return 0 ; $ num = findPrime ( $ sum ) ; return $ num - $ sum ; } $ arr = array ( 2 , 4 , 6 , 8 , 12 ) ; $ n = sizeof ( $ arr ) ; echo minNumber ( $ arr , $ n ) ; ? >
Sum of divisors of factorial of a number | function to calculate factorial ; function to calculate sum of divisor ; Returns sum of divisors of n ! ; Driver Code
< ? php function fact ( $ n ) { if ( $ n == 0 ) return 1 ; return $ n * fact ( $ n - 1 ) ; } function div ( $ x ) { $ ans = 0 ; for ( $ i = 1 ; $ i <= $ x ; $ i ++ ) if ( $ x % $ i == 0 ) $ ans += $ i ; return $ ans ; } function sumFactDiv ( $ n ) { return div ( fact ( $ n ) ) ; } $ n = 4 ; echo sumFactDiv ( $ n ) ; ? >