codeparrot/xlcost-text-to-code huggingface dataset:
Find integers that divides maximum number of elements of the array | Function to print the integers that divide the maximum number of elements from the array ; Initialize two lists to store rank and factors ; Start from 2 till the maximum element in arr ; Initialize a variable to count the number of elements it is a factor of ; Maximum rank in the rank list ; Print all the elements with rank m ; Driver code
< ? php function maximumFactor ( $ arr ) { $ rank = array ( ) ; $ factors = array ( ) ; for ( $ i = 2 ; $ i <= max ( $ arr ) ; $ i ++ ) { $ count = 0 ; for ( $ j = 0 ; $ j < sizeof ( $ arr ) ; $ j ++ ) if ( $ arr [ $ j ] % $ i == 0 ) $ count += 1 ; array_push ( $ rank , $ count ) ; array_push ( $ factors , $ i ) ; } $ m = max ( $ rank ) ; for ( $ i = 0 ; $ i < sizeof ( $ rank ) ; $ i ++ ) { if ( $ rank [ $ i ] == $ m ) echo $ factors [ $ i ] , " ▁ " ; } } $ arr = array ( 120 , 15 , 24 , 63 , 18 ) ; maximumFactor ( $ arr ) ? >
Natural Numbers | Returns sum of first n natural numbers ; Driver code
< ? php function findSum ( $ n ) { $ sum = 0 ; for ( $ x = 1 ; $ x <= $ n ; $ x ++ ) $ sum = $ sum + $ x ; return $ sum ; } $ n = 5 ; echo findSum ( $ n ) ; ? >
Median | Function for calculating median ; First we sort the array ; check for even case ; Driver Code
< ? php function findMedian ( & $ a , $ n ) { sort ( $ a ) ; if ( $ n % 2 != 0 ) return ( double ) $ a [ $ n / 2 ] ; return ( double ) ( $ a [ ( $ n - 1 ) / 2 ] + $ a [ $ n / 2 ] ) / 2.0 ; } $ a = array ( 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 ) ; $ n = sizeof ( $ a ) ; echo " Median ▁ = ▁ " . findMedian ( $ a , $ n ) ; ? >
Mean | Function for calculating mean ; Driver Code
< ? php function findMean ( & $ a , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ a [ $ i ] ; return ( double ) $ sum / ( double ) $ n ; } $ a = array ( 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 ) ; $ n = sizeof ( $ a ) ; echo " Mean ▁ = ▁ " . findMean ( $ a , $ n ) . " STRNEWLINE " ; ? >
Check if the array has an element which is equal to product of remaining elements | Function to Check if the array has an element which is equal to product of all the remaining elements ; Storing frequency in map ; Calculate the product of all the elements ; If the prod is a perfect square ; then check if its square root exist in the array or not ; Driver code
< ? php function CheckArray ( $ arr , $ n ) { $ prod = 1 ; $ freq = array ( ) ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { array_push ( $ freq , $ arr [ $ i ] ) ; $ prod *= $ arr [ $ i ] ; } $ freq = array_unique ( $ freq ) ; $ root = ( int ) ( sqrt ( $ prod ) ) ; if ( $ root * $ root == $ prod ) if ( in_array ( $ root , $ freq ) ) return true ; return false ; } $ arr = array ( 1 , 2 , 12 , 3 , 2 ) ; $ n = count ( $ arr ) ; if ( CheckArray ( $ arr , $ n ) ) echo " YES " ; else echo " NO " ; ? >
Check whether a number has consecutive 0 's in the given base or not | We first convert to given base , then check if the converted number has two consecutive 0 s or not ; Function to convert N into base K ; Weight of each digit ; Function to check for consecutive 0 ; Flag to check if there are consecutive zero or not ; If there are two consecutive zero then returning false ; Driver code
< ? php function hasConsecutiveZeroes ( $ N , $ K ) { $ z = toK ( $ N , $ K ) ; if ( check ( $ z ) ) print ( " Yes " ) ; else print ( " No " ) ; } function toK ( $ N , $ K ) { $ w = 1 ; $ s = 0 ; while ( $ N != 0 ) { $ r = $ N % $ K ; $ N = ( int ) ( $ N / $ K ) ; $ s = $ r * $ w + $ s ; $ w *= 10 ; } return $ s ; } function check ( $ N ) { $ fl = false ; while ( $ N != 0 ) { $ r = $ N % 10 ; $ N = ( int ) ( $ N / 10 ) ; if ( $ fl == true and $ r == 0 ) return false ; if ( $ r > 0 ) { $ fl = false ; continue ; } $ fl = true ; } return true ; } $ N = 15 ; $ K = 8 ; hasConsecutiveZeroes ( $ N , $ K ) ; ? >
Sum of every Kâ €™ th prime number in an array | PHP implementation of the approach ; 0 and 1 are not prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; compute the answer ; count of primes ; sum of the primes ; traverse the array ; if the number is a prime ; increase the count ; if it is the K 'th prime ; create the sieve
< ? php $ MAX = 100000 ; $ prime = array_fill ( 0 , $ MAX + 1 , true ) ; function SieveOfEratosthenes ( ) { global $ MAX , $ prime ; $ prime [ 1 ] = false ; $ prime [ 0 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ MAX ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ MAX ; $ i += $ p ) $ prime [ $ i ] = false ; } } } function SumOfKthPrimes ( $ arr , $ n , $ k ) { global $ MAX , $ prime ; $ c = 0 ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ prime [ $ arr [ $ i ] ] ) { $ c ++ ; if ( $ c % $ k == 0 ) { $ sum += $ arr [ $ i ] ; $ c = 0 ; } } } echo $ sum . " STRNEWLINE " ; } SieveOfEratosthenes ( ) ; $ arr = array ( 2 , 3 , 5 , 7 , 11 ) ; $ n = sizeof ( $ arr ) ; $ k = 2 ; SumOfKthPrimes ( $ arr , $ n , $ k ) ; ? >
Find the super power of a given Number | PHP for finding super power of n ; global hash for prime ; sieve method for storing a list of prime ; function to return super power ; find the super power ; Driver Code
< ? php $ MAX = 100000 ; $ prime = array_fill ( 0 , 100002 , true ) ; function SieveOfEratosthenes ( ) { global $ MAX , $ prime ; for ( $ p = 2 ; $ p * $ p <= $ MAX ; $ p ++ ) if ( $ prime [ $ p ] == true ) for ( $ i = $ p * 2 ; $ i <= $ MAX ; $ i += $ p ) $ prime [ $ i ] = false ; } function superpower ( $ n ) { SieveOfEratosthenes ( ) ; global $ MAX , $ prime ; $ superPower = 0 ; $ factor = 0 ; $ i = 2 ; while ( $ n > 1 && $ i <= $ MAX ) { if ( $ prime [ $ i ] ) { $ factor = 0 ; while ( $ n % $ i == 0 && $ n > 1 ) { $ factor ++ ; $ n = $ n / $ i ; } if ( $ superPower < $ factor ) $ superPower = $ factor ; } $ i ++ ; } return $ superPower ; } $ n = 256 ; echo superpower ( $ n ) ; ? >
Smallest prime divisor of a number | Function to find the smallest divisor ; if divisible by 2 ; iterate from 3 to sqrt ( n ) ; Driver Code
< ? php function smallestDivisor ( $ n ) { if ( $ n % 2 == 0 ) return 2 ; for ( $ i = 3 ; $ i * $ i <= $ n ; $ i += 2 ) { if ( $ n % $ i == 0 ) return $ i ; } return $ n ; } $ n = 31 ; echo smallestDivisor ( $ n ) ; ? >
Count Number of animals in a zoo from given number of head and legs | Function that calculates Rabbits ; Driver code
< ? php function countRabbits ( $ Heads , $ Legs ) { $ count = 0 ; $ count = ( $ Legs ) - 2 * ( $ Heads ) ; $ count = ( int ) $ count / 2 ; return $ count ; } $ Heads = 100 ; $ Legs = 300 ; $ Rabbits = countRabbits ( $ Heads , $ Legs ) ; echo " Rabbits = " ▁ , ▁ $ Rabbits ▁ , ▁ " " ; STRNEWLINE echo ▁ " Pigeons = " $ Rabbits , " STRNEWLINE " ; ? >
Program to evaluate the expression ( ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¾¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬¹ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’ Ã ¢ â ‚¬¦¡¬ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬¦¡ X + 1 ) ^ 6 + ( ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¾¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬¹ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’ Ã ¢ â ‚¬¦¡¬ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬¦¡ X | Function to find the sum ; Driver Code
< ? php function calculateSum ( $ n ) { $ a = ( int ) $ n ; return ( 2 * ( pow ( $ n , 6 ) + 15 * pow ( $ n , 4 ) + 15 * pow ( $ n , 2 ) + 1 ) ) ; } $ n = 1.4142 ; echo ceil ( calculateSum ( $ n ) ) ; ? >
Find the sum of series 3 , | calculate sum upto N term of series ; Driver code
< ? php function Sum_upto_nth_Term ( $ n ) { return ( 1 - pow ( -2 , $ n ) ) ; } $ N = 5 ; echo ( Sum_upto_nth_Term ( $ N ) ) ; ? >
Count numbers whose XOR with N is equal to OR with N | Function to calculate count of numbers with XOR equals OR ; variable to store count of unset bits ; Driver code
< ? php function xorEqualsOrCount ( $ N ) { $ count = 0 ; while ( $ N > 0 ) { $ bit = $ N % 2 ; if ( $ bit == 0 ) $ count ++ ; $ N = intval ( $ N / 2 ) ; } return pow ( 2 , $ count ) ; } $ N = 7 ; echo xorEqualsOrCount ( $ N ) ; ? >
Program to find sum of 1 + x / 2 ! + x ^ 2 / 3 ! + ... + x ^ n / ( n + 1 ) ! | Function to find the factorial of a number ; Function to compute the sum ; Iterate the loop till n and compute the formula ; Get x and n ; Print output
< ? php function fact ( $ n ) { if ( $ n == 1 ) return 1 ; return $ n * fact ( $ n - 1 ) ; } function sum ( $ x , $ n ) { $ total = 1.0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ total = $ total + ( pow ( $ x , $ i ) / fact ( $ i + 1 ) ) ; } return $ total ; } $ x = 5 ; $ n = 4 ; echo " Sum ▁ is : ▁ " , sum ( $ x , $ n ) ; ? >
Find Sum of Series 1 ^ 2 | PHP program to find sum of series 1 ^ 2 - 2 ^ 2 + 3 ^ 3 - 4 ^ 4 + ... Function to find sum of series ; If i is even ; If i is odd ; return the result ; Get n ; Find the sum ; Get n ; Find the sum ; This Code is Contributed by anuj_67
< ? php function sum_of_series ( $ n ) { $ result = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( $ i % 2 == 0 ) $ result = $ result - pow ( $ i , 2 ) ; else $ result = $ result + pow ( $ i , 2 ) ; } return $ result ; } $ n = 3 ; echo sum_of_series ( $ n ) , " STRNEWLINE " ; $ n = 10 ; echo sum_of_series ( $ n ) , " STRNEWLINE " ; ? >
Program to find the sum of the series 23 + 45 + 75 + ... . . upto N terms | calculate Nth term of series ; Get the value of N ; Get the sum of the series
< ? php function findSum ( $ N ) { return ( 2 * $ N * ( $ N + 1 ) * ( 4 * $ N + 17 ) + 54 * $ N ) / 6 ; } $ N = 4 ; echo findSum ( $ N ) ; ? >
Program to find the value of sin ( nÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬¦½ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬¹ ÃƒÆ ’ Ã ¢ â ‚¬¦ Ãƒ ¢ Ã ¢ â € š ¬ Ã … â € œ ) | PHP Program to find the value of sin ( n ? ) ; This function use to calculate the binomial coefficient upto 15 ; use simple DP to find coefficient ; Function to find the value of ; find cosTheta from sinTheta ; store required answer ; use to toggle sign in sequence . ; Driver code .
< ? php $ MAX = 16 ; $ nCr = array_fill ( 0 , $ MAX , array_fill ( 0 , $ MAX , 0 ) ) ; function binomial ( ) { global $ MAX , $ nCr ; for ( $ i = 0 ; $ i < $ MAX ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ i ; $ j ++ ) { if ( $ j == 0 $ j == $ i ) $ nCr [ $ i ] [ $ j ] = 1 ; else $ nCr [ $ i ] [ $ j ] = $ nCr [ $ i - 1 ] [ $ j ] + $ nCr [ $ i - 1 ] [ $ j - 1 ] ; } } } function findCosNTheta ( $ sinTheta , $ n ) { global $ MAX , $ nCr ; $ cosTheta = sqrt ( 1 - $ sinTheta * $ sinTheta ) ; $ ans = 0 ; $ toggle = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i += 2 ) { $ ans = $ ans + $ nCr [ $ n ] [ $ i ] * pow ( $ cosTheta , $ n - $ i ) * pow ( $ sinTheta , $ i ) * $ toggle ; $ toggle = $ toggle * -1 ; } return $ ans ; } binomial ( ) ; $ sinTheta = 0.5 ; $ n = 10 ; echo findCosNTheta ( $ sinTheta , $ n ) ; ? >
Program to find Nth term of series 9 , 23 , 45 , 75 , 113. . . | calculate Nth term of series ; Get the value of N ; Find the Nth term and print it
< ? php function nthTerm ( $ N ) { return ( 2 * $ N + 3 ) * ( 2 * $ N + 3 ) - 2 * $ N ; } $ N = 4 ; echo nthTerm ( $ N ) ; ? >
Find sum of the series 1 + 22 + 333 + 4444 + ... ... upto n terms | Function to calculate sum ; Return sum ; Driver code
< ? php function findSum ( $ n ) { return ( pow ( 10 , $ n + 1 ) * ( 9 * $ n - 1 ) + 10 ) / pow ( 9 , 3 ) - $ n * ( $ n + 1 ) / 18 ; } $ n = 3 ; echo findSum ( $ n ) ; ? >
Find sum of the series 1 | Function to calculate sum ; when n is odd ; when n is not odd ; Driver code
< ? php function solve_sum ( $ n ) { if ( $ n % 2 == 1 ) return ( $ n + 1 ) / 2 ; return - $ n / 2 ; } $ n = 8 ; echo solve_sum ( $ n ) ; ? >
Check if a number can be expressed as a ^ b | Set 2 | PHP program to check if a number can be expressed as a ^ b . ; Driver code
< ? php function isPower ( $ a ) { if ( $ a == 1 ) return true ; for ( $ i = 2 ; $ i * $ i <= $ a ; $ i ++ ) { $ val = log ( $ a ) / log ( $ i ) ; if ( ( $ val - $ val ) < 0.00000001 ) return true ; } return false ; } $ n = 16 ; echo ( isPower ( $ n ) ? " Yes " : " No " ) ;
Program to calculate Root Mean Square | Function that Calculate Root Mean Square ; Calculate square . ; Calculate Mean . ; Calculate Root . ; Driver code
< ? php function rmsValue ( $ arr , $ n ) { $ square = 0 ; $ mean = 0.0 ; $ root = 0.0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ square += pow ( $ arr [ $ i ] , 2 ) ; } $ mean = ( $ square / ( float ) ( $ n ) ) ; $ root = sqrt ( $ mean ) ; return $ root ; } $ arr = array ( 10 , 4 , 6 , 8 ) ; $ n = sizeof ( $ arr ) ; echo rmsValue ( $ arr , $ n ) ; ? >
Program to find the quantity after mixture replacement | Function to calculate the Remaining amount . ; calculate Right hand Side ( RHS ) . ; calculate Amount left by multiply it with original value . ; Driver Code
< ? php function Mixture ( $ X , $ Y , $ Z ) { $ result = 0.0 ; $ result1 = 0.0 ; $ result1 = ( ( $ X - $ Y ) / $ X ) ; $ result = pow ( $ result1 , $ Z ) ; $ result = $ result * $ X ; return $ result ; } $ X = 10 ; $ Y = 2 ; $ Z = 2 ; echo Mixture ( $ X , $ Y , $ Z ) , " ▁ litres " ; ? >
Sum of sum of all subsets of a set formed by first N natural numbers | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; $x = $x % $p ; Update x if it is more than or equal to p ; If y is odd , multiply x with the result ; y must be even now $y = $y >> 1 ; y = y / 2 ; function to find ff ( n ) ; In formula n is starting from zero ; calculate answer using formula 2 ^ n * ( n ^ 2 + n + 2 ) - 1 ; whenever answer is greater than or equals to mod then modulo it . ; adding modulo while subtraction is very necessary otherwise it will cause wrong answer ; Driver code ; function call
< ? php function power ( $ x , $ y , $ p ) { while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function check ( $ n ) { $ mod = 1e9 + 7 ; $ n -- ; $ ans = $ n * $ n ; if ( $ ans >= $ mod ) $ ans %= $ mod ; $ ans += $ n + 2 ; if ( $ ans >= $ mod ) $ ans %= $ mod ; $ ans = ( power ( 2 , $ n , $ mod ) % $ mod * $ ans % $ mod ) % $ mod ; $ ans = ( $ ans - 1 + $ mod ) % $ mod ; return $ ans ; } $ n = 4 ; echo check ( $ n ) . " STRNEWLINE " ; ? >
Program to find LCM of 2 numbers without using GCD | Function to return LCM of two numbers ; Driver Code
< ? php function findLCM ( $ a , $ b ) { $ lar = max ( $ a , $ b ) ; $ small = min ( $ a , $ b ) ; for ( $ i = $ lar ; ; $ i += $ lar ) { if ( $ i % $ small == 0 ) return $ i ; } } $ a = 5 ; $ b = 7 ; echo " LCM ▁ of ▁ " , $ a , " ▁ and ▁ " , $ b , " ▁ is ▁ " , findLCM ( $ a , $ b ) ; ? >
Smarandache | Function to collect first ' n ' prime numbers ; List to store first ' n ' primes ; Function to generate Smarandache - Wellin Sequence ; Storing the first ' n ' prime numbers in a list ; Driver Code
< ? php function primes ( $ n ) { $ i = 2 ; $ j = 0 ; $ result ; $ z = 0 ; while ( $ j < $ n ) { $ flag = true ; for ( $ item = 2 ; $ item <= ( int ) ( $ i * 1 / 2 ) ; $ item ++ ) if ( $ i % $ item == 0 && $ i != $ item ) { $ flag = false ; break ; } if ( $ flag ) { $ result [ $ z ++ ] = $ i ; $ j += 1 ; } $ i += 1 ; } return $ result ; } function smar_wln ( $ n ) { $ arr = primes ( $ n ) ; for ( $ i = 0 ; $ i < count ( $ arr ) ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ i ; $ j ++ ) echo $ arr [ $ j ] ; echo " ▁ " ; } } $ n = 5 ; echo " First ▁ $ n ▁ terms ▁ of ▁ the " . " ▁ Sequence ▁ are STRNEWLINE " ; smar_wln ( $ n ) ; ? >
Pentatope number | function for Pentatope number ; formula for find Pentatope nth term ; Driver Code
< ? php function Pentatope_number ( $ n ) { return $ n * ( $ n + 1 ) * ( $ n + 2 ) * ( $ n + 3 ) / 24 ; } $ n = 7 ; echo $ n , " th ▁ Pentatope ▁ number ▁ : " , Pentatope_number ( $ n ) , " STRNEWLINE " ; $ n = 12 ; echo $ n , " th ▁ Pentatope ▁ number ▁ : " , Pentatope_number ( $ n ) ; ? >
Program for Centered Icosahedral Number | Function to find Centered icosahedral number ; Formula to calculate nth Centered icosahedral number and return it into main function . ; Driver Code
< ? php function centeredIcosahedralNum ( $ n ) { return ( 2 * $ n + 1 ) * ( 5 * $ n * $ n + 5 * $ n + 3 ) / 3 ; } $ n = 10 ; echo centeredIcosahedralNum ( $ n ) , " STRNEWLINE " ; $ n = 12 ; echo centeredIcosahedralNum ( $ n ) , " STRNEWLINE " ; ? >
Centered Square Number | Function to calculate Centered square number function ; Formula to calculate nth Centered square number ; Driver Code
< ? php function centered_square_num ( $ n ) { return $ n * $ n + ( ( $ n - 1 ) * ( $ n - 1 ) ) ; } $ n = 7 ; echo $ n , " th ▁ Centered ▁ square ▁ number : ▁ " ; echo centered_square_num ( $ n ) ; ? >
Sum of first n natural 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 ) ) ; ? >
Dodecagonal number | function for Dodecagonal number ; formula for find Dodecagonal nth term ; Driver code
< ? php function Dodecagonal_number ( $ n ) { return 5 * $ n * $ n - 4 * $ n ; } $ n = 7 ; echo Dodecagonal_number ( $ n ) , " STRNEWLINE " ; $ n = 12 ; echo Dodecagonal_number ( $ n ) , " STRNEWLINE " ; ? >
Arithmetic Number | Sieve Of Eratosthenes ; 1 is not a prime number ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Storing primes in an array ; Update value in primesquare [ p * p ] , if p is prime . ; Function to count divisors ; If number is 1 , then it will have only 1 as a factor . So , total factors will be 1. ; for storing primes upto n ; Calling SieveOfEratosthenes to store prime factors of n and to store square of prime factors of n ; ans will contain total number of distinct divisors ; Loop for counting factors of n ; a [ i ] is not less than cube root n ; Calculating power of a [ i ] in n . cnt is power of prime a [ i ] in n . ; if a [ i ] is a factor of n ; incrementing power ; Calculating number of divisors . If n = a ^ p * b ^ q then total divisors of n are ( p + 1 ) * ( q + 1 ) ; First case ; Second case ; Third casse ; return $ans ; Total divisors ; Returns sum of all factors of n . ; Traversing through all prime factors . ; This condition is to handle the case when n is a prime number greater than 2. ; Check if number is Arithmetic Number or not . ; Driver code
< ? php function SieveOfEratosthenes ( $ n , & $ prime , & $ primesquare , & $ a ) { for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ prime [ $ i ] = true ; for ( $ i = 0 ; $ i <= ( $ n * $ n + 1 ) ; $ i ++ ) $ primesquare [ $ i ] = false ; $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = false ; } } $ j = 0 ; for ( $ p = 2 ; $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] ) { $ a [ $ j ] = $ p ; $ primesquare [ $ p * $ p ] = true ; $ j ++ ; } } } function countDivisors ( $ n ) { if ( $ n == 1 ) return 1 ; $ prime = array_fill ( 0 , ( $ n + 1 ) , false ) ; $ primesquare = array_fill ( 0 , ( $ n * $ n + 1 ) , false ) ; $ a = array_fill ( 0 , $ n , 0 ) ; SieveOfEratosthenes ( $ n , $ prime , $ primesquare , $ a ) ; $ ans = 1 ; for ( $ i = 0 ; ; $ i ++ ) { if ( $ a [ $ i ] * $ a [ $ i ] * $ a [ $ i ] > $ n ) break ; $ cnt = 1 ; while ( $ n % $ a [ $ i ] == 0 ) { $ n = ( int ) ( $ n / $ a [ $ i ] ) ; $ cnt = $ cnt + 1 ; } $ ans = $ ans * $ cnt ; } if ( $ prime [ $ n ] ) $ ans = $ ans * 2 ; else if ( $ primesquare [ $ n ] ) $ ans = $ ans * 3 ; else if ( $ n != 1 ) $ ans = $ ans * 4 ; } function sumofFactors ( $ n ) { $ res = 1 ; for ( $ i = 2 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { $ count = 0 ; $ curr_sum = 1 ; $ curr_term = 1 ; while ( $ n % $ i == 0 ) { $ count ++ ; $ n = ( int ) ( $ n / $ i ) ; $ curr_term *= $ i ; $ curr_sum += $ curr_term ; } $ res *= $ curr_sum ; } if ( $ n >= 2 ) $ res *= ( 1 + $ n ) ; return $ res ; } function checkArithmetic ( $ n ) { $ count = countDivisors ( $ n ) ; $ sum = sumofFactors ( $ n ) ; return ( $ sum % $ count == 0 ) ; } $ n = 6 ; echo ( checkArithmetic ( $ n ) ) ? " Yes " : " No " ; ? >
Finding n | A formula based PHP program to find sum of series with cubes of first n natural numbers ; Driver Code
< ? php function magicOfSequence ( $ N ) { return ( $ N * ( $ N + 1 ) / 2 ) + 2 * $ N ; } $ N = 6 ; echo magicOfSequence ( $ N ) . " STRNEWLINE " ; ? >
Form a number using corner digits of powers | Find next power by multiplying N with current power ; Store digits of Power one by one . ; Calculate carry . ; Store carry in Power array . ; Prints number formed by corner digits of powers of N . ; Storing N raised to power 0 ; Initializing empty result ; One by one compute next powers and add their corner digits . ; Call Function that store power in Power array . ; Store unit and last digits of power in res . ; Driver Code
< ? php function nextPower ( $ N , & $ power ) { $ carry = 0 ; for ( $ i = 0 ; $ i < count ( $ power ) ; $ i ++ ) { $ prod = ( $ power [ $ i ] * $ N ) + $ carry ; $ power [ $ i ] = $ prod % 10 ; $ carry = ( int ) ( $ prod / 10 ) ; } while ( $ carry ) { array_push ( $ power , $ carry % 10 ) ; $ carry = ( int ) ( $ carry / 10 ) ; } } function printPowerNumber ( $ X , $ N ) { $ power = array ( ) ; array_push ( $ power , 1 ) ; $ res = array ( ) ; for ( $ i = 1 ; $ i <= $ X ; $ i ++ ) { nextPower ( $ N , $ power ) ; array_push ( $ res , $ power [ count ( $ power ) - 1 ] ) ; array_push ( $ res , $ power [ 0 ] ) ; } for ( $ i = 0 ; $ i < count ( $ res ) ; $ i ++ ) echo ( $ res [ $ i ] ) ; } $ N = 19 ; $ X = 4 ; printPowerNumber ( $ X , $ N ) ; ? >
First digit in factorial of a number | PHP program for finding the First digit of the large factorial number ; Removing trailing 0 s as this does not change first digit . ; loop for divide the fact until it become the single digit and return the fact ; Driver Code
< ? php function firstDigit ( $ n ) { $ fact = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ fact = $ fact * $ i ; while ( $ fact % 10 == 0 ) $ fact = $ fact / 10 ; } while ( $ fact >= 10 ) $ fact = $ fact / 10 ; return floor ( $ fact ) ; } $ n = 5 ; echo firstDigit ( $ n ) ; ? >
Sum of the series 1.2 . 3 + 2.3 . 4 + ... + n ( n + 1 ) ( n + 2 ) | PHP program to find sum of the series 1.2 . 3 + 2.3 . 4 + 3.4 . 5 + ... ; Driver Code
< ? php function sumofseries ( $ n ) { $ res = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ res += ( $ i ) * ( $ i + 1 ) * ( $ i + 2 ) ; return $ res ; } echo sumofseries ( 3 ) ; ? >
Find N Geometric Means between A and B | Pr$s N geometric means between A and B . ; calculate common ratio ( R ) ; for finding N the Geometric mean between A and B ; Driver Code
< ? php function printGMeans ( $ A , $ B , $ N ) { $ R = pow ( ( $ B / $ A ) , 1.0 / ( $ N + 1 ) ) ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) echo $ A * pow ( $ R , $ i ) , " ▁ " ; } $ A = 3 ; $ B = 81 ; $ N = 2 ; printGMeans ( $ A , $ B , $ N ) ; ? >