codeparrot/xlcost-text-to-code huggingface dataset:
Addition of two numbers without propagating Carry | Function to print sum of 2 numbers without propagating carry ; Reverse a ; Reverse b ; Generate sum Since length of both a and b are same , take any one of them . ; Extract digits from a and b and add ; If sum is single digit ; If sum is not single digit reverse sum ; Extract digits from sum and append to result ; Driver code
< ? php function printSum ( $ a , $ b ) { $ res = 0 ; $ temp1 = 0 ; $ temp2 = 0 ; while ( $ a != 0 ) { $ temp1 = $ temp1 * 10 + ( $ a % 10 ) ; $ a = ( int ) ( $ a / 10 ) ; } $ a = $ temp1 ; while ( $ b != 0 ) { $ temp2 = $ temp2 * 10 + ( $ b % 10 ) ; $ b = ( int ) ( $ b / 10 ) ; } $ b = $ temp2 ; while ( $ a != 0 ) { $ sum = ( $ a % 10 + $ b % 10 ) ; if ( ( int ) ( $ sum / 10 ) == 0 ) { $ res = $ res * 10 + $ sum ; } else { $ temp1 = 0 ; while ( $ sum != 0 ) { $ temp1 = $ temp1 * 10 + ( $ sum % 10 ) ; $ sum = ( int ) ( $ sum / 10 ) ; } $ sum = $ temp1 ; while ( $ sum != 0 ) { $ res = $ res * 10 + ( $ sum % 10 ) ; $ sum = ( int ) ( $ sum / 10 ) ; } } $ a = ( int ) ( $ a / 10 ) ; $ b = ( int ) ( $ b / 10 ) ; } return $ res ; } $ a = 7752 ; $ b = 8834 ; echo ( printSum ( $ a , $ b ) ) ; ? >
Number of digits before the decimal point in the division of two numbers | Function to return the number of digits before the decimal in a / b ; Absolute value of a / b ; If result is 0 ; Count number of digits in the result ; Return the required count of digits ; Driver code
< ? php function countDigits ( $ a , $ b ) { $ count = 0 ; $ p = abs ( $ a / $ b ) ; if ( $ p == 0 ) return 1 ; while ( $ p > 0 ) { $ count ++ ; $ p = ( int ) ( $ p / 10 ) ; } return $ count ; } $ a = 100 ; $ b = 10 ; echo countDigits ( $ a , $ b ) ; ? >
Number of digits before the decimal point in the division of two numbers | Function to return the number of digits before the decimal in a / b ; Return the required count of digits ; Driver code
< ? php function countDigits ( $ a , $ b ) { return floor ( log10 ( abs ( $ a ) ) - log10 ( abs ( $ b ) ) ) + 1 ; } $ a = 100 ; $ b = 10 ; echo countDigits ( $ a , $ b ) ; ? >
Smallest odd number with N digits | Function to return smallest even number with n digits ; Driver Code
< ? php function smallestOdd ( $ n ) { if ( $ n == 1 ) return 1 ; return pow ( 10 , $ n - 1 ) + 1 ; } $ n = 4 ; echo smallestOdd ( $ n ) ; ? >
Largest Even and Odd N | Function to print the largest n - digit even and odd numbers ; Driver code
< ? php function findNumbers ( $ n ) { $ odd = pow ( 10 , $ n ) - 1 ; $ even = $ odd - 1 ; echo " Even ▁ = ▁ $ even ▁ STRNEWLINE " ; echo " Odd ▁ = ▁ $ odd " ; } $ n = 4 ; findNumbers ( $ n ) ; ? >
Longest sub | Function to return the length of the longest sub - array whose product of elements is 0 ; Driver code
< ? php function longestSubArray ( $ arr , $ n ) { $ isZeroPresent = false ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] == 0 ) { $ isZeroPresent = true ; break ; } } if ( $ isZeroPresent ) return $ n ; return 0 ; } $ arr = array ( 1 , 2 , 3 , 0 , 1 , 2 , 0 ) ; $ n = sizeof ( $ arr ) ; echo longestSubArray ( $ arr , $ n ) ; ? >
Smallest Even number with N digits | Function to return smallest even number with n digits ; Driver Code
< ? php function smallestEven ( $ n ) { if ( $ n == 1 ) return 0 ; return pow ( 10 , $ n - 1 ) ; } $ n = 4 ; echo smallestEven ( $ n ) ; ? >
Maximize profit when divisibility by two numbers have associated profits | PHP implementation of the approach ; Function to return the maximum profit ; min ( x , y ) * n / lcm ( a , b ) ; Driver code
< ? php function __gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return __gcd ( $ b , $ a % $ b ) ; } function maxProfit ( $ n , $ a , $ b , $ x , $ y ) { $ res = $ x * ( $ n / $ a ) ; $ res += $ y * ( $ n / $ b ) ; $ res -= min ( $ x , $ y ) * ( $ n / ( ( $ a * $ b ) / __gcd ( $ a , $ b ) ) ) ; return $ res ; } $ n = 6 ; $ a = 6 ; $ b = 2 ; $ x = 8 ; $ y = 2 ; print ( maxProfit ( $ n , $ a , $ b , $ x , $ y ) ) ; ? >
Series summation if T ( n ) is given and n is very large | PHP implementation of the approach ; Function to return the sum of the given series ; Driver code
< ? php $ GLOBALS [ ' MOD ' ] = 1000000007 ; function sumOfSeries ( $ n ) { $ ans = pow ( $ n % $ GLOBALS [ ' MOD ' ] , 2 ) ; return ( $ ans % $ GLOBALS [ ' MOD ' ] ) ; } $ n = 10 ; echo sumOfSeries ( $ n ) ; ? >
Kth odd number in an array | Function to return the kth odd element from the array ; Traverse the array ; If current element is odd ; If kth odd element is found ; Total odd elements in the array are < k ; Driver code
< ? php function kthOdd ( $ arr , $ n , $ k ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] % 2 == 1 ) $ k -- ; if ( $ k == 0 ) return $ arr [ $ i ] ; } return -1 ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 ) ; $ n = sizeof ( $ arr ) ; $ k = 2 ; echo ( kthOdd ( $ arr , $ n , $ k ) ) ; ? >
Find last five digits of a given five digit number raised to power five | Function to find the last five digits of a five digit number raised to power five ; Driver code
< ? php function lastFiveDigits ( $ n ) { $ n = ( int ) ( $ n / 10000 ) * 10000 + ( ( int ) ( $ n / 100 ) % 10 ) * 1000 + ( $ n % 10 ) * 100 + ( ( int ) ( $ n / 10 ) % 10 ) * 10 + ( int ) ( $ n / 1000 ) % 10 ; $ ans = 1 ; for ( $ i = 0 ; $ i < 5 ; $ i ++ ) { $ ans *= $ n ; $ ans %= 100000 ; } echo $ ans ; } $ n = 12345 ; lastFiveDigits ( $ n ) ; ? >
Sum of ( maximum element | PHP implementation of the above approach ; Function to return a ^ n % mod ; Compute sum of max ( A ) - min ( A ) for all subsets ; Sort the array . ; Maxs = 2 ^ i - 1 ; Mins = 2 ^ ( n - 1 - i ) - 1 ; Driver code
< ? php $ mod = 1000000007 ; function power ( $ a , $ n ) { global $ mod ; if ( $ n == 0 ) return 1 ; $ p = power ( $ a , $ n / 2 ) % $ mod ; $ p = ( $ p * $ p ) % $ mod ; if ( $ n & 1 ) { $ p = ( $ p * $ a ) % $ mod ; } return $ p ; } function computeSum ( & $ arr , $ n ) { global $ mod ; sort ( $ arr ) ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ maxs = ( power ( 2 , $ i ) - 1 + $ mod ) % $ mod ; $ maxs = ( $ maxs * $ arr [ $ i ] ) % $ mod ; $ mins = ( power ( 2 , $ n - 1 - $ i ) - 1 + $ mod ) % $ mod ; $ mins = ( $ mins * $ arr [ $ i ] ) % $ mod ; $ V = ( $ maxs - $ mins + $ mod ) % $ mod ; $ sum = ( $ sum + $ V ) % $ mod ; } return $ sum ; } $ arr = array ( 4 , 3 , 1 ) ; $ n = sizeof ( $ arr ) ; echo computeSum ( $ arr , $ n ) ; ? >
Count of all N digit numbers such that num + Rev ( num ) = 10 ^ N | Function to return the count of such numbers ; If n is odd ; Driver code
< ? php function countNumbers ( $ n ) { if ( $ n % 2 == 1 ) return 0 ; return ( 9 * ( int ) pow ( 10 , $ n / 2 - 1 ) ) ; } $ n = 2 ; echo ( countNumbers ( $ n ) ) ;
Count of numbers having only 1 set bit in the range [ 0 , n ] | Function to return the required count ; To store the count of numbers ; Every power of 2 contains only 1 set bit ; Driver code
< ? php function count_t ( $ n ) { $ cnt = 0 ; $ p = 1 ; while ( $ p <= $ n ) { $ cnt ++ ; $ p *= 2 ; } return $ cnt ; } $ n = 7 ; echo count_t ( $ n ) ; ? >
Find the K | Function to find the K - th minimum element from an array concatenated M times ; Sort the elements in ascending order ; Return the K 'th Min element  present at ( (K-1) / M ) index ; Driver Code
< ? php function KthMinValAfterMconcatenate ( $ A , $ N , $ M , $ K ) { sort ( $ A ) ; return ( $ A [ ( ( $ K - 1 ) / $ M ) ] ) ; } $ A = array ( 3 , 1 , 2 ) ; $ M = 3 ; $ K = 4 ; $ N = sizeof ( $ A ) ; echo ( KthMinValAfterMconcatenate ( $ A , $ N , $ M , $ K ) ) ; ? >
Sum of all i such that ( 2 ^ i + 1 ) % 3 = 0 where i is in range [ 1 , n ] | Function to return the required sum ; Total odd numbers from 1 to n ; Sum of first n odd numbers ; Driver code
< ? php function sumN ( $ n ) { $ n = ( int ) ( ( $ n + 1 ) / 2 ) ; return ( $ n * $ n ) ; } $ n = 3 ; echo sumN ( $ n ) ; ? >
Numbers that are not divisible by any number in the range [ 2 , 10 ] | Function to return the count of numbers from 1 to N which are not divisible by any number in the range [ 2 , 10 ] ; Driver code
< ? php function countNumbers ( $ n ) { return ( int ) ( $ n - $ n / 2 ) - ( int ) ( $ n / 3 ) - ( int ) ( $ n / 5 ) - ( int ) ( $ n / 7 ) + ( int ) ( $ n / 6 ) + ( int ) ( $ n / 10 ) + ( int ) ( $ n / 14 ) + ( int ) ( $ n / 15 ) + ( int ) ( $ n / 21 ) + ( int ) ( $ n / 35 ) - ( int ) ( $ n / 30 ) - ( int ) ( $ n / 42 ) - ( int ) ( $ n / 70 ) - ( int ) ( $ n / 105 ) + ( int ) ( $ n / 210 ) ; } $ n = 20 ; echo ( countNumbers ( $ n ) ) ; ? >
Maximum Primes whose sum is equal to given N | Function to find max count of primes ; if n is even n / 2 is required answer if n is odd floor ( n / 2 ) = ( int ) ( n / 2 ) is required answer ; Driver Code
< ? php function maxPrimes ( $ n ) { return ( int ) ( $ n / 2 ) ; } $ n = 17 ; echo maxPrimes ( $ n ) ; ? >
Sum of the series ( 1 * 2 ) + ( 2 * 3 ) + ( 3 * 4 ) + ... ... upto n terms | Function to return sum ; Driver code
< ? php function sum ( $ n ) { return $ n * ( $ n + 1 ) * ( $ n + 2 ) / 3 ; } $ n = 2 ; echo sum ( $ n ) ; ? >
Smallest divisor D of N such that gcd ( D , M ) is greater than 1 | PHP implementation of the above approach ; Function to find the minimum divisor ; Iterate for all factors of N ; Check for gcd > 1 ; Check for gcd > 1 ; If gcd is m itself ; Driver code
< ? php function __gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return __gcd ( $ b , $ a % $ b ) ; } function findMinimum ( $ n , $ m ) { $ mini = $ m ; for ( $ i = 1 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { $ sec = $ n / $ i ; if ( __gcd ( $ m , $ i ) > 1 ) { return $ i ; } else if ( __gcd ( $ sec , $ m ) > 1 ) { $ mini = min ( $ sec , $ mini ) ; } } } if ( $ mini == $ m ) return -1 ; else return $ mini ; } $ n = 8 ; $ m = 10 ; echo ( findMinimum ( $ n , $ m ) ) ;
Find Nth term of the series 1 , 5 , 32 , 288 ... | Function to generate a fixed \ number ; Finding nth term ; Driver Code
< ? php function nthTerm ( $ N ) { $ nth = 0 ; $ i ; for ( $ i = $ N ; $ i > 0 ; $ i -- ) { $ nth += pow ( $ i , $ i ) ; } return $ nth ; } $ N = 3 ; echo ( nthTerm ( $ N ) ) ; ? >
Find kth smallest number in range [ 1 , n ] when all the odd numbers are deleted | Function to return the kth smallest element from the range [ 1 , n ] after removing all the odd elements ; Driver code
< ? php function kthSmallest ( $ n , $ k ) { return ( 2 * $ k ) ; } $ n = 8 ; $ k = 4 ; echo ( kthSmallest ( $ n , $ k ) ) ; ? >
Check if a number can be represented as sum of non zero powers of 2 | Function that return true if n can be represented as the sum of powers of 2 without using 2 ^ 0 ; Driver code
< ? php function isSumOfPowersOfTwo ( $ n ) { if ( $ n % 2 == 1 ) return false ; else return true ; } $ n = 10 ; if ( isSumOfPowersOfTwo ( $ n ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >
Minimize sum of adjacent difference with removal of one element from array | Function to find the element ; Value variable for storing the total value ; Declaring maximum value as zero ; If array contains on element ; Storing the maximum value in temp variable ; Adding the adjacent difference modulus values of removed element . Removing adjacent difference modulus value after removing element ; Returning total value - maximum value ; Drivers code
< ? php function findMinRemoval ( $ arr , $ n ) { $ value = 0 ; $ maximum = 0 ; if ( $ n == 1 ) return 0 ; $ temp = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ i != 0 && $ i != $ n - 1 ) { $ value = $ value + abs ( $ arr [ $ i ] - $ arr [ $ i + 1 ] ) ; $ temp = abs ( $ arr [ $ i ] - $ arr [ $ i + 1 ] ) + abs ( $ arr [ $ i ] - $ arr [ $ i - 1 ] ) - abs ( $ arr [ $ i - 1 ] - $ arr [ $ i + 1 ] ) ; } else if ( $ i == 0 ) { $ value = $ value + abs ( $ arr [ $ i ] - $ arr [ $ i + 1 ] ) ; $ temp = abs ( $ arr [ $ i ] - $ arr [ $ i + 1 ] ) ; } else $ temp = abs ( $ arr [ $ i ] - $ arr [ $ i - 1 ] ) ; $ maximum = max ( $ maximum , $ temp ) ; } return ( $ value - $ maximum ) ; } $ arr = array ( 1 , 5 , 3 , 2 , 10 ) ; $ n = count ( $ arr ) ; echo findMinRemoval ( $ arr , $ n ) ; ? >
Time until distance gets equal to X between two objects moving in opposite direction | Function to return the time for which the two policemen can communicate ; time = distance / speed ; Driver code
< ? php function getTime ( $ u , $ v , $ x ) { $ speed = $ u + $ v ; $ time = $ x / $ speed ; return $ time ; } $ u = 3 ; $ v = 3 ; $ x = 3 ; echo getTime ( $ u , $ v , $ x ) ; ? >
Given number of matches played , find number of teams in tournament | Function to return the number of teams ; sqrt ( b ^ 2 - 4 ac ) ; First root ( - b + sqrt ( b ^ 2 - 4 ac ) ) / 2 a ; Second root ( - b - sqrt ( b ^ 2 - 4 ac ) ) / 2 a ; Return the positive root ; Driver code
< ? php function number_of_teams ( $ M ) { $ sqr = sqrt ( 1 + ( 8 * $ M ) ) ; $ N1 = ( 1 + $ sqr ) / 2 ; $ N2 = ( 1 - $ sqr ) / 2 ; if ( $ N1 > 0 ) return $ N1 ; return $ N2 ; } $ M = 45 ; echo number_of_teams ( $ M ) ; ? >
Sum of numbers from 1 to N which are in Lucas Sequence | Function to return the required sum ; Generate lucas number and keep on adding them ; Driver code
< ? php function LucasSum ( $ N ) { $ sum = 0 ; $ a = 2 ; $ b = 1 ; $ c ; $ sum += $ a ; while ( $ b <= $ N ) { $ sum += $ b ; $ c = $ a + $ b ; $ a = $ b ; $ b = $ c ; } return $ sum ; } $ N = 20 ; echo ( LucasSum ( $ N ) ) ; ? >
Count of all even numbers in the range [ L , R ] whose sum of digits is divisible by 3 | Function to return the count of required numbers ; Count of numbers in range which are divisible by 6 ; Driver code
< ? php function countNumbers ( $ l , $ r ) { return ( ( int ) ( $ r / 6 ) - ( int ) ( ( $ l - 1 ) / 6 ) ) ; } $ l = 1000 ; $ r = 6000 ; echo ( countNumbers ( $ l , $ r ) ) ; ? >
Sum of minimum element of all sub | Function to find the sum of minimum of all subsequence ; Driver code
< ? php function findMinSum ( $ arr , $ n ) { $ occ1 = ( $ n ) ; $ occ = $ occ1 - 1 ; $ Sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ Sum += $ arr [ $ i ] * pow ( 2 , $ occ ) ; $ occ -= 1 ; } return $ Sum ; } $ arr = array ( 1 , 2 , 4 , 5 ) ; $ n = count ( $ arr ) ; echo findMinSum ( $ arr , $ n ) ; ? >
Count numbers in range 1 to N which are divisible by X but not by Y | Function to count total numbers divisible by x but not y in range 1 to N ; Check if Number is divisible by x but not Y if yes , Increment count ; Driver Code
< ? php function countNumbers ( $ X , $ Y , $ N ) { $ count = 0 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) { if ( ( $ i % $ X == 0 ) && ( $ i % $ Y != 0 ) ) $ count ++ ; } return $ count ; } $ X = 2 ; $ Y = 3 ; $ N = 10 ; echo ( countNumbers ( $ X , $ Y , $ N ) ) ; ? >
Position of a person diametrically opposite on a circle | Function to return the required position ; Driver code
< ? php function getPosition ( $ n , $ m ) { if ( $ m > ( $ n / 2 ) ) return ( $ m - ( $ n / 2 ) ) ; return ( $ m + ( $ n / 2 ) ) ; } $ n = 8 ; $ m = 5 ; echo getPosition ( $ n , $ m ) ; ? >
Minimum operations required to modify the array such that parity of adjacent elements is different | Function to return the parity of a number ; Function to return the minimum number of operations required ; Operation needs to be performed ; Parity of previous element ; Parity of next element ; Update parity of current element to be other than the parities of the previous and the next number ; Driver Code
< ? php function parity ( $ a ) { return $ a % 3 ; } function solve ( $ array , $ size ) { $ operations = 0 ; for ( $ i = 0 ; $ i < $ size - 1 ; $ i ++ ) { if ( parity ( $ array [ $ i ] ) == parity ( $ array [ $ i + 1 ] ) ) { $ operations ++ ; if ( $ i + 2 < $ size ) { $ pari1 = parity ( $ array [ $ i ] ) ; $ pari2 = parity ( $ array [ $ i + 2 ] ) ; if ( $ pari1 == $ pari2 ) { if ( $ pari1 == 0 ) $ array [ $ i + 1 ] = 1 ; else if ( $ pari1 == 1 ) $ array [ $ i + 1 ] = 0 ; else $ array [ $ i + 1 ] = 1 ; } else { if ( ( $ pari1 == 0 && $ pari2 == 1 ) || ( $ pari1 == 1 && $ pari2 == 0 ) ) $ array [ $ i + 1 ] = 2 ; if ( ( $ pari1 == 1 && $ pari2 == 2 ) || ( $ pari1 == 2 && $ pari2 == 1 ) ) $ array [ $ i + 1 ] = 0 ; if ( ( $ pari1 == 2 && $ pari2 == 0 ) || ( $ pari1 == 0 && $ pari2 == 2 ) ) $ array [ $ i + 1 ] = 1 ; } } } } return $ operations ; } $ array = array ( 2 , 1 , 3 , 0 ) ; $ size = count ( $ array ) ; echo solve ( $ array , $ size ) ; ? >
XOR of XORs of all sub | Function to find to required XOR value ; Nested loop to find the number of sub - matrix each index belongs to ; Number of ways to choose from top - left elements ; Number of ways to choose from bottom - right elements ; Driver Code
< ? php function submatrixXor ( $ arr ) { $ ans = 0 ; $ n = 3 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ top_left = ( $ i + 1 ) * ( $ j + 1 ) ; $ bottom_right = ( $ n - $ i ) * ( $ n - $ j ) ; if ( ( $ top_left % 2 == 1 ) && ( $ bottom_right % 2 == 1 ) ) $ ans = ( $ ans ^ $ arr [ $ i ] [ $ j ] ) ; } } return $ ans ; } $ arr = array ( array ( 6 , 7 , 13 ) , array ( 8 , 3 , 4 ) , array ( 9 , 7 , 6 ) ) ; echo submatrixXor ( $ arr ) ; # This  code is contributed by Ryuga NEW_LINE ? >
Find Nth positive number whose digital root is X | Function to find the digital root of a number ; Function to find the Nth number with digital root as X ; Counter variable to keep the count of valid numbers ; Find digital root ; Check if is required answer or not ; Print the answer if you have found it and breakout of the loop ; Driver Code
< ? php function findDigitalRoot ( $ num ) { $ sum = PHP_INT_MAX ; $ tempNum = $ num ; while ( $ sum >= 10 ) { $ sum = 0 ; while ( $ tempNum > 0 ) { $ sum += $ tempNum % 10 ; $ tempNum /= 10 ; } $ tempNum = $ sum ; } return $ sum ; } function findAnswer ( $ X , $ N ) { $ counter = 0 ; for ( $ i = 1 ; $ counter < $ N ; ++ $ i ) { $ digitalRoot = findDigitalRoot ( $ i ) ; if ( $ digitalRoot == $ X ) { ++ $ counter ; } if ( $ counter == $ N ) { echo ( $ i ) ; break ; } } } $ X = 1 ; $ N = 3 ; findAnswer ( $ X , $ N ) ;
Find Nth positive number whose digital root is X | Function to find the N - th number with digital root as X ; Driver Code
< ? php function findAnswer ( $ X , $ N ) { return ( $ N - 1 ) * 9 + $ X ; } $ X = 7 ; $ N = 43 ; echo ( findAnswer ( $ X , $ N ) ) ; ? >
Sum of the natural numbers ( up to N ) whose modulo with K yield R | Function to return the sum ; If current number gives R as the remainder on dividing by K ; Update the sum ; Return the sum ; Driver code
< ? php function count1 ( $ N , $ K , $ R ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) { if ( $ i % $ K == $ R ) $ sum += $ i ; } return $ sum ; } $ N = 20 ; $ K = 4 ; $ R = 3 ; echo count1 ( $ N , $ K , $ R ) ; ? >
Find the number of solutions to the given equation | Function to return the count of valid values of X ; Iterate through all possible sum of digits of X ; Get current value of X for sum of digits i ; Find sum of digits of cr ; If cr is a valid choice for X ; Return the count ; Driver code
< ? php function getCount ( $ a , $ b , $ c ) { $ count = 0 ; for ( $ i = 1 ; $ i <= 81 ; $ i ++ ) { $ cr = $ b * ( int ) pow ( $ i , $ a ) + $ c ; $ tmp = $ cr ; $ sm = 0 ; while ( $ tmp != 0 ) { $ sm += $ tmp % 10 ; $ tmp /= 10 ; } if ( $ sm == $ i && $ cr < 1e9 ) $ count ++ ; } return $ count ; } { $ a = 3 ; $ b = 2 ; $ c = 8 ; echo ( getCount ( $ a , $ b , $ c ) ) ; }
Check if an array of 1 s and 2 s can be divided into 2 parts with equal sum | Function to check if it is possible to split the array in two parts with equal sum ; Calculate sum of elements and count of 1 's ; If total sum is odd , return False ; If sum of each part is even , return True ; If sum of each part is even but there is atleast one 1 ; Driver Code
< ? php function isSpiltPossible ( $ n , $ a ) { $ sum = 0 ; $ c1 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum += $ a [ $ i ] ; if ( $ a [ $ i ] == 1 ) { $ c1 ++ ; } } if ( $ sum % 2 != 0 ) return false ; if ( ( $ sum / 2 ) % 2 == 0 ) return true ; if ( $ c1 > 0 ) return true ; else return false ; } $ n = 3 ; $ a = array ( 1 , 1 , 2 ) ; if ( isSpiltPossible ( $ n , $ a ) ) echo ( " YES " ) ; else echo ( " NO " ) ; ? >
Sum of all Submatrices of a Given Matrix | Function to find the sum of all possible submatrices of a given Matrix ; Variable to store the required sum ; Nested loop to find the number of submatrices , each number belongs to ; Number of ways to choose from top - left elements ; Number of ways to choose from bottom - right elements ; Driver Code
< ? php function matrixSum ( $ arr ) { $ n = 3 ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ top_left = ( $ i + 1 ) * ( $ j + 1 ) ; $ bottom_right = ( $ n - $ i ) * ( $ n - $ j ) ; $ sum += ( $ top_left * $ bottom_right * $ arr [ $ i ] [ $ j ] ) ; } return $ sum ; } $ arr = array ( array ( 1 , 1 , 1 ) , array ( 1 , 1 , 1 ) , array ( 1 , 1 , 1 ) ) ; echo matrixSum ( $ arr ) ; ? >
Maximum Bitwise AND pair from given range | Function to return the maximum bitwise AND possible among all the possible pairs ; Maximum among all ( i , j ) pairs ; Driver code
< ? php function maxAND ( $ L , $ R ) { $ maximum = $ L & $ R ; for ( $ i = $ L ; $ i < $ R ; $ i ++ ) for ( $ j = $ i + 1 ; $ j <= $ R ; $ j ++ ) $ maximum = max ( $ maximum , ( $ i & $ j ) ) ; return $ maximum ; } $ L = 1 ; $ R = 632 ; echo ( maxAND ( $ L , $ R ) ) ; ? >
Split the array into odd number of segments of odd lengths | Function to check ; Check the result by processing the first & last element and size ; Driver code
< ? php function checkArray ( $ arr , $ n ) { return ( $ arr [ 0 ] % 2 ) && ( $ arr [ $ n - 1 ] % 2 ) && ( $ n % 2 ) ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 ) ; $ n = sizeof ( $ arr ) ; echo checkArray ( $ arr , $ n ) ; ? >
Minimum removals to make array sum odd | Function to find minimum removals ; Count odd numbers ; If the counter is odd return 0 otherwise return 1 ; Driver Code
< ? php function findCount ( $ arr , $ n ) { $ countOdd = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] % 2 == 1 ) $ countOdd ++ ; if ( $ countOdd % 2 == 0 ) return 1 ; else return 0 ; } $ arr = array ( 1 , 2 , 3 , 5 , 1 ) ; $ n = sizeof ( $ arr ) ; echo ( findCount ( $ arr , $ n ) ) ; ? >
Check whether the number can be made perfect square after adding 1 | Function that returns true if x is a perfect square ; Find floating point value of square root of x ; If square root is an integer ; Function that returns true if n is a sunny number ; If ( n + 1 ) is a perfect square ; Driver code
< ? php function isPerfectSquare ( $ x ) { $ sr = sqrt ( $ x ) ; return ( ( $ sr - floor ( $ sr ) ) == 0 ) ; } function isSunnyNum ( $ n ) { if ( isPerfectSquare ( $ n + 1 ) ) return true ; return false ; } $ n = 3 ; if ( isSunnyNum ( $ n ) ) echo " Yes " ; else echo " No " ; ? >
Count primes that can be expressed as sum of two consecutive primes and 1 | PHP implementation of the approach ; To check if a number is prime or not ; To store possible numbers ; Function to return all prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to count all possible prime numbers that can be expressed as the sum of two consecutive primes and one ; All possible prime numbers below N ; Driver code
< ? php $ N = 10005 ; $ isprime = array_fill ( 0 , $ N , true ) ; $ can = array_fill ( 0 , $ N , false ) ; function SieveOfEratosthenes ( ) { global $ N , $ isprime ; for ( $ p = 2 ; $ p * $ p < $ N ; $ p ++ ) { if ( $ isprime [ $ p ] == true ) { for ( $ i = $ p * $ p ; $ i < $ N ; $ i += $ p ) $ isprime [ $ i ] = false ; } } $ primes = array ( ) ; for ( $ i = 2 ; $ i < $ N ; $ i ++ ) if ( $ isprime [ $ i ] ) array_push ( $ primes , $ i ) ; return $ primes ; } function Prime_Numbers ( $ n ) { global $ N , $ can , $ isprime ; $ primes = SieveOfEratosthenes ( ) ; for ( $ i = 0 ; $ i < count ( $ primes ) - 1 ; $ i ++ ) if ( $ primes [ $ i ] + $ primes [ $ i + 1 ] + 1 < $ N ) $ can [ $ primes [ $ i ] + $ primes [ $ i + 1 ] + 1 ] = true ; $ ans = 0 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { if ( $ can [ $ i ] and $ isprime [ $ i ] ) { $ ans ++ ; } } return $ ans ; } $ n = 50 ; echo Prime_Numbers ( $ n ) ; ? >