codeparrot/xlcost-text-to-code huggingface dataset:
Sum of divisors of factorial of a number | allPrimes [ ] stores all prime numbers less than or equal to n . ; Fills above vector allPrimes [ ] for a given n ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is not a prime , else true . ; Loop to update prime [ ] ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store primes in the vector allPrimes ; Function to find all result of factorial number ; Initialize result ; find exponents of all primes which divides n and less than n ; Current divisor ; Find the highest power ( stored in exp ) ' ▁ ▁ of ▁ allPrimes [ i ] ▁ that ▁ divides ▁ n ▁ using ▁ ▁ Legendre ' s formula . ; Using the divisor function to calculate the sum ; return total divisors ; Driver program to run the cases
< ? php $ allPrimes = array ( ) ; function sieve ( $ n ) { global $ allPrimes ; $ prime = array_fill ( 0 , $ n + 1 , true ) ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = false ; } } for ( $ p = 2 ; $ p <= $ n ; $ p ++ ) if ( $ prime [ $ p ] ) array_push ( $ allPrimes , $ p ) ; } function factorialDivisors ( $ n ) { global $ allPrimes ; $ result = 1 ; for ( $ i = 0 ; $ i < count ( $ allPrimes ) ; $ i ++ ) { $ p = $ allPrimes [ $ i ] ; $ exp = 0 ; while ( $ p <= $ n ) { $ exp = $ exp + ( int ) ( $ n / $ p ) ; $ p = $ p * $ allPrimes [ $ i ] ; } $ result = $ result * ( pow ( $ allPrimes [ $ i ] , $ exp +1 ) - 1 ) / ( $ allPrimes [ $ i ] - 1 ) ; } return $ result ; } print ( factorialDivisors ( 4 ) ) ; ? >
Pandigital number in a given base | Return true if n is pandigit else return false . ; Checking length is less than base ; Traversing each digit of the number . ; If digit is integer ; If digit is alphabet ; Checking hash array , if any index is unmarked . ; Driver Program
< ? php function checkPandigital ( $ b , $ n ) { if ( strlen ( $ n ) < $ b ) return 0 ; $ hash = array ( ) ; for ( $ i = 0 ; $ i < $ b ; $ i ++ ) $ hash [ $ i ] = 0 ; for ( $ i = 0 ; $ i < strlen ( $ n ) ; $ i ++ ) { if ( $ n [ $ i ] >= '0' && $ n [ $ i ] <= '9' ) $ hash [ $ n [ $ i ] - '0' ] = 1 ; else if ( ord ( $ n [ $ i ] ) - ord ( ' A ' ) <= $ b - 11 ) $ hash [ ord ( $ n [ $ i ] ) - ord ( ' A ' ) + 10 ] = 1 ; } for ( $ i = 0 ; $ i < $ b ; $ i ++ ) if ( $ hash [ $ i ] == 0 ) return 0 ; return 1 ; } $ b = 13 ; $ n = "1298450376ABC " ; if ( checkPandigital ( $ b , $ n ) ) echo " Yes " ; else echo " No " ; ? >
Convert a number m to n using minimum number of given operations | Function to find minimum number of given operations to convert m to n ; only way is to do - 1 ( m - n ) times ; not possible ; n is greater and n is odd ; perform ' - 1' on m ( or + 1 on n ) ; n is even ; perform ' * 2' on m ( or n / 2 on n ) ; Driver code
< ? php function convert ( $ m , $ n ) { if ( $ m == $ n ) return 0 ; if ( $ m > $ n ) return $ m - $ n ; if ( $ m <= 0 && $ n > 0 ) return -1 ; if ( $ n % 2 == 1 ) return 1 + convert ( $ m , $ n + 1 ) ; else return 1 + convert ( $ m , $ n / 2 ) ; } { $ m = 3 ; $ n = 11 ; echo " Minimum ▁ number ▁ of ▁ " . " operations ▁ : ▁ " , convert ( $ m , $ n ) ; return 0 ; } ? >
Seeds ( Or Seed Roots ) of a number | PHP program to find Seed of a number ; Stores product of digits of x in prodDig [ x ] ; If x has single digit ; If digit product is already computed ; If digit product is not computed before . ; Prints all seeds of n ; Find all seeds using prodDig [ ] ; If there was no seed ; Print seeds ; Driver code
< ? php $ MAX = 10000 ; $ prodDig = array_fill ( 0 , $ MAX , 0 ) ; function getDigitProduct ( $ x ) { global $ prodDig ; if ( $ x < 10 ) return $ x ; if ( $ prodDig [ $ x ] != 0 ) return $ prodDig [ $ x ] ; $ prod = ( int ) ( $ x % 10 ) * getDigitProduct ( ( int ) ( $ x / 10 ) ) ; $ prodDig [ $ x ] = $ prod ; return $ prod ; } function findSeed ( $ n ) { $ res = array ( ) ; for ( $ i = 1 ; $ i <= ( int ) ( $ n / 2 + 1 ) ; $ i ++ ) if ( $ i * getDigitProduct ( $ i ) == $ n ) array_push ( $ res , $ i ) ; if ( count ( $ res ) == 0 ) { echo " NO ▁ seed ▁ exists STRNEWLINE " ; return ; } for ( $ i = 0 ; $ i < count ( $ res ) ; $ i ++ ) echo $ res [ $ i ] . " ▁ " ; } $ n = 138 ; findSeed ( $ n ) ; ? >
Number with maximum number of prime factors | Return smallest number having maximum prime factors . ; Sieve of eratosthenes method to count number of prime factors . ; Finding number having maximum number of prime factor . ; Driver Code
< ? php function maxPrimefactorNum ( $ N ) { $ arr [ $ N + 5 ] = array ( ) ; $ arr = array_fill ( 0 , $ N + 1 , NULL ) ; for ( $ i = 2 ; ( $ i * $ i ) <= $ N ; $ i ++ ) { if ( ! $ arr [ $ i ] ) for ( $ j = 2 * $ i ; $ j <= $ N ; $ j += $ i ) $ arr [ $ j ] ++ ; $ arr [ $ i ] = 1 ; } $ maxval = 0 ; $ maxint = 1 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) { if ( $ arr [ $ i ] > $ maxval ) { $ maxval = $ arr [ $ i ] ; $ maxint = $ i ; } } return $ maxint ; } $ N = 40 ; echo maxPrimefactorNum ( $ N ) , " STRNEWLINE " ; ? >
Sum of all Subarrays | Set 1 | function compute sum all sub - array ; computing sum of subarray using formula ; return all subarray sum ; Driver Code
< ? php function SubArraySum ( $ arr , $ n ) { $ result = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ result += ( $ arr [ $ i ] * ( $ i + 1 ) * ( $ n - $ i ) ) ; return $ result ; } $ arr = array ( 1 , 2 , 3 ) ; $ n = sizeof ( $ arr ) ; echo " Sum ▁ of ▁ SubArray ▁ : ▁ " , SubArraySum ( $ arr , $ n ) , " STRNEWLINE " ; #This  code is contributed by aj_36 NEW_LINE ? >
Highest power of 2 less than or equal to given number | PHP program to find highest power of 2 smaller than or equal to n . ; If i is a power of 2 ; Driver code
< ? php function highestPowerof2 ( $ n ) { $ res = 0 ; for ( $ i = $ n ; $ i >= 1 ; $ i -- ) { if ( ( $ i & ( $ i - 1 ) ) == 0 ) { $ res = $ i ; break ; } } return $ res ; } $ n = 10 ; echo highestPowerof2 ( $ n ) ; ? >
Find Cube Pairs | Set 2 ( A n ^ ( 1 / 3 ) Solution ) | Function to find pairs that can represent the given number as sum of two cubes ; find cube root of n ; create a array of size of size ' cubeRoot ' ; for index i , cube [ i ] will contain i ^ 3 ; Find all pairs in above sorted array cube [ ] whose sum is equal to n ; Driver code
< ? php function findPairs ( $ n ) { $ cubeRoot = pow ( $ n , 1.0 / 3.0 ) ; $ cube = array ( ) ; for ( $ i = 1 ; $ i <= $ cubeRoot ; $ i ++ ) $ cube [ $ i ] = $ i * $ i * $ i ; $ l = 1 ; $ r = $ cubeRoot ; while ( $ l < $ r ) { if ( $ cube [ $ l ] + $ cube [ $ r ] < $ n ) $ l ++ ; else if ( $ cube [ $ l ] + $ cube [ $ r ] > $ n ) $ r -- ; else { echo " ( " , $ l , " , ▁ " , floor ( $ r ) , " ) " ; echo " STRNEWLINE " ; $ l ++ ; $ r -- ; } } } $ n = 20683 ; findPairs ( $ n ) ; ? >
Find the minimum difference between Shifted tables of two numbers | Utility function to find GCD of a and b ; Returns minimum difference between any two terms of shifted tables of ' a ' and ' b ' . ' x ' is shift in table of ' a ' and ' y ' is shift in table of ' b ' . ; Calculate gcd of a nd b ; Calculate difference between x and y ; Driver Code
< ? php function gcd ( $ a , $ b ) { while ( $ b != 0 ) { $ t = $ b ; $ b = $ a % $ b ; $ a = $ t ; } return $ a ; } function findMinDiff ( $ a , $ b , $ x , $ y ) { $ g = gcd ( $ a , $ b ) ; $ diff = abs ( $ x - $ y ) % $ g ; return min ( $ diff , $ g - $ diff ) ; } $ a = 20 ; $ b = 52 ; $ x = 5 ; $ y = 7 ; echo findMinDiff ( $ a , $ b , $ x , $ y ) , " STRNEWLINE " ; ? >
Find all divisors of a natural number | Set 2 | function to print the divisors ; Vector to store half of the divisors ; check if divisors are equal ; push the second divisor in the vector ; The vector will be printed in reverse ; Driver code
< ? php function printDivisors ( $ n ) { $ v ; $ t = 0 ; for ( $ i = 1 ; $ i <= ( int ) sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( ( int ) $ n / $ i == $ i ) echo $ i . " " ; else { echo $ i . " " ; $ v [ $ t ++ ] = ( int ) $ n / $ i ; } } } for ( $ i = count ( $ v ) - 1 ; $ i >= 0 ; $ i -- ) echo $ v [ $ i ] . " ▁ " ; } echo " The ▁ divisors ▁ of ▁ 100 ▁ are : ▁ STRNEWLINE " ; printDivisors ( 100 ) ; ? >
Find all factors of a natural number | Set 1 | function to print the divisors ; Driver Code
< ? php function printDivisors ( $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) if ( $ n % $ i == 0 ) echo $ i , " ▁ " ; } echo " The ▁ divisors ▁ of ▁ 100 ▁ are : STRNEWLINE " ; printDivisors ( 100 ) ; ? >
Find all factors of a natural number | Set 1 | Function to print the divisors ; Note that this loop runs till square root ; If divisors are equal , print only one ; Otherwise print both ; Driver Code
< ? php function printDivisors ( $ n ) { for ( $ i = 1 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) echo $ i , " ▁ " ; else echo $ i , " ▁ " , $ n / $ i , " ▁ " ; } } } echo " The ▁ divisors ▁ of ▁ 100 ▁ are : ▁ STRNEWLINE " ; printDivisors ( 100 ) ; ? >
Sieve of Atkin | PHP program for implementation of Sieve of Atkin ; 2 and 3 are known to be prime ; Initialise the sieve array with false values ; Mark sieve [ n ] is true if one of the following is true : a ) n = ( 4 * x * x ) + ( y * y ) has odd number of solutions , i . e . , there exist odd number of distinct pairs ( x , y ) that satisfy the equation and n % 12 = 1 or n % 12 = 5. b ) n = ( 3 * x * x ) + ( y * y ) has odd number of solutions and n % 12 = 7 c ) n = ( 3 * x * x ) - ( y * y ) has odd number of solutions , x > y and n % 12 = 11 ; Main part of Sieve of Atkin ; Mark all multiples of squares as non - prime ; Print primes using sieve [ ] ; Driver Code
< ? php function SieveOfAtkin ( $ limit ) { if ( $ limit > 2 ) echo 2 , " ▁ " ; if ( $ limit > 3 ) echo 3 , " ▁ " ; $ sieve [ $ limit ] = 0 ; for ( $ i = 0 ; $ i < $ limit ; $ i ++ ) $ sieve [ $ i ] = false ; for ( $ x = 1 ; $ x * $ x < $ limit ; $ x ++ ) { for ( $ y = 1 ; $ y * $ y < $ limit ; $ y ++ ) { $ n = ( 4 * $ x * $ x ) + ( $ y * $ y ) ; if ( $ n <= $ limit && ( $ n % 12 == 1 $ n % 12 == 5 ) ) $ sieve [ $ n ] ^= true ; $ n = ( 3 * $ x * $ x ) + ( $ y * $ y ) ; if ( $ n <= $ limit && $ n % 12 == 7 ) $ sieve [ $ n ] = true ; $ n = ( 3 * $ x * $ x ) - ( $ y * $ y ) ; if ( $ x > $ y && $ n <= $ limit && $ n % 12 == 11 ) $ sieve [ $ n ] ^= true ; } } for ( $ r = 5 ; $ r * $ r < $ limit ; $ r ++ ) { if ( $ sieve [ $ r ] ) { for ( $ i = $ r * $ r ; $ i < $ limit ; $ i += $ r * $ r ) $ sieve [ $ i ] = false ; } } for ( $ a = 5 ; $ a < $ limit ; $ a ++ ) if ( $ sieve [ $ a ] ) echo $ a , " ▁ " ; } $ limit = 20 ; SieveOfAtkin ( $ limit ) ; ? >
Find if a point lies inside a Circle | PHP program to check if a point lies inside a circle or not ; Compare radius of circle with distance of its center from given point ; Driver Code
< ? php function isInside ( $ circle_x , $ circle_y , $ rad , $ x , $ y ) { if ( ( $ x - $ circle_x ) * ( $ x - $ circle_x ) + ( $ y - $ circle_y ) * ( $ y - $ circle_y ) <= $ rad * $ rad ) return true ; else return false ; } $ x = 1 ; $ y = 1 ; $ circle_x = 0 ; $ circle_y = 1 ; $ rad = 2 ; if ( isInside ( $ circle_x , $ circle_y , $ rad , $ x , $ y ) ) echo " Inside " ; else echo " Outside " ; ? >
Find all possible outcomes of a given expression | Utility function to eval1uate a simple expression with one operator only . ; This function eval1uates all possible values and returns a list of eval1uated values . ; To store result ( all possible evaluations of given expression ' expr ' ) ; If there is only one character , it must be a digit ( or operand ) , return it . ; If there are only three characters , middle one must be operator and corner ones must be operand ; every i refers to an operator ; l refers to all the possible values in the left of operator ' expr [ i ] ' ; r refers to all the possible values in the right of operator ' expr [ i ] ' ; Take above eval1uated all possible values in left side of ' i ' ; Take above eval1uated all possible values in right side of ' i ' ; Calculate value for every pair and add the value to result . ; Driver Code
< ? php function eval1 ( $ a , $ op , $ b ) { if ( $ op == ' + ' ) return $ a + $ b ; if ( $ op == ' - ' ) return $ a - $ b ; if ( $ op == ' * ' ) return $ a * $ b ; } function eval1uateAll ( $ expr , $ low , $ high ) { $ res = array ( ) ; if ( $ low == $ high ) { array_push ( $ res , ord ( $ expr [ $ low ] ) - ord ( ' 0 ' ) ) ; return $ res ; } if ( $ low == ( $ high - 2 ) ) { $ num = eval1 ( ord ( $ expr [ $ low ] ) - ord ( '0' ) , $ expr [ $ low + 1 ] , ord ( $ expr [ $ low + 2 ] ) - ord ( '0' ) ) ; array_push ( $ res , $ num ) ; return $ res ; } for ( $ i = $ low + 1 ; $ i <= $ high ; $ i += 2 ) { $ l = eval1uateAll ( $ expr , $ low , $ i - 1 ) ; $ r = eval1uateAll ( $ expr , $ i + 1 , $ high ) ; for ( $ s1 = 0 ; $ s1 < count ( $ l ) ; $ s1 ++ ) { for ( $ s2 = 0 ; $ s2 < count ( $ r ) ; $ s2 ++ ) { $ val = eval1 ( $ l [ $ s1 ] , $ expr [ $ i ] , $ r [ $ s2 ] ) ; array_push ( $ res , $ val ) ; } } } return $ res ; } $ expr = "1*2 + 3*4" ; $ len = strlen ( $ expr ) ; $ ans = eval1uateAll ( $ expr , 0 , $ len - 1 ) ; for ( $ i = 0 ; $ i < count ( $ ans ) ; $ i ++ ) echo $ ans [ $ i ] . " STRNEWLINE " ; ? >
Program to check if a given number is Lucky ( all digits are different ) | This function returns true if n is lucky ; Create an array of size 10 and initialize all elements as false . This array is used to check if a digit is already seen or not . ; Traverse through all digits of given number ; Find the last digit ; If digit is already seen , return false ; Mark this digit as seen ; Remove the last digit from number ; Driver Code
< ? php function isLucky ( $ n ) { $ arr = array ( ) ; for ( $ i = 0 ; $ i < 10 ; $ i ++ ) $ arr [ $ i ] = false ; while ( $ n > 0 ) { $ digit = $ n % 10 ; if ( $ arr [ $ digit ] ) return false ; $ arr [ $ digit ] = true ; $ n = ( int ) ( $ n / 10 ) ; } return true ; } $ arr = array ( 1291 , 897 , 4566 , 1232 , 80 , 700 ) ; $ n = sizeof ( $ arr ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( isLucky ( $ arr [ $ i ] ) ) echo $ arr [ $ i ] , " ▁ is ▁ Lucky ▁ STRNEWLINE " ; else echo $ arr [ $ i ] , " ▁ is ▁ not ▁ Lucky ▁ STRNEWLINE " ; ? >
Print squares of first n natural numbers without using * , / and | PHP program to print squares of first ' n ' natural numbers wothout using * , / and - ; Initialize ' square ' and first odd number ; Calculate and print squares ; Print square ; Update ' square ' and ' odd ' ; Driver Code
< ? php function printSquares ( $ n ) { $ square = 0 ; $ odd = 1 ; for ( $ x = 0 ; $ x < $ n ; $ x ++ ) { echo $ square , " " ; $ square = $ square + $ odd ; $ odd = $ odd + 2 ; } } $ n = 5 ; printSquares ( $ n ) ; ? >
Write a program to reverse digits of a number | PHP program to reverse digits of a number ; Recursive function to reverse digits of num ; Driver Code
< ? php $ rev_num = 0 ; $ base_pos = 1 ; function reversDigits ( $ num ) { global $ rev_num ; global $ base_pos ; if ( $ num > 0 ) { reversDigits ( ( int ) ( $ num / 10 ) ) ; $ rev_num += ( $ num % 10 ) * $ base_pos ; $ base_pos *= 10 ; } return $ rev_num ; } $ num = 4562 ; echo " Reverse ▁ of ▁ no . ▁ is ▁ " , reversDigits ( $ num ) ; ? >
Print bitwise AND set of a number N | function to find bitwise subsets Efficient approach ; Driver Code
< ? php function printSubsets ( $ n ) { for ( $ i = $ n ; $ i > 0 ; $ i = ( $ i - 1 ) & $ n ) echo $ i . " " ; echo "0" ; } $ n = 9 ; printSubsets ( $ n ) ; ? >
Check if a number is divisible by 17 using bitwise operators | function to check recursively if the number is divisible by 17 or not ; if n = 0 or n = 17 then yes ; if n is less then 17 , not divisible by 17 ; reducing the number by floor ( n / 16 ) - n % 16 ; Driver Code
< ? php function isDivisibleby17 ( $ n ) { if ( $ n == 0 $ n == 17 ) return true ; if ( $ n < 17 ) return false ; return isDivisibleby17 ( ( int ) ( $ n >> 4 ) - ( int ) ( $ n & 15 ) ) ; } $ n = 35 ; if ( isDivisibleby17 ( $ n ) ) echo $ n . " ▁ is ▁ divisible ▁ by ▁ 17" ; else echo $ n . " ▁ is ▁ not ▁ divisible ▁ by ▁ 17" ; ? >
Largest number with binary representation is m 1 's and m | Returns largest number with m set bits then m - 1 0 bits . ; Start with 2 bits . ; initial answer is 1 which meets the given condition ; check for all numbers ; compute the number ; if less then N ; increment m to get the next number ; Driver Code
< ? php function answer ( $ n ) { $ m = 2 ; $ ans = 1 ; $ r = 1 ; while ( $ r < $ n ) { $ r = ( pow ( 2 , $ m ) - 1 ) * ( pow ( 2 , $ m - 1 ) ) ; if ( $ r < $ n ) $ ans = $ r ; $ m ++ ; } return $ ans ; } $ n = 7 ; echo answer ( $ n ) ; ? >
Find most significant set bit of a number | Simple PhP program to find MSB number for given n . ; Driver code
< ? php function setBitNumber ( $ n ) { if ( $ n == 0 ) return 0 ; $ msb = 0 ; $ n = $ n / 2 ; while ( $ n != 0 ) { $ n = $ n / 2 ; $ msb ++ ; } return ( 1 << $ msb ) ; } $ n = 0 ; echo setBitNumber ( $ n ) ; ? >
Find most significant set bit of a number | PHP program to find MSB number for given n . ; Suppose n is 273 ( binary is 100010001 ) . It does following 100010001 | 010001000 = 110011001 ; This makes sure 4 bits ( From MSB and including MSB ) are set . It does following 110011001 | 001100110 = 111111111 ; Increment n by 1 so that there is only one set bit which is just before original MSB . n now becomes 1000000000 ; Return original MSB after shifting . n now becomes 100000000 ; Driver code
< ? php function setBitNumber ( $ n ) { $ n |= $ n >> 1 ; $ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ; $ n = $ n + 1 ; return ( $ n >> 1 ) ; } $ n = 273 ; echo setBitNumber ( $ n ) ; ? >
Count trailing zero bits using lookup table | Simple PHP code for counting trailing zeros in binary representation of a number ; Driver Code
< ? php function countTrailingZero ( $ x ) { $ count = 0 ; while ( ( $ x & 1 ) == 0 ) { $ x = $ x >> 1 ; $ count ++ ; } return $ count ; } echo countTrailingZero ( 11 ) , " STRNEWLINE " ; ? >
Count trailing zero bits using lookup table | PHP code for counting trailing zeros in binary representation of a number ; Map a bit value mod 37 to its position ; Only difference between ( x and - x ) is the value of signed magnitude ( leftmostbit ) negative numbers signed bit is 1 ; Driver Code
< ? php function countTrailingZero ( $ x ) { $ lookup = array ( 32 , 0 , 1 , 26 , 2 , 23 , 27 , 0 , 3 , 16 , 24 , 30 , 28 , 11 , 0 , 13 , 4 , 7 , 17 , 0 , 25 , 22 , 31 , 15 , 29 , 10 , 12 , 6 , 0 , 21 , 14 , 9 , 5 , 20 , 8 , 19 , 18 ) ; return $ lookup [ ( - $ x & $ x ) % 37 ] ; } echo countTrailingZero ( 48 ) , " STRNEWLINE " ; ? >
Calculate 7 n / 8 without using division and multiplication operators | PHP program to evaluate ceil ( 7 n / 8 ) without using * and ; Note the inner bracket here . This is needed because precedence of ' - ' operator is higher than ' < < ' ; Driver Code
< ? php function multiplyBySevenByEight ( $ n ) { return ( $ n - ( $ n >> 3 ) ) ; } $ n = 9 ; echo multiplyBySevenByEight ( $ n ) ; ? >
Calculate 7 n / 8 without using division and multiplication operators | PHP program to evaluate 7 n / 8 without using * and / ; Step 1 ) First multiply number by 7 i . e . 7 n = ( n << 3 ) - n Step 2 ) Divide result by 8 ; Driver Code
< ? php function multiplyBySevenByEight ( $ n ) { return ( ( $ n << 3 ) - $ n ) >> 3 ; } $ n = 15 ; echo multiplyBySevenByEight ( $ n ) ; ? >
Maximize the median of the given array after adding K elements to the same array | PHP implementation of the approach Function to return the maximized median ; Sort the array ; If size is even ; If size is odd ; Driver code
< ? php function getMaxMedian ( $ arr , $ n , $ k ) { $ size = $ n + $ k ; sort ( $ arr , $ n ) ; if ( $ size % 2 == 0 ) { $ median = ( float ) ( $ arr [ ( $ size / 2 ) - 1 ] + $ arr [ $ size / 2 ] ) / 2 ; return $ median ; } $ median = $ arr [ $ size / 2 ] ; return $ median ; } $ arr = array ( 3 , 2 , 3 , 4 , 2 ) ; $ n = sizeof ( $ arr ) ; $ k = 2 ; echo ( getMaxMedian ( $ arr , $ n , $ k ) ) ;
Sort 3 Integers without using if condition or using only max ( ) function | PHP program to print three numbers in sorted order using max function ; Find maximum element ; Find minimum element ; Driver Code
< ? php function printSorted ( $ a , $ b , $ c ) { $ get_max = max ( $ a , max ( $ b , $ c ) ) ; $ get_min = - max ( - $ a , max ( - $ b , - $ c ) ) ; $ get_mid = ( $ a + $ b + $ c ) - ( $ get_max + $ get_min ) ; echo $ get_min , " " ▁ , ▁ $ get _ mid , ▁ " " } $ a = 4 ; $ b = 1 ; $ c = 9 ; printSorted ( $ a , $ b , $ c ) ; ? >
Insertion Sort | Function to sort an array using insertion sort ; Move elements of arr [ 0. . i - 1 ] , that are greater than key , to one position ahead of their current position ; A utility function to print an array of size n ; Driver Code
< ? php function insertionSort ( & $ arr , $ n ) { for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ key = $ arr [ $ i ] ; $ j = $ i - 1 ; while ( $ j >= 0 && $ arr [ $ j ] > $ key ) { $ arr [ $ j + 1 ] = $ arr [ $ j ] ; $ j = $ j - 1 ; } $ arr [ $ j + 1 ] = $ key ; } } function printArray ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; echo " STRNEWLINE " ; } $ arr = array ( 12 , 11 , 13 , 5 , 6 ) ; $ n = sizeof ( $ arr ) ; insertionSort ( $ arr , $ n ) ; printArray ( $ arr , $ n ) ; ? >
Counts paths from a point to reach Origin | DP based function to count number of paths ; $dp [ $n + 1 ] [ $m + 1 ] ; Fill entries in bottommost row and leftmost columns ; Fill DP in bottom up manner ; Driver Code
< ? php function countPaths ( $ n , $ m ) { for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ dp [ $ i ] [ 0 ] = 1 ; for ( $ i = 0 ; $ i <= $ m ; $ i ++ ) $ dp [ 0 ] [ $ i ] = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) for ( $ j = 1 ; $ j <= $ m ; $ j ++ ) $ dp [ $ i ] [ $ j ] = $ dp [ $ i - 1 ] [ $ j ] + $ dp [ $ i ] [ $ j - 1 ] ; return $ dp [ $ n ] [ $ m ] ; } $ n = 3 ; $ m = 2 ; echo " ▁ Number ▁ of ▁ Paths ▁ " , countPaths ( $ n , $ m ) ; ? >
Encrypt string with product of number of vowels and consonants in substring of size k | isVowel ( ) is a function that returns true for a vowel and false otherwise . ; function to Encrypt the dtring ; for each substring ; substring of size k ; counting number of vowels and consonants ; append product to answer . ; Driver Code
< ? php function isVowel ( $ c ) { return ( $ c == ' a ' $ c == ' e ' $ c == ' i ' $ c == ' o ' $ c == ' u ' ) ; } function encryptString ( $ s , $ n , $ k ) { $ countVowels = 0 ; $ countConsonants = 0 ; $ ans = " " ; for ( $ l = 0 ; $ l <= $ n - $ k ; $ l ++ ) { $ countVowels = 0 ; $ countConsonants = 0 ; for ( $ r = $ l ; $ r <= $ l + $ k - 1 ; $ r ++ ) { if ( isVowel ( $ s [ $ r ] ) == true ) $ countVowels ++ ; else $ countConsonants ++ ; } $ ans = $ ans . ( string ) ( $ countVowels * $ countConsonants ) ; } return $ ans ; } $ s = " hello " ; $ n = strlen ( $ s ) ; $ k = 2 ; echo encryptString ( $ s , $ n , $ k ) . " STRNEWLINE " ;
Largest right circular cylinder within a cube | Function to find the biggest right circular cylinder ; side cannot be negative ; radius of right circular cylinder ; height of right circular cylinder ; volume of right circular cylinder ; Driver code
< ? php function findVolume ( $ a ) { if ( $ a < 0 ) return -1 ; $ r = $ a / 2 ; $ h = $ a ; $ V = 3.14 * pow ( $ r , 2 ) * $ h ; return $ V ; } $ a = 5 ; echo findVolume ( $ a ) . " STRNEWLINE " ;