codeparrot/xlcost-text-to-code huggingface dataset:
An interesting solution to get all prime numbers smaller than n | PHP program to prints prime numbers smaller than n ; Compute factorials and apply Wilson 's  theorem. ; Driver code
< ? php function primesInRange ( $ n ) { $ fact = 1 ; for ( $ k = 2 ; $ k < $ n ; $ k ++ ) { $ fact = $ fact * ( $ k - 1 ) ; if ( ( $ fact + 1 ) % $ k == 0 ) print ( $ k . " STRNEWLINE " ) ; } } $ n = 15 ; primesInRange ( $ n ) ; ? >
A product array puzzle | Set 2 ( O ( 1 ) Space ) | epsilon value to maintain precision ; to hold sum of all values ; output product for each index antilog to find original product value ; Driver code
< ? php $ EPS = 1e-9 ; function productPuzzle ( $ a , $ n ) { global $ EPS ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += ( double ) log10 ( $ a [ $ i ] ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo ( int ) ( $ EPS + pow ( ( double ) 10.00 , $ sum - log10 ( $ a [ $ i ] ) ) ) . " ▁ " ; } $ a = array ( 10 , 3 , 5 , 6 , 2 ) ; $ n = count ( $ a ) ; echo " The ▁ product ▁ array ▁ is : ▁ STRNEWLINE " ; productPuzzle ( $ a , $ n ) ; ? >
Change all even bits in a number to 0 | Returns modified number with all even bits 0. ; To store sum of bits at even positions . ; To store bits to shift ; One by one put all even bits to end ; If current last bit is set , add it to ans ; Next shift position ; Driver code
< ? php function changeEvenBits ( $ n ) { $ to_subtract = 0 ; $ m = 0 ; for ( $ x = $ n ; $ x ; $ x >>= 2 ) { if ( $ x & 1 ) $ to_subtract += ( 1 << $ m ) ; $ m += 2 ; } return $ n - $ to_subtract ; } $ n = 30 ; echo changeEvenBits ( $ n ) ; ? >
Find the number closest to n and divisible by m | function to find the number closest to n and divisible by m ; find the quotient ; 1 st possible closest number ; 2 nd possible closest number ; if true , then n1 is the required closest number ; else n2 is the required closest number ; Driver Code
< ? php function closestNumber ( $ n , $ m ) { $ q = ( int ) ( $ n / $ m ) ; $ n1 = $ m * $ q ; $ n2 = ( $ n * $ m ) > 0 ? ( $ m * ( $ q + 1 ) ) : ( $ m * ( $ q - 1 ) ) ; if ( abs ( $ n - $ n1 ) < abs ( $ n - $ n2 ) ) return $ n1 ; return $ n2 ; } $ n = 13 ; $ m = 4 ; echo closestNumber ( $ n , $ m ) , " STRNEWLINE " ; $ n = -15 ; $ m = 6 ; echo closestNumber ( $ n , $ m ) , " STRNEWLINE " ; $ n = 0 ; $ m = 8 ; echo closestNumber ( $ n , $ m ) , " STRNEWLINE " ; $ n = 18 ; $ m = -7 ; echo closestNumber ( $ n , $ m ) , " STRNEWLINE " ; ? >
Check if a given number is Pronic | function to check Pronic Number ; Checking Pronic Number by multiplying consecutive numbers ; Printing Pronic Numbers upto 200
< ? php function checkPronic ( $ x ) { for ( $ i = 0 ; $ i <= ( sqrt ( $ x ) ) ; $ i ++ ) if ( $ x == $ i * ( $ i + 1 ) ) return true ; return false ; } for ( $ i = 0 ; $ i <= 200 ; $ i ++ ) if ( checkPronic ( $ i ) ) echo $ i , " ▁ " ; ? >
Find minimum sum of factors of number | To find minimum sum of product of number ; Find factors of number and add to the sum ; Return sum of numbers having minimum product ; Driver Code
< ? php function findMinSum ( $ num ) { $ sum = 0 ; for ( $ i = 2 ; $ i * $ i <= $ num ; $ i ++ ) { while ( $ num % $ i == 0 ) { $ sum += $ i ; $ num /= $ i ; } } $ sum += $ num ; return $ sum ; } $ num = 12 ; echo ( findMinSum ( $ num ) ) ; ? >
Minimum number with digits as 4 and 7 only and given sum | Prints minimum number with given digit sum and only allowed digits as 4 and 7. ; Cases where all remaining digits are 4 or 7 ( Remaining sum of digits should be multiple of 4 or 7 ) ; If both 4 s and 7 s are there in digit sum , we subtract a 4. ; Driver code
< ? php function findMin ( $ sum ) { $ a = 0 ; $ b = 0 ; while ( $ sum > 0 ) { if ( $ sum % 7 == 0 ) { $ b ++ ; $ sum -= 7 ; } else if ( $ sum % 4 == 0 ) { $ a ++ ; $ sum -= 4 ; } else { $ a ++ ; $ sum -= 4 ; } } if ( $ sum < 0 ) { echo ( " - 1n " ) ; return ; } for ( $ i = 0 ; $ i < $ a ; $ i ++ ) echo ( "4" ) ; for ( $ i = 0 ; $ i < $ b ; $ i ++ ) echo ( "7" ) ; echo ( " STRNEWLINE " ) ; } findMin ( 15 ) ; ? >
Add minimum number to an array so that the sum becomes even | Function to find out minimum number ; Count odd number of terms in array ; Driver code
< ? php function minNum ( $ arr , $ n ) { $ odd = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] % 2 ) $ odd += 1 ; return ( $ odd % 2 ) ? 1 : 2 ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) ; $ n = count ( $ arr ) ; echo minNum ( $ arr , $ n ) ; ? >
Find maximum number that can be formed using digits of a given number | Function to print the maximum number ; hashed array to store count of digits ; Converting given number to string ; Updating the count array ; result is to store the final number ; Traversing the count array to calculate the maximum number ; return the result ; Driver program to test above function
< ? php function printMaxNum ( $ num ) { $ count = array_fill ( 0 , 10 , NULL ) ; $ str = ( string ) $ num ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) $ count [ ord ( $ str [ $ i ] ) - ord ( '0' ) ] ++ ; $ result = 0 ; $ multiplier = 1 ; for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { while ( $ count [ $ i ] > 0 ) { $ result = $ result + ( $ i * $ multiplier ) ; $ count [ $ i ] -- ; $ multiplier = $ multiplier * 10 ; } } return $ result ; } $ num = 38293367 ; echo printMaxNum ( $ num ) ; ? >
Form the largest number using at most one swap operation | function to form the largest number by applying atmost one swap operation ; for the rightmost digit , there will be no greater right digit ; index of the greatest right digit till the current index from the right direction ; traverse the array from second right element up to the left element ; if ' num [ i ] ' is less than the greatest digit encountered so far ; else ; there is no greater right digit for ' num [ i ] ' ; update ' right ' index ; traverse the ' rightMax [ ] ' array from left to right ; if for the current digit , greater right digit exists then swap it with its greater right digit and break ; performing the required swap operation ; required largest number ; Driver Code
< ? php function largestNumber ( $ num ) { $ n = strlen ( $ num ) ; $ rightMax [ $ n ] = array ( 0 ) ; $ right ; $ rightMax [ $ n - 1 ] = -1 ; $ right = $ n - 1 ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { if ( $ num [ $ i ] < $ num [ $ right ] ) $ rightMax [ $ i ] = $ right ; else { $ rightMax [ $ i ] = -1 ; $ right = $ i ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ rightMax [ $ i ] != -1 ) { list ( $ num [ $ i ] , $ num [ $ rightMax [ $ i ] ] ) = array ( $ num [ $ rightMax [ $ i ] ] , $ num [ $ i ] ) ; break ; } } return $ num ; } $ num = "8725634" ; echo " Largest ▁ number : ▁ " , largestNumber ( $ num ) ; ? >
Binomial Random Variables | function to calculate nCr i . e . , number of ways to choose r out of n objects ; Since nCr is same as nC ( n - r ) To decrease number of iterations ; function to calculate binomial r . v . probability ; Driver code
< ? php function nCr ( $ n , $ r ) { if ( $ r > $ n / 2 ) $ r = $ n - $ r ; $ answer = 1 ; for ( $ i = 1 ; $ i <= $ r ; $ i ++ ) { $ answer *= ( $ n - $ r + $ i ) ; $ answer /= $ i ; } return $ answer ; } function binomialProbability ( $ n , $ k , $ p ) { return nCr ( $ n , $ k ) * pow ( $ p , $ k ) * pow ( 1 - $ p , $ n - $ k ) ; } $ n = 10 ; $ k = 5 ; $ p = 1.0 / 3 ; $ probability = binomialProbability ( $ n , $ k , $ p ) ; echo " Probability ▁ of ▁ " . $ k ; echo " ▁ heads ▁ when ▁ a ▁ coin ▁ is ▁ tossed ▁ " . $ n ; echo " ▁ times ▁ where ▁ probability ▁ of ▁ " . " each ▁ head ▁ is ▁ " . $ p ; echo " is = " ? >
Find pair with maximum GCD in an array | Function to find GCD of pair with max GCD in the array ; Computing highest element ; Array to store the count of divisors i . e . Potential GCDs ; Iterating over every element ; Calculating all the divisors ; Divisor found ; Incrementing count for divisor ; Element / divisor is also a divisor Checking if both divisors are not same ; Checking the highest potential GCD ; If this divisor can divide at least 2 numbers , it is a GCD of at least 1 pair ; Array in which pair with max GCD is to be found ; Size of array
< ? php function findMaxGCD ( $ arr , $ n ) { $ high = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ high = max ( $ high , $ arr [ $ i ] ) ; $ divisors = array_fill ( 0 , $ high + 1 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 1 ; $ j <= ( int ) ( sqrt ( $ arr [ $ i ] ) ) ; $ j ++ ) { if ( $ arr [ $ i ] % $ j == 0 ) { $ divisors [ $ j ] ++ ; if ( $ j != ( int ) ( $ arr [ $ i ] / $ j ) ) $ divisors [ ( int ) ( $ arr [ $ i ] / $ j ) ] ++ ; } } } for ( $ i = $ high ; $ i >= 1 ; $ i -- ) if ( $ divisors [ $ i ] > 1 ) return $ i ; } $ arr = array ( 1 , 2 , 4 , 8 , 8 , 12 ) ; $ n = sizeof ( $ arr ) ; echo findMaxGCD ( $ arr , $ n ) ; ? >
Find pair with maximum GCD in an array | function to find GCD of pair with max GCD in the array ; Calculating MAX in array ; Maintaining count array ; Variable to store the multiples of a number ; Iterating from MAX to 1 GCD is always between MAX and 1. The first GCD found will be the highest as we are decrementing the potential GCD ; Iterating from current potential GCD till it is less than MAX ; A multiple found ; Incrementing potential GCD by itself To check i , 2 i , 3 i ... . ; 2 multiples found , max GCD found ; Array in which pair with max GCD is to be found ; Size of array
< ? php function findMaxGCD ( $ arr , $ n ) { $ high = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ high = max ( $ high , $ arr [ $ i ] ) ; $ count = array_fill ( 0 , $ high + 1 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ count [ $ arr [ $ i ] ] ++ ; $ counter = 0 ; for ( $ i = $ high ; $ i >= 1 ; $ i -- ) { $ j = $ i ; $ counter = 0 ; while ( $ j <= $ high ) { if ( $ count [ $ j ] >= 2 ) return $ j ; else if ( $ count [ $ j ] == 1 ) $ counter ++ ; $ j += $ i ; if ( $ counter == 2 ) return $ i ; } } } $ arr = array ( 1 , 2 , 4 , 8 , 8 , 12 ) ; $ n = count ( $ arr ) ; print ( findMaxGCD ( $ arr , $ n ) ) ; ? >
Evil Number | returns number of 1 s from the binary number ; counting 1 s ; Check if number is evil or not ; converting n to binary form ; calculating remainder ; storing the remainders in binary form as a number ; Calling the count_one function to count and return number of 1 s in bin ; Driver Code
< ? php function count_one ( $ n ) { $ c_one = 0 ; while ( $ n != 0 ) { $ rem = $ n % 10 ; if ( $ rem == 1 ) $ c_one = $ c_one + 1 ; $ n = $ n / 10 ; } return $ c_one ; } function checkEvil ( $ n ) { $ i = 0 ; $ bin = 0 ; $ n_one = 0 ; while ( $ n != 0 ) { $ r = $ n % 2 ; $ bin = $ bin + $ r * ( pow ( 10 , $ i ) ) ; $ n = $ n / 2 ; } $ n_one = count_one ( $ bin ) ; if ( $ n_one % 2 == 0 ) return 1 ; else return 0 ; } $ i ; $ check ; $ num ; $ num = 32 ; $ check = checkEvil ( $ num ) ; if ( $ check == 1 ) echo $ num , " ▁ is ▁ Evil ▁ Number STRNEWLINE " ; else echo $ num , " ▁ is ▁ Odious ▁ Number STRNEWLINE " ; ? >
Count number of pairs ( A <= N , B <= N ) such that gcd ( A , B ) is B | returns number of valid pairs ; initialize k ; loop till imin <= n ; Initialize result ; max i with given k floor ( n / k ) ; adding k * ( number of i with floor ( n / i ) = k to ans ; set imin = imax + 1 and k = n / imin ; Driver Code
< ? php function CountPairs ( $ n ) { $ k = $ n ; $ imin = 1 ; $ ans = 0 ; while ( $ imin <= $ n ) { $ imax = $ n / $ k ; $ ans += $ k * ( $ imax - $ imin + 1 ) ; $ imin = $ imax + 1 ; $ k = ( int ) ( $ n / $ imin ) ; } return $ ans ; } echo ( CountPairs ( 1 ) . " STRNEWLINE " ) ; echo ( CountPairs ( 2 ) . " STRNEWLINE " ) ; echo ( CountPairs ( 3 ) . " STRNEWLINE " ) ; ? >
Find the last digit of given series | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; $x = $x % $p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now $y = $y >> 1 ; y = y / 2 ; Returns modulo inverse of a with respect to m using extended Euclid Algorithm ; q is quotient ; m is remainder now , process same as Euclid 's algo  ; Make x1 positive ; Function to calculate the above expression ; Initialize the result ; Compute first part of expression ; Compute second part of expression i . e . , ( ( 4 ^ ( n + 1 ) - 1 ) / 3 ) mod 10 Since division of 3 in modulo can ' t ▁ ▁ ▁ be ▁ performed ▁ directly ▁ therefore ▁ we ▁ ▁ ▁ need ▁ to ▁ find ▁ it ' s modulo Inverse ; Driver code
< ? php function powermod ( $ x , $ y , $ p ) { while ( $ y > 0 ) { if ( ( $ y & 1 ) > 0 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function modInverse ( $ a , $ m ) { $ m0 = $ m ; $ x0 = 0 ; $ x1 = 1 ; if ( $ m == 1 ) return 0 ; while ( $ a > 1 ) { $ q = ( int ) ( $ a / $ m ) ; $ t = $ m ; $ m = $ a % $ m ; $ a = $ t ; $ t = $ x0 ; $ x0 = $ x1 - $ q * $ x0 ; $ x1 = $ t ; } if ( $ x1 < 0 ) $ x1 += $ m0 ; return $ x1 ; } function evaluteExpression ( $ n ) { $ firstsum = 0 ; $ mod = 10 ; for ( $ i = 2 , $ j = 0 ; ( 1 << $ j ) <= $ n ; $ i *= $ i , ++ $ j ) $ firstsum = ( $ firstsum + $ i ) % $ mod ; $ secondsum = ( powermod ( 4 , $ n + 1 , $ mod ) - 1 ) * modInverse ( 3 , $ mod ) ; return ( $ firstsum * $ secondsum ) % $ mod ; } $ n = 3 ; echo evaluteExpression ( $ n ) . " STRNEWLINE " ; $ n = 10 ; echo evaluteExpression ( $ n ) ; ? >
Finding power of prime number p in n ! | Returns power of p in n ! ; initializing answer ; initializing ; loop until temp <= n ; add number of numbers divisible by n ; each time multiply temp by p ; Driver function
< ? php function PowerOFPINnfactorial ( $ n , $ p ) { $ ans = 0 ; $ temp = $ p ; while ( $ temp <= $ n ) { $ ans += $ n / $ temp ; $ temp = $ temp * $ p ; } return $ ans ; } echo PowerOFPINnfactorial ( 4 , 2 ) . " STRNEWLINE " ; ? >
Program for Binary To Decimal Conversion | Function to convert binary to decimal ; Initializing base value to 1 , i . e 2 ^ 0 ; Driver Code
< ? php function binaryToDecimal ( $ n ) { $ num = $ n ; $ dec_value = 0 ; $ base = 1 ; $ temp = $ num ; while ( $ temp ) { $ last_digit = $ temp % 10 ; $ temp = $ temp / 10 ; $ dec_value += $ last_digit * $ base ; $ base = $ base * 2 ; } return $ dec_value ; } $ num = 10101001 ; echo binaryToDecimal ( $ num ) , " STRNEWLINE " ; ? >
Calculating Factorials using Stirling Approximation | Function for calculating factorial ; value of natural e ; evaluating factorial using stirling approximation ; Driver Code
< ? php function stirlingFactorial ( $ n ) { if ( $ n == 1 ) return 1 ; $ z ; $ e = 2.71 ; $ z = sqrt ( 2 * 3.14 * $ n ) * pow ( ( $ n / $ e ) , $ n ) ; return floor ( $ z ) ; } echo stirlingFactorial ( 1 ) , " STRNEWLINE " ; echo stirlingFactorial ( 2 ) , " STRNEWLINE " ; echo stirlingFactorial ( 3 ) , " STRNEWLINE " ; echo stirlingFactorial ( 4 ) , " STRNEWLINE " ; echo stirlingFactorial ( 5 ) , " STRNEWLINE " ; echo stirlingFactorial ( 6 ) , " ▁ STRNEWLINE " ; echo stirlingFactorial ( 7 ) , " ▁ STRNEWLINE " ; ? >
Count pairs with Odd XOR | A function will return number of pair whose XOR is odd ; To store count of odd and even numbers ; Increase even if number is even otherwise increase odd ; Return number of pairs ; Driver Code
< ? php function countXorPair ( $ arr , $ n ) { $ odd = 0 ; $ even = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] % 2 == 0 ) $ even ++ ; else $ odd ++ ; } return $ odd * $ even ; } $ arr = array ( 1 , 2 , 3 ) ; $ n = sizeof ( $ arr ) ; echo countXorPair ( $ arr , $ n ) ; ? >
Lychrel Number Implementation | Max Iterations ; Function to check whether number is Lychrel Number ; Function to check whether the number is Palindrome ; Function to reverse the number ; Driver Code
< ? php $ MAX_ITERATIONS = 20 ; function isLychrel ( $ number ) { global $ MAX_ITERATIONS ; for ( $ i = 0 ; $ i < $ MAX_ITERATIONS ; $ i ++ ) { $ number = $ number + reverse ( $ number ) ; if ( isPalindrome ( $ number ) ) return " false " ; } return " true " ; } function isPalindrome ( $ number ) { return $ number == reverse ( $ number ) ; } function reverse ( $ number ) { $ reverse = 0 ; while ( $ number > 0 ) { $ remainder = $ number % 10 ; $ reverse = ( $ reverse * 10 ) + $ remainder ; $ number = ( int ) ( $ number / 10 ) ; } return $ reverse ; } $ number = 295 ; echo $ number . " ▁ is ▁ lychrel ? ▁ " . isLychrel ( $ number ) ; ? >
Rectangular ( or Pronic ) Numbers | Returns n - th rectangular number ; Driver Code
< ? php function findRectNum ( $ n ) { return $ n * ( $ n + 1 ) ; } $ n = 6 ; echo findRectNum ( $ n ) ; ? >
Program for Muller Method | PHP Program to find root of a function , f ( x ) ; Function to calculate f ( x ) ; Taking f ( x ) = x ^ 3 + 2 x ^ 2 + 10 x - 20 ; Calculating various constants required to calculate x3 ; Taking the root which is closer to x2 ; checking for resemblance of x3 with x2 till two decimal places ; Driver main function
< ? php $ MAX_ITERATIONS = 10000 ; function f ( $ x ) { return 1 * pow ( $ x , 3 ) + 2 * $ x * $ x + 10 * $ x - 20 ; } function Muller ( $ a , $ b , $ c ) { global $ MAX_ITERATIONS ; $ res = 0 ; for ( $ i = 0 ; ; ++ $ i ) { $ f1 = f ( $ a ) ; $ f2 = f ( $ b ) ; $ f3 = f ( $ c ) ; $ d1 = $ f1 - $ f3 ; $ d2 = $ f2 - $ f3 ; $ h1 = $ a - $ c ; $ h2 = $ b - $ c ; $ a0 = $ f3 ; $ a1 = ( ( ( $ d2 * pow ( $ h1 , 2 ) ) - ( $ d1 * pow ( $ h2 , 2 ) ) ) / ( ( $ h1 * $ h2 ) * ( $ h1 - $ h2 ) ) ) ; $ a2 = ( ( ( $ d1 * $ h2 ) - ( $ d2 * $ h1 ) ) / ( ( $ h1 * $ h2 ) * ( $ h1 - $ h2 ) ) ) ; $ x = ( ( -2 * $ a0 ) / ( $ a1 + abs ( sqrt ( $ a1 * $ a1 - 4 * $ a0 * $ a2 ) ) ) ) ; $ y = ( ( -2 * $ a0 ) / ( $ a1 - abs ( sqrt ( $ a1 * $ a1 - 4 * $ a0 * $ a2 ) ) ) ) ; if ( $ x >= $ y ) $ res = $ x + $ c ; else $ res = $ y + $ c ; $ m = $ res * 100 ; $ n = $ c * 100 ; $ m = floor ( $ m ) ; $ n = floor ( $ n ) ; if ( $ m == $ n ) break ; $ a = $ b ; $ b = $ c ; $ c = $ res ; if ( $ i > $ MAX_ITERATIONS ) { echo " Root ▁ cannot ▁ be ▁ found ▁ using ▁ Muller ' s ▁ method " ; break ; } } if ( $ i <= $ MAX_ITERATIONS ) echo " The ▁ value ▁ of ▁ the ▁ root ▁ is ▁ " . round ( $ res , 4 ) ; } $ a = 0 ; $ b = 1 ; $ c = 2 ; Muller ( $ a , $ b , $ c ) ; ? >
Optimized Euler Totient Function for Multiple Evaluations | PHP program to efficiently compute values of euler totient function for multiple inputs . ; Stores prime numbers upto MAX - 1 values ; Finds prime numbers upto MAX - 1 and stores them in vector p ; if prime [ i ] is not marked before ; fill vector for every newly encountered prime ; run this loop till square root of MAX , mark the index i * j as not prime ; function to find totient of n ; this loop runs sqrt ( n / ln ( n ) ) times ; subtract multiples of p [ i ] from r ; Remove all occurrences of p [ i ] in n ; when n has prime factor greater than sqrt ( n ) ; preprocess all prime numbers upto 10 ^ 5
< ? php $ MAX = 100001 ; $ p = array ( ) ; function sieve ( ) { global $ MAX , $ p ; $ isPrime = array_fill ( 0 , $ MAX + 1 , 0 ) ; for ( $ i = 2 ; $ i <= $ MAX ; $ i ++ ) { if ( $ isPrime [ $ i ] == 0 ) { array_push ( $ p , $ i ) ; for ( $ j = 2 ; $ i * $ j <= $ MAX ; $ j ++ ) $ isPrime [ $ i * $ j ] = 1 ; } } } function phi ( $ n ) { global $ p ; $ res = $ n ; for ( $ i = 0 ; $ p [ $ i ] * $ p [ $ i ] <= $ n ; $ i ++ ) { if ( $ n % $ p [ $ i ] == 0 ) { $ res -= ( int ) ( $ res / $ p [ $ i ] ) ; while ( $ n % $ p [ $ i ] == 0 ) $ n = ( int ) ( $ n / $ p [ $ i ] ) ; } } if ( $ n > 1 ) $ res -= ( int ) ( $ res / $ n ) ; return $ res ; } sieve ( ) ; print ( phi ( 11 ) . " STRNEWLINE " ) ; print ( phi ( 21 ) . " STRNEWLINE " ) ; print ( phi ( 31 ) . " STRNEWLINE " ) ; print ( phi ( 41 ) . " STRNEWLINE " ) ; print ( phi ( 51 ) . " STRNEWLINE " ) ; print ( phi ( 61 ) . " STRNEWLINE " ) ; print ( phi ( 91 ) . " STRNEWLINE " ) ; print ( phi ( 101 ) . " STRNEWLINE " ) ; ? >
Finding n | Prints n - th number where each digit is a prime number ; Finding the length of n - th number ; Count of numbers with len - 1 digits ; Count of numbers with i digits ; if i is the length of such number then n < 4 * ( 4 ^ ( i - 1 ) - 1 ) / 3 and n >= 4 * ( 4 ^ i - 1 ) / 3 if a valid i is found break the loop ; check for i + 1 ; Finding ith digit at ith place ; j = 1 means 2 j = 2 means ... j = 4 means 7 ; if prev_count + 4 ^ ( len - i ) is less than n , increase prev_count by 4 ^ ( x - i ) ; else print the ith digit and break ; Driver Code
< ? php function nthprimedigitsnumber ( $ n ) { $ len = 1 ; $ prev_count = 0 ; while ( true ) { $ curr_count = $ prev_count + pow ( 4 , $ len ) ; if ( $ prev_count < $ n && $ curr_count >= $ n ) break ; $ len ++ ; $ prev_count = $ curr_count ; } for ( $ i = 1 ; $ i <= $ len ; $ i ++ ) { for ( $ j = 1 ; $ j <= 4 ; $ j ++ ) { if ( $ prev_count + pow ( 4 , $ len - $ i ) < $ n ) $ prev_count += pow ( 4 , $ len - $ i ) ; else { if ( $ j == 1 ) echo "2" ; else if ( $ j == 2 ) echo "3" ; else if ( $ j == 3 ) echo "5" ; else if ( $ j == 4 ) echo "7" ; break ; } } } echo " STRNEWLINE " ; } nthprimedigitsnumber ( 10 ) ; nthprimedigitsnumber ( 21 ) ; ? >
Cassiniâ €™ s Identity | Returns ( - 1 ) ^ n ; Driver Code
< ? php function cassini ( $ n ) { return ( $ n & 1 ) ? -1 : 1 ; } $ n = 5 ; echo ( cassini ( $ n ) ) ; ? >
Find if a number is divisible by every number in a list | Function which check is a number divided with every element in list or not ; Driver Code
< ? php function findNoIsDivisibleOrNot ( $ a , $ n , $ l ) { for ( $ i = 0 ; $ i < $ l ; $ i ++ ) { if ( $ a [ $ i ] % $ n != 0 ) return false ; } return true ; } $ a = array ( 14 , 12 , 4 , 18 ) ; $ n = 2 ; $ l = sizeof ( $ a ) ; if ( findNoIsDivisibleOrNot ( $ a , $ n , $ l ) ) echo " Yes " ; else echo " No " ; ? >
Find a range of composite numbers of given length | method to find factorial of given number ; to print range of length n having all composite integers ; Driver Code
< ? php function factorial ( $ n ) { if ( $ n == 0 ) return 1 ; return $ n * factorial ( $ n - 1 ) ; } function printRange ( $ n ) { $ a = factorial ( $ n + 2 ) + 2 ; $ b = $ a + $ n - 1 ; echo " [ " , $ a , " , ▁ " , $ b , " ] " ; return 0 ; } $ n = 3 ; printRange ( $ n ) ; ? >
Find minimum value to assign all array elements so that array product becomes greater | PHP program to find minimum value that can be assigned to all elements so that product becomes greater than current product . ; sort the array to apply Binary search ; using log property add every logarithmic value of element to val $val = 0 ; where ld is long double ; set left and right extremities to find min value ; multiplying n to mid , to find the correct min value ; Driver code
< ? php function findMinValue ( $ arr , $ n ) { sort ( $ arr ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ val += ( log ( $ arr [ $ i ] ) ) ; $ left = $ arr [ 0 ] ; $ right = $ arr [ $ n - 1 ] + 1 ; $ ans = 0 ; while ( $ left <= $ right ) { $ mid = ( int ) ( $ left + $ right ) / 2 ; $ temp = $ n * ( log ( $ mid ) ) ; if ( $ val < $ temp ) { $ ans = $ mid ; $ right = $ mid - 1 ; } else $ left = $ mid + 1 ; } return ( floor ( $ ans ) ) ; } $ arr = array ( 4 , 2 , 1 , 10 , 6 ) ; $ n = sizeof ( $ arr ) ; echo findMinValue ( $ arr , $ n ) , " STRNEWLINE " ; ? >
Find the sum of all the terms in the n | function to find the required sum ; sum = n * ( 2 * n ^ 2 + 1 ) ; Driver Code
< ? php function sumOfTermsInNthRow ( $ n ) { $ sum = $ n * ( 2 * pow ( $ n , 2 ) + 1 ) ; return $ sum ; } $ n = 4 ; echo " Sum ▁ of ▁ all ▁ the ▁ terms ▁ in ▁ nth ▁ row ▁ = ▁ " , sumOfTermsInNthRow ( $ n ) ; ? >
First digit in product of an array of numbers | PHP implementation to find first digit of a single number ; Keep dividing by 10 until it is greater than equal to 10 ; Driver Code
< ? php function firstDigit ( $ x ) { while ( $ x >= 10 ) $ x = $ x / 10 ; return floor ( $ x ) ; } echo firstDigit ( 12345 ) , " STRNEWLINE " ; echo firstDigit ( 5432 ) ; ? >
Find the occurrences of digit d in the range [ 0. . n ] | PHP program to count appearances of a digit ' d ' in range from [ 0. . n ] ; Initialize result ; Count appearances in numbers starting from d . ; When the last digit is equal to d ; When the first digit is equal to d then ; increment result as well as number ; In case of reverse of number such as 12 and 21 ; Driver code
< ? php function getOccurence ( $ n , $ d ) { $ result = 0 ; $ itr = $ d ; while ( $ itr <= $ n ) { if ( $ itr % 10 == $ d ) $ result ++ ; if ( $ itr != 0 && floor ( $ itr / 10 ) == $ d ) { $ result ++ ; $ itr ++ ; } else if ( floor ( $ itr / 10 ) == $ d - 1 ) $ itr = $ itr + ( 10 - $ d ) ; else $ itr = $ itr + 10 ; } return $ result ; } $ n = 11 ; $ d = 1 ; echo getOccurence ( $ n , $ d ) ; ? >