codeparrot/xlcost-text-to-code huggingface dataset:
A Sum Array Puzzle | PHP implementation of above approach ; Allocate memory for temporary arrays leftSum [ ] , rightSum [ ] and Sum [ ] ; Left most element of left array is always 0 ; Rightmost most element of right array is always 0 ; Construct the left array ; Construct the right array ; Construct the sum array using left [ ] and right [ ] ; print the constructed prod array ; Driver Code
< ? php function sumArray ( $ arr , $ n ) { $ leftSum = array_fill ( 0 , $ n , 0 ) ; $ rightSum = array_fill ( 0 , $ n , 0 ) ; $ Sum = array_fill ( 0 , $ n , 0 ) ; $ leftSum [ 0 ] = 0 ; $ rightSum [ $ n - 1 ] = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ leftSum [ $ i ] = $ arr [ $ i - 1 ] + $ leftSum [ $ i - 1 ] ; for ( $ j = $ n - 2 ; $ j >= 0 ; $ j -- ) $ rightSum [ $ j ] = $ arr [ $ j + 1 ] + $ rightSum [ $ j + 1 ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ Sum [ $ i ] = $ leftSum [ $ i ] + $ rightSum [ $ i ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ Sum [ $ i ] . " ▁ " ; } $ arr = array ( 3 , 6 , 4 , 8 , 9 ) ; $ n = count ( $ arr ) ; sumArray ( $ arr , $ n ) ; ? >
Find minimum x such that ( x % k ) * ( x / k ) == n | Set | Function gives the required answer ; Iterate for all the factors ; Check if i is a factor ; Consider i to be A and n / i to be B ; Consider i to be B and n / i to be A ; Driver Code
< ? php function minimumX ( $ n , $ k ) { $ mini = PHP_INT_MAX ; for ( $ i = 1 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { $ fir = $ i ; $ sec = ( int ) $ n / $ i ; $ num1 = $ fir * $ k + $ sec ; $ res = ( int ) ( $ num1 / $ k ) * ( $ num1 % $ k ) ; if ( $ res == $ n ) $ mini = min ( $ num1 , $ mini ) ; $ num2 = $ sec * $ k + $ fir ; $ res = ( int ) ( $ num2 / $ k ) * ( $ num2 % $ k ) ; if ( $ res == $ n ) $ mini = min ( $ num2 , $ mini ) ; } } return $ mini ; } $ n = 4 ; $ k = 6 ; echo minimumX ( $ n , $ k ) , " STRNEWLINE " ; $ n = 5 ; $ k = 5 ; echo minimumX ( $ n , $ k ) , " STRNEWLINE " ; ? >
Find minimum x such that ( x % k ) * ( x / k ) == n | This function gives the required answer ; Iterate over all possible remainders ; it must divide n ; Driver Code
< ? php function minimumX ( $ n , $ k ) { $ ans = PHP_INT_MAX ; for ( $ rem = $ k - 1 ; $ rem > 0 ; $ rem -- ) { if ( $ n % $ rem == 0 ) $ ans = min ( $ ans , $ rem + ( $ n / $ rem ) * $ k ) ; } return $ ans ; } $ n = 4 ; $ k = 6 ; echo minimumX ( $ n , $ k ) , " STRNEWLINE " ; $ n = 5 ; $ k = 5 ; echo minimumX ( $ n , $ k ) ; ? >
Find nth Hermite number | Function to return nth Hermite number ; Base conditions ; Driver Code ; Print nth Hermite number
< ? php function getHermiteNumber ( $ n ) { if ( $ n == 0 ) return 1 ; if ( $ n == 1 ) return 0 ; else return -2 * ( $ n - 1 ) * getHermiteNumber ( $ n - 2 ) ; } $ n = 6 ; echo getHermiteNumber ( $ n ) ; ? >
Find numbers a and b that satisfy the given conditions | Function to print the required numbers ; Suppose b = n and we want a % b = 0 and also ( a / b ) < n so a = b * ( n - 1 ) ; Special case if n = 1 we get a = 0 so ( a * b ) < n ; If no pair satisfies the conditions ; Driver code
< ? php function find ( $ n ) { $ b = $ n ; $ a = $ b * ( $ n - 1 ) ; if ( $ a * $ b > $ n && $ a / $ b < $ n ) { echo " a = " ▁ , ▁ $ a ▁ , ▁ " , b = " } else echo - 1 ; } $ n = 10 ; find ( $ n ) ; ? >
Closest perfect square and its distance | Function to check if a number is perfect square or not ; Function to find the closest perfect square taking minimum steps to reach from a number ; Variables to store first perfect square number above and below N ; Finding first perfect square number greater than N ; Finding first perfect square number less than N ; Variables to store the differences ; Driver code
< ? php function isPerfect ( $ N ) { if ( ( sqrt ( $ N ) - floor ( sqrt ( $ N ) ) ) != 0 ) return false ; return true ; } function getClosestPerfectSquare ( $ N ) { if ( isPerfect ( $ N ) ) { echo $ N , " ▁ " , "0" , " STRNEWLINE " ; return ; } $ aboveN = -1 ; $ belowN = -1 ; $ n1 ; $ n1 = $ N + 1 ; while ( true ) { if ( isPerfect ( $ n1 ) ) { $ aboveN = $ n1 ; break ; } else $ n1 ++ ; } $ n1 = $ N - 1 ; while ( true ) { if ( isPerfect ( $ n1 ) ) { $ belowN = $ n1 ; break ; } else $ n1 -- ; } $ diff1 = $ aboveN - $ N ; $ diff2 = $ N - $ belowN ; if ( $ diff1 > $ diff2 ) echo $ belowN , " ▁ " , $ diff2 ; else echo $ aboveN , " ▁ " , $ diff1 ; } $ N = 1500 ; getClosestPerfectSquare ( $ N ) ; ? >
Fraction | Function to return gcd of a and b ; Function to convert the obtained fraction into it 's simplest form ; Finding gcd of both terms ; Converting both terms into simpler terms by dividing them by common factor ; Function to add two fractions ; Finding gcd of den1 and den2 ; Denominator of final fraction obtained finding LCM of den1 and den2 LCM * GCD = a * b ; Changing the fractions to have same denominator Numerator of the final fraction obtained ; Calling function to convert final fraction into it 's  simplest form ; Driver Code
< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function lowest ( & $ den3 , & $ num3 ) { $ common_factor = gcd ( $ num3 , $ den3 ) ; $ den3 = ( int ) $ den3 / $ common_factor ; $ num3 = ( int ) $ num3 / $ common_factor ; } function addFraction ( $ num1 , $ den1 , $ num2 , $ den2 , & $ num3 , & $ den3 ) { $ den3 = gcd ( $ den1 , $ den2 ) ; $ den3 = ( $ den1 * $ den2 ) / $ den3 ; $ num3 = ( $ num1 ) * ( $ den3 / $ den1 ) + ( $ num2 ) * ( $ den3 / $ den2 ) ; lowest ( $ den3 , $ num3 ) ; } $ num1 = 1 ; $ den1 = 500 ; $ num2 = 2 ; $ den2 = 1500 ; $ den3 ; $ num3 ; addFraction ( $ num1 , $ den1 , $ num2 , $ den2 , $ num3 , $ den3 ) ; echo $ num1 , " / " , $ den1 , " ▁ + ▁ " , $ num2 , " / " , $ den2 , " ▁ is ▁ equal ▁ to ▁ " , $ num3 , " / " , $ den3 , " STRNEWLINE " ; ? >
Largest Divisor of a Number not divisible by a perfect square | Function to find the largest divisor not divisible by any perfect square greater than 1 ; If the number is divisible by i * i , then remove one i ; Now all squares are removed from n ; Driver Code
< ? php function findLargestDivisor ( $ n ) { for ( $ i = 2 ; $ i < sqrt ( $ n ) + 1 ; $ i ++ ) { while ( $ n % ( $ i * $ i ) == 0 ) { $ n = $ n / $ i ; } } return $ n ; } $ n = 12 ; echo ( findLargestDivisor ( $ n ) ) ; echo ( " STRNEWLINE " ) ; $ n = 97 ; echo ( findLargestDivisor ( $ n ) ) ; ? >
Arithmetic Progression | Returns true if a permutation of arr [ 0. . n - 1 ] can form arithmetic progression ; Sort array ; After sorting , difference between consecutive elements must be same . ; Driver Code
< ? php function checkIsAP ( $ arr , $ n ) { if ( $ n == 1 ) return true ; sort ( $ arr ) ; $ d = $ arr [ 1 ] - $ arr [ 0 ] ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] - $ arr [ $ i - 1 ] != $ d ) return false ; return true ; } $ arr = array ( 20 , 15 , 5 , 0 , 10 ) ; $ n = count ( $ arr ) ; if ( checkIsAP ( $ arr , $ n ) ) echo " Yes " ; else echo " No " ; ? >
Check if a number is Triperfect Number | Returns true if n is Triperfect ; To store sum of divisors . Adding 1 and n since they are divisors of n . ; Find all divisors and add them ; If sum of divisors is equal to 3 * n , then n is a Triperfect number ; Driver Code
< ? php function isTriPerfect ( $ n ) { $ sum = 1 + $ n ; $ i = 2 ; while ( $ i * $ i <= $ n ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) $ sum = $ sum + $ i ; else $ sum = $ sum + $ i + $ n / $ i ; } $ i += 1 ; } if ( $ sum == 3 * $ n and $ n != 1 ) return true ; else false ; } $ n = 120 ; if ( isTriPerfect ( $ n ) ) echo $ n . " ▁ is ▁ a ▁ Triperfect ▁ number " ; else echo $ n . " ▁ is ▁ not ▁ a ▁ Triperfect ▁ number " ; ? >
Sum of first N natural numbers which are divisible by X or Y | Function to calculate the sum of numbers divisible by X or Y ; Driver code
< ? php function sum ( $ N , $ X , $ Y ) { $ S1 ; $ S2 ; $ S3 ; $ S1 = floor ( ( ( int ) $ N / $ X ) ) * ( 2 * $ X + ( int ) ( ( int ) $ N / $ X - 1 ) * $ X ) / 2 ; $ S2 = floor ( ( ( int ) $ N / $ Y ) ) * ( 2 * $ Y + ( int ) ( ( int ) $ N / $ Y - 1 ) * $ Y ) / 2 ; $ S3 = floor ( ( ( int ) $ N / ( $ X * $ Y ) ) ) * ( 2 * ( $ X * $ Y ) + ( ( int ) $ N / ( $ X * $ Y ) - 1 ) * ( int ) ( $ X * $ Y ) ) / 2 ; return ceil ( $ S1 + ( $ S2 - $ S3 ) ) ; } $ N = 14 ; $ X = 3 ; $ Y = 5 ; echo sum ( $ N , $ X , $ Y ) ; #This  code is contributed by ajit. NEW_LINE ? >
Count numbers from range whose prime factors are only 2 and 3 | Function to count the number within a range whose prime factors are only 2 and 3 ; Start with 2 so that 1 doesn 't get counted ; While num is divisible by 2 , divide it by 2 ; While num is divisible by 3 , divide it by 3 ; If num got reduced to 1 then it has only 2 and 3 as prime factors ; Driver code
< ? php function findTwoThreePrime ( $ l , $ r ) { if ( $ l == 1 ) $ l ++ ; $ count = 0 ; for ( $ i = $ l ; $ i <= $ r ; $ i ++ ) { $ num = $ i ; while ( $ num % 2 == 0 ) $ num /= 2 ; while ( $ num % 3 == 0 ) $ num /= 3 ; if ( $ num == 1 ) $ count ++ ; } return $ count ; } $ l = 1 ; $ r = 10 ; echo findTwoThreePrime ( $ l , $ r ) ; ? >
Maximum number with same digit factorial product | Function to return the required number ; Count the frequency of each digit ; 4 ! can be expressed as 2 ! * 2 ! * 3 ! ; 6 ! can be expressed as 5 ! * 3 ! ; 8 ! can be expressed as 7 ! * 2 ! * 2 ! * 2 ! ; 9 ! can be expressed as 7 ! * 3 ! * 3 ! * 2 ! ; To store the required number ; If number has only either 1 and 0 as its digits ; Generate the greatest number possible ; Driver code
< ? php function getNumber ( $ s ) { $ number_of_digits = strlen ( $ s ) ; $ freq = array_fill ( 0 , 10 , 0 ) ; for ( $ i = 0 ; $ i < $ number_of_digits ; $ i ++ ) { if ( $ s [ $ i ] == '1' $ s [ $ i ] == '2' $ s [ $ i ] == '3' $ s [ $ i ] == '5' $ s [ $ i ] == '7' ) { $ freq [ ord ( $ s [ $ i ] ) - 48 ] += 1 ; } if ( $ s [ $ i ] == '4' ) { $ freq [ 2 ] += 2 ; $ freq [ 3 ] ++ ; } if ( $ s [ $ i ] == '6' ) { $ freq [ 5 ] ++ ; $ freq [ 3 ] ++ ; } if ( $ s [ $ i ] == '8' ) { $ freq [ 7 ] ++ ; $ freq [ 2 ] += 3 ; } if ( $ s [ $ i ] == '9' ) { $ freq [ 7 ] ++ ; $ freq [ 3 ] += 2 ; $ freq [ 2 ] ++ ; } } $ t = " " ; if ( $ freq [ 1 ] == $ number_of_digits || $ freq [ 0 ] == $ number_of_digits || ( $ freq [ 0 ] + $ freq [ 1 ] ) == $ number_of_digits ) { return $ s ; } else { for ( $ i = 9 ; $ i >= 2 ; $ i -- ) { $ ctr = $ freq [ $ i ] ; while ( $ ctr -- ) { $ t . = chr ( $ i + 48 ) ; } } return $ t ; } } $ s = "1280" ; echo getNumber ( $ s ) ; ? >
Program to find first N Iccanobif Numbers | Iterative function to reverse digits of num ; Function to print first N Icanobif Numbers ; Initialize first , second numbers ; Print first two numbers ; Reversing digit of previous two terms and adding them ; Driver Code
< ? php function reversDigits ( $ num ) { $ rev_num = 0 ; while ( $ num > 0 ) { $ rev_num = ( $ rev_num * 10 ) + ( $ num % 10 ) ; $ num = ( int ) ( $ num / 10 ) ; } return $ rev_num ; } function icanobifNumbers ( $ N ) { $ first = 0 ; $ second = 1 ; if ( $ N == 1 ) echo $ first ; else if ( $ N == 2 ) echo $ first , " ▁ " , $ second ; else { echo $ first , " " ▁ , ▁ $ second , ▁ " " for ( $ i = 3 ; $ i <= $ N ; $ i ++ ) { $ x = reversDigits ( $ first ) ; $ y = reversDigits ( $ second ) ; echo ( $ x + $ y ) , " ▁ " ; $ temp = $ second ; $ second = $ x + $ y ; $ first = $ temp ; } } } $ N = 12 ; icanobifNumbers ( $ N ) ; ? >
Add N digits to A such that it is divisible by B after each addition | PHP implementation of the approach ; Try all digits from ( 0 to 9 ) ; Fails in the first move itself ; Add ( n - 1 ) 0 's ; Driver Code
< ? php function addNDigits ( $ a , $ b , $ n ) { $ num = $ a ; for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { $ tmp = $ a * 10 + $ i ; if ( $ tmp % $ b == 0 ) { $ a = $ tmp ; break ; } } if ( $ num == $ a ) return -1 ; for ( $ j = 0 ; $ j < $ n - 1 ; $ j ++ ) $ a *= 10 ; return $ a ; } $ a = 5 ; $ b = 3 ; $ n = 3 ; echo addNDigits ( $ a , $ b , $ n ) ;
Count number of triplets ( a , b , c ) such that a ^ 2 + b ^ 2 = c ^ 2 and 1 <= a <= b <= c <= n | Function to ind number of Triplets 1 <= a <= b <= c <= n , Such that a ^ 2 + b ^ 2 = c ^ 2 ; to store required answer ; run nested loops for first two numbers . ; third number ; check if third number is perfect square and less than n ; Driver code ; function call
< ? php function Triplets ( $ n ) { $ ans = 0 ; for ( $ i = 1 ; $ i <= $ n ; ++ $ i ) { for ( $ j = $ i ; $ j <= $ n ; ++ $ j ) { $ x = $ i * $ i + $ j * $ j ; $ y = ( int ) sqrt ( $ x ) ; if ( $ y * $ y == $ x && $ y <= $ n ) ++ $ ans ; } } return $ ans ; } $ n = 10 ; echo Triplets ( $ n ) ; ? >
Sum of the digits of a number N written in all bases from 2 to N / 2 | Function to calculate the sum of the digits of n in the given base ; Sum of digits ; Digit of n in the given base ; Add the digit ; Function to calculate the sum of digits of n in bases from 2 to n / 2 ; to store digit sum in all bases ; function call for multiple bases ; Driver Code
< ? php function solve ( $ n , $ base ) { $ sum = 0 ; while ( $ n > 0 ) { $ remainder = $ n % $ base ; $ sum += $ remainder ; $ n = $ n / $ base ; } return $ sum ; } function SumsOfDigits ( $ n ) { $ sum = 0 ; for ( $ base = 2 ; $ base <= $ n / 2 ; ++ $ base ) $ sum += solve ( $ n , $ base ) ; echo $ sum ; } $ n = 8 ; SumsOfDigits ( $ n ) ; ? >
Largest number in an array that is not a perfect cube | Function to check if a number is perfect cube number or not ; takes the sqrt of the number ; checks if it is a perfect cube number ; Function to find the largest non perfect cube number in the array ; stores the maximum of all perfect cube numbers ; Traverse all elements in the array ; store the maximum if current element is a non perfect cube ; Driver Code
< ? php function checkPerfectcube ( $ n ) { $ d = ( int ) round ( pow ( $ n , 1 / 3 ) ) ; if ( $ d * $ d * $ d == $ n ) return true ; return false ; } function largestNonPerfectcubeNumber ( $ a , $ n ) { $ maxi = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( ! checkPerfectcube ( $ a [ $ i ] ) ) $ maxi = max ( $ a [ $ i ] , $ maxi ) ; } return $ maxi ; } $ a = array ( 16 , 64 , 25 , 2 , 3 , 10 ) ; $ n = count ( $ a ) ; echo largestNonPerfectcubeNumber ( $ a , $ n ) ; ? >
Check if N can be represented as sum of integers chosen from set { A , B } | Function to find if number N can be represented as sum of a ' s ▁ and ▁ b ' s ; base condition ; if x is already visited ; set x as possible ; recursive call ; Driver Code
< ? php function checkIfPossibleRec ( $ x , $ a , $ b , $ isPossible , $ n ) { if ( $ x > $ n ) return ; if ( $ isPossible == true ) return ; $ isPossible [ $ x ] = true ; checkIfPossibleRec ( $ x + $ a , $ a , $ b , $ isPossible , $ n ) ; checkIfPossibleRec ( $ x + $ b , $ a , $ b , $ isPossible , $ n ) ; } function checkPossible ( $ n , $ a , $ b ) { $ isPossible [ $ n + 1 ] = array ( false ) ; checkIfPossibleRec ( 0 , $ a , $ b , $ isPossible , $ n ) ; return $ isPossible ; } $ a = 3 ; $ b = 7 ; $ n = 8 ; if ( checkPossible ( $ a , $ b , $ n ) ) echo " No " ; else echo " Yes " ; ? >
Sum of all odd natural numbers in range L and R | Function to return the sum of all odd natural numbers ; Function to return the sum of all odd numbers in range L and R ; Driver Code
< ? php function sumOdd ( $ n ) { $ terms = ( int ) ( $ n + 1 ) / 2 ; $ sum = $ terms * $ terms ; return $ sum ; } function suminRange ( $ l , $ r ) { return sumOdd ( $ r ) - sumOdd ( $ l - 1 ) ; } $ l = 2 ; $ r = 5 ; echo " Sum ▁ of ▁ odd ▁ natural ▁ numbers ▁ from ▁ L ▁ to ▁ R ▁ is ▁ " , suminRange ( $ l , $ r ) ; ? >
Sum of common divisors of two numbers A and B | Function to calculate gcd of two numbers ; Function to calculate all common divisors of two given numbers a , b -- > input integer numbers ; find gcd of a , b ; Find the sum of divisors of n . ; if ' i ' is factor of n ; check if divisors are equal ; Driver program to run the case
< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function sumcommDiv ( $ a , $ b ) { $ n = gcd ( $ a , $ b ) ; $ sum = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) $ sum += $ i ; else $ sum += ( $ n / $ i ) + $ i ; } } return $ sum ; } $ a = 10 ; $ b = 15 ; echo " Sum = " ? >
Check if a number is formed by Concatenation of 1 , 14 or 144 only | Function to check if a number is formed by Concatenation of 1 , 14 or 144 only ; check for each possible digit if given number consist other then 1 , 14 , 144 print NO else print YES ; Driver Code
< ? php function checkNumber ( $ N ) { $ temp = $ N ; while ( $ temp > 0 ) { if ( $ temp % 1000 == 144 ) $ temp /= 1000 ; else if ( $ temp % 100 == 14 ) $ temp /= 100 ; else if ( $ temp % 10 == 1 ) $ temp /= 10 ; else { return " YES " ; } } return " NO " ; } $ N = 1414 ; echo checkNumber ( $ N ) ; ? >
Fibonacci problem ( Value of Fib ( N ) * Fib ( N ) | PHP implementation of the approach ; Driver code
< ? php function getResult ( $ n ) { if ( $ n & 1 ) return 1 ; return -1 ; } $ n = 3 ; echo getResult ( $ n ) ; ? >
Find two numbers with sum and product both same as N | Function to return the smallest string ; Not possible ; find a and b ; Driver Code
< ? php function findAandB ( $ N ) { $ val = $ N * $ N - 4.0 * $ N ; if ( $ val < 0 ) { echo " NO " ; return ; } $ a = ( $ N + sqrt ( $ val ) ) / 2.0 ; $ b = ( $ N - sqrt ( $ val ) ) / 2.0 ; echo " a = " ▁ , ▁ $ a , ▁ " " ; STRNEWLINE TABSYMBOL echo ▁ " b = " ▁ , ▁ $ b , ▁ " " } $ N = 69.0 ; findAandB ( $ N ) ; ? >
Find minimum operations needed to make an Array beautiful | Function to find minimum operations required to make array beautiful ; counting consecutive zeros . ; check that start and end are same ; check is zero and one are equal ; Driver Code
< ? php function minOperations ( $ A , $ n ) { if ( $ n & 1 ) return -1 ; $ zeros = 0 ; $ consZeros = 0 ; $ ones = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ A [ $ i ] == 0 ? $ zeros ++ : $ ones ++ ; if ( ( $ i + 1 ) < $ n ) { if ( $ A [ $ i ] == 0 && $ A [ $ i + 1 ] == 0 ) $ consZeros ++ ; } } if ( $ A [ 0 ] == $ A [ $ n - 1 ] && $ A [ 0 ] == 0 ) $ consZeros ++ ; if ( $ zeros == $ ones ) return $ consZeros ; else return -1 ; } $ A = array ( 1 , 1 , 0 , 0 ) ; $ n = sizeof ( $ A ) ; echo minOperations ( $ A , $ n ) ; ? >
Steps to reduce N to zero by subtracting its most significant digit at every step | Function to count the number of digits in a number m ; Function to count the number of steps to reach 0 ; count the total number of stesp ; iterate till we reach 0 ; count the digits in last ; decrease it by 1 ; find the number on whose division , we get the first digit ; first digit in last ; find the first number less than last where the first digit changes ; find the number of numbers with same first digit that are jumped ; count the steps ; the next number with a different first digit ; Driver code
< ? php function countdig ( $ m ) { if ( $ m == 0 ) return 0 ; else return 1 + countdig ( ( int ) ( $ m / 10 ) ) ; } function countSteps ( $ x ) { $ c = 0 ; $ last = $ x ; while ( $ last ) { $ digits = countdig ( $ last ) ; $ digits -= 1 ; $ divisor = pow ( 10 , $ digits ) ; $ first = ( int ) ( $ last / $ divisor ) ; $ lastnumber = $ first * $ divisor ; $ skipped = ( $ last - $ lastnumber ) / $ first ; $ skipped += 1 ; $ c += $ skipped ; $ last = $ last - ( $ first * $ skipped ) ; } return $ c ; } $ n = 14 ; echo countSteps ( $ n ) ;
GCD of a number raised to some power and another number | Calculates modular exponentiation , i . e . , ( 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 GCD of a ^ n and b ; Driver code
< ? php function power ( $ x , $ y , $ p ) { while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function powerGCD ( $ a , $ b , $ n ) { $ e = power ( $ a , $ n , $ b ) ; return gcd ( $ e , $ b ) ; } $ a = 5 ; $ b = 4 ; $ n = 2 ; echo powerGCD ( $ a , $ b , $ n ) ; ? >
Largest number not greater than N all the digits of which are odd | Function to check if all digits of a number are odd ; iterate for all digits ; if digit is even ; all digits are odd ; function to return the largest number with all digits odd ; iterate till we find a number with all digits odd ; Driver Code
< ? php function allOddDigits ( $ n ) { while ( $ n > 1 ) { if ( ( $ n % 10 ) % 2 == 0 ) return false ; $ n = ( int ) $ n / 10 ; } return true ; } function largestNumber ( $ n ) { if ( $ n % 2 == 0 ) $ n -- ; for ( $ i = $ n ; ; $ i = ( $ i - 2 ) ) if ( allOddDigits ( $ i ) ) return $ i ; } $ N = 23 ; echo largestNumber ( $ N ) ; ? >
Count number less than N which are product of perfect squares | Function to count number less than N which are product of any two perfect squares ; Driver Code
< ? php function countNumbers ( $ N ) { return ( int ) ( sqrt ( $ N ) ) - 1 ; } $ N = 36 ; echo countNumbers ( $ N ) ; ? >
Count ordered pairs with product less than N | Function to return count of Ordered pairs whose products are less than N ; Initialize count to 0 ; count total pairs ; multiply by 2 to get ordered_pairs ; subtract redundant pairs ( a , b ) where a == b . ; return answer ; Driver code ; function call to print required answer
< ? php function countOrderedPairs ( $ N ) { $ count_pairs = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ N - 1 ) ; ++ $ i ) { for ( $ j = $ i ; $ j * $ i < $ N ; ++ $ j ) ++ $ count_pairs ; } $ count_pairs *= 2 ; $ count_pairs -= ( sqrt ( $ N - 1 ) ) ; return $ count_pairs ; } $ N = 5 ; echo countOrderedPairs ( $ N ) ; ? >
Absolute Difference of all pairwise consecutive elements in an array | Function to print pairwise absolute difference of consecutive elements ; absolute difference between consecutive numbers ; Driver Code
< ? php function pairwiseDifference ( $ arr , $ n ) { $ diff = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { $ diff = abs ( $ arr [ $ i ] - $ arr [ $ i + 1 ] ) ; echo $ diff . " " ; } } $ arr = array ( 4 , 10 , 15 , 5 , 6 ) ; $ n = sizeof ( $ arr ) ; pairwiseDifference ( $ arr , $ n ) ; ? >
Find the sum of all multiples of 2 and 5 below N | Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of 2 and 5 below N ; Since , we need the sum of multiples less than N ; Driver code
< ? php function sumAP ( $ n , $ d ) { $ n = ( int ) ( $ n / $ d ) ; return ( $ n ) * ( ( 1 + $ n ) * ( int ) $ d / 2 ) ; } function sumMultiples ( $ n ) { $ n -- ; return sumAP ( $ n , 2 ) + sumAP ( $ n , 5 ) - sumAP ( $ n , 10 ) ; } $ n = 20 ; echo sumMultiples ( $ n ) ; ? >
Find the total marks obtained according to given marking scheme | Function that calculates marks . ; for not attempt score + 0 ; for each correct answer score + 3 ; for each wrong answer score - 1 ; calculate total marks ; Driver code
< ? php function markingScheme ( $ N , $ answerKey , $ studentAnswer ) { $ positive = 0 ; $ negative = 0 ; $ notattempt = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { if ( $ studentAnswer [ $ i ] == 0 ) $ notattempt ++ ; else if ( $ answerKey [ $ i ] == $ studentAnswer [ $ i ] ) $ positive ++ ; else if ( $ answerKey [ $ i ] != $ studentAnswer [ $ i ] ) $ negative ++ ; } return ( $ positive * 3 ) + ( $ negative * -1 ) ; } $ answerKey = array ( 1 , 2 , 3 , 4 , 1 ) ; $ studentAnswer = array ( 1 , 2 , 3 , 4 , 0 ) ; $ N = sizeof ( $ answerKey ) ; echo markingScheme ( $ N , $ answerKey , $ studentAnswer ) ; ? >