codeparrot/xlcost-text-to-code huggingface dataset:
Sum of first N natural numbers by taking powers of 2 as negative number | to store power of 2 ; to store presum of the power of 2 's ; function to find power of 2 ; to store power of 2 ; to store pre sum ; Function to find the sum ; first store sum of first n natural numbers . ; find the first greater number than given number then minus double of this from answer ; function call ; function call
< ? php $ power = array_fill ( 0 , 31 , 0 ) ; $ pre = array_fill ( 0 , 31 , 0 ) ; function PowerOfTwo ( ) { global $ power , $ pre ; $ x = 1 ; for ( $ i = 0 ; $ i < 31 ; $ i ++ ) { $ power [ $ i ] = $ x ; $ x *= 2 ; } $ pre [ 0 ] = 1 ; for ( $ i = 1 ; $ i < 31 ; $ i ++ ) $ pre [ $ i ] = $ pre [ $ i - 1 ] + $ power [ $ i ] ; } function Sum ( $ n ) { global $ power , $ pre ; $ ans = $ n * ( $ n + 1 ) / 2 ; for ( $ i = 0 ; $ i < 31 ; $ i ++ ) if ( $ power [ $ i ] > $ n ) { $ ans -= 2 * $ pre [ $ i - 1 ] ; break ; } return $ ans ; } PowerOfTwo ( ) ; $ n = 4 ; print ( Sum ( $ n ) ) ; ? >
Check if a number is Quartan Prime or not | Function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver Code ; Check if number is prime and of the form 16 * n + 1
< ? php function isPrime ( $ 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 ; } $ n = 17 ; if ( isPrime ( $ n ) && ( $ n % 16 == 1 ) ) { echo " YES " ; } else { echo " NO " ; }
Print a number strictly less than a given number such that all its digits are distinct . | Function to find a number less than n such that all its digits are distinct ; looping through numbers less than n ; initializing a hash array ; creating a copy of i ; initializing variables to compare lengths of digits ; counting frequency of the digits ; checking if each digit is present once ; Driver code
< ? php function findNumber ( $ n ) { for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { $ count = array_fill ( 0 , 10 , 0 ) ; $ x = $ i ; $ count1 = 0 ; $ count2 = 0 ; while ( $ x ) { $ count [ $ x % 10 ] ++ ; $ x = ( int ) ( $ x / 10 ) ; $ count1 ++ ; } for ( $ j = 0 ; $ j < 10 ; $ j ++ ) { if ( $ count [ $ j ] == 1 ) $ count2 ++ ; } if ( $ count1 == $ count2 ) return $ i ; } } $ n = 8490 ; echo findNumber ( $ n ) ; ? >
Find two distinct prime numbers with given product | Function to generate all prime numbers less than n ; Initialize all entries of boolean array as true . A value in isPrime [ i ] will finally be false if i is Not a prime , else true bool isPrime [ n + 1 ] ; ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to print a prime pair with given product ; Generating primes using Sieve ; Traversing all numbers to find first pair ; Driver Code
< ? php function SieveOfEratosthenes ( $ n , & $ isPrime ) { $ isPrime [ 0 ] = false ; $ isPrime [ 1 ] = false ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ isPrime [ $ i ] = true ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ isPrime [ $ p ] ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ isPrime [ $ i ] = false ; } } } function findPrimePair ( $ n ) { $ flag = 0 ; $ isPrime = array_fill ( 0 , ( $ n + 1 ) , false ) ; SieveOfEratosthenes ( $ n , $ isPrime ) ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) { $ x = ( int ) ( $ n / $ i ) ; if ( $ isPrime [ $ i ] && $ isPrime [ $ x ] and $ x != $ i and $ x * $ i == $ n ) { echo $ i . " ▁ " . $ x ; $ flag = 1 ; return ; } } if ( ! $ flag ) echo " No ▁ such ▁ pair ▁ found " ; } $ n = 39 ; findPrimePair ( $ n ) ; ? >
Program to find the common ratio of three numbers | Utility function ; Function to print a : b : c ; To print the given proportion in simplest form . ; Get the ratios Get ratio a : b1 ; Get ratio b2 : c ; Find the ratio a : b : c
< ? php function __gcd ( $ a , $ b ) { return $ b == 0 ? $ a : __gcd ( $ b , $ a % $ b ) ; } function solveProportion ( $ a , $ b1 , $ b2 , $ c ) { $ A = $ a * $ b2 ; $ B = $ b1 * $ b2 ; $ C = $ b1 * $ c ; $ gcd = __gcd ( __gcd ( $ A , $ B ) , $ C ) ; echo ( $ A / $ gcd ) . " : " . ( $ B / $ gcd ) . " : " . ( $ C / $ gcd ) ; } $ a = 3 ; $ b1 = 4 ; $ b2 = 8 ; $ c = 9 ; solveProportion ( $ a , $ b1 , $ b2 , $ c ) ; ? >
Number of divisors of a given number N which are divisible by K | Function to count number of divisors of N which are divisible by K ; Variable to store count of divisors ; Traverse from 1 to n ; increase the count if both the conditions are satisfied ; Driver code
< ? php function countDivisors ( $ n , $ k ) { $ count = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 && $ i % $ k == 0 ) { $ count ++ ; } } return $ count ; } $ n = 12 ; $ k = 3 ; echo countDivisors ( $ n , $ k ) ;
Calculate volume and surface area of a cone | Function to calculate Volume of cone ; Function to calculate Surface area of cone ; Driver Code ; Printing value of volume and surface area
< ? php function volume ( $ r , $ h ) { $ pi = 3.14159 ; return ( 1 / 3 ) * $ pi * $ r * $ r * $ h ; } function surface_area ( $ r , $ s ) { $ pi = 3.14159 ; return $ pi * $ r * $ s + $ pi * $ r * $ r ; } $ radius = 5 ; $ slant_height = 13 ; $ height = 12 ; echo ( " Volume ▁ Of ▁ Cone ▁ : ▁ " ) ; echo ( volume ( $ radius , $ height ) ) ; echo ( " STRNEWLINE " ) ; echo ( " Surface ▁ Area ▁ Of ▁ Cone ▁ : ▁ " ) ; echo ( surface_area ( $ radius , $ slant_height ) ) ; ? >
Program to find the Nth term of the series 0 , 14 , 40 , 78 , 124 , ... | calculate sum upto Nth term of series ; return the final sum ; Driver code
< ? php function nthTerm ( $ n ) { return 6 * pow ( $ n , 2 ) - 4 * $ n - 2 ; } $ N = 4 ; echo nthTerm ( $ N ) ;
Program to find the Nth term of series 5 , 10 , 17 , 26 , 37 , 50 , 65 , 82 , ... | calculate Nth term of series ; return the final sum ; Driver Code
< ? php function nthTerm ( $ n ) { return pow ( $ n , 2 ) + 2 * $ n + 2 ; } $ N = 4 ; echo nthTerm ( $ N ) ;
Find nth term of a given recurrence relation | function to return required value ; Get the answer ; Return the answer ; Get the value of n ; function call to print result
< ? php function sum ( $ n ) { $ ans = ( $ n * ( $ n - 1 ) ) / 2 ; return $ ans ; } $ n = 5 ; echo sum ( $ n ) ; ? >
Program to find Nth term of the series 3 , 12 , 29 , 54 , 87 , ... | calculate Nth term of series ; Return Nth term ; declaration of number of terms ; Get the Nth term
< ? php function getNthTerm ( $ N ) { return 4 * pow ( $ N , 2 ) - 3 * $ N + 2 ; } $ N = 10 ; echo getNthTerm ( $ N ) ; ? >
Find sum of product of number in given series | function to calculate ( a ^ b ) % p ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now $y = $y >> 1 ; y = y / 2 ; function to return required answer ; modulo inverse of denominator ; calculating commentator part ; calculating t ! ; accumulating the final answer ; Driver code ; function call to print required sum
< ? php function power ( $ x , $ y , $ p ) { $ res = 1 ; $ x = $ x % $ p ; while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function sumProd ( $ n , $ t ) { $ MOD = 1000000007 ; $ dino = power ( $ t + 1 , $ MOD - 2 , $ MOD ) ; $ ans = 1 ; for ( $ i = $ n + $ t + 1 ; $ i > $ n ; -- $ i ) $ ans = ( $ ans % $ MOD * $ i % $ MOD ) % $ MOD ; $ tfact = 1 ; for ( $ i = 1 ; $ i <= $ t ; ++ $ i ) $ tfact = ( $ tfact * $ i ) % $ MOD ; $ ans = $ ans * $ dino - $ tfact + $ MOD ; return $ ans % $ MOD ; } $ n = 3 ; $ t = 2 ; echo sumProd ( $ n , $ t ) ; ? >
Find the sum of series 3 , 7 , 13 , 21 , 31. ... | Function to calculate sum ; Return sum ; Driver code
< ? php function findSum ( $ n ) { return ( $ n * ( pow ( $ n , 2 ) + 3 * $ n + 5 ) ) / 3 ; } $ n = 25 ; echo findSum ( $ n ) ; ? >
Minimum Players required to win the game | function to calculate ( a ^ b ) % ( 10 ^ 9 + 7 ) . ; function to find the minimum required player ; computing the nenomenator ; computing modulo inverse of denominator ; final result ; Driver code
< ? php function power ( $ a , $ b ) { $ mod = 1000000007 ; $ res = 1 ; while ( $ b ) { if ( $ b & 1 ) { $ res *= $ a ; $ res %= $ mod ; } $ b /= 2 ; $ a *= $ a ; $ a %= $ mod ; } return $ res ; } function minPlayer ( $ n , $ k ) { $ mod = 1000000007 ; $ num = ( ( power ( $ k , $ n ) - 1 ) + $ mod ) % $ mod ; $ den = ( power ( $ k - 1 , $ mod - 2 ) + $ mod ) % $ mod ; $ ans = ( ( ( $ num * $ den ) % $ mod ) * $ k ) % $ mod ; return $ ans ; } $ n = 3 ; $ k = 3 ; echo minPlayer ( $ n , $ k ) ; ? >
Sum of Factors of a Number using Prime Factorization | Using SieveOfEratosthenes to find smallest prime factor of all the numbers . For example , if N is 10 , s [ 2 ] = s [ 4 ] = s [ 6 ] = s [ 10 ] = 2 s [ 3 ] = s [ 9 ] = 3 s [ 5 ] = 5 s [ 7 ] = 7 ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries in it as false . ; Initializing smallest factor equal to 2 for all the even numbers ; For odd numbers less then equal to n ; s ( i ) for a prime is the number itself ; For all multiples of current prime number ; i is the smallest prime factor for number " i * j " . ; Function to find sum of all prime factors ; Declaring array to store smallest prime factor of i at i - th index ; Filling values in s [ ] using sieve ; Current prime factor of N ; Power of current prime factor ; N is now N / s [ N ] . If new N als has smallest prime factor as currFactor , increment power ; Update current prime factor as s [ N ] and initializing power of factor as 1. ; Driver code
< ? php function sieveOfEratosthenes ( $ N , & $ s ) { $ prime = array_fill ( 0 , $ N + 1 , false ) ; for ( $ i = 2 ; $ i <= $ N ; $ i += 2 ) $ s [ $ i ] = 2 ; for ( $ i = 3 ; $ i <= $ N ; $ i += 2 ) { if ( $ prime [ $ i ] == false ) { $ s [ $ i ] = $ i ; for ( $ j = $ i ; $ j * $ i <= $ N ; $ j += 2 ) { if ( $ prime [ $ i * $ j ] == false ) { $ prime [ $ i * $ j ] = true ; $ s [ $ i * $ j ] = $ i ; } } } } } function findSum ( $ N ) { $ s = array_fill ( 0 , $ N + 1 , 0 ) ; $ ans = 1 ; sieveOfEratosthenes ( $ N , $ s ) ; $ currFactor = $ s [ $ N ] ; $ power = 1 ; while ( $ N > 1 ) { $ N /= $ s [ $ N ] ; if ( $ currFactor == $ s [ $ N ] ) { $ power ++ ; continue ; } $ sum = 0 ; for ( $ i = 0 ; $ i <= $ power ; $ i ++ ) $ sum += ( int ) pow ( $ currFactor , $ i ) ; $ ans *= $ sum ; $ currFactor = $ s [ $ N ] ; $ power = 1 ; } return $ ans ; } $ n = 12 ; echo " Sum ▁ of ▁ the ▁ factors ▁ is ▁ : ▁ " ; echo findSum ( $ n ) ; ? >
Find Multiples of 2 or 3 or 5 less than or equal to N | Bit count function ; Function to count number of multiples of 2 or 3 or 5 less than or equal to N ; As we have to check divisibility by three numbers , So we can implement bit masking ; we check whether jth bit is set or not , if jth bit is set , simply multiply to prod ; check for set bit ; check multiple of product ; Driver code
< ? php function popcount ( $ value ) { $ count = 0 ; while ( $ value ) { $ count += ( $ value & 1 ) ; $ value = $ value >> 1 ; } return $ count ; } function countMultiples ( $ n ) { $ multiple = array ( 2 , 3 , 5 ) ; $ count = 0 ; $ mask = pow ( 2 , 3 ) ; for ( $ i = 1 ; $ i < $ mask ; $ i ++ ) { $ prod = 1 ; for ( $ j = 0 ; $ j < 3 ; $ j ++ ) { if ( $ i & 1 << $ j ) $ prod = $ prod * $ multiple [ $ j ] ; } if ( popcount ( $ i ) % 2 == 1 ) $ count = $ count + ( int ) ( $ n / $ prod ) ; else $ count = $ count - ( int ) ( $ n / $ prod ) ; } return $ count ; } $ n = 10 ; echo countMultiples ( $ n ) ; ? >
Minimum value of N such that xor from 1 to N is equal to K | Function to find the value of N ; variable to store the result ; handling case for '0' ; handling case for '1' ; when number is completely divided by 4 then minimum ' x ' will be ' k ' ; when number divided by 4 gives 3 as remainder then minimum ' x ' will be ' k - 1' ; else it is not possible to get k for any value of x ; let the given number be 7
< ? php function findN ( $ k ) { $ ans ; if ( $ k == 0 ) $ ans = 3 ; if ( $ k == 1 ) $ ans = 1 ; else if ( $ k % 4 == 0 ) $ ans = $ k ; else if ( $ k % 4 == 3 ) $ ans = $ k - 1 ; else $ ans = -1 ; return $ ans ; } $ k = 7 ; $ res = findN ( $ k ) ; if ( $ res == -1 ) echo " Not ▁ possible " ; else echo $ res ; ? >
Permutations to arrange N persons around a circular table | Function to find no . of permutations ; Driver Code
< ? php function Circular ( $ n ) { $ Result = 1 ; while ( $ n > 0 ) { $ Result = $ Result * $ n ; $ n -- ; } return $ Result ; } $ n = 4 ; echo Circular ( $ n - 1 ) ; ? >
Minimum time required to complete a work by N persons together | Function to calculate the time ; Driver Code
< ? php function calTime ( & $ arr , $ n ) { $ work = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ work += 1 / $ arr [ $ i ] ; return 1 / $ work ; } $ arr = array ( 6.0 , 3.0 , 4.0 ) ; $ n = sizeof ( $ arr ) ; echo calTime ( $ arr , $ n ) ; echo " ▁ Hours " ;
Find the largest twins in given range | Function to find twins ; Create a boolean array " prime [ 0 . . high ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Look for the smallest twin ; If p is not marked , then it is a prime ; Update all multiples of p ; Now print the largest twin in range ; Driver program
< ? php function printTwins ( $ low , $ high ) { $ prime [ $ high + 1 ] = array ( ) ; $ twin = false ; $ prime = array_fill ( 0 , ( $ high + 1 ) , true ) ; $ prime [ 0 ] = $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p <= floor ( sqrt ( $ high ) ) + 1 ; $ p ++ ) { if ( $ prime [ $ p ] ) { for ( $ i = $ p * 2 ; $ i <= $ high ; $ i += $ p ) $ prime [ $ i ] = false ; } } for ( $ i = $ high ; $ i >= $ low ; $ i -- ) { if ( $ prime [ $ i ] && ( $ i - 2 >= $ low && $ prime [ $ i - 2 ] == true ) ) { echo " Largest ▁ twins ▁ in ▁ given ▁ range : ▁ ( " , $ i - 2 , " , ▁ " , $ i , " ) " ; $ twin = true ; break ; } } if ( $ twin == false ) echo " No ▁ such ▁ pair ▁ exists " ; } printTwins ( 10 , 100 ) ; ? >
Complement of a number with any base b | Function to find ( b - 1 ) 's complement ; Calculate number of digits in the given number ; Largest digit in the number system with base b ; Largest number in the number system with base b ; return Complement ; Function to find b 's complement ; b ' s ▁ complement ▁ = ▁ ( b - 1 ) ' s complement + 1 ; Driver code
< ? php function prevComplement ( $ n , $ b ) { $ maxNum = 0 ; $ digits = 0 ; $ num = $ n ; while ( ( int ) $ n != 0 ) { $ digits ++ ; $ n = $ n / 10 ; } $ maxDigit = $ b - 1 ; while ( $ digits -- ) { $ maxNum = $ maxNum * 10 + $ maxDigit ; } return $ maxNum - $ num ; } function complement ( $ n , $ b ) { return prevComplement ( $ n , $ b ) + 1 ; } echo prevComplement ( 25 , 7 ) , " STRNEWLINE " ; echo ( complement ( 25 , 7 ) ) ; ? >
Minimum positive integer value possible of X for given A and B in X = P * A + Q * B | Function to return gcd of a and b ; Driver Code
< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } $ a = 2 ; $ b = 4 ; echo gcd ( $ a , $ b ) ; ? >
Count elements in the given range which have maximum number of divisors | Function to count the elements with maximum number of divisors ; to store number of divisors initialise with zero ; to store the maximum number of divisors ; to store required answer ; Find the first divisible number ; Count number of divisors ; Find number of elements with maximum number of divisors ; Driver code
< ? php function MaximumDivisors ( $ X , $ Y ) { $ arr = array_fill ( 0 , ( $ Y - $ X + 1 ) , NULL ) ; $ mx = PHP_INT_MIN ; $ cnt = 0 ; for ( $ i = 1 ; $ i * $ i <= $ Y ; $ i ++ ) { $ sq = $ i * $ i ; if ( ( $ X / $ i ) * $ i >= $ X ) $ first_divisible = ( $ X / $ i ) * $ i ; else $ first_divisible = ( $ X / $ i + 1 ) * $ i ; for ( $ j = $ first_divisible ; $ j < $ Y ; $ j += $ i ) { if ( $ j < $ sq ) continue ; else if ( $ j == $ sq ) $ arr [ $ j - $ X ] ++ ; else $ arr [ $ j - $ X ] += 2 ; } } for ( $ i = $ X ; $ i <= $ Y ; $ i ++ ) { if ( $ arr [ $ i - $ X ] > $ mx ) { $ cnt = 1 ; $ mx = $ arr [ $ i - $ X ] ; } else if ( $ arr [ $ i - $ X ] == $ mx ) $ cnt ++ ; } return $ cnt ; } $ X = 1 ; $ Y = 10 ; echo MaximumDivisors ( $ X , $ Y ) . " STRNEWLINE " ; ? >
Find First element in AP which is multiple of given prime | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now $y = $y >> 1 ; y = y / 2 ; function to find nearest element in common ; base conditions ; Driver code ; module both A and D ; function call
< ? php function power ( $ x , $ y , $ p ) { $ res = 1 ; $ x = $ x % $ p ; while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function NearestElement ( $ A , $ D , $ P ) { if ( $ A == 0 ) return 0 ; else if ( $ D == 0 ) return -1 ; else { $ X = power ( $ D , $ P - 2 , $ P ) ; return ( $ X * ( $ P - $ A ) ) % $ P ; } } $ A = 4 ; $ D = 9 ; $ P = 11 ; $ A %= $ P ; $ D %= $ P ; echo NearestElement ( $ A , $ D , $ P ) ; ? >
Cunningham chain | Function to print Cunningham chain of the first kind ; Iterate till all elements are printed ; check prime or not ; Driver Code
< ? php function print_C ( $ p0 ) { $ p1 = 0 ; $ i = 0 ; $ x ; $ flag ; $ k ; while ( 1 ) { $ flag = 1 ; $ x = pow ( 2 , $ i ) ; $ p1 = $ x * $ p0 + ( $ x - 1 ) ; for ( $ k = 2 ; $ k < $ p1 ; $ k ++ ) { if ( $ p1 % $ k == 0 ) { $ flag = 0 ; break ; } } if ( $ flag == 0 ) break ; echo $ p1 . " " ; $ i ++ ; } } $ p0 = 2 ; print_C ( $ p0 ) ;
Count pairs with Bitwise AND as ODD number | Function to count number of odd pairs ; variable for counting odd pairs ; find all pairs ; find AND operation check odd or even ; return number of odd pair ; Driver Code ; calling function findOddPair and print number of odd pair
< ? php function findOddPair ( & $ A , $ N ) { $ oddPair = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ N ; $ j ++ ) { if ( ( $ A [ $ i ] & $ A [ $ j ] ) % 2 != 0 ) $ oddPair = $ oddPair + 1 ; } } return $ oddPair ; } $ a = array ( 5 , 1 , 3 , 2 ) ; $ n = sizeof ( $ a ) ; echo ( findOddPair ( $ a , $ n ) ) ; ? >
Sum of all elements up to Nth row in a Pascal triangle | Function to find sum of all elements upto nth row . ; Initialize sum with 0 ; Calculate 2 ^ n ; Driver Code
< ? php function calculateSum ( $ n ) { $ sum = 0 ; $ sum = 1 << $ n ; return ( $ sum - 1 ) ; } $ n = 10 ; echo " ▁ Sum ▁ of ▁ all ▁ elements : " , calculateSum ( $ n ) ; ? >
Divide two integers without using multiplication , division and mod operator | Set2 | Returns the quotient of dividend / divisor . ; Calculate sign of divisor i . e . , sign will be negative only if either one of them is negative otherwise it will be positive ; Remove signs of dividend and divisor ; Zero division Exception . ; Using Formula derived above . ; Driver code
< ? php function Divide ( $ a , $ b ) { $ dividend = $ a ; $ divisor = $ b ; $ sign = ( $ dividend < 0 ) ^ ( $ divisor < 0 ) ? -1 : 1 ; $ dividend = abs ( $ dividend ) ; $ divisor = abs ( $ divisor ) ; if ( $ divisor == 0 ) { echo " Cannot ▁ Divide ▁ by ▁ 0" ; echo " " ; } if ( $ dividend == 0 ) { echo $ a , " ▁ / ▁ " , $ b , " ▁ is ▁ equal ▁ to ▁ : ▁ " , 0 ; echo " " ; } if ( $ divisor == 1 ) { echo $ a , " ▁ / ▁ " , $ b , " ▁ is ▁ equal ▁ to ▁ : ▁ " , $ sign * $ dividend . " STRNEWLINE " ; echo " " ; } echo $ a , " ▁ / ▁ " , $ b , " ▁ is ▁ equal ▁ to ▁ : ▁ " , $ sign * exp ( log ( $ dividend ) - log ( $ divisor ) ) . " STRNEWLINE " ; echo " " ; } $ a = 10 ; $ b = 5 ; Divide ( $ a , $ b ) ; $ a = 49 ; $ b = -7 ; Divide ( $ a , $ b ) ; ? >
Check if the n | Return if the nth term is even or odd . ; If a is even ; If b is even ; If b is odd ; If a is odd ; If b is odd ; If b is eve ; Driver Code
< ? php function findNature ( $ a , $ b , $ n ) { if ( $ n == 0 ) return ( $ a & 1 ) ; if ( $ n == 1 ) return ( $ b & 1 ) ; if ( ! ( $ a & 1 ) ) { if ( ! ( $ b & 1 ) ) return false ; else return ( $ n % 3 != 0 ) ; } else { if ( ! ( $ b & 1 ) ) return ( ( $ n - 1 ) % 3 != 0 ) ; else return ( ( $ n + 1 ) % 3 != 0 ) ; } } $ a = 2 ; $ b = 4 ; $ n = 3 ; if ( findNature ( $ a , $ b , $ n ) == true ) echo " Odd " , " ▁ " ; else echo " Even " , " ▁ " ; ? >
Check if mirror image of a number is same if displayed in seven segment display | Return " Yes " , if the mirror image of number is same as the given number Else return " No " ; Checking if the number contain only 0 , 1 , 8. ; Checking if the number is palindrome or not . ; If corresponding index is not equal . ; Driver Code
< ? php function checkEqual ( $ S ) { for ( $ i = 0 ; $ i < strlen ( $ S ) ; $ i ++ ) { if ( $ S [ $ i ] != '1' && $ S [ $ i ] != '0' && $ S [ $ i ] != '8' ) { return " No " ; } } $ start = 0 ; $ end = strlen ( $ S ) - 1 ; while ( $ start < $ end ) { if ( $ S [ $ start ] != $ S [ $ end ] ) { return " No " ; } $ start ++ ; $ end -- ; } return " Yes " ; } $ S = "101" ; echo checkEqual ( $ S ) ; ? >
Check if given number is a power of d where d is a power of 2 | PHP program to find if a number is power of d where d is power of 2. ; Function to count the number of ways to paint N * 3 grid based on given conditions ; Check if there is only one bit set in n ; count 0 bits before set bit ; If count is a multiple of log2 ( d ) then return true else false ; If there are more than 1 bit set then n is not a power of 4 ; Driver Code
< ? php function Log2n ( $ n ) { return ( $ n > 1 ) ? 1 + Log2n ( $ n / 2 ) : 0 ; } function isPowerOfd ( $ n , $ d ) { $ count = 0 ; if ( $ n && ! ( $ n & ( $ n - 1 ) ) ) { while ( $ n > 1 ) { $ n >>= 1 ; $ count += 1 ; } return ( $ count % ( Log2n ( $ d ) ) == 0 ) ; } return false ; } $ n = 64 ; $ d = 8 ; if ( isPowerOfd ( $ n , $ d ) ) echo $ n , " ▁ " , " is ▁ a ▁ power ▁ of ▁ " , $ d ; else echo $ n , " ▁ " , " is ▁ not ▁ a ▁ power ▁ of ▁ " , $ d ; ? >
Octahedral Number | Function to find octahedral number ; Formula to calculate nth octahedral number and return it into main function . ; Drivers Code ; print result
< ? php function octahedral_num ( $ n ) { return $ n * ( 2 * $ n * $ n + 1 ) / 3 ; } $ n = 5 ; echo $ n , " th ▁ Octahedral ▁ number : ▁ " ; echo octahedral_num ( $ n ) ; ? >
Centered tetrahedral number | Function to find centered Centered tetrahedral number ; Formula to calculate nth Centered tetrahedral number and return it into main function . ; Driver Code
< ? php function centeredTetrahedralNumber ( $ n ) { return ( 2 * $ n + 1 ) * ( $ n * $ n + $ n + 3 ) / 3 ; } $ n = 6 ; echo centeredTetrahedralNumber ( $ n ) ; ? >
Swapping four variables without temporary variable | PHP program to swap 4 variables without using temporary variable ; swapping a and b variables ; swapping b and c variables ; swapping c and d variables ; initialising variables ; Function call
< ? php function swap ( $ a , $ b , $ c , $ d ) { $ a = $ a + $ b ; $ b = $ a - $ b ; $ a = $ a - $ b ; $ b = $ b + $ c ; $ c = $ b - $ c ; $ b = $ b - $ c ; $ c = $ c + $ d ; $ d = $ c - $ d ; $ c = $ c - $ d ; echo " values ▁ after ▁ swapping ▁ are ▁ : ▁ " , " STRNEWLINE " ; echo " a = " ▁ , ▁ $ a ▁ , " " ; STRNEWLINE TABSYMBOL echo ▁ " b = " ▁ , ▁ $ b ▁ , " " ; STRNEWLINE TABSYMBOL echo ▁ " c = " ▁ , $ c ▁ , " " ; STRNEWLINE TABSYMBOL echo ▁ " d = " ▁ , ▁ $ d ▁ , " " } $ a = 1 ; $ b = 2 ; $ c = 3 ; $ d = 4 ; echo " Values ▁ before ▁ swapping ▁ are ▁ : " , " STRNEWLINE " ; echo " a = " ▁ , ▁ $ a ▁ , " " ; STRNEWLINE TABSYMBOL echo ▁ " b = " ▁ , $ b , " " ; STRNEWLINE TABSYMBOL echo ▁ " c = " ▁ , ▁ $ c ▁ , " " ; STRNEWLINE TABSYMBOL echo ▁ " d = " ▁ , ▁ $ d ▁ , " " , " " swap ( $ a , $ b , $ c , $ d ) ; ? >
Sum of first n natural numbers | Function to find the sum of series ; Driver code
< ? php function seriesSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum += $ i * ( $ i + 1 ) / 2 ; return $ sum ; } $ n = 4 ; echo ( seriesSum ( $ n ) ) ; ? >