codeparrot/xlcost-text-to-code huggingface dataset:
Finding n | PHP program to find n - th number with prime digits 2 , 3 and 7 ; remainder for check element position ; if number is 1 st position in tree ; if number is 2 nd position in tree ; if number is 3 rd position in tree ; if number is 4 th position in tree ; Driver code
< ? php function nthprimedigitsnumber ( $ number ) { $ num = " " ; while ( $ number > 0 ) { $ rem = $ number % 4 ; switch ( $ rem ) { case 1 : $ num . = '2' ; break ; case 2 : $ num . = '3' ; break ; case 3 : $ num . = '5' ; break ; case 0 : $ num . = '7' ; break ; } if ( $ number % 4 == 0 ) $ number -- ; $ number = ( int ) ( $ number / 4 ) ; } return strrev ( $ num ) ; } $ number = 21 ; print ( nthprimedigitsnumber ( 10 ) . " STRNEWLINE " ) ; print ( nthprimedigitsnumber ( $ number ) ) ;
Count pairs ( a , b ) whose sum of cubes is N ( a ^ 3 + b ^ 3 = N ) | Function to count the pairs satisfying a ^ 3 + b ^ 3 = N ; Check for each number 1 to cbrt ( N ) ; Store cube of a number ; Subtract the cube from given N ; Check if the difference is also a perfect cube ; If yes , then increment count ; Return count ; Loop to Count no . of pairs satisfying a ^ 3 + b ^ 3 = i for N = 1 to 10
< ? php function countPairs ( $ N ) { $ count = 0 ; for ( $ i = 1 ; $ i <= ( int ) pow ( $ N , 1 / 3 ) ; $ i ++ ) { $ cb = $ i * $ i * $ i ; $ diff = ( $ N - $ cb ) ; $ cbrtDiff = ( int ) pow ( $ diff , 1 / 3 ) ; if ( $ cbrtDiff * $ cbrtDiff * $ cbrtDiff == $ diff ) $ count ++ ; } return $ count ; } for ( $ i = 1 ; $ i <= 10 ; $ i ++ ) echo " For ▁ n ▁ = ▁ " , $ i , " , ▁ " , countPairs ( $ i ) , " ▁ pair ▁ exists STRNEWLINE " ; ? >
Finding ' k ' such that its modulus with each array element is same | Prints all k such that arr [ i ] % k is same for all i ; sort the numbers ; max difference will be the difference between first and last element of sorted array ; Case when all the array elements are same ; Find all divisors of d and store in a vector v [ ] ; check for each v [ i ] if its modulus with each array element is same or not ; checking for each array element if its modulus with k is equal to k or not ; if check is true print v [ i ] ; Driver method
< ? php function printEqualModNumbers ( $ arr , $ n ) { sort ( $ arr ) ; $ d = $ arr [ $ n - 1 ] - $ arr [ 0 ] ; if ( d == 0 ) { print ( " Infinite ▁ solution " ) ; return ; } $ v = array ( ) ; for ( $ i = 1 ; $ i * $ i <= $ d ; $ i ++ ) { if ( $ d % $ i == 0 ) { array_push ( $ v , $ i ) ; if ( $ i != $ d / $ i ) array_push ( $ v , $ d / $ i ) ; } } for ( $ i = 0 ; $ i < count ( $ v ) ; $ i ++ ) { $ temp = $ arr [ 0 ] % $ v [ $ i ] ; $ j = 1 ; for ( ; $ j < $ n ; $ j ++ ) if ( $ arr [ $ j ] % $ v [ $ i ] != $ temp ) break ; if ( $ j == $ n ) print ( $ v [ $ i ] . " ▁ " ) ; } } $ arr = array ( 38 , 6 , 34 ) ; printEqualModNumbers ( $ arr , count ( $ arr ) ) ; ? >
First digit in product of an array of numbers | Returns the first digit of product of elements of arr [ ] ; stores the logarithm of product of elements of arr [ ] ; fractional ( s ) = s - floor ( s ) ; ans = 10 ^ fract_s ; Driver Code
< ? php function FirstDigit ( $ arr , $ n ) { $ S = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ S = $ S + log10 ( $ arr [ $ i ] * 1.0 ) ; $ fract_S = $ S - floor ( $ S ) ; $ ans = pow ( 10 , $ fract_S ) ; return floor ( $ ans ) ; } $ arr = array ( 5 , 8 , 3 , 7 ) ; $ n = sizeof ( $ arr ) ; echo FirstDigit ( $ arr , $ n ) ; ? >
Find count of digits in a number that divide the number | Return the number of digits that divides the number . ; Fetching each digit of the number ; Checking if digit is greater than 0 and can divides n . ; Driver Code
< ? php function countDigit ( $ n ) { $ temp = $ n ; $ count = 0 ; while ( $ temp != 0 ) { $ d = $ temp % 10 ; $ temp /= 10 ; if ( $ d > 0 && $ n % $ d == 0 ) $ count ++ ; } return $ count ; } $ n = 1012 ; echo countDigit ( $ n ) , " STRNEWLINE " ; ? >
Minimum positive integer to divide a number such that the result is an odd | Function to find the value ; Return 1 if already odd ; Check how many times it is divided by 2 ; Driver code
< ? php function makeOdd ( $ n ) { if ( $ n % 2 != 0 ) return 1 ; $ resul = 1 ; while ( $ n % 2 == 0 ) { $ n /= 2 ; $ resul *= 2 ; } return $ resul ; } $ n = 36 ; echo makeOdd ( $ n ) ; ? >
Multiple of x closest to n | Function to calculate the smallest multiple ; Driver Code
< ? php function closestMultiple ( $ n , $ x ) { if ( $ x > $ n ) return $ x ; $ n = $ n + $ x / 2 ; $ n = $ n - ( $ n % $ x ) ; return $ n ; } $ n = 9 ; $ x = 4 ; echo closestMultiple ( $ n , $ x ) ; ? >
Perfect cubes in a range | An efficient solution to print perfect cubes between a and b ; Find cube root of both a and b ; Print cubes between acrt and bcrt ; Driver code
< ? php function printCubes ( $ a , $ b ) { $ acrt = ( int ) pow ( $ a , 1 / 3 ) ; $ bcrt = ( int ) pow ( $ b , 1 / 3 ) ; for ( $ i = $ acrt ; $ i <= $ bcrt ; $ i ++ ) if ( $ i * $ i * $ i >= $ a && $ i * $ i * $ i <= $ b ) echo $ i * $ i * $ i , " ▁ " ; } $ a = 24 ; $ b = 576 ; echo " Perfect ▁ cubes ▁ in ▁ given ▁ range : STRNEWLINE " , printCubes ( $ a , $ b ) ; ? >
Number of occurrences of 2 as a digit in numbers from 0 to n | Counts the number of '2' digits in a single number ; Counts the number of '2' digits between 0 and n ; Initialize result ; Count 2 's in every number  from 2 to n ; Driver Code
< ? php function number0f2s ( $ n ) { $ count = 0 ; while ( $ n > 0 ) { if ( $ n % 10 == 2 ) $ count ++ ; $ n = $ n / 10 ; } return $ count ; } function numberOf2sinRange ( $ n ) { $ count = 0 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ count += number0f2s ( $ i ) ; return $ count ; } echo ( numberOf2sinRange ( 22 ) ) ; echo " STRNEWLINE " ; echo numberOf2sinRange ( 100 ) ; ? >
Minimum toggles to partition a binary array so that it has first 0 s then 1 s | Function to calculate minimum toggling required by using Dynamic programming ; Fill entries in zero [ ] such that zero [ i ] stores count of zeroes to the left of i ( exl ; If zero found update zero [ ] array ; Finding the minimum toggle required from every index ( 0 to n - 1 ) ; Driver Program
< ? php function minToggle ( $ arr , $ n ) { $ zero [ 0 ] = 0 ; $ zero [ $ n + 1 ] = 0 ; for ( $ i = 1 ; $ i <= $ n ; ++ $ i ) { if ( $ arr [ $ i - 1 ] == 0 ) $ zero [ $ i ] = $ zero [ $ i - 1 ] + 1 ; else $ zero [ $ i ] = $ zero [ $ i - 1 ] ; } $ ans = $ n ; for ( $ i = 1 ; $ i <= $ n ; ++ $ i ) $ ans = min ( $ ans , $ i - $ zero [ $ i ] + $ zero [ $ n ] - $ zero [ $ i ] ) ; return $ ans ; } $ arr = array ( 1 , 0 , 1 , 1 , 0 ) ; $ n = sizeof ( $ arr ) ; echo minToggle ( $ arr , $ n ) , " STRNEWLINE " ; ? >
Check if a large number is divisible by 6 or not | Function to find that number divisible by 6 or not ; Return false if number is not divisible by 2. ; Compute sum of digits ; Check if sum of digits is divisible by 3 ; Driver code
< ? php function check ( $ str ) { $ n = strlen ( $ str ) ; if ( ( $ str [ $ n - 1 ] - '0' ) % 2 != 0 ) return false ; $ digitSum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ digitSum += ( $ str [ $ i ] - '0' ) ; return ( $ digitSum % 3 == 0 ) ; } $ str = "1332" ; if ( check ( $ str ) ) echo " Yes " ; else echo " ▁ No ▁ " ; return 0 ; ? >
Find ways an Integer can be expressed as sum of n | PHP program to find number of ways to express a number as sum of n - th powers of numbers . ; Wrapper over checkRecursive ( ) ; Driver Code
< ? php $ res = 0 ; function checkRecursive ( $ num , $ x , $ k , $ n ) { global $ res ; if ( $ x == 0 ) $ res ++ ; $ r = ( int ) floor ( pow ( $ num , 1.0 / $ n ) ) ; for ( $ i = $ k + 1 ; $ i <= $ r ; $ i ++ ) { $ a = $ x - ( int ) pow ( $ i , $ n ) ; if ( $ a >= 0 ) checkRecursive ( $ num , $ x - ( int ) pow ( $ i , $ n ) , $ i , $ n ) ; } return $ res ; } function check ( $ x , $ n ) { return checkRecursive ( $ x , $ x , 0 , $ n ) ; } echo ( check ( 10 , 2 ) ) ; ? >
N 'th palindrome of K digits | Utility function to reverse the number n ; Boolean Function to check for palindromic number ; Function for finding nth palindrome of k digits ; Get the smallest k digit number ; check the number is palindrom or not ; if n 'th palindrome found  break the loop ; Increment number for checking next palindrome ; Driver code
< ? php function reverseNum ( $ n ) { $ rem ; $ rev = 0 ; while ( $ n ) { $ rem = $ n % 10 ; $ rev = ( $ rev * 10 ) + $ rem ; $ n = ( int ) ( $ n / 10 ) ; } return $ rev ; } function isPalindrom ( $ num ) { return $ num == reverseNum ( $ num ) ; } function nthPalindrome ( $ n , $ k ) { $ num = pow ( 10 , $ k - 1 ) ; while ( true ) { if ( isPalindrom ( $ num ) ) -- $ n ; if ( ! $ n ) break ; ++ $ num ; } return $ num ; } $ n = 6 ; $ k = 5 ; echo $ n , " th ▁ palindrome ▁ of ▁ " , $ k , " ▁ digit ▁ = ▁ " , nthPalindrome ( $ n , $ k ) , " STRNEWLINE " ; $ n = 10 ; $ k = 6 ; echo $ n , " th ▁ palindrome ▁ of ▁ " , $ k , " ▁ digit ▁ = ▁ " , nthPalindrome ( $ n , $ k ) , " STRNEWLINE " ; ? >
N 'th palindrome of K digits | PHP program of finding nth palindrome of k digit ; Determine the first half digits ; Print the first half digits of palindrome ; If k is odd , truncate the last digit ; print the last half digits of palindrome ; Driver code
< ? php function nthPalindrome ( $ n , $ k ) { $ temp = ( $ k & 1 ) ? ( int ) ( $ k / 2 ) : ( int ) ( $ k / 2 - 1 ) ; $ palindrome = ( int ) pow ( 10 , $ temp ) ; $ palindrome += $ n - 1 ; print ( $ palindrome ) ; if ( $ k & 1 ) $ palindrome = ( int ) ( $ palindrome / 10 ) ; while ( $ palindrome > 0 ) { print ( $ palindrome % 10 ) ; $ palindrome = ( int ) ( $ palindrome / 10 ) ; } print ( " STRNEWLINE " ) ; } $ n = 6 ; $ k = 5 ; print ( $ n . " th ▁ palindrome ▁ of ▁ $ k ▁ digit ▁ = ▁ " ) ; nthPalindrome ( $ n , $ k ) ; $ n = 10 ; $ k = 6 ; print ( $ n . " th ▁ palindrome ▁ of ▁ $ k ▁ digit ▁ = ▁ " ) ; nthPalindrome ( $ n , $ k ) ; ? >
Summation of GCD of all the pairs up to N | PHP approach of finding sum of GCD of all pairs ; phi [ i ] stores euler totient function for i result [ j ] stores result for value j ; Precomputation of phi [ ] numbers . Refer link for details : https : goo . gl / LUqdtY ; Refer https : goo . gl / LUqdtY ; Precomputes result for all numbers till MAX ; Precompute all phi value ; Iterate throght all the divisors of i . ; Add summation of previous calculated sum ; Function to calculate sum of all the GCD pairs
< ? php $ MAX = 100001 ; $ phi = array_fill ( 0 , $ MAX , 0 ) ; $ result = array_fill ( 0 , $ MAX , 0 ) ; function computeTotient ( ) { global $ MAX , $ phi ; $ phi [ 1 ] = 1 ; for ( $ i = 2 ; $ i < $ MAX ; $ i ++ ) { if ( ! $ phi [ $ i ] ) { $ phi [ $ i ] = $ i - 1 ; for ( $ j = ( $ i << 1 ) ; $ j < $ MAX ; $ j += $ i ) { if ( ! $ phi [ $ j ] ) $ phi [ $ j ] = $ j ; $ phi [ $ j ] = ( $ phi [ $ j ] / $ i ) * ( $ i - 1 ) ; } } } } function sumOfGcdPairs ( ) { global $ MAX , $ phi , $ result ; computeTotient ( ) ; for ( $ i = 1 ; $ i < $ MAX ; ++ $ i ) { for ( $ j = 2 ; $ i * $ j < $ MAX ; ++ $ j ) $ result [ $ i * $ j ] += $ i * $ phi [ $ j ] ; } for ( $ i = 2 ; $ i < $ MAX ; $ i ++ ) $ result [ $ i ] += $ result [ $ i - 1 ] ; } sumOfGcdPairs ( ) ; $ N = 4 ; echo " Summation ▁ of ▁ " . $ N . " ▁ = ▁ " . $ result [ $ N ] . " STRNEWLINE " ; $ N = 12 ; echo " Summation ▁ of ▁ " . $ N . " ▁ = ▁ " . $ result [ $ N ] . " STRNEWLINE " ; $ N = 5000 ; echo " Summation ▁ of ▁ " . $ N . " ▁ = ▁ " . $ result [ $ N ] . " STRNEWLINE " ; ? >
Find coordinates of the triangle given midpoint of each side | PHP program to find coordinate of the triangle given midpoint of each side ; Return after solving the equations and finding the vertices coordinate . ; Finding sum of all three coordinate . ; Solving the equation . ; Finds vertices of a triangles from given middle vertices . ; Find X coordinates of vertices . ; Find Y coordinates of vertices . ; Output the solution . ; Driver code
< ? php $ N = 3 ; function solve ( $ v ) { $ res = array ( ) ; $ all3 = $ v [ 0 ] + $ v [ 1 ] + $ v [ 2 ] ; array_push ( $ res , $ all3 - $ v [ 1 ] * 2 ) ; array_push ( $ res , $ all3 - $ v [ 2 ] * 2 ) ; array_push ( $ res , $ all3 - $ v [ 0 ] * 2 ) ; return $ res ; } function findVertex ( $ xmid , $ ymid ) { $ V1 = solve ( $ xmid ) ; $ V2 = solve ( $ ymid ) ; for ( $ i = 0 ; $ i < 3 ; $ i ++ ) print ( $ V1 [ $ i ] . " ▁ " . $ V2 [ $ i ] . " STRNEWLINE " ) ; } $ xmid = array ( 5 , 4 , 5 ) ; $ ymid = array ( 3 , 4 , 5 ) ; findVertex ( $ xmid , $ ymid ) ? >
N | Return the n - th number in the sorted list of multiples of two numbers . ; Generating first n multiple of a . ; Sorting the sequence . ; Generating and storing first n multiple of b and storing if not present in the sequence . ; If not present in the sequence ; Storing in the sequence . ; Driver Code
< ? php function nthElement ( $ a , $ b , $ n ) { $ seq = array ( ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) array_push ( $ seq , $ a * $ i ) ; sort ( $ seq ) ; for ( $ i = 1 , $ k = $ n ; $ i <= $ n && $ k > 0 ; $ i ++ ) { if ( array_search ( $ b * $ i , $ seq ) == 0 ) { array_push ( $ seq , $ b * $ i ) ; sort ( $ seq ) ; $ k -- ; } } return $ seq [ $ n - 1 ] ; } $ a = 3 ; $ b = 5 ; $ n = 5 ; echo nthElement ( $ a , $ b , $ n ) ; ? >
Count pairs of natural numbers with GCD equal to given number | Return the GCD of two numbers . ; Return the count of pairs having GCD equal to g . ; Setting the value of L , R . ; For each possible pair check if GCD is 1. ; Driver Code
< ? php function gcd ( $ a , $ b ) { return $ b ? gcd ( $ b , $ a % $ b ) : $ a ; } function countGCD ( $ L , $ R , $ g ) { $ L = ( $ L + $ g - 1 ) / $ g ; $ R = $ R / $ g ; $ ans = 0 ; for ( $ i = $ L ; $ i <= $ R ; $ i ++ ) for ( $ j = $ L ; $ j <= $ R ; $ j ++ ) if ( gcd ( $ i , $ j ) == 1 ) $ ans ++ ; return $ ans ; } $ L = 1 ; $ R = 11 ; $ g = 5 ; echo countGCD ( $ L , $ R , $ g ) ; ? >
Last non | Initialize values of last non - zero digit of numbers from 0 to 9 ; Check whether tens ( or second last ) digit is odd or even If n = 375 , So n / 10 = 37 and ( n / 10 ) % 10 = 7 Applying formula for even and odd cases . ; Driver code
< ? php $ dig = array ( 1 , 1 , 2 , 6 , 4 , 2 , 2 , 4 , 2 , 8 ) ; function lastNon0Digit ( $ n ) { global $ dig ; if ( $ n < 10 ) return $ dig [ $ n ] ; if ( ( ( $ n / 10 ) % 10 ) % 2 == 0 ) return ( 6 * lastNon0Digit ( $ n / 5 ) * $ dig [ $ n % 10 ] ) % 10 ; else return ( 4 * lastNon0Digit ( $ n / 5 ) * $ dig [ $ n % 10 ] ) % 10 ; } $ n = 14 ; echo ( lastNon0Digit ( $ n ) ) ; ? >
Find the first natural number whose factorial is divisible by x | GCD function to compute the greatest divisor among a and b ; Returns first number whose factorial divides x . ; Result ; Remove common factors ; We found first i . ; Driver code
< ? php function gcd ( $ a , $ b ) { if ( ( $ a % $ b ) == 0 ) return $ b ; return gcd ( $ b , $ a % $ b ) ; } function firstFactorialDivisibleNumber ( $ x ) { $ i = 1 ; $ new_x = $ x ; for ( $ i = 1 ; $ i < $ x ; $ i ++ ) { $ new_x /= gcd ( $ i , $ new_x ) ; if ( $ new_x == 1 ) break ; } return $ i ; } $ x = 16 ; echo ( firstFactorialDivisibleNumber ( $ x ) ) ; ? >
Find the highest occurring digit in prime numbers in a range | Sieve of Eratosthenes ; Returns maximum occurring digits in primes from l to r . ; Finding the prime number up to R . ; Initialise frequency of all digit to 0. ; For all number between L to R , check if prime or not . If prime , incrementing the frequency of digits present in the prime number . ; $p = $i ; If i is prime ; Finding digit with highest frequency . ; Driver Code
< ? php function sieve ( & $ prime , $ n ) { for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] == false ) for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = true ; } } function maxDigitInPrimes ( $ L , $ R ) { $ prime = array_fill ( 0 , $ R + 1 , false ) ; sieve ( $ prime , $ R ) ; $ freq = array_fill ( 0 , 10 , 0 ) ; for ( $ i = $ L ; $ i <= $ R ; $ i ++ ) { if ( ! $ prime [ $ i ] ) { while ( $ p ) { $ freq [ $ p % 10 ] ++ ; $ p = ( int ) ( $ p / 10 ) ; } } } $ max = $ freq [ 0 ] ; $ ans = 0 ; for ( $ j = 1 ; $ j < 10 ; $ j ++ ) { if ( $ max <= $ freq [ $ j ] ) { $ max = $ freq [ $ j ] ; $ ans = $ j ; } } return $ ans ; } $ L = 1 ; $ R = 20 ; echo maxDigitInPrimes ( $ L , $ R ) ; ? >
Common Divisors of Two Numbers | 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 ; Count divisors of n . ; if ' i ' is factor of n ; check if divisors are equal ; Driver Code
< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function commDiv ( $ a , $ b ) { $ n = gcd ( $ a , $ b ) ; $ result = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) $ result += 1 ; else $ result += 2 ; } } return $ result ; } $ a = 12 ; $ b = 24 ; echo ( commDiv ( $ a , $ b ) ) ; ? >
Count ways to spell a number with repeated digits | Function to calculate all possible spells of a number with repeated digits num -- > string which is favourite number ; final count of total possible spells ; iterate through complete number ; count contiguous frequency of particular digit num [ i ] ; Compute 2 ^ ( count - 1 ) and multiply with result ; Driver Code
< ? php function spellsCount ( $ num ) { $ n = strlen ( $ num ) ; $ result = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ count = 1 ; while ( $ i < $ n - 1 && $ num [ $ i + 1 ] == $ num [ $ i ] ) { $ count ++ ; $ i ++ ; } $ result = $ result * pow ( 2 , $ count - 1 ) ; } return $ result ; } $ num = "11112" ; echo spellsCount ( $ num ) ; ? >
Happy Number | Utility method to return sum of square of digit of n ; method return true if n is Happy number ; initialize slow and fast by n ; move slow number by one iteration ; move fast number by two iteration ; if both number meet at 1 , then return true ; Driver Code
< ? php function numSquareSum ( $ n ) { $ squareSum = 0 ; while ( $ n ) { $ squareSum += ( $ n % 10 ) * ( $ n % 10 ) ; $ n /= 10 ; } return $ squareSum ; } function isHappynumber ( $ n ) { $ slow ; $ fast ; $ slow = $ n ; $ fast = $ n ; do { $ slow = numSquareSum ( $ slow ) ; $ fast = numSquareSum ( numSquareSum ( $ fast ) ) ; } while ( $ slow != $ fast ) ; return ( $ slow == 1 ) ; } $ n = 13 ; if ( isHappynumber ( $ n ) ) echo $ n , " ▁ is ▁ a ▁ Happy ▁ number STRNEWLINE " ; else echo n , " ▁ is ▁ not ▁ a ▁ Happy ▁ number STRNEWLINE " ; ? >
Count Divisors of Factorial | allPrimes [ ] stores all prime numbers less than or equal to n . ; Fills above vector allPrimes [ ] for a given 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 . ; Loop to update prime [ ] ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store primes in the vector allPrimes ; Function to find all result of factorial number ; Initialize result ; find exponents of all primes which divides n and less than n ; Current divisor ; Find the highest power ( stored in exp ) of allPrimes [ i ] that divides n using Legendre 's formula. ; Multiply exponents of all primes less than n ; return total divisors ; Driver Code
< ? php $ allPrimes = array ( ) ; function sieve ( $ n ) { global $ allPrimes ; $ prime = array_fill ( 0 , $ n + 1 , true ) ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = false ; } } for ( $ p = 2 ; $ p <= $ n ; $ p ++ ) if ( $ prime [ $ p ] ) array_push ( $ allPrimes , $ p ) ; } function factorialDivisors ( $ n ) { global $ allPrimes ; $ result = 1 ; for ( $ i = 0 ; $ i < count ( $ allPrimes ) ; $ i ++ ) { $ p = $ allPrimes [ $ i ] ; $ exp = 0 ; while ( $ p <= $ n ) { $ exp = $ exp + ( int ) ( $ n / $ p ) ; $ p = $ p * $ allPrimes [ $ i ] ; } $ result = $ result * ( $ exp + 1 ) ; } return $ result ; } echo factorialDivisors ( 6 ) ; ? >
Non Fibonacci Numbers | Returns n 'th Non- Fibonacci number ; curr is to keep track of current fibonacci number , prev is previous , prevPrev is previous of previous . ; While count of non - fibonacci numbers doesn 't become  negative or zero ; Simple Fibonacci number logic ; ( curr - prev - 1 ) is count of non - Fibonacci numbers between curr and prev . ; n might be negative now . Make sure it becomes positive by removing last added gap . ; Now add the positive n to previous Fibonacci number to find the n 'th non-fibonacci. ; Driver code
< ? php function nonFibonacci ( $ n ) { $ prevPrev = 1 ; $ prev = 2 ; $ curr = 3 ; while ( $ n > 0 ) { $ prevPrev = $ prev ; $ prev = $ curr ; $ curr = $ prevPrev + $ prev ; $ n = $ n - ( $ curr - $ prev - 1 ) ; } $ n = $ n + ( $ curr - $ prev - 1 ) ; return $ prev + $ n ; } echo nonFibonacci ( 5 ) ; ? >
Stein 's Algorithm for finding GCD | Function to implement Stein 's Algorithm ; GCD ( 0 , b ) == b ; GCD ( a , 0 ) == a , GCD ( 0 , 0 ) == 0 ; Finding K , where K is the greatest power of 2 that divides both a and b . ; Dividing a by 2 until a becomes odd ; From here on , ' a ' is always odd . ; If b is even , remove all factor of 2 in b ; Now a and b are both odd . Swap if necessary so a <= b , then set b = b - a ( which is even ) ; restore common factors of 2 ; Driver code
< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; if ( $ b == 0 ) return $ a ; $ k ; for ( $ k = 0 ; ( ( $ a $ b ) & 1 ) == 0 ; ++ $ k ) { $ a >>= 1 ; $ b >>= 1 ; } while ( ( $ a & 1 ) == 0 ) $ a >>= 1 ; do { while ( ( $ b & 1 ) == 0 ) $ b >>= 1 ; if ( $ a > $ b ) swap ( $ a , $ b ) ; $ b = ( $ b - $ a ) ; } while ( $ b != 0 ) ; return $ a << $ k ; } $ a = 34 ; $ b = 17 ; echo " Gcd ▁ of ▁ given ▁ numbers ▁ is ▁ " . gcd ( $ a , $ b ) ; ? >
Print all n | n -- > value of input out -- > output array index -- > index of next digit to be filled in output array evenSum , oddSum -- > sum of even and odd digits so far ; Base case ; If number becomes n - digit ; if absolute difference between sum of even and odd digits is 1 , print the number ; If current index is odd , then add it to odd sum and recurse ; else else add to even sum and recurse ; This is mainly a wrapper over findNDigitNumsUtil . It explicitly handles leading digit and calls findNDigitNumsUtil ( ) for remaining indexes . ; output array to store n - digit numbers ; Initialize number index considered so far ; Initialize even and odd sums ; Explicitly handle first digit and call recursive function findNDigitNumsUtil for remaining indexes . Note that the first digit is considered to be present in even position . ; Driver program
< ? php function findNDigitNumsUtil ( $ n , $ out , $ index , $ evenSum , $ oddSum ) { if ( $ index > $ n ) return ; if ( $ index == $ n ) { if ( abs ( $ evenSum - $ oddSum ) == 1 ) { echo implode ( " " , $ out ) . " " } return ; } if ( $ index & 1 ) { for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { $ out [ $ index ] = $ i + '0' ; findNDigitNumsUtil ( $ n , $ out , $ index + 1 , $ evenSum , $ oddSum + $ i ) ; } } { for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { $ out [ $ index ] = $ i + '0' ; findNDigitNumsUtil ( $ n , $ out , $ index + 1 , $ evenSum + $ i , $ oddSum ) ; } } } function findNDigitNums ( $ n ) { $ out = array_fill ( 0 , $ n + 1 , " " ) ; $ index = 0 ; $ evenSum = 0 ; $ oddSum = 0 ; for ( $ i = 1 ; $ i <= 9 ; $ i ++ ) { $ out [ $ index ] = $ i + '0' ; findNDigitNumsUtil ( $ n , $ out , $ index + 1 , $ evenSum + $ i , $ oddSum ) ; } } $ n = 3 ; findNDigitNums ( $ n ) ; ? >