codeparrot/xlcost-text-to-code huggingface dataset:
Chinese Remainder Theorem | Set 1 ( Introduction ) | k is size of num [ ] and rem [ ] . Returns the smallest number x such that : x % num [ 0 ] = rem [ 0 ] , x % num [ 1 ] = rem [ 1 ] , ... ... ... ... ... ... x % num [ k - 2 ] = rem [ k - 1 ] Assumption : Numbers in num [ ] are pairwise coprime ( gcd forevery pair is 1 ) ; As per the Chinise remainder theorem , this loop will always break . ; Check if remainder of x % num [ j ] is rem [ j ] or not ( for all j from 0 to k - 1 ) ; If all remainders matched , we found x ; Else try next number ; Driver Code
< ? php function findMinX ( $ num , $ rem , $ k ) { while ( true ) { $ j ; for ( $ j = 0 ; $ j < $ k ; $ j ++ ) if ( $ x % $ num [ $ j ] != $ rem [ $ j ] ) break ; if ( $ j == $ k ) return $ x ; $ x ++ ; } return $ x ; } $ num = array ( 3 , 4 , 5 ) ; $ rem = array ( 2 , 3 , 1 ) ; $ k = sizeof ( $ num ) ; echo " x ▁ is ▁ " , findMinX ( $ num , $ rem , $ k ) ; ? >
Compute nCr % p | Set 2 ( Lucas Theorem ) | Returns nCr % p . In this Lucas Theorem based program , this function is only called for n < p and r < p . ; The array C is going to store last row of pascal triangle at the end . And last entry of last row is nCr ; Top row of Pascal Triangle ; One by constructs remaining rows of Pascal Triangle from top to bottom ; Fill entries of current row using previous row values ; Lucas Theorem based function that returns nCr % p . This function works like decimal to binary conversion recursive function . First we compute last digits of n and r in base p , then recur for remaining digits ; Base case ; Compute last digits of n and r in base p ; Compute result for last digits computed above , and for remaining digits . Multiply the two results and compute the result of multiplication in modulo p . ; $r / $p , $p ) * Last digits of n and r nCrModpDP ( $ni , $ri , $p ) ) % $p ; Remaining digits ; Driver Code
< ? php function nCrModpDP ( $ n , $ r , $ p ) { $ C = array_fill ( 0 , $ n + 1 , false ) ; $ C [ 0 ] = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = min ( $ i , $ r ) ; $ j > 0 ; $ j -- ) $ C [ $ j ] = ( $ C [ $ j ] + $ C [ $ j - 1 ] ) % $ p ; } return $ C [ $ r ] ; } function nCrModpLucas ( $ n , $ r , $ p ) { if ( $ r == 0 ) return 1 ; $ ni = $ n % $ p ; $ ri = $ r % $ p ; return ( nCrModpLucas ( $ n / $ p , } $ n = 1000 ; $ r = 900 ; $ p = 13 ; echo " Value ▁ of ▁ nCr ▁ % ▁ p ▁ is ▁ " , nCrModpLucas ( $ n , $ r , $ p ) ; ? >
Print all Good numbers in given range | To check whether n is a good number and doesn ' t ▁ contain ▁ digit ▁ ' d '. ; Get last digit and initialize sum from right side ; If last digit is d , return ; Traverse remaining digits ; Current digit ; If digit is d or digit is less than or equal to sum of digits on right side ; Update sum and n ; Print Good numbers in range [ L , R ] ; Traverse all numbers in given range ; If current numbers is good , print it . ; Driver Code ; Print good numbers in [ L , R ]
< ? php function isValid ( $ n , $ d ) { $ digit = $ n % 10 ; $ sum = $ digit ; if ( $ digit == $ d ) return false ; $ n = ( int ) ( $ n / 10 ) ; while ( $ n ) { $ digit = $ n % 10 ; if ( $ digit == $ d $ digit <= $ sum ) return false ; else { $ sum += $ digit ; $ n = ( int ) ( $ n / 10 ) ; } } return 1 ; } function printGoodNumbers ( $ L , $ R , $ d ) { for ( $ i = $ L ; $ i <= $ R ; $ i ++ ) { if ( isValid ( $ i , $ d ) ) echo $ i . " " ; } } $ L = 410 ; $ R = 520 ; $ d = 3 ; printGoodNumbers ( $ L , $ R , $ d ) ; ? >
Zeckendorf 's Theorem (Non | Returns the greatest Fibonacci Number smaller than or equal to n . ; Corner cases ; Find the greatest Fibonacci Number smaller than n . ; Prints Fibonacci Representation of n using greedy algorithm ; Find the greates Fibonacci Number smaller than or equal to n ; Print the found fibonacci number ; Reduce n ; Driver Code
< ? php function nearestSmallerEqFib ( $ n ) { if ( $ n == 0 $ n == 1 ) return $ n ; $ f1 = 0 ; $ f2 = 1 ; $ f3 = 1 ; while ( $ f3 <= $ n ) { $ f1 = $ f2 ; $ f2 = $ f3 ; $ f3 = $ f1 + $ f2 ; } return $ f2 ; } function printFibRepresntation ( $ n ) { while ( $ n > 0 ) { $ f = nearestSmallerEqFib ( $ n ) ; echo $ f , " " ; $ n = $ n - $ f ; } } $ n = 30 ; echo " Non - neighbouring ▁ Fibonacci ▁ Representation ▁ of ▁ " , $ n , " ▁ is ▁ STRNEWLINE " ; printFibRepresntation ( $ n ) ; ? >
Count number of ways to divide a number in 4 parts | A Dynamic Programming based solution to count number of ways to represent n as sum of four numbers ; " parts " is number of parts left , n is the value left " nextPart " is starting point from where we start trying for next part . ; Base cases ; If this subproblem is already solved ; Count number of ways for remaining number n - i remaining parts " parts - 1" , and for all part varying from ' nextPart ' to ' n ' ; Store computed answer in table and return result ; This function mainly initializes dp table and calls countWaysUtil ( ) ; Driver Code
< ? php $ dp = array_fill ( 0 , 501 , array_fill ( 0 , 501 , array_fill ( 0 , 5 , -1 ) ) ) ; function countWaysUtil ( $ n , $ parts , $ nextPart ) { global $ dp ; if ( $ parts == 0 && $ n == 0 ) return 1 ; if ( $ n <= 0 $ parts <= 0 ) return 0 ; if ( $ dp [ $ n ] [ $ nextPart ] [ $ parts ] != -1 ) return $ dp [ $ n ] [ $ nextPart ] [ $ parts ] ; for ( $ i = $ nextPart ; $ i <= $ n ; $ i ++ ) $ ans += countWaysUtil ( $ n - $ i , $ parts - 1 , $ i ) ; return ( $ dp [ $ n ] [ $ nextPart ] [ $ parts ] = $ ans ) ; } function countWays ( $ n ) { return countWaysUtil ( $ n , 4 , 1 ) ; } $ n = 8 ; echo countWays ( $ n ) ; ? >
Find the smallest twins in given range | PHP program to find the smallest twin in given range ; Create a boolean array " prime [ 0 . . high ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Look for the smallest twin ; If p is not marked , then it is a prime ; Update all multiples of p ; Now print the smallest twin in range ; Driver Code
< ? php function printTwins ( $ low , $ high ) { $ prime = array_fill ( 0 , $ high + 1 , true ) ; $ twin = false ; $ prime [ 0 ] = $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p <= floor ( sqrt ( $ high ) ) + 1 ; $ p ++ ) { if ( $ prime [ $ p ] ) { for ( $ i = $ p * 2 ; $ i <= $ high ; $ i += $ p ) $ prime [ $ i ] = false ; } } for ( $ i = $ low ; $ i <= $ high ; $ i ++ ) { if ( $ prime [ $ i ] && $ prime [ $ i + 2 ] ) { print ( " Smallest ▁ twins ▁ in ▁ given ▁ range : ▁ ( $ i , ▁ " . ( $ i + 2 ) . " ) " ) ; $ twin = true ; break ; } } if ( $ twin == false ) print ( " No ▁ such ▁ pair ▁ exists STRNEWLINE " ) ; } printTwins ( 10 , 100 ) ; ? >
Find Next Sparse Number | PHP program to find next sparse number ; Find binary representation of x and store it in bin [ ] . bin [ 0 ] contains least significant bit ( LSB ) , next bit is in bin [ 1 ] , and so on . ; There my be extra bit in result , so add one extra bit ; The position till which all bits are finalized ; Start from second bit ( next to LSB ) ; If current bit and its previous bit are 1 , but next bit is not 1. ; Make the next bit 1 ; Make all bits before current bit as 0 to make sure that we get the smallest next number ; Store position of the bit set so that this bit and bits before it are not changed next time . ; Find decimal equivalent of modified bin [ ] ; Driver Code
< ? php function nextSparse ( $ x ) { $ bin = array ( ) ; while ( $ x != 0 ) { array_push ( $ bin , $ x & 1 ) ; $ x >>= 1 ; } array_push ( $ bin , 0 ) ; $ last_final = 0 ; for ( $ i = 1 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ bin [ $ i ] == 1 && $ bin [ $ i - 1 ] == 1 && $ bin [ $ i + 1 ] != 1 ) { $ bin [ $ i + 1 ] = 1 ; for ( $ j = $ i ; $ j >= $ last_final ; $ j -- ) $ bin [ $ j ] = 0 ; $ last_final = $ i + 1 ; } } $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ ans += $ bin [ $ i ] * ( 1 << $ i ) ; return $ ans ; } $ x = 38 ; echo " Next ▁ Sparse ▁ Number ▁ is ▁ " . nextSparse ( $ x ) ; ? >
Sum of bit differences among all pairs | PHP program to compute sum of pairwise bit differences ; Initialize result ; traverse over all bits ; count number of elements with i 'th bit set ; Add " count ▁ * ▁ ( n ▁ - ▁ count ) ▁ * ▁ 2" to the answer ; Driver Code
< ? php function sumBitDifferences ( $ arr , $ n ) { $ ans = 0 ; for ( $ i = 0 ; $ i < 32 ; $ i ++ ) { $ count = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( ( $ arr [ $ j ] & ( 1 << $ i ) ) ) $ count ++ ; $ ans += ( $ count * ( $ n - $ count ) * 2 ) ; } return $ ans ; } $ arr = array ( 1 , 3 , 5 ) ; $ n = sizeof ( $ arr ) ; echo sumBitDifferences ( $ arr , $ n ) , " STRNEWLINE " ; ? >
Print all non | function to print array arr [ 0. . n - 1 ] ; Recursive Function to generate all non - increasing sequences with sum x arr [ ] -- > Elements of current sequence curr_sum -- > Current Sum curr_idx -- > Current index in arr [ ] ; If current sum is equal to x , then we found a sequence ; Try placing all numbers from 1 to x - curr_sum at current index ; The placed number must also be smaller than previously placed numbers and it may be equal to the previous stored value , i . e . , arr [ curr_idx - 1 ] if there exists a previous number ; Place number at curr_idx ; Recur ; Try next number ; A wrapper over generateUtil ( ) ; Array to store sequences on by one ; Driver Code
< ? php function printArr ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] , " ▁ " ; echo " ▁ STRNEWLINE " ; } function generateUtil ( $ x , $ arr , $ curr_sum , $ curr_idx ) { if ( $ curr_sum == $ x ) { printArr ( $ arr , $ curr_idx ) ; return ; } $ num = 1 ; while ( $ num <= $ x - $ curr_sum and ( $ curr_idx == 0 or $ num <= $ arr [ $ curr_idx - 1 ] ) ) { $ arr [ $ curr_idx ] = $ num ; generateUtil ( $ x , $ arr , $ curr_sum + $ num , $ curr_idx + 1 ) ; $ num ++ ; } } function generate ( $ x ) { $ arr = array ( ) ; generateUtil ( $ x , $ arr , 0 , 0 ) ; } $ x = 5 ; generate ( $ x ) ; ? >
Perfect Number | Returns true if n is perfect ; To store sum of divisors ; Find all divisors and add them ; If sum of divisors is equal to n , then n is a perfect number ; Driver Code
< ? php function isPerfect ( $ n ) { $ sum = 1 ; for ( $ i = 2 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ i * $ i != $ n ) $ sum = $ sum + $ i + ( int ) ( $ n / $ i ) ; else $ sum = $ sum + $ i ; } } if ( $ sum == $ n && $ n != 1 ) return true ; return false ; } echo " Below ▁ are ▁ all ▁ perfect ▁ numbers ▁ till ▁ 10000 STRNEWLINE " ; for ( $ n = 2 ; $ n < 10000 ; $ n ++ ) if ( isPerfect ( $ n ) ) echo " $ n ▁ is ▁ a ▁ perfect ▁ number STRNEWLINE " ; ? >
Check if a given number can be represented in given a no . of digits in any base | Returns true if ' num ' can be represented usind ' dig ' digits in ' base ' ; Base case ; If there are more than 1 digits left and number is more than base , then remove last digit by doing num / base , reduce the number of digits and recur ; return true of num can be represented in ' dig ' digits in any base from 2 to 32 ; Check for all bases one by one ; Driver Code
< ? php function checkUtil ( $ num , $ dig , $ base ) { if ( $ dig == 1 && $ num < $ base ) return true ; if ( $ dig > 1 && $ num >= $ base ) return checkUtil ( $ num / $ base , -- $ dig , $ base ) ; return false ; } function check ( $ num , $ dig ) { for ( $ base = 2 ; $ base <= 32 ; $ base ++ ) if ( checkUtil ( $ num , $ dig , $ base ) ) return true ; return false ; } $ num = 8 ; $ dig = 3 ; if ( check ( $ num , $ dig ) == true ) echo " Yes " ; else echo " No " ; ? >
How to compute mod of a big number ? | Function to compute num ( mod a ) ; Initialize result ; One by one process all digits of ' num ' ; Driver Code
< ? php function mod ( $ num , $ a ) { $ res = 0 ; for ( $ i = 0 ; $ i < $ r = strlen ( $ num ) ; $ i ++ ) $ res = ( $ res * 10 + $ num [ $ i ] - '0' ) % $ a ; return $ res ; } $ num = "12316767678678" ; echo mod ( $ num , 10 ) ; ? >
Modular multiplicative inverse | A naive method to find modulor multiplicative inverse of ' a ' under modulo ' m ' ; Driver Code ; Function call
< ? php function modInverse ( $ a , $ m ) { for ( $ x = 1 ; $ x < $ m ; $ x ++ ) if ( ( ( $ a % $ m ) * ( $ x % $ m ) ) % $ m == 1 ) return $ x ; } $ a = 3 ; $ m = 11 ; echo modInverse ( $ a , $ m ) ; a . . . >
Modular multiplicative inverse | Returns modulo inverse of a with respect to m using extended Euclid Algorithm Assumption : a and m are coprimes , i . e . , gcd ( a , m ) = 1 ; q is quotient ; m is remainder now , process same as Euclid 's algo ; Update y and x ; Make x positive ; Driver Code ; Function call
< ? php function modInverse ( $ a , $ m ) { $ m0 = $ m ; $ y = 0 ; $ x = 1 ; if ( $ m == 1 ) return 0 ; while ( $ a > 1 ) { $ q = ( int ) ( $ a / $ m ) ; $ t = $ m ; $ m = $ a % $ m ; $ a = $ t ; $ t = $ y ; $ y = $ x - $ q * $ y ; $ x = $ t ; } if ( $ x < 0 ) $ x += $ m0 ; return $ x ; } $ a = 3 ; $ m = 11 ; echo " Modular ▁ multiplicative ▁ inverse ▁ is STRNEWLINE " , modInverse ( $ a , $ m ) ; a . . . >
Euler 's Totient Function | Function to return gcd of a and b ; A simple method to evaluate Euler Totient Function ; Driver Code
< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function phi ( $ n ) { $ result = 1 ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) if ( gcd ( $ i , $ n ) == 1 ) $ result ++ ; return $ result ; } for ( $ n = 1 ; $ n <= 10 ; $ n ++ ) echo " phi ( " . $ n . " ) ▁ = " . phi ( $ n ) . " STRNEWLINE " ; I >
Euler 's Totient Function | PHP program to calculate Euler ' s ▁ Totient ▁ Function ▁ using ▁ Euler ' s product formula ; Initialize result as n ; Consider all prime factors of n and for every prime factor p , multiply result with ( 1 - 1 / p ) ; Check if p is a prime factor . ; If yes , then update n and result ; If n has a prime factor greater than sqrt ( n ) ( There can be at - most one such prime factor ) ; Driver Code
< ? php function phi ( $ n ) { $ result = $ n ; for ( $ p = 2 ; $ p * $ p <= $ n ; ++ $ p ) { if ( $ n % $ p == 0 ) { while ( $ n % $ p == 0 ) $ n /= $ p ; $ result *= ( 1.0 - ( 1.0 / $ p ) ) ; } } if ( $ n > 1 ) $ result *= ( 1.0 - ( 1.0 / $ n ) ) ; return intval ( $ result ) ; } for ( $ n = 1 ; $ n <= 10 ; $ n ++ ) echo " phi ( " . $ n . " ) ▁ = " . phi ( $ n ) . " STRNEWLINE " ; I >
Efficient Program to Compute Sum of Series 1 / 1 ! + 1 / 2 ! + 1 / 3 ! + 1 / 4 ! + . . + 1 / n ! | Utility function to find ; A Simple Function to return value of 1 / 1 ! + 1 / 2 ! + . . + 1 / n ! ; Driver Code
< ? php function factorial ( $ n ) { $ res = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res *= $ i ; return $ res ; } function sum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum += 1.0 / factorial ( $ i ) ; return $ sum ; } $ n = 5 ; echo ( sum ( $ n ) ) ; ? >
Find the number of valid parentheses expressions of given length | Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Function to find possible ways to put balanced parenthesis in an expression of length n ; If n is odd , not possible to create any valid parentheses ; Otherwise return n / 2 'th  Catalan Number ; Driver Code
< ? php function binomialCoeff ( $ n , $ k ) { $ res = 1 ; if ( $ k > $ n - $ k ) $ k = $ n - $ k ; for ( $ i = 0 ; $ i < $ k ; ++ $ i ) { $ res *= ( $ n - $ i ) ; $ res /= ( $ i + 1 ) ; } return $ res ; } function catalan ( $ n ) { $ c = binomialCoeff ( 2 * $ n , $ n ) ; return $ c / ( $ n + 1 ) ; } function findWays ( $ n ) { if ( $ n & 1 ) return 0 ; return catalan ( $ n / 2 ) ; } $ n = 6 ; echo " Total ▁ possible ▁ expressions ▁ of ▁ length ▁ " , $ n , " ▁ is ▁ " , findWays ( 6 ) ; ? >
Program to evaluate simple expressions | A utility function to check if a given character is operand ; utility function to find value of and operand ; This function evaluates simple expressions . It returns - 1 if the given expression is invalid . ; Base Case : Given expression is empty ; The first character must be an operand , find its value ; Traverse the remaining characters in pairs ; The next character must be an operator , and next to next an operand ; If next to next character is not an operand ; Update result according to the operator ; If not a valid operator ; Driver Code
< ? php function isOperand ( $ c ) { return ( $ c >= '0' && $ c <= '9' ) ; } function value ( $ c ) { return ( $ c - '0' ) ; } function evaluate ( $ exp ) { $ len = strlen ( $ exp ) ; if ( $ len == 0 ) return -1 ; $ res = ( int ) ( value ( $ exp [ 0 ] ) ) ; for ( $ i = 1 ; $ i < $ len ; $ i += 2 ) { $ opr = $ exp [ $ i ] ; $ opd = $ exp [ $ i + 1 ] ; if ( ! isOperand ( $ opd ) ) return -1 ; if ( $ opr == ' + ' ) $ res += value ( $ opd ) ; else if ( $ opr == ' - ' ) $ res -= ( int ) ( value ( $ opd ) ) ; else if ( $ opr == ' * ' ) $ res *= ( int ) ( value ( $ opd ) ) ; else if ( $ opr == ' / ' ) $ res /= ( int ) ( value ( $ opd ) ) ; else return -1 ; } return $ res ; } $ expr1 = "1 + 2*5 + 3" ; $ res = evaluate ( $ expr1 ) ; ( $ res == -1 ) ? print ( $ expr1 . " ▁ is ▁ Invalid STRNEWLINE " ) : print ( " Value ▁ of ▁ " . $ expr1 . " ▁ is ▁ " . $ res . " STRNEWLINE " ) ; $ expr2 = "1 + 2*3" ; $ res = evaluate ( $ expr2 ) ; ( $ res == -1 ) ? print ( $ expr2 . " ▁ is ▁ Invalid STRNEWLINE " ) : print ( " Value ▁ of ▁ " . $ expr2 . " ▁ is ▁ " . $ res . " STRNEWLINE " ) ; $ expr3 = "4-2 + 6*3" ; $ res = evaluate ( $ expr3 ) ; ( $ res == -1 ) ? print ( $ expr3 . " ▁ is ▁ Invalid STRNEWLINE " ) : print ( " Value ▁ of ▁ " . $ expr3 . " ▁ is ▁ " . $ res . " STRNEWLINE " ) ; $ expr4 = "1 + + 2" ; $ res = evaluate ( $ expr4 ) ; ( $ res == -1 ) ? print ( $ expr4 . " ▁ is ▁ Invalid STRNEWLINE " ) : print ( " Value ▁ of ▁ " . $ expr4 . " ▁ is ▁ " . $ res . " STRNEWLINE " ) ; ? >
Program to print first n Fibonacci Numbers | Set 1 | Function to print first n Fibonacci Numbers ; Driver Code
< ? php function printFibonacciNumbers ( $ n ) { $ f1 = 0 ; $ f2 = 1 ; $ i ; if ( $ n < 1 ) return ; echo ( $ f1 ) ; echo ( " ▁ " ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { echo ( $ f2 ) ; echo ( " ▁ " ) ; $ next = $ f1 + $ f2 ; $ f1 = $ f2 ; $ f2 = $ next ; } } printFibonacciNumbers ( 7 ) ; ? >
Program to find LCM of two numbers | Recursive function to return gcd of a and b ; Function to return LCM of two numbers ; Driver Code
< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function lcm ( $ a , $ b ) { return ( $ a / gcd ( $ a , $ b ) ) * $ b ; } $ a = 15 ; $ b = 20 ; echo " LCM ▁ of ▁ " , $ a , " ▁ and ▁ " , $ b , " ▁ is ▁ " , lcm ( $ a , $ b ) ; ? >
Program to convert a given number to words | A function that prints given number in words ; Get number of digits in given number ; Base cases ; The first string is not used , it is to make array indexing simple ; The first string is not used , it is to make array indexing simple ; The first two string are not used , they are to make array indexing simple ; Used for debugging purpose only ; For single digit number ; Iterate while num is not ' \0' ; Code path for first 2 digits ; here len can be 3 or 4 ; Code path for last 2 digits ; Need to explicitly handle 10 - 19. Sum of the two digits is used as index of " two _ digits " array of strings ; Need to explicitely handle 20 ; Rest of the two digit numbers i . e . , 21 to 99 ; Driver Code
< ? php function convert_to_words ( $ num ) { $ len = strlen ( $ num ) ; if ( $ len == 0 ) { echo " empty ▁ string STRNEWLINE " ; return ; } if ( $ len > 4 ) { echo " Length ▁ more ▁ than ▁ 4 ▁ " . " is ▁ not ▁ supported STRNEWLINE " ; return ; } $ single_digits = array ( " zero " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " ) ; $ two_digits = array ( " " , " ten " , " eleven " , " twelve " , " thirteen " , " fourteen " , " fifteen " , " sixteen " , " seventeen " , " eighteen " , " nineteen " ) ; $ tens_multiple = array ( " " , " " , " twenty " , " thirty " , " forty " , " fifty " , " sixty " , " seventy " , " eighty " , " ninety " ) ; $ tens_power = array ( " hundred " , " thousand " ) ; echo $ num . " : ▁ " ; if ( $ len == 1 ) { echo $ single_digits [ $ num [ 0 ] - '0' ] . " ▁ STRNEWLINE " ; return ; } $ x = 0 ; while ( $ x < strlen ( $ num ) ) { if ( $ len >= 3 ) { if ( $ num [ $ x ] - '0' != 0 ) { echo $ single_digits [ $ num [ $ x ] - '0' ] . " " ; echo $ tens_power [ $ len - 3 ] . " " ; } -- $ len ; } else { if ( $ num [ $ x ] - '0' == 1 ) { $ sum = $ num [ $ x ] - '0' + $ num [ $ x ] - '0' ; echo $ two_digits [ $ sum ] . " ▁ STRNEWLINE " ; return ; } else if ( $ num [ $ x ] - '0' == 2 && $ num [ $ x + 1 ] - '0' == 0 ) { echo " twenty STRNEWLINE " ; return ; } else { $ i = $ num [ $ x ] - '0' ; if ( $ i > 0 ) echo $ tens_multiple [ $ i ] . " ▁ " ; else echo " " ; ++ $ x ; if ( $ num [ $ x ] - '0' != 0 ) echo $ single_digits [ $ num [ $ x ] - '0' ] . " ▁ STRNEWLINE " ; } } ++ $ x ; } } convert_to_words ( "9923" ) ; convert_to_words ( "523" ) ; convert_to_words ( "89" ) ; convert_to_words ( "8" ) ; ? >
Check if a number is multiple of 5 without using / and % operators | Assuming that integre takes 4 bytes , there can be maximum 10 digits in a integer ; Check the last character of string ; Driver Code
< ? php $ MAX = 11 ; function isMultipleof5 ( $ n ) { global $ MAX ; $ str = ( string ) $ n ; $ len = strlen ( $ str ) ; if ( $ str [ $ len - 1 ] == '5' $ str [ $ len - 1 ] == '0' ) return true ; return false ; } $ n = 19 ; if ( isMultipleof5 ( $ n ) == true ) echo " $ n ▁ is ▁ multiple ▁ of ▁ 5" ; else echo " $ n ▁ is ▁ not ▁ a ▁ multiple ▁ of ▁ 5" ; ? >
Sum of Bitwise OR of all pairs in a given array | Returns value of " arr [ 0 ] ▁ | ▁ arr [ 1 ] ▁ + ▁ arr [ 0 ] ▁ | ▁ arr [ 2 ] ▁ + ▁ . . . ▁ arr [ i ] ▁ | ▁ arr [ j ] ▁ + ▁ . . . . . ▁ arr [ n - 2 ] ▁ | ▁ arr [ n - 1 ] " ; Initialize result ; Consider all pairs ( arr [ i ] , arr [ j ) such that i < j ; Driver Code
< ? php function pairORSum ( $ arr , $ n ) { $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) $ ans += $ arr [ $ i ] | $ arr [ $ j ] ; return $ ans ; } $ arr = array ( 1 , 2 , 3 , 4 ) ; $ n = sizeof ( $ arr ) ; echo pairORSum ( $ arr , $ n ) , " STRNEWLINE " ; ? >
Multiply a number by 15 without using * and / operators | Function to return ( 15 * N ) without using ' * ' or ' / ' operator ; prod = 16 * n ; ( ( 16 * n ) - n ) = 15 * n ; Driver code
< ? php function multiplyByFifteen ( $ n ) { $ prod = ( $ n << 4 ) ; $ prod = $ prod - $ n ; return $ prod ; } $ n = 7 ; echo multiplyByFifteen ( $ n ) ; ? >
Minimum value among AND of elements of every subset of an array | PHP program for the above approach ; Find AND of whole array ; Print the answer ; Driver code
< ? php function minAND ( $ arr , $ n ) { $ s = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ s = $ s & $ arr [ $ i ] ; } print ( $ s . " " ) ; } $ arr = array ( 1 , 2 , 3 ) ; $ n = count ( $ arr ) ; minAND ( $ arr , $ n ) ; ? >
Modify a binary array to Bitwise AND of all elements as 1 | Function to check if it is possible or not ; Driver code
< ? php function check ( $ a , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ a [ $ i ] ) return true ; return false ; } $ a = array ( 0 , 1 , 0 , 1 ) ; $ n = sizeof ( $ a ) ; if ( check ( $ a , $ n ) ) echo " YES STRNEWLINE " ; else echo " NO STRNEWLINE " ; ? >
Find array using different XORs of elements in groups of size 4 | Utility function to print the contents of the array ; Function to find the required array ; Print the array ; Driver code
< ? php function printArray ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo ( $ arr [ $ i ] . " ▁ " ) ; } function findArray ( $ q , $ n ) { $ ans ; $ arr = array ( $ n ) ; for ( $ k = 0 , $ j = 0 ; $ j < $ n / 4 ; $ j ++ ) { $ ans = $ q [ $ k ] ^ $ q [ $ k + 3 ] ; $ arr [ $ k + 1 ] = $ q [ $ k + 1 ] ^ $ ans ; $ arr [ $ k + 2 ] = $ q [ $ k + 2 ] ^ $ ans ; $ arr [ $ k ] = $ q [ $ k ] ^ ( ( $ arr [ $ k + 1 ] ) ^ ( $ arr [ $ k + 2 ] ) ) ; $ arr [ $ k + 3 ] = $ q [ $ k + 3 ] ^ ( $ arr [ $ k + 1 ] ^ $ arr [ $ k + 2 ] ) ; $ k += 4 ; } printArray ( $ arr , $ n ) ; } { $ q = array ( 4 , 1 , 7 , 0 ) ; $ n = sizeof ( $ q ) ; findArray ( $ q , $ n ) ; }