codeparrot/xlcost-text-to-code huggingface dataset:
Woodall Number | PHP program to check if a number is Woodball or not . ; If number is even , return false . ; If x is 1 , return true . ; Add 1 to make x even ; While x is divisible by 2 ; Divide x by 2 ; Count the power ; If at any point power and x became equal , return true . ; Driver Code
< ? php function isWoodall ( $ x ) { if ( $ x % 2 == 0 ) return false ; if ( $ x == 1 ) return true ; $ x ++ ; $ p = 0 ; while ( $ x % 2 == 0 ) { $ x = $ x / 2 ; $ p ++ ; if ( $ p == $ x ) return true ; } return false ; } $ x = 383 ; if ( isWoodall ( $ x ) ) echo " Yes " ; else echo " No " ; ? >
Print k numbers where all pairs are divisible by m | function to generate k numbers whose difference is divisible by m ; Using an adjacency list like representation to store numbers that lead to same remainder . ; stores the modulus when divided by m ; If we found k elements which have same remainder . ; If we could not find k elements ; Driver Code
< ? php function print_result ( $ a , $ n , $ k , $ m ) { $ v = array_fill ( 0 , $ m + 1 , array ( ) ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ rem = $ a [ $ i ] % $ m ; array_push ( $ v [ $ rem ] , $ a [ $ i ] ) ; if ( count ( $ v [ $ rem ] ) == $ k ) { for ( $ j = 0 ; $ j < $ k ; $ j ++ ) echo $ v [ $ rem ] [ $ j ] . " ▁ " ; return ; } } echo " - 1" ; } $ a = array ( 1 , 8 , 4 ) ; $ n = count ( $ a ) ; print_result ( $ a , $ n , 2 , 3 ) ; ? >
Smallest x such that 1 * n , 2 * n , ... x * n have all digits from 1 to 9 | Returns smallest value x such that 1 * n , 2 * n , 3 * n ... x * n have all digits from 1 to 9 at least once ; taking temporary array and variable . ; iterate till we get all the 10 digits at least once ; checking all the digits ; Driver code
< ? php function smallestX ( $ n ) { $ temp = array_fill ( 0 , 10 , false ) ; if ( $ n == 0 ) return -1 ; $ count = 0 ; $ x = 0 ; for ( $ x = 1 ; $ count < 10 ; $ x ++ ) { $ y = $ x * $ n ; while ( $ y ) { if ( $ temp [ $ y % 10 ] == false ) { $ count ++ ; $ temp [ $ y % 10 ] = true ; } $ y = ( int ) ( $ y / 10 ) ; } } return $ x - 1 ; } $ n = 5 ; echo smallestX ( $ n ) ; ? >
Find a number x such that sum of x and its digits is equal to given n . | utility function for digit sum ; function for finding x ; iterate from 1 to n . For every no . check if its digit sum with it is equal to n . ; if no such i found return - 1 ; Driver Code
< ? php function digSum ( $ n ) { $ sum = 0 ; $ rem = 0 ; while ( $ n ) { $ rem = $ n % 10 ; $ sum += $ rem ; $ n /= 10 ; } return $ sum ; } function findX ( $ n ) { for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) if ( $ i + digSum ( $ i ) == $ n ) return $ i ; return -1 ; } $ n = 43 ; echo " x = " ? >
9 's complement of a decimal number | PHP program to find 9 's complement of a number. ; Driver code
< ? php function complement ( $ number ) { for ( $ i = 0 ; $ i < strlen ( $ number ) ; $ i ++ ) if ( $ number [ $ i ] != ' . ' ) $ number [ $ i ] = '9' - $ number [ $ i ] + '0' ; echo "9 ' s ▁ complement ▁ is ▁ : ▁ " , $ number ; } $ number = "345.45" ; complement ( $ number ) ; ? >
Ways to express a number as product of two different factors | To count number of ways in which number expressed as product of two different numbers ; To store count of such pairs ; Counting number of pairs upto sqrt ( n ) - 1 ; To return count of pairs ; Driver Code
< ? php function countWays ( $ n ) { $ count = 0 ; for ( $ i = 1 ; $ i * $ i < $ n ; $ i ++ ) if ( $ n % $ i == 0 ) $ count ++ ; return $ count ; } $ n = 12 ; echo countWays ( $ n ) , " STRNEWLINE " ; ? >
Count divisors of n that have at | function to return true if any digit of m is present in hash [ ] . ; check till last digit ; if number is also present in original number then return true ; if no number matches then return 1 ; Count the no of divisors that have at least 1 digits same ; Store digits present in n in a hash [ ] ; marks that the number is present ; last digit removed ; loop to traverse from 1 to sqrt ( n ) to count divisors ; if i is the factor ; call the function to check if any digits match or not ; if n / i != i then a different number , then check it also ; return the answer ; Driver code
< ? php function isDigitPresent ( $ m , $ hash ) { while ( $ m ) { if ( $ hash [ $ m % 10 ] ) return true ; $ m = ( int ) ( $ m / 10 ) ; } return false ; } function countDivisibles ( $ n ) { $ hash = array_fill ( 0 , 10 , false ) ; $ m = $ n ; while ( $ m ) { $ hash [ $ m % 10 ] = true ; $ m = ( int ) ( $ m / 10 ) ; } $ ans = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( isDigitPresent ( $ i , $ hash ) ) $ ans ++ ; if ( ( int ) ( $ n / $ i ) != $ i ) { if ( isDigitPresent ( ( int ) ( $ n / $ i ) , $ hash ) ) $ ans ++ ; } } } return $ ans ; } $ n = 15 ; echo countDivisibles ( $ n ) ; ? >
Doolittle Algorithm : LU Decomposition | PHP Program to decompose a matrix into lower and upper triangular matrix ; Decomposing matrix into Upper and Lower triangular matrix ; Upper Triangular ; Summation of L ( i , j ) * U ( j , k ) ; Evaluating U ( i , k ) ; Lower Triangular ; $lower [ $i ] [ $i ] = 1 ; Diagonal as 1 ; Summation of L ( k , j ) * U ( j , i ) ; Evaluating L ( k , i ) ; setw is for displaying nicely ; Displaying the result : ; Lower ; Upper ; Driver code
< ? php $ MAX = 100 ; function luDecomposition ( $ mat , $ n ) { $ lower ; $ upper ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ lower [ $ i ] [ $ j ] = 0 ; $ upper [ $ i ] [ $ j ] = 0 ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ k = $ i ; $ k < $ n ; $ k ++ ) { $ sum = 0 ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) $ sum += ( $ lower [ $ i ] [ $ j ] * $ upper [ $ j ] [ $ k ] ) ; $ upper [ $ i ] [ $ k ] = $ mat [ $ i ] [ $ k ] - $ sum ; } for ( $ k = $ i ; $ k < $ n ; $ k ++ ) { if ( $ i == $ k ) else { $ sum = 0 ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) $ sum += ( $ lower [ $ k ] [ $ j ] * $ upper [ $ j ] [ $ i ] ) ; $ lower [ $ k ] [ $ i ] = ( int ) ( ( $ mat [ $ k ] [ $ i ] - $ sum ) / $ upper [ $ i ] [ $ i ] ) ; } } } echo " TABSYMBOL TABSYMBOL Lower ▁ Triangular " ; echo " TABSYMBOL TABSYMBOL TABSYMBOL Upper ▁ Triangular STRNEWLINE " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) echo " TABSYMBOL " . $ lower [ $ i ] [ $ j ] . " TABSYMBOL " ; echo " TABSYMBOL " ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) echo $ upper [ $ i ] [ $ j ] . " TABSYMBOL " ; echo " STRNEWLINE " ; } } $ mat = array ( array ( 2 , -1 , -2 ) , array ( -4 , 6 , 3 ) , array ( -4 , -2 , 8 ) ) ; luDecomposition ( $ mat , 3 ) ; ? >
Divide number into two parts divisible by given numbers | method prints divisible parts if possible , otherwise prints ' Not ▁ possible ' ; creating arrays to store reminder ; looping over all suffix and storing reminder with f ; getting suffix reminder from previous suffix reminder ; looping over all prefix and storing reminder with s ; getting prefix reminder from next prefix reminder ; updating base value ; now looping over all reminders to check partition condition ; if both reminders are 0 and digit itself is not 0 , then print result and return ; if we reach here , then string can ' be partitioned  under constraints ; Driver code
< ? php function printTwoDivisibleParts ( $ num , $ f , $ s ) { $ N = strlen ( $ num ) ; $ prefixReminder = array_fill ( 0 , $ N + 1 , 0 ) ; $ suffixReminder = array_fill ( 0 , $ N + 1 , 0 ) ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) $ suffixReminder [ $ i ] = ( $ suffixReminder [ $ i - 1 ] * 10 + ( ord ( $ num [ $ i - 1 ] ) - 48 ) ) % $ f ; $ base = 1 ; for ( $ i = $ N - 1 ; $ i >= 0 ; $ i -- ) { $ prefixReminder [ $ i ] = ( $ prefixReminder [ $ i + 1 ] + ( ord ( $ num [ $ i ] ) - 48 ) * $ base ) % $ s ; $ base = ( $ base * 10 ) % $ s ; } for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { if ( $ prefixReminder [ $ i ] == 0 && $ suffixReminder [ $ i ] == 0 && $ num [ $ i ] != '0' ) { echo substr ( $ num , 0 , $ i ) . " ▁ " . substr ( $ num , $ i ) . " STRNEWLINE " ; return ; } } echo " Not ▁ Possible STRNEWLINE " ; } $ num = "246904096" ; $ f = 12345 ; $ s = 1024 ; printTwoDivisibleParts ( $ num , $ f , $ s ) ; ? >
Number of subarrays whose minimum and maximum are same | calculate the no of contiguous subarrays which has same minimum and maximum ; stores the answer ; loop to traverse from 0 - n ; start checking subarray from next element ; traverse for finding subarrays ; if the elements are same then we check further and keep a count of same numbers in ' r ' ; the no of elements in between r and i with same elements . ; the no of subarrays that can be formed between i and r ; again start checking from the next index ; returns answer ; Driver Code
< ? php function calculate ( $ a , $ n ) { $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ r = $ i + 1 ; for ( $ j = $ r ; $ j < $ n ; $ j ++ ) { if ( $ a [ $ i ] == $ a [ $ j ] ) $ r += 1 ; else break ; } $ d = $ r - $ i ; $ ans += ( $ d * ( $ d + 1 ) / 2 ) ; $ i = $ r - 1 ; } return $ ans ; } $ a = array ( 2 , 4 , 5 , 3 , 3 , 3 ) ; $ n = count ( $ a ) ; echo calculate ( $ a , $ n ) ; ? >
Count of numbers satisfying m + sum ( m ) + sum ( sum ( m ) ) = N | function that returns sum of digits in a number ; initially sum of digits is 0 ; loop runs till all digits have been extracted ; last digit from backside ; sums up the digits ; the number is reduced to the number removing the last digit ; returns the sum of digits in a number ; function to calculate the count of such occurrences ; counter to calculate the occurrences ; loop to traverse from n - 97 to n ; calls the function to calculate the sum of digits of i ; calls the function to calculate the sum of digits of a ; if the summation is equal to n then increase counter by 1 ; returns the count ; Driver Code ; calls the function to get the answer
< ? php function sum ( $ n ) { $ rem = 0 ; $ sum_of_digits = 0 ; while ( $ n > 0 ) { $ rem = $ n % 10 ; $ sum_of_digits += $ rem ; $ n = $ n / 10 ; } return $ sum_of_digits ; } function countt ( $ n ) { $ c = 0 ; for ( $ i = $ n - 97 ; $ i <= $ n ; $ i ++ ) { $ a = sum ( $ i ) ; $ b = sum ( $ a ) ; if ( ( $ i + $ a + $ b ) == $ n ) { $ c += 1 ; } } return $ c ; } $ n = 9939 ; echo countt ( $ n ) ; ? >
Check if a number is power of k using base changing method | PHP program to check if a number can be raised to k ; loop to change base n to base = k ; Find current digit in base k ; If digit is neither 0 nor 1 ; Make sure that only one 1 is present . ; Driver code
< ? php function isPowerOfK ( $ n , $ k ) { $ oneSeen = false ; while ( $ n > 0 ) { $ digit = $ n % $ k ; if ( $ digit > 1 ) return false ; if ( $ digit == 1 ) { if ( $ oneSeen ) return false ; $ oneSeen = true ; } $ n = ( int ) $ n / $ k ; } return true ; } $ n = 64 ; $ k = 4 ; if ( isPowerOfK ( $ n , $ k ) ) echo " Yes " ; else echo " No " ; ? >
Check if number is palindrome or not in Octal | PHP program to check if octal representation of a number is prime ; Function to Check no is in octal or not ; Function To check no is palindrome or not ; If number is already in octal , we traverse digits using repeated division with 10. Else we traverse digits using repeated division with 8 ; To store individual digits ; Traversing all digits ; checking if octal no is palindrome ; Driver Code
< ? php $ MAX_DIGITS = 20 ; function isOctal ( $ n ) { while ( $ n ) { if ( ( $ n % 10 ) >= 8 ) return false ; else $ n = ( int ) $ n / 10 ; } return true ; } function isPalindrome ( $ n ) { global $ MAX_DIGITS ; $ divide = ( isOctal ( $ n ) == false ) ? 8 : 10 ; $ octal ; $ i = 0 ; while ( $ n != 0 ) { $ octal [ $ i ++ ] = $ n % $ divide ; $ n = ( int ) $ n / $ divide ; } for ( $ j = $ i - 1 , $ k = 0 ; $ k <= $ j ; $ j -- , $ k ++ ) if ( $ octal [ $ j ] != $ octal [ $ k ] ) return -1 ; return 0 ; } $ n = 97 ; if ( isPalindrome ( $ n ) ) echo " Yes " ; else echo " No " ; ? >
Find all factorial numbers less than or equal to n | PHP program to find all factorial numbers smaller than or equal to n . ; Compute next factorial using previous ; Driver code
< ? php function printFactorialNums ( $ n ) { $ fact = 1 ; $ x = 2 ; while ( $ fact <= $ n ) { echo $ fact , " " ; $ fact = $ fact * $ x ; $ x ++ ; } } $ n = 100 ; echo printFactorialNums ( $ n ) ; ? >
Check whether a number has exactly three distinct factors or not | Utility function to check whether a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check whether given number has three distinct factors or not ; Find square root of number ; Check whether number is perfect square or not ; If number is perfect square , check whether square root is prime or not ; 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 isThreeDisctFactors ( $ n ) { $ sq = sqrt ( $ n ) ; if ( $ sq * $ sq != $ n ) return false ; return isPrime ( $ sq ) ? true : false ; } $ num = 9 ; if ( isThreeDisctFactors ( $ num ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; $ num = 15 ; if ( isThreeDisctFactors ( $ num ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; $ num = 12397923568441 ; if ( isThreeDisctFactors ( $ num ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; ? >
Find the last digit when factorial of A divides factorial of B | Function which computes the last digit of resultant of B ! / A ! ; If A = B , B ! = A ! and B ! / A ! = 1 ; If difference ( B - A ) >= 5 , answer = 0 ; If non of the conditions are true , we iterate from A + 1 to B and multiply them . We are only concerned for the last digit , thus we take modulus of 10 ; Driver Code
< ? php function computeLastDigit ( $ A , $ B ) { $ variable = 1 ; if ( $ A == $ B ) return 1 ; else if ( ( $ B - $ A ) >= 5 ) return 0 ; else { for ( $ i = $ A + 1 ; $ i <= $ B ; $ i ++ ) $ variable = ( $ variable * ( $ i % 10 ) ) % 10 ; return $ variable % 10 ; } } echo computeLastDigit ( 2632 , 2634 ) ; ? >
Program for sum of arithmetic series | Function to find sum of series . ; Driver Code
< ? php function sumOfAP ( $ a , $ d , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum = $ sum + $ a ; $ a = $ a + $ d ; } return $ sum ; } $ n = 20 ; $ a = 2.5 ; $ d = 1.5 ; echo ( sumOfAP ( $ a , $ d , $ n ) ) ; ? >
Product of factors of number | PHP program to calculate product of factors of number ; function to product the factors ; If factors are equal , multiply only once ; Otherwise multiply both ; Driver Code
< ? php $ M = 1000000007 ; function multiplyFactors ( $ n ) { global $ M ; $ prod = 1 ; for ( $ i = 1 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) $ prod = ( $ prod * $ i ) % $ M ; else { $ prod = ( $ prod * $ i ) % $ M ; $ prod = ( $ prod * $ n / $ i ) % $ M ; } } } return $ prod ; } $ n = 12 ; echo multiplyFactors ( $ n ) ; ? >
Product of factors of number | PHP program to calculate product of factors of number ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; function to count the factors ; If factors are equal , count only once ; Otherwise count both ; Calculate product of factors ; If numFactor is odd return power ( n , numFactor / 2 ) * sqrt ( n ) ; Driver code
< ? php $ M = 1000000007 ; function power ( $ x , $ y ) { global $ M ; $ res = 1 ; while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ M ; $ y = ( $ y >> 1 ) % $ M ; $ x = ( $ x * $ x ) % $ M ; } return $ res ; } function countFactors ( $ n ) { $ count = 0 ; for ( $ i = 1 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) $ count ++ ; else $ count += 2 ; } } return $ count ; } function multiplyFactors ( $ n ) { $ numFactor = countFactors ( $ n ) ; $ product = power ( $ n , $ numFactor / 2 ) ; if ( $ numFactor & 1 ) $ product = ( $ product * sqrt ( $ n ) ) % $ M ; return $ product ; } $ n = 12 ; echo multiplyFactors ( $ n ) ; ? >
Decimal representation of given binary string is divisible by 10 or not | function to check whether decimal representation of given binary number is divisible by 10 or not ; if last digit is '1' , then number is not divisible by 10 ; to accumulate the sum of last digits in perfect powers of 2 ; traverse from the 2 nd last up to 1 st digit in ' bin ' ; if digit in '1' ; calculate digit 's  position from the right ; according to the digit 's  position, obtain the last  digit of the applicable  perfect power of 2 ; if last digit is 0 , then divisible by 10 ; not divisible by 10 ; Driver Code
< ? php function isDivisibleBy10 ( $ bin ) { $ n = strlen ( $ bin ) ; if ( $ bin [ $ n - 1 ] == '1' ) return false ; $ sum = 0 ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { if ( $ bin [ $ i ] == '1' ) { $ posFromRight = $ n - $ i - 1 ; if ( $ posFromRight % 4 == 1 ) $ sum = $ sum + 2 ; else if ( $ posFromRight % 4 == 2 ) $ sum = $ sum + 4 ; else if ( $ posFromRight % 4 == 3 ) $ sum = $ sum + 8 ; else if ( $ posFromRight % 4 == 0 ) $ sum = $ sum + 6 ; } } if ( $ sum % 10 == 0 ) return true ; return false ; } $ bin = "11000111001110" ; if ( isDivisibleBy10 ( $ bin ) ) echo " Yes " ; else echo " No " ; ? >
Tribonacci Numbers | A space optimized based PHP program to print first n Tribinocci numbers . | ; Initialize first three numbers ; Loop to add previous three numbers for each number starting from 3 and then assign first , second , third to second , third , and curr to third respectively ; Driver code
< ? php function printTrib ( $ n ) { if ( $ n < 1 ) return ; $ first = 0 ; $ second = 0 ; $ third = 1 ; echo $ first , " " ; if ( $ n > 1 ) echo $ second , " ▁ " ; if ( $ n > 2 ) echo $ second , " ▁ " ; for ( $ i = 3 ; $ i < $ n ; $ i ++ ) { $ curr = $ first + $ second + $ third ; $ first = $ second ; $ second = $ third ; $ third = $ curr ; echo $ curr , " " ; } } $ n = 10 ; printTrib ( $ n ) ; ? >
Prime Number of Set Bits in Binary Representation | Set 2 | Function to create an array of prime numbers upto number ' 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 . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Append all the prime numbers to the list ; Utility function to count the number of set bits ; Driver program ; Here prime numbers are checked till the maximum number of bits possible because that the maximum bit sum possible is the number of bits itself .
< ? php function SieveOfEratosthenes ( $ n ) { $ prime = array_fill ( 0 , $ n + 1 , true ) ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] == true ) for ( $ i = $ p * 2 ; $ i < $ n + 1 ; $ i += $ p ) $ prime [ $ i ] = false ; } $ lis = array ( ) ; for ( $ p = 2 ; $ p <= $ n ; $ p ++ ) if ( $ prime [ $ p ] ) array_push ( $ lis , $ p ) ; return $ lis ; } function setBits ( $ n ) { $ cnt = 0 ; while ( $ n ) { if ( $ n & 1 ) $ cnt ++ ; $ n >>= 1 ; } ; return $ cnt ; } $ x = 4 ; $ y = 8 ; $ count = 0 ; $ primeArr = SieveOfEratosthenes ( ceil ( log ( $ y , 2 ) ) ) ; for ( $ i = $ x ; $ i < $ y + 1 ; $ i ++ ) { $ temp = setBits ( $ i ) ; if ( in_array ( $ temp , $ primeArr ) ) $ count += 1 ; } print ( $ count ) ; ? >
Count squares with odd side length in Chessboard | Function to return the count of odd length squares possible ; To store the required count ; For all odd values of i ; Add the count of possible squares of length i ; Return the required count ; Driver code
< ? php function count_square ( $ n ) { $ count = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i = $ i + 2 ) { $ k = $ n - $ i + 1 ; $ count += ( $ k * $ k ) ; } return $ count ; } $ N = 8 ; echo count_square ( $ N ) ; ? >
Count of elements whose absolute difference with the sum of all the other elements is greater than k | Function to return the number of anomalies ; To store the count of anomalies ; To store the sum of the array elements ; Find the sum of the array elements ; Count the anomalies ; Driver code
< ? php function countAnomalies ( $ arr , $ n , $ k ) { $ cnt = 0 ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( abs ( $ arr [ $ i ] - ( $ sum - $ arr [ $ i ] ) ) > $ k ) $ cnt ++ ; return $ cnt ; } $ arr = array ( 1 , 3 , 5 ) ; $ n = count ( $ arr ) ; $ k = 1 ; echo countAnomalies ( $ arr , $ n , $ k ) ; ? >
Find the number of integers x in range ( 1 , N ) for which x and x + 1 have same number of divisors | PHP implementation of the approach ; To store number of divisors and Prefix sum of such numbers ; Function to find the number of integers 1 < x < N for which x and x + 1 have the same number of positive divisors ; Count the number of divisors ; Run a loop upto sqrt ( i ) ; If j is divisor of i ; If it is perfect square ; x and x + 1 have same number of positive divisors ; Function call ; Required answer
< ? php $ N = 100005 ; $ d = array_fill ( 0 , $ N , NULL ) ; $ pre = array_fill ( 0 , $ N , NULL ) ; function Positive_Divisors ( ) { global $ N , $ d , $ pre ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) { for ( $ j = 1 ; $ j * $ j <= $ i ; $ j ++ ) { if ( $ i % $ j == 0 ) { if ( $ j * $ j == $ i ) $ d [ $ i ] ++ ; else $ d [ $ i ] += 2 ; } } } $ ans = 0 ; for ( $ i = 2 ; $ i < $ N ; $ i ++ ) { if ( $ d [ $ i ] == $ d [ $ i - 1 ] ) $ ans ++ ; $ pre [ $ i ] = $ ans ; } } Positive_Divisors ( ) ; $ n = 15 ; echo $ pre [ $ n ] ; return 0 ; ? >
Length of the smallest number which is divisible by K and formed by using 1 's only | Function to return length of the resultant number ; If K is a multiple of 2 or 5 ; Generate all possible numbers 1 , 11 , 111 , 111 , ... , K 1 's ; If number is divisible by k then return the length ; Driver code
< ? php function numLen ( $ K ) { if ( $ K % 2 == 0 $ K % 5 == 0 ) return -1 ; $ number = 0 ; $ len = 1 ; for ( $ len = 1 ; $ len <= $ K ; $ len ++ ) { $ number = $ number * 10 + 1 ; if ( ( $ number % $ K == 0 ) ) return $ len ; } return -1 ; } $ K = 7 ; echo numLen ( $ K ) ; ? >
Find if the given number is present in the infinite sequence or not | Function that returns true if the sequence will contain B ; Driver code
< ? php function doesContainB ( $ a , $ b , $ c ) { if ( $ a == $ b ) return true ; if ( ( $ b - $ a ) * $ c > 0 && ( $ b - $ a ) % $ c == 0 ) return true ; return false ; } $ a = 1 ; $ b = 7 ; $ c = 3 ; if ( doesContainB ( $ a , $ b , $ c ) ) echo " Yes " ; else echo " No " ; ? >
Find a permutation of 2 N numbers such that the result of given expression is exactly 2 K | Function to find the required permutation of first 2 * N natural numbers ; Iterate in blocks of 2 ; We need more increments , so print in reverse order ; We have enough increments , so print in same order ; Driver Code
< ? php function printPermutation ( $ n , $ k ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ x = 2 * $ i - 1 ; $ y = 2 * $ i ; if ( $ i <= $ k ) echo $ y . " " ▁ . ▁ $ x ▁ . ▁ " " else echo $ x . " ▁ " . $ y . " ▁ " ; } } $ n = 2 ; $ k = 1 ; printPermutation ( $ n , $ k ) ; ? >
Maximize the sum of products of the degrees between any two vertices of the tree | Function to return the maximum possible sum ; Initialize degree for node u to 2 ; If u is the leaf node or the root node ; Initialize degree for node v to 2 ; If v is the leaf node or the root node ; Update the sum ; Driver code
< ? php function maxSum ( $ N ) { $ ans = 0 ; for ( $ u = 1 ; $ u <= $ N ; $ u ++ ) { for ( $ v = 1 ; $ v <= $ N ; $ v ++ ) { if ( $ u == $ v ) continue ; $ degreeU = 2 ; if ( $ u == 1 $ u == $ N ) $ degreeU = 1 ; $ degreeV = 2 ; if ( $ v == 1 $ v == $ N ) $ degreeV = 1 ; $ ans += ( $ degreeU * $ degreeV ) ; } } return $ ans ; } $ N = 6 ; echo maxSum ( $ N ) ; ? >