codeparrot/xlcost-text-to-code huggingface dataset:
Bitwise AND of sub | Function to return the minimum possible value of | K - X | where X is the bitwise AND of the elements of some sub - array ; Check all possible sub - arrays ; Find the overall minimum ; Driver code
< ? php function closetAND ( & $ arr , $ n , $ k ) { $ ans = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ X = $ arr [ $ i ] ; for ( $ j = $ i ; $ j < $ n ; $ j ++ ) { $ X &= $ arr [ $ j ] ; $ ans = min ( $ ans , abs ( $ k - $ X ) ) ; } } return $ ans ; } $ arr = array ( 4 , 7 , 10 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; $ k = 2 ; echo closetAND ( $ arr , $ n , $ k ) ; return 0 ; ? >
Program to find the rate percentage from compound interest of consecutive years | Function to return the required rate percentage ; Driver code
< ? php function Rate ( $ N1 , $ N2 ) { $ rate = ( $ N2 - $ N1 ) * 100 / $ N1 ; return $ rate ; } $ N1 = 100 ; $ N2 = 120 ; echo Rate ( $ N1 , $ N2 ) , " % " ; ? >
Find prime number K in an array such that ( A [ i ] % K ) is maximum | Function to return the count of primes in the given array ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; To store the maximum prime number ; If current element is prime then update the maximum prime ; Return the maximum prime number from the array ; Driver code
< ? php function getPrime ( $ arr , $ n ) { $ max_val = max ( $ arr ) ; $ prime = array_fill ( 0 , $ max_val + 1 , true ) ; $ prime [ 0 ] = false ; $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ max_val ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ max_val ; $ i += $ p ) $ prime [ $ i ] = false ; } } $ maximum = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ prime [ $ arr [ $ i ] ] ) $ maximum = max ( $ maximum , $ arr [ $ i ] ) ; } return $ maximum ; } $ arr = array ( 2 , 10 , 15 , 7 , 6 , 8 , 13 ) ; $ n = count ( $ arr ) ; echo getPrime ( $ arr , $ n ) ; ? >
Minimum integer such that it leaves a remainder 1 on dividing with any element from the range [ 2 , N ] | Function to return the smallest number which on dividing with any element from the range [ 2 , N ] leaves a remainder of 1 ; Find the LCM of the elements from the range [ 2 , N ] ; Return the required number ; Driver code
< ? php function getMinNum ( $ N ) { $ lcm = 1 ; for ( $ i = 2 ; $ i <= $ N ; $ i ++ ) $ lcm = ( ( $ i * $ lcm ) / ( __gcd ( $ i , $ lcm ) ) ) ; return ( $ lcm + 1 ) ; } function __gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return __gcd ( $ b , $ a % $ b ) ; } $ N = 5 ; echo ( getMinNum ( $ N ) ) ; ? >
Maximum number of edges in Bipartite graph | Function to return the maximum number of edges possible in a Bipartite graph with N vertices ; Driver code
< ? php function maxEdges ( $ N ) { $ edges = 0 ; $ edges = floor ( ( $ N * $ N ) / 4 ) ; return $ edges ; } $ N = 5 ; echo maxEdges ( $ N ) ; ? >
Find permutation of first N natural numbers that satisfies the given condition | Function to find permutation ( p ) of first N natural numbers such that there are exactly K elements of permutation such that GCD ( p [ i ] , i ) > 1 ; First place all the numbers in their respective places ; Modify for first n - k integers ; In first index place n - k ; Print the permutation ; Driver code
< ? php function Permutation ( $ n , $ k ) { $ p = array ( ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ p [ $ i ] = $ i ; for ( $ i = 1 ; $ i < $ n - $ k ; $ i ++ ) $ p [ $ i + 1 ] = $ i ; $ p [ 1 ] = $ n - $ k ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) echo $ p [ $ i ] , " ▁ " ; } $ n = 5 ; $ k = 2 ; Permutation ( $ n , $ k ) ; ? >
Number of positions such that adding K to the element is greater than sum of all other elements | Function that will find out the valid position ; find sum of all the elements ; adding K to the element and check whether it is greater than sum of all other elements ; Driver code
< ? php function validPosition ( $ arr , $ N , $ K ) { $ count = 0 ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ sum += $ arr [ $ i ] ; } for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { if ( ( $ arr [ $ i ] + $ K ) > ( $ sum - $ arr [ $ i ] ) ) $ count ++ ; } return $ count ; } $ arr = array ( 2 , 1 , 6 , 7 ) ; $ K = 4 ; $ N = count ( $ arr ) ; echo validPosition ( $ arr , $ N , $ K ) ; ? >
Find element with the maximum set bits in an array | Function to return the element from the array which has the maximum set bits ; To store the required element and the maximum set bits so far ; Count of set bits in the current element ; Update the max ; Driver code
< ? php function maxBitElement ( $ arr , $ n ) { $ num = 0 ; $ max = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ cnt = BitCount ( $ arr [ $ i ] ) ; if ( $ cnt > $ max ) { $ max = $ cnt ; $ num = $ arr [ $ i ] ; } } return $ num ; } function BitCount ( $ n ) { $ count = 0 ; while ( $ n != 0 ) { $ count ++ ; $ n &= ( $ n - 1 ) ; } return $ count ; } $ arr = array ( 3 , 2 , 4 , 7 , 1 , 10 , 5 , 8 , 9 , 6 ) ; $ n = count ( $ arr ) ; echo ( maxBitElement ( $ arr , $ n ) ) ; ? >
Smallest and Largest sum of two n | Function to return the smallest sum of 2 n - digit numbers ; Function to return the largest sum of 2 n - digit numbers ; Driver code
< ? php function smallestSum ( $ n ) { if ( $ n == 1 ) return 0 ; return ( 2 * pow ( 10 , $ n - 1 ) ) ; } function largestSum ( $ n ) { return 2 * ( pow ( 10 , $ n ) - 1 ) ; } $ n = 4 ; echo " Largest = " ▁ . ▁ largestSum ( $ n ) ▁ . ▁ " " ; STRNEWLINE echo ▁ " Smallest = " ? >
Given two arrays count all pairs whose sum is an odd number | Function that returns the number of pairs ; Count of odd and even numbers ; Traverse in the first array and count the number of odd and evene numbers in them ; Traverse in the second array and count the number of odd and evene numbers in them ; Count the number of pairs ; Return the number of pairs ; Driver code
< ? php function count_pairs ( $ a , $ b , $ n , $ m ) { $ odd1 = 0 ; $ even1 = 0 ; $ odd2 = 0 ; $ even2 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] % 2 ) $ odd1 ++ ; else $ even1 ++ ; } for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { if ( $ b [ $ i ] % 2 ) $ odd2 ++ ; else $ even2 ++ ; } $ pairs = min ( $ odd1 , $ even2 ) + min ( $ odd2 , $ even1 ) ; return $ pairs ; } $ a = array ( 9 , 14 , 6 , 2 , 11 ) ; $ b = array ( 8 , 4 , 7 , 20 ) ; $ n = count ( $ a ) ; $ m = count ( $ b ) ; echo count_pairs ( $ a , $ b , $ n , $ m ) ; ? >
Print steps to make a number in form of 2 ^ X | Function to find the leftmost unset bit in a number . ; Function that perform the step ; Find the leftmost unset bit ; If the number has no bit unset , it means it is in form 2 ^ x - 1 ; Count the steps ; Iterate till number is of form 2 ^ x - 1 ; At even step increase by 1 ; Odd step xor with any 2 ^ m - 1 ; Find the leftmost unset bit ; 2 ^ m - 1 ; Perform the step ; Increase the steps ; Driver code
< ? php function find_leftmost_unsetbit ( $ n ) { $ ind = -1 ; $ i = 1 ; while ( $ n ) { if ( ! ( $ n & 1 ) ) $ ind = $ i ; $ i ++ ; $ n >>= 1 ; } return $ ind ; } function perform_steps ( $ n ) { $ left = find_leftmost_unsetbit ( $ n ) ; if ( $ left == -1 ) { echo " No ▁ steps ▁ required " ; return ; } $ step = 1 ; while ( find_leftmost_unsetbit ( $ n ) != -1 ) { if ( $ step % 2 == 0 ) { $ n += 1 ; echo " Step " , $ step , " : ▁ Increase ▁ by ▁ 1 STRNEWLINE " ; } else { $ m = find_leftmost_unsetbit ( $ n ) ; $ num = pow ( 2 , $ m ) - 1 ; $ n = $ n ^ $ num ; echo " Step " , $ step , " : ▁ Xor ▁ with ▁ " , $ num , " STRNEWLINE " ; } $ step += 1 ; } } $ n = 39 ; perform_steps ( $ n ) ; ? >
Determine the position of the third person on regular N sided polygon | Function to find out the number of that vertices ; Another person can 't stand on  vertex on which 2 children stand. ; calculating minimum jumps from each vertex . ; Calculate sum of jumps . ; Driver code ; Calling function
< ? php function vertices ( $ N , $ A , $ B ) { $ position = 0 ; $ minisum = PHP_INT_MAX ; $ sum = 0 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) { if ( $ i == $ A $ i == $ B ) continue ; else { $ x = abs ( $ i - $ A ) ; $ y = abs ( $ i - $ B ) ; $ sum = $ x + $ y ; if ( $ sum < $ minisum ) { $ minisum = $ sum ; $ position = $ i ; } } } return $ position ; } $ N = 3 ; $ A = 1 ; $ B = 2 ; echo " Vertex = " ? >
Find sum of factorials in an array | Function to return the factorial of n ; Function to return the sum of factorials of the array elements ; To store the required sum ; Add factorial of all the elements ; Driver code
< ? php function factorial ( $ n ) { $ f = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ f *= $ i ; } return $ f ; } function sumFactorial ( $ arr , $ n ) { $ s = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ s += factorial ( $ arr [ $ i ] ) ; } return $ s ; } $ arr = array ( 7 , 3 , 5 , 4 , 8 ) ; $ n = sizeof ( $ arr ) ; echo sumFactorial ( $ arr , $ n ) ; ? >
Highest power of 2 that divides a number represented in binary | Function to return the highest power of 2 which divides the given binary number ; To store the highest required power of 2 ; Counting number of consecutive zeros from the end in the given binary string ; Driver code
< ? php function highestPower ( $ str , $ len ) { $ ans = 0 ; for ( $ i = $ len - 1 ; $ i >= 0 ; $ i -- ) { if ( $ str [ $ i ] == '0' ) $ ans ++ ; else break ; } return $ ans ; } $ str = "100100" ; $ len = strlen ( $ str ) ; echo highestPower ( $ str , $ len ) ; ? >
Number of ways to arrange K different objects taking N objects at a time | PHP implementation of the approach ; Function to return n ! % p ; $res = 1 ; Initialize result ; 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 result ; y must be even now $y = $y >> 1 ; y = y / 2 ; Returns n ^ ( - 1 ) mod p ; Returns nCr % p using Fermat 's little theorem. ; Base case ; Fill factorial array so that we can find all factorial of r , n and n - r ; Function to return the number of ways to arrange K different objects taking N objects at a time ; Driver Code ; Function call
< ? php $ mod = ( 1e9 + 7 ) ; function factorial ( $ n , $ p ) { for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res = ( $ res * $ i ) % $ p ; return $ res ; } function power ( $ x , $ y , $ p ) { while ( $ y > 0 ) { if ( ( $ y & 1 ) == 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function modInverse ( $ n , $ p ) { return power ( $ n , $ p - 2 , $ p ) ; } function nCrModP ( $ n , $ r , $ p ) { if ( $ r == 0 ) return 1 ; $ fac = array ( ( int ) $ n + 1 ) ; $ fac [ 0 ] = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ fac [ $ i ] = $ fac [ $ i - 1 ] * $ i % $ p ; return ( $ fac [ ( int ) $ n ] * modInverse ( $ fac [ ( int ) $ r ] , $ p ) % $ p * modInverse ( $ fac [ ( int ) $ n - ( int ) $ r ] , $ p ) % $ p ) % $ p ; } function countArrangements ( $ n , $ k , $ p ) { return ( factorial ( $ n , $ p ) * nCrModP ( $ k , $ n , $ p ) ) % $ p ; } { $ N = 5 ; $ K = 8 ; echo ( countArrangements ( $ N , $ K , $ mod ) ) ; }
Find maximum product of digits among numbers less than or equal to N | Function that returns the maximum product of digits among numbers less than or equal to N ; Driver code
< ? php function maxProd ( $ N ) { if ( $ N == 0 ) return 1 ; if ( $ N < 10 ) return $ N ; return max ( maxProd ( ( int ) ( $ N / 10 ) ) * ( $ N % 10 ) , maxProd ( ( int ) ( $ N / 10 ) - 1 ) * 9 ) ; } $ N = 390 ; echo maxProd ( $ N ) ; ? >
Check if a number from every row can be selected such that xor of the numbers is greater than zero | PHP program to implement the above approach ; Function to check if a number from every row can be selected such that xor of the numbers is greater than zero ; Find the xor of first column for every row ; If Xorr is 0 ; Traverse in the matrix ; Check is atleast 2 distinct elements ; Driver code
< ? php $ N = 2 ; $ M = 3 ; function check ( $ mat ) { global $ N ; global $ M ; $ xorr = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ xorr = $ xorr ^ $ mat [ $ i ] [ 0 ] ; } if ( $ xorr != 0 ) return true ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = 1 ; $ j < $ M ; $ j ++ ) { if ( $ mat [ $ i ] [ $ j ] != $ mat [ $ i ] [ 0 ] ) return true ; } } return false ; } $ mat = array ( array ( 7 , 7 , 7 ) , array ( 10 , 10 , 7 ) ) ; if ( check ( $ mat ) ) echo " Yes " ; else echo " No " ; ? >
Sum of the series 1 , 2 , 4 , 3 , 5 , 7 , 9 , 6 , 8 , 10 , 11 , 13. . till N | Function to find the sum of first N odd numbers ; Function to find the sum of first N even numbers ; Function to overall find the sum of series ; Initial odd numbers ; Initial even numbers ; First power of 2 ; Check for parity for odd / even ; Counts the sum ; Get the minimum out of remaining num or power of 2 ; Decrease that much numbers from num ; If the segment has odd numbers ; Summate the odd numbers By exclusion ; Increase number of odd numbers ; If the segment has even numbers ; Summate the even numbers By exclusion ; Increase number of even numbers ; Next set of numbers ; Change parity for odd / even ; Driver code
< ? php function sumodd ( $ n ) { return ( $ n * $ n ) ; } function sumeven ( $ n ) { return ( $ n * ( $ n + 1 ) ) ; } function findSum ( $ num ) { $ sumo = 0 ; $ sume = 0 ; $ x = 1 ; $ cur = 0 ; $ ans = 0 ; while ( $ num > 0 ) { $ inc = min ( $ x , $ num ) ; $ num -= $ inc ; if ( $ cur == 0 ) { $ ans = $ ans + sumodd ( $ sumo + $ inc ) - sumodd ( $ sumo ) ; $ sumo += $ inc ; } else { $ ans = $ ans + sumeven ( $ sume + $ inc ) - sumeven ( $ sume ) ; $ sume += $ inc ; } $ x *= 2 ; $ cur ^= 1 ; } return $ ans ; } $ n = 4 ; echo findSum ( $ n ) ; ? >
Find the nth term of the given series | Function to return the nth term of the given series ; Driver code
< ? php function oddTriangularNumber ( $ N ) { return ( $ N * ( ( 2 * $ N ) - 1 ) ) ; } $ N = 3 ; echo oddTriangularNumber ( $ N ) ; ? >
Check if given two straight lines are identical or not | Function to check if they are identical ; Driver Code
< ? php function idstrt ( $ a1 , $ b1 , $ c1 , $ a2 , $ b2 , $ c2 ) { if ( ( $ a1 / $ a2 == $ b1 / $ b2 ) && ( $ a1 / $ a2 == $ c1 / $ c2 ) && ( $ b1 / $ b2 == $ c1 / $ c2 ) ) echo " The ▁ given ▁ straight ▁ lines ▁ are ▁ identical " , " STRNEWLINE " ; else echo " The ▁ given ▁ straight ▁ lines ▁ are ▁ not ▁ identical " , " STRNEWLINE " ; } $ a1 = -2 ; $ b1 = 4 ; $ c1 = 3 ; $ a2 = -6 ; $ b2 = 12 ; $ c2 = 9 ; idstrt ( $ a1 , $ b1 , $ c1 , $ a2 , $ b2 , $ c2 ) ; ? >
Equation of straight line passing through a given point which bisects it into two equal line segments | Function to print the equation of the required line ; Driver code
< ? php function line ( $ x0 , $ y0 ) { $ c = 2 * $ y0 * $ x0 ; echo $ y0 , " x " , " ▁ + ▁ " , $ x0 , " y ▁ = ▁ " , $ c ; } $ x0 = 4 ; $ y0 = 3 ; line ( $ x0 , $ y0 ) ; ? >
Find the original matrix when largest element in a row and a column are given | PHP implementation of the approach ; Function that prints the original matrix ; Iterate in the row ; Iterate in the column ; If previously existed an element ; Driver code
< ? php $ N = 3 ; $ M = 7 ; function printOriginalMatrix ( $ a , $ b , $ mat ) { for ( $ i = 0 ; $ i < $ GLOBALS [ ' N ' ] ; $ i ++ ) { for ( $ j = 0 ; $ j < $ GLOBALS [ ' M ' ] ; $ j ++ ) { if ( $ mat [ $ i ] [ $ j ] == 1 ) echo min ( $ a [ $ i ] , $ b [ $ j ] ) . " ▁ " ; else echo "0" . " ▁ " ; } echo " \r STRNEWLINE " ; } } $ a = array ( 2 , 1 , 3 ) ; $ b = array ( 2 , 3 , 0 , 0 , 2 , 0 , 1 ) ; $ mat = array ( array ( 1 , 0 , 0 , 0 , 1 , 0 , 0 ) , array ( 0 , 0 , 0 , 0 , 0 , 0 , 1 ) , array ( 1 , 1 , 0 , 0 , 0 , 0 , 0 ) ) ; printOriginalMatrix ( $ a , $ b , $ mat ) ; ? >
Calculate the loss incurred in selling the given items at discounted price | Function to return the x % of n ; Function to return the total loss ; To store the total loss ; Original price of the item ; The price at which the item will be sold ; The discounted price of the item ; Loss incurred ; Driver code ; Total items
< ? php function percent ( $ n , $ x ) { $ p = ( int ) ( $ n ) * $ x ; $ p /= 100 ; return $ p ; } function getLoss ( $ price , $ quantity , $ X , $ n ) { $ loss = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ originalPrice = $ price [ $ i ] ; $ sellingPrice = $ originalPrice + percent ( $ originalPrice , $ X [ $ i ] ) ; $ afterDiscount = $ sellingPrice - percent ( $ sellingPrice , $ X [ $ i ] ) ; $ loss += ( ( $ originalPrice - $ afterDiscount ) * $ quantity [ $ i ] ) ; } return $ loss ; } $ price = array ( 20 , 48 , 200 , 100 ) ; $ quantity = array ( 20 , 48 , 1 , 1 ) ; $ X = array ( 0 , 48 , 200 , 5 ) ; $ n = count ( $ X ) ; echo getLoss ( $ price , $ quantity , $ X , $ n ) ; ? >
Maximum difference between two elements in an Array | Function to return the maximum absolute difference between any two elements of the array ; To store the minimum and the maximum elements from the array ; Driver code
< ? php function maxAbsDiff ( $ arr , $ n ) { $ minEle = $ arr [ 0 ] ; $ maxEle = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ minEle = min ( $ minEle , $ arr [ $ i ] ) ; $ maxEle = max ( $ maxEle , $ arr [ $ i ] ) ; } return ( $ maxEle - $ minEle ) ; } $ arr = array ( 2 , 1 , 5 , 3 ) ; $ n = sizeof ( $ arr ) ; echo maxAbsDiff ( $ arr , $ n ) ;
Maximize the maximum subarray sum after removing atmost one element | Function to return the maximum sub - array sum ; Initialized ; Traverse in the array ; Increase the sum ; If sub - array sum is more than the previous ; If sum is negative ; Function that returns the maximum sub - array sum after removing an element from the same sub - array ; Maximum sub - array sum using Kadane 's Algorithm ; Re - apply Kadane 's with minor changes ; Increase the sum ; If sub - array sum is greater than the previous ; If elements are 0 , no removal ; If elements are more , then store the minimum value in the sub - array obtained till now ; If sum is negative ; Re - initialize everything ; Driver code
< ? php function maxSubArraySum ( $ a , $ size ) { $ max_so_far = PHP_INT_MIN ; $ max_ending_here = 0 ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { $ max_ending_here = $ max_ending_here + $ a [ $ i ] ; if ( $ max_so_far < $ max_ending_here ) $ max_so_far = $ max_ending_here ; if ( $ max_ending_here < 0 ) $ max_ending_here = 0 ; } return $ max_so_far ; } function maximizeSum ( $ a , $ n ) { $ cnt = 0 ; $ mini = PHP_INT_MAX ; $ minSubarray = PHP_INT_MAX ; $ sum = maxSubArraySum ( $ a , $ n ) ; $ max_so_far = PHP_INT_MIN ; $ max_ending_here = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ max_ending_here = $ max_ending_here + $ a [ $ i ] ; $ cnt ++ ; $ minSubarray = min ( $ a [ $ i ] , $ minSubarray ) ; if ( $ sum == $ max_ending_here ) { if ( $ cnt == 1 ) $ mini = min ( $ mini , 0 ) ; else $ mini = min ( $ mini , $ minSubarray ) ; } if ( $ max_ending_here < 0 ) { $ max_ending_here = 0 ; $ cnt = 0 ; $ minSubarray = PHP_INT_MAX ; } } return $ sum - $ mini ; } $ a = array ( 1 , 2 , 3 , -2 , 3 ) ; $ n = sizeof ( $ a ) / sizeof ( $ a [ 0 ] ) ; echo maximizeSum ( $ a , $ n ) ; ? >
3 | Function that returns true if n is an Osiris number ; 3 rd digit ; 2 nd digit ; 1 st digit ; Check the required condition ; Driver code
< ? php function isOsiris ( $ n ) { $ a = $ n % 10 ; $ b = floor ( $ n / 10 ) % 10 ; $ c = floor ( $ n / 100 ) ; $ digit_sum = $ a + $ b + $ c ; if ( $ n == ( 2 * ( $ digit_sum ) * 11 ) ) { return true ; } return false ; } $ n = 132 ; if ( isOsiris ( $ n ) ) echo " Yes " ; else echo " No " ; ? >
Dudeney Numbers | Function that returns true if n is a Dudeney number ; If n is not a perfect cube ; Last digit ; Update the digit sum ; Remove the last digit ; If cube root of n is not equal to the sum of its digits ; Driver code
< ? php function isDudeney ( $ n ) { $ cube_rt = floor ( round ( ( pow ( $ n , 1.0 / 3.0 ) ) ) ) ; if ( $ cube_rt * $ cube_rt * $ cube_rt != $ n ) return false ; $ dig_sum = 0 ; $ temp = $ n ; while ( $ temp > 0 ) { $ rem = $ temp % 10 ; $ dig_sum += $ rem ; $ temp = $ temp / 10 ; } if ( $ cube_rt != $ dig_sum ) return false ; return true ; } $ n = 17576 ; if ( isDudeney ( $ n ) ) echo " Yes " ; else echo " No " ; ? >
Number of triangles possible with given lengths of sticks which are powers of 2 | Function to return the number of positive area triangles ; To store the count of total triangles ; To store the count of pairs of sticks with equal lengths ; Back - traverse and count the number of triangles ; Count the number of pairs ; If we have one remaining stick and we have a pair ; Count 1 triangle ; Reduce one pair ; Count the remaining triangles that can be formed ; Driver code
< ? php Function countTriangles ( $ a , $ n ) { $ cnt = 0 ; $ pairs = 0 ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { $ pairs += $ a [ $ i ] / 2 ; if ( $ a [ $ i ] % 2 == 1 && $ pairs > 0 ) { $ cnt += 1 ; $ pairs -= 1 ; } } $ cnt += ( int ) ( ( 2 * $ pairs ) / 3 ) ; return $ cnt ; } $ a = array ( 1 , 2 , 2 , 2 , 2 ) ; $ n = sizeof ( $ a ) ; echo ( countTriangles ( $ a , $ n ) ) ; ? >
Smallest N digit number which is a multiple of 5 | Function to return the smallest n digit number which is a multiple of 5 ; Driver code
< ? php function smallestMultiple ( $ n ) { if ( $ n == 1 ) return 5 ; return pow ( 10 , $ n - 1 ) ; } $ n = 4 ; echo smallestMultiple ( $ n ) ; ? >
Find HCF of two numbers without using recursion or Euclidean algorithm | Function to return the HCF of x and y ; Minimum of the two numbers ; If both the numbers are divisible by the minimum of these two then the HCF is equal to the minimum ; Highest number between 2 and minimum / 2 which can divide both the numbers is the required HCF ; If both the numbers are divisible by i ; 1 divides every number ; Driver code
< ? php function getHCF ( $ x , $ y ) { $ minimum = min ( $ x , $ y ) ; if ( $ x % $ minimum == 0 && $ y % $ minimum == 0 ) return $ minimum ; for ( $ i = $ minimum / 2 ; $ i >= 2 ; $ i -- ) { if ( $ x % $ i == 0 && $ y % $ i == 0 ) return $ i ; } return 1 ; } $ x = 16 ; $ y = 32 ; echo ( getHCF ( $ x , $ y ) ) ; ? >
Check if product of first N natural numbers is divisible by their sum | Function that returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that return true if the product of the first n natural numbers is divisible by the sum of first n natural numbers ; Driver code
< ? 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 ; } function isDivisible ( $ n ) { if ( isPrime ( $ n + 1 ) ) return false ; return true ; } $ n = 6 ; if ( isDivisible ( $ n ) ) echo " Yes " ; else echo " No " ; ? >
Maximum sum of cocktail glass in a 2D matrix | PHP implementation of the approach ; Function to return the maximum sum of a cocktail glass ; If no cocktail glass is possible ; Initialize max_sum with the mini ; Here loop runs ( R - 2 ) * ( C - 2 ) times considering different top left cells of cocktail glasses ; Considering mat [ i ] [ j ] as the top left cell of the cocktail glass ; Update the max_sum ; Driver code
< ? php $ R = 5 ; $ C = 5 ; function findMaxCock ( $ ar ) { global $ R , $ C ; if ( $ R < 3 $ C < 3 ) return -1 ; $ max_sum = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ R - 2 ; $ i ++ ) { for ( $ j = 0 ; $ j < $ C - 2 ; $ j ++ ) { $ sum = ( $ ar [ $ i ] [ $ j ] + $ ar [ $ i ] [ $ j + 2 ] ) + ( $ ar [ $ i + 1 ] [ $ j + 1 ] ) + ( $ ar [ $ i + 2 ] [ $ j ] + $ ar [ $ i + 2 ] [ $ j + 1 ] + $ ar [ $ i + 2 ] [ $ j + 2 ] ) ; $ max_sum = max ( $ max_sum , $ sum ) ; } } return $ max_sum ; } $ ar = array ( array ( 0 , 3 , 0 , 6 , 0 ) , array ( 0 , 1 , 1 , 0 , 0 ) , array ( 1 , 1 , 1 , 0 , 0 ) , array ( 0 , 0 , 2 , 0 , 1 ) , array ( 0 , 2 , 0 , 1 , 3 ) ) ; echo ( findMaxCock ( $ ar ) ) ; ? >