codeparrot/xlcost-text-to-code huggingface dataset:
Program to calculate the value of sin ( x ) and cos ( x ) using Expansion | Function for calculation ; Converting degrees to radian ; maps the sum along the series ; holds the actual value of sin ( n ) ; Driver Code
< ? php function cal_cos ( $ n ) { $ accuracy = 0.0001 ; $ n = $ n * ( 3.142 / 180.0 ) ; $ x1 = 1 ; $ cosx = $ x1 ; $ cosval = cos ( $ n ) ; $ i = 1 ; do { $ denominator = 2 * $ i * ( 2 * $ i - 1 ) ; $ x1 = - $ x1 * $ n * $ n / $ denominator ; $ cosx = $ cosx + $ x1 ; $ i = $ i + 1 ; } while ( $ accuracy <= abs ( $ cosval - $ cosx ) ) ; echo round ( $ cosx , 6 ) ; } $ n = 30 ; cal_cos ( $ n ) ; ? >
Find sum of digits in factorial of a number | Function to multiply x with large number stored in vector v . Result is stored in v . ; Calculate res + prev carry ; updation at ith position ; Returns sum of digits in n ! ; One by one multiply i to current vector and update the vector . ; Find sum of digits in vector v [ ] ; Driver code
< ? php function multiply ( & $ v , $ x ) { $ carry = 0 ; $ size = count ( $ v ) ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { $ res = $ carry + $ v [ $ i ] * $ x ; $ v [ $ i ] = $ res % 10 ; $ carry = ( int ) ( $ res / 10 ) ; } while ( $ carry != 0 ) { array_push ( $ v , $ carry % 10 ) ; $ carry = ( int ) ( $ carry / 10 ) ; } } function findSumOfDigits ( $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) multiply ( $ v , $ i ) ; $ sum = 0 ; $ size = count ( $ v ) ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) $ sum += $ v [ $ i ] ; return $ sum ; } $ n = 1000 ; print ( findSumOfDigits ( $ n ) ) ; ? >
Find other two sides of a right angle triangle | Finds two sides of a right angle triangle if it they exist . ; if n is odd ; case of n = 1 handled separately ; case of n = 2 handled separately ; Driver Code
< ? php function printOtherSides ( $ n ) { if ( $ n & 1 ) { if ( $ n == 1 ) echo - 1 ; else { $ b = ( $ n * $ n - 1 ) / 2 ; $ c = ( $ n * $ n + 1 ) / 2 ; echo " b ▁ = ▁ " , $ b , " , ▁ c ▁ = ▁ " , $ c ; } } else { if ( $ n == 2 ) echo - 1 ; else { $ b = $ n * $ n / 4 - 1 ; $ c = $ n * $ n / 4 + 1 ; echo " b ▁ = ▁ " , $ b , " , ▁ c ▁ = ▁ " , $ c ; } } } $ a = 3 ; printOtherSides ( $ a ) ; return 0 ; ? >
Minimum positive integer to divide a number such that the result is an odd | PHP program to make a number odd ; Return 1 if already odd ; Check on dividing with a number when the result becomes odd Return that number ; If n is divided by i and n / i is odd then return i ; Driver code
< ? php function makeOdd ( $ n ) { if ( $ n % 2 != 0 ) return 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) if ( ( $ n % $ i == 0 ) && ( ( $ n / $ i ) % 2 == 1 ) ) return $ i ; } $ n = 36 ; echo makeOdd ( $ n ) ; ? >
XOR of all subarray XORs | Set 2 | Returns XOR of all subarray xors ; if even number of terms are there , all numbers will appear even number of times . So result is 0. ; else initialize result by 0 as ( a xor 0 = a ) ; Driver Code
< ? php function getTotalXorOfSubarrayXors ( $ arr , $ N ) { if ( $ N % 2 == 0 ) return 0 ; $ res = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i += 2 ) $ res ^= $ arr [ $ i ] ; return $ res ; } $ arr = array ( 3 , 5 , 2 , 4 , 6 ) ; $ N = count ( $ arr ) ; echo getTotalXorOfSubarrayXors ( $ arr , $ N ) ; ? >
Fill array with 1 's using minimum iterations of filling neighbors | Returns count of iterations to fill arr [ ] with 1 s . ; Start traversing the array ; Traverse until a 0 is found ; Count contiguous 0 s ; Condition for Case 3 ; Condition to check if Case 1 satisfies : ; If count_zero is even ; If count_zero is odd ; Reset count_zero ; Case 2 ; Update res ; Driver code
< ? php function countIterations ( $ arr , $ n ) { $ oneFound = false ; $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; ) { if ( $ arr [ $ i ] == 1 ) $ oneFound = true ; while ( $ i < $ n && $ arr [ $ i ] == 1 ) $ i ++ ; $ count_zero = 0 ; while ( $ i < $ n && $ arr [ $ i ] == 0 ) { $ count_zero ++ ; $ i ++ ; } if ( $ oneFound == false && $ i == $ n ) return -1 ; $ curr_count ; if ( $ i < $ n && $ oneFound == true ) { if ( $ count_zero & 1 == 0 ) $ curr_count = $ count_zero / 2 ; else $ curr_count = ( $ count_zero + 1 ) / 2 ; $ count_zero = 0 ; } else { $ curr_count = $ count_zero ; $ count_zero = 0 ; } $ res = max ( $ res , $ curr_count ) ; } return $ res ; } $ arr = array ( 0 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo countIterations ( $ arr , $ n ) ; ? >
Express a number as sum of consecutive numbers | Print consecutive numbers from last to first ; Driver Code
< ? php function printConsecutive ( $ last , $ first ) { echo $ first ++ ; for ( $ x = $ first ; $ x <= $ last ; $ x ++ ) echo " + " } function findConsecutive ( $ N ) { for ( $ last = 1 ; $ last < $ N ; $ last ++ ) { for ( $ first = 0 ; $ first < $ last ; $ first ++ ) { if ( 2 * $ N == ( $ last - $ first ) * ( $ last + $ first + 1 ) ) { echo $ N , " ▁ = ▁ " ; printConsecutive ( $ last , $ first + 1 ) ; return ; } } } echo " - 1" ; } $ n = 12 ; findConsecutive ( $ n ) ; ? >
Find n | Return n - th number in series made of 4 and 7 ; create an array of size ( n + 1 ) ; If i is odd ; Driver code
< ? php function printNthElement ( $ n ) { $ arr [ 1 ] = 4 ; $ arr [ 2 ] = 7 ; for ( $ i = 3 ; $ i <= $ n ; $ i ++ ) { if ( $ i % 2 != 0 ) $ arr [ $ i ] = $ arr [ $ i / 2 ] * 10 + 4 ; else $ arr [ $ i ] = $ arr [ ( $ i / 2 ) - 1 ] * 10 + 7 ; } return $ arr [ $ n ] ; } $ n = 6 ; echo ( printNthElement ( $ n ) ) ; ? >
Maximum sum of distinct numbers such that LCM of these numbers is N | Returns maximum sum of numbers with LCM as N ; Initialize result ; Finding a divisor of n and adding it to max_sum ; Driver code
< ? php function maxSumLCM ( $ n ) { $ max_sum = 0 ; for ( $ i = 1 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { $ max_sum += $ i ; if ( $ n / $ i != $ i ) $ max_sum += ( $ n / $ i ) ; } } return $ max_sum ; } $ n = 2 ; echo MaxSumLCM ( $ n ) ; ? >
Square root of a number using log | PHP program to demonstrate finding square root of a number using sqrt ( )
< ? php $ n = 12 ; echo sqrt ( $ n ) ; ? >
Maximum value of an integer for which factorial can be calculated on a machine | PHP program to find maximum value of an integer for which factorial can be calculated on your system ; when fact crosses its size , it gives negative value ; Driver Code
< ? php function findMaxValue ( ) { $ res = 2 ; $ fact = 2 ; $ pos = -1 ; while ( true ) { $ mystring = $ fact ; $ pos = strpos ( $ mystring , ' E ' ) ; if ( $ pos > 0 ) break ; $ res ++ ; $ fact = $ fact * $ res ; } return $ res - 1 ; } echo " Maximum ▁ value ▁ of " . " ▁ integer ▁ " . findMaxValue ( ) ; ? >
Check if LCM of array elements is divisible by a prime number or not | Function to check any number of array is divisible by k or not ; If any array element is divisible by k , then LCM of whole array should also be divisible . ; Driver Code
< ? php function func ( $ a , $ k , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ a [ $ i ] % $ k == 0 ) return true ; return false ; } $ a = array ( 14 , 27 , 38 , 76 , 84 ) ; $ k = 19 ; $ res = func ( $ a , $ k , 5 ) ; if ( $ res ) echo " true " ; else echo " false " ; ? >
Find the closest and smaller tidy number | PHP program to find closest tidy number smaller than the given number ; check whether string violates tidy property ; if string violates tidy property , then decrease the value stored at that index by 1 and replace all the value stored right to that index by 9 ; Driver code ; num will store closest tidy number
< ? php function tidyNum ( $ str , $ len ) { for ( $ i = $ len - 2 ; $ i >= 0 ; $ i -- ) { if ( $ str [ $ i ] > $ str [ $ i + 1 ] ) { $ x = ord ( $ str [ $ i ] ) ; $ x -- ; $ str [ $ i ] = chr ( $ x ) ; for ( $ j = $ i + 1 ; $ j < $ len ; $ j ++ ) $ str [ $ j ] = '9' ; } } return $ str ; } $ str = "11333445538" ; $ len = strlen ( $ str ) ; $ num = tidyNum ( $ str , $ len ) ; echo $ num ; ? >
Count of m digit integers that are divisible by an integer n | Returns count of m digit numbers having n as divisor ; generating largest number of m digit ; generating largest number of m - 1 digit ; returning number of dividend ; Driver code
< ? php function findCount ( $ m , $ n ) { $ num1 = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) $ num1 = ( $ num1 * 10 ) + 9 ; $ num2 = 0 ; for ( $ i = 0 ; $ i < ( $ m - 1 ) ; $ i ++ ) $ num2 = ( $ num2 * 10 ) + 9 ; return ( ( $ num1 / $ n ) - ( $ num2 / $ n ) ) ; } $ m = 2 ; $ n = 6 ; echo findCount ( $ m , $ n ) , " STRNEWLINE " ; ? >
Find the n | function to calculate nth number made of even digits only ; variable to note how many such numbers have been found till now ; bool variable to check if 1 , 3 , 5 , 7 , 9 is there or not ; checking each digit of the number ; If 1 , 3 , 5 , 7 , 9 is found temp is changed to false ; temp is true it means that it does not have 1 , 3 , 5 , 7 , 9 ; If nth such number is found return it ; Driver Code
< ? php function findNthEvenDigitNumber ( $ n ) { $ count = 0 ; for ( $ i = 0 ; ; $ i ++ ) { $ curr = $ i ; $ isCurrEvenDigit = true ; while ( $ curr != 0 ) { if ( $ curr % 10 == 1 $ curr % 10 == 3 $ curr % 10 == 5 $ curr % 10 == 7 $ curr % 10 == 9 ) $ isCurrEvenDigit = false ; $ curr = $ curr / 10 ; } if ( $ isCurrEvenDigit == true ) $ count ++ ; if ( $ count == $ n ) return $ i ; } } echo findNthEvenDigitNumber ( 2 ) , " STRNEWLINE " ; echo findNthEvenDigitNumber ( 10 ) ; ? >
Find the n | function to find nth number made of even digits only ; If n = 1 return 0 ; vector to store the digits when converted into base 5 ; Reduce n to n - 1 to exclude 0 ; Reduce n to base 5 number and store digits ; pushing the digits into vector ; variable to represent the number after converting it to base 5. Since the digits are be in reverse order , we traverse vector from back ; return 2 * result ( to convert digits 0 , 1 , 2 , 3 , 4 to 0 , 2 , 4 , 6 , 8. ; Driver Code
< ? php function findNthEvenDigitNumber ( $ n ) { if ( $ n == 1 ) return 0 ; $ v = array ( ) ; $ n = $ n - 1 ; while ( $ n > 0 ) { array_push ( $ v , $ n % 5 ) ; $ n = ( int ) ( $ n / 5 ) ; } $ result = 0 ; for ( $ i = count ( $ v ) - 1 ; $ i >= 0 ; $ i -- ) { $ result = $ result * 10 ; $ result = $ result + $ v [ $ i ] ; } return 2 * $ result ; } echo findNthEvenDigitNumber ( 2 ) . " STRNEWLINE " ; echo findNthEvenDigitNumber ( 10 ) . " STRNEWLINE " ? >
Check if a large number is divisible by 25 or not | Function to find that number divisible by 25 or not . ; If length of string is single digit then it 's not divisible by 25 ; Driver code
< ? php function isDivisibleBy25 ( $ str ) { $ n = strlen ( $ str ) ; if ( $ n == 1 ) return false ; return ( ( $ str [ $ n - 1 ] - '0' == 0 && $ str [ $ n - 2 ] - '0' == 0 ) || ( ( $ str [ $ n - 2 ] - '0' ) * 10 + ( $ str [ $ n - 1 ] - '0' ) ) % 25 == 0 ) ; } $ str = "76955" ; $ x = isDivisibleBy25 ( $ str ) ? " Yes " : " No ▁ " ; echo ( $ x ) ; ? >
Check a large number is divisible by 16 or not | Function to find that number divisible by 16 or not ; Empty string ; If there is double digit ; If there is triple digit ; If number formed by last four digits is divisible by 16. ; Driver code
< ? php function check ( $ str ) { $ n = strlen ( $ str ) ; if ( $ n == 0 && $ n == 1 ) return false ; if ( $ n == 2 ) return ( ( ( $ str [ $ n - 2 ] - '0' ) * 10 + ( $ str [ $ n - 1 ] - '0' ) ) % 16 == 0 ) ; if ( $ n == 3 ) return ( ( ( $ str [ $ n - 3 ] - '0' ) * 100 + ( $ str [ $ n - 2 ] - '0' ) * 10 + ( $ str [ $ n - 1 ] - '0' ) ) % 16 == 0 ) ; $ last = $ str [ $ n - 1 ] - '0' ; $ second_last = $ str [ $ n - 2 ] - '0' ; $ third_last = $ str [ $ n - 3 ] - '0' ; $ fourth_last = $ str [ $ n - 4 ] - '0' ; return ( ( $ fourth_last * 1000 + $ third_last * 100 + $ second_last * 10 + $ last ) % 16 == 0 ) ; } $ str = "769528" ; $ x = check ( $ str ) ? " Yes " : " No ▁ " ; echo ( $ x ) ; ? >
Find Index of given fibonacci number in constant time | A simple PHP program to find index of given Fibonacci number . ; if Fibonacci number is less than 2 , its index will be same as number ; iterate until generated fibonacci number is less than given fibonacci number ; res keeps track of number of generated fibonacci number ; Driver Code
< ? php function findIndex ( $ n ) { if ( $ n <= 1 ) return $ n ; $ a = 0 ; $ b = 1 ; $ c = 1 ; $ res = 1 ; while ( $ c < $ n ) { $ c = $ a + $ b ; $ res ++ ; $ a = $ b ; $ b = $ c ; } return $ res ; } $ result = findIndex ( 21 ) ; echo ( $ result ) ; ? >
Determine whether a given number is a Hyperperfect Number | function to find the sum of all proper divisors ( excluding 1 and N ) ; Iterate only until sqrt N as we are going to generate pairs to produce divisors ; As divisors occur in pairs , we can take the values i and N / i as long as i divides N ; Function to check whether the given number is prime ; base and corner cases ; Since integers can be represented as some 6 * k + y where y >= 0 , we can eliminate all integers that can be expressed in this form ; start from 5 as this is the next prime number ; Returns true if N is a K - Hyperperfect number Else returns false . ; Condition from the definition of hyperperfect ; Driver Code ; First two statements test against the condition N = 1 + K * ( sum ( proper divisors ) )
< ? php function divisorSum ( $ N , $ K ) { $ sum = 0 ; for ( $ i = 2 ; $ i <= ceil ( sqrt ( $ N ) ) ; $ i ++ ) if ( $ N % $ i == 0 ) $ sum += ( $ i + $ N / $ i ) ; return $ sum ; } function isPrime ( $ n ) { if ( $ n == 1 $ n == 0 ) 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 isHyperPerfect ( $ N , $ K ) { $ sum = divisorSum ( $ N , $ K ) ; if ( ( 1 + $ K * ( $ sum ) ) == $ N ) return true ; else return false ; } $ N1 = 1570153 ; $ K1 = 12 ; $ N2 = 321 ; $ K2 = 3 ; if ( isHyperPerfect ( $ N1 , $ K1 ) ) echo $ N1 , " ▁ is ▁ " , $ K1 , " - HyperPerfect " , " STRNEWLINE " ; else echo $ N1 , " ▁ is ▁ not ▁ " , $ K1 , " - HyperPerfect " , " STRNEWLINE " ; if ( isHyperPerfect ( $ N2 , $ K2 ) ) echo $ N2 , " ▁ is ▁ " , K2 , " - HyperPerfect " , " STRNEWLINE " ; else echo $ N2 , " ▁ is ▁ not ▁ " , $ K2 , " - HyperPerfect " , " STRNEWLINE " ; ? >
Given a number n , find the first k digits of n ^ n | function to calculate first k digits of n ^ n ; take log10 of n ^ n . log10 ( n ^ n ) = n * log10 ( n ) ; We now try to separate the decimal and integral part of the / product . The floor function returns the smallest integer less than or equal to the argument . So in this case , product - floor ( product ) will give us the decimal part of product ; we now exponentiate this back by raising 10 to the power of decimal part ; We now try to find the power of 10 by which we will have to multiply the decimal part to obtain our final answer ; Driver Code
< ? php function firstkdigits ( $ n , $ k ) { $ product = $ n * log10 ( $ n ) ; $ decimal_part = $ product - floor ( $ product ) ; $ decimal_part = pow ( 10 , $ decimal_part ) ; $ digits = pow ( 10 , $ k - 1 ) ; $ i = 0 ; return floor ( $ decimal_part * $ digits ) ; } $ n = 1450 ; $ k = 6 ; echo firstkdigits ( $ n , $ k ) ; ? >
Generate k digit numbers with digits in strictly increasing order | number -- > Current value of number . x -- > Current digit to be considered k -- > Remaining number of digits ; Try all possible greater digits ; Generates all well ordered numbers of length k ; Driver code
< ? php function printWellOrdered ( $ number , $ x , $ k ) { if ( $ k == 0 ) { echo $ number , " " ; return ; } for ( $ i = ( $ x + 1 ) ; $ i < 10 ; $ i ++ ) printWellOrdered ( $ number * 10 + $ i , $ i , $ k - 1 ) ; } function generateWellOrdered ( $ k ) { printWellOrdered ( 0 , 0 , $ k ) ; } $ k = 3 ; generateWellOrdered ( $ k ) ; ? >
Multiply large integers under large modulo | Returns ( a * b ) % mod ; Update a if it is more than or equal to mod ; If b is odd , add a with result ; Here we assume that doing 2 * a doesn 't cause overflow ; $b >>= 1 ; b = b / 2 ; Driver Code
< ? php function moduloMultiplication ( $ a , $ b , $ mod ) { $ a %= $ mod ; while ( $ b ) { if ( $ b & 1 ) $ res = ( $ res + $ a ) % $ mod ; $ a = ( 2 * $ a ) % $ mod ; } return $ res ; } $ a = 10123465234878998 ; $ b = 65746311545646431 ; $ m = 10005412336548794 ; echo moduloMultiplication ( $ a , $ b , $ m ) ; ? >
Number of occurrences of 2 as a digit in numbers from 0 to n | Counts the number of 2 s in a number at d - th digit ; if the digit in spot digit is ; Counts the number of '2' digits between 0 and n ; Convert integer to String to find its length ; Traverse every digit and count for every digit ; Driver Code
< ? php function count2sinRangeAtDigit ( $ number , $ d ) { $ powerOf10 = ( int ) pow ( 10 , $ d ) ; $ nextPowerOf10 = $ powerOf10 * 10 ; $ right = $ number % $ powerOf10 ; $ roundDown = $ number - $ number % $ nextPowerOf10 ; $ roundup = $ roundDown + $ nextPowerOf10 ; $ digit = ( $ number / $ powerOf10 ) % 10 ; if ( $ digit < 2 ) return $ roundDown / 10 ; if ( $ digit == 2 ) return $ roundDown / 10 + $ right + 1 ; return $ roundup / 10 ; } function numberOf2sinRange ( $ number ) { $ s = strval ( $ number ) ; $ len = strlen ( $ s ) ; $ count = 0 ; for ( $ digit = 0 ; $ digit < $ len ; $ digit ++ ) $ count += count2sinRangeAtDigit ( $ number , $ digit ) ; return $ count ; } print ( numberOf2sinRange ( 22 ) . " STRNEWLINE " ) ; print ( numberOf2sinRange ( 100 ) . " STRNEWLINE " ) ; ? >
Program to find Star number | Returns n - th star number ; Driver code
< ? php function findStarNum ( $ n ) { return ( 6 * $ n * ( $ n - 1 ) + 1 ) ; } $ n = 3 ; echo findStarNum ( $ n ) ; ? >
Check if a large number is divisible by 5 or not | Function to find that number divisible by 5 or not . The function assumes that string length is at least one . ; Driver code
< ? php function isDivisibleBy5 ( $ str ) { $ n = strlen ( $ str ) ; return ( ( ( $ str [ $ n - 1 ] - '0' ) == 0 ) || ( ( $ str [ $ n - 1 ] - '0' ) == 5 ) ) ; } $ str = "76955" ; $ x = isDivisibleBy5 ( $ str ) ? " Yes " : " No " ; echo ( $ x ) ; ? >
Tidy Number ( Digits in non | Returns true if num is Tidy ; To store previous digit ( Assigning initial value which is more than any digit ) ; Traverse all digits from right to left and check if any digit is smaller than previous . ; Driver code
< ? php function isTidy ( $ num ) { $ prev = 10 ; while ( $ num ) { $ rem = $ num % 10 ; $ num = ( int ) $ num / 10 ; if ( $ rem > $ prev ) return false ; $ prev = $ rem ; } return true ; } $ num = 1556 ; if ( isTidy ( $ num ) == true ) echo " Yes " ; else echo " No " ; ? >
Nth Square free number | Function to find nth square free number ; To maintain count of square free number ; Loop for square free numbers ; Checking whether square of a number is divisible by any number which is a perfect square ; If number is square free ; If the cnt becomes n , return the number ; Driver Code
< ? php function squareFree ( $ n ) { $ cnt = 0 ; for ( $ i = 1 ; ; $ i ++ ) { $ isSqFree = true ; for ( $ j = 2 ; $ j * $ j <= $ i ; $ j ++ ) { if ( $ i % ( $ j * $ j ) == 0 ) { $ isSqFree = false ; break ; } } if ( $ isSqFree == true ) { $ cnt ++ ; if ( $ cnt == $ n ) return $ i ; } } return 0 ; } $ n = 10 ; echo ( squareFree ( $ n ) ) ; ? >
Find if n can be written as product of k numbers | Prints k factors of n if n can be written as multiple of k numbers . Else prints - 1. ; A vector to store all prime factors of n ; Insert all 2 's in vector ; n must be odd at this point So we skip one element ( i = i + 2 ) ; This is to handle when n > 2 and n is prime ; If size ( P ) < k , k factors are not possible ; printing first k - 1 factors ; calculating and printing product of rest of numbers ; Driver Code
< ? php function kFactors ( $ n , $ k ) { $ P = array ( ) ; while ( $ n % 2 == 0 ) { array_push ( $ P , 2 ) ; $ n = ( int ) ( $ n / 2 ) ; } for ( $ i = 3 ; $ i * $ i <= $ n ; $ i = $ i + 2 ) { while ( $ n % $ i == 0 ) { $ n = ( int ) ( $ n / $ i ) ; array_push ( $ P , $ i ) ; } } if ( $ n > 2 ) array_push ( $ P , $ n ) ; if ( count ( $ P ) < $ k ) { echo " - 1 STRNEWLINE " ; return ; } for ( $ i = 0 ; $ i < $ k - 1 ; $ i ++ ) echo $ P [ $ i ] . " , " $ product = 1 ; for ( $ i = $ k - 1 ; $ i < count ( $ P ) ; $ i ++ ) $ product = $ product * $ P [ $ i ] ; echo $ product ; } $ n = 54 ; $ k = 3 ; kFactors ( $ n , $ k ) ; ? >
Largest number smaller than or equal to n and digits in non | Returns the required number ; loop to recursively check the numbers less than or equal to given number ; Keep traversing digits from right to left . For every digit check if it is smaller than prev_dig ; We found the required number ; Driver Code
< ? php function nondecdigits ( $ n ) { $ x = 0 ; for ( $ x = $ n ; $ x >= 1 ; $ x -- ) { $ no = $ x ; $ prev_dig = 11 ; $ flag = true ; while ( $ no != 0 ) { if ( $ prev_dig < $ no % 10 ) { $ flag = false ; break ; } $ prev_dig = $ no % 10 ; $ no /= 10 ; } if ( $ flag == true ) break ; } return $ x ; } $ n = 200 ; echo nondecdigits ( $ n ) ; ? >
Largest number smaller than or equal to n and digits in non | Prints the largest number smaller than s and digits in non - decreasing order . ; array to store digits of number ; conversion of characters of string int number ; variable holds the value of index after which all digits are set 9 ; Checking the condition if the digit is less than its left digit ; If first digit is 0 no need to print it ; Driver Code
< ? php function nondecdigits ( $ s ) { $ m = strlen ( $ s ) ; $ a [ $ m ] = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) $ a [ $ i ] = $ s [ $ i ] - '0' ; $ level = $ m - 1 ; for ( $ i = $ m - 1 ; $ i > 0 ; $ i -- ) { if ( $ a [ $ i ] < $ a [ $ i - 1 ] ) { $ a [ $ i - 1 ] -- ; $ level = $ i - 1 ; } } if ( $ a [ 0 ] != 0 ) { for ( $ i = 0 ; $ i <= $ level ; $ i ++ ) echo $ a [ $ i ] ; for ( $ i = $ level + 1 ; $ i < $ m ; $ i ++ ) echo "9" ; } else { for ( $ i = 1 ; $ i < $ level ; $ i ++ ) echo $ a [ $ i ] ; for ( $ i = $ level + 1 ; $ i < $ m ; $ i ++ ) echo "9" ; } } $ n = "200" ; nondecdigits ( $ n ) ; ? >
Count Divisors of n in O ( n ^ 1 / 3 ) | function to count the divisors ; If divisors are equal , count only one ; Otherwise count both ; Driver Code
< ? php function countDivisors ( $ n ) { $ cnt = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) $ cnt ++ ; else $ cnt = $ cnt + 2 ; } } return $ cnt ; } echo " Total ▁ distinct ▁ divisors ▁ of ▁ 100 ▁ are ▁ : ▁ " , countDivisors ( 100 ) ; ? >
Check if the door is open or closed | Function to check whether ' n ' has even number of factors or not ; if ' n ' is a perfect square it has odd number of factors ; else ' n ' has even number of factors ; Function to find and print status of each door ; If even number of factors final status is closed ; else odd number of factors final status is open ; Driver Code
< ? php function hasEvenNumberOfFactors ( $ n ) { $ root_n = sqrt ( $ n ) ; if ( ( $ root_n * $ root_n ) == $ n ) return false ; return true ; } function printStatusOfDoors ( $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( hasEvenNumberOfFactors ( $ i ) ) echo " closed " , " ▁ " ; else echo " open " , " ▁ " ; } } $ n = 5 ; printStatusOfDoors ( $ n ) ; ? >
Check if frequency of each digit is less than the digit | Function to validate number ( Check if frequency of adigit is less than thedigit itself or not ) ; If current digit of temp is same as i ; if frequency is greater than digit value , return false ; Driver Code
< ? php function validate ( $ n ) { for ( $ i = 0 ; $ i < 10 ; $ i ++ ) { $ temp = $ n ; $ count = 0 ; while ( $ temp ) { if ( $ temp % 10 == $ i ) $ count ++ ; if ( $ count > $ i ) return -1 ; $ temp /= 10 ; } } return 1 ; } $ n = 1552793 ; $ geek = validate ( $ n ) ? " True " : " False " ; echo ( $ geek ) ; ? >