codeparrot/xlcost-text-to-code huggingface dataset:
Sum of bitwise AND of all subarrays | Function to find the sum of bitwise AND of all subarrays ; variable to store the final sum ; multiplier ; variable to check if counting is on ; variable to store the length of the subarrays ; loop to find the contiguous segments ; updating the multiplier ; returning the sum ; Driver Code
< ? php function findAndSum ( $ arr , $ n ) { $ sum = 0 ; $ mul = 1 ; for ( $ i = 0 ; $ i < 30 ; $ i ++ ) { $ count_on = 0 ; $ l = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( ( $ arr [ $ j ] & ( 1 << $ i ) ) > 0 ) if ( $ count_on ) $ l ++ ; else { $ count_on = 1 ; $ l ++ ; } else if ( $ count_on ) { $ sum += ( ( $ mul * $ l * ( $ l + 1 ) ) / 2 ) ; $ count_on = 0 ; $ l = 0 ; } } if ( $ count_on ) { $ sum += ( ( $ mul * $ l * ( $ l + 1 ) ) / 2 ) ; $ count_on = 0 ; $ l = 0 ; } $ mul *= 2 ; } return $ sum ; } $ arr = array ( 7 , 1 , 1 , 5 ) ; $ n = sizeof ( $ arr ) ; echo findAndSum ( $ arr , $ n ) ; ? >
Source to destination in 2 | Function that returns true if it is possible to move from source to the destination with the given moves ; Driver code
< ? php function isPossible ( $ Sx , $ Sy , $ Dx , $ Dy , $ x , $ y ) { if ( abs ( $ Sx - $ Dx ) % $ x == 0 && abs ( $ Sy - $ Dy ) % $ y == 0 && ( abs ( $ Sx - $ Dx ) / $ x ) % 2 == ( abs ( $ Sy - $ Dy ) / $ y ) % 2 ) return true ; return false ; } $ Sx = 0 ; $ Sy = 0 ; $ Dx = 0 ; $ Dy = 0 ; $ x = 3 ; $ y = 4 ; if ( isPossible ( $ Sx , $ Sy , $ Dx , $ Dy , $ x , $ y ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >
Count of pairs ( x , y ) in an array such that x < y | Function to return the number of pairs ( x , y ) such that x < y ; To store the number of valid pairs ; If a valid pair is found ; Return the count of valid pairs ; Driver code
< ? php function getPairs ( $ a ) { $ count = 0 ; for ( $ i = 0 ; $ i < sizeof ( $ a ) ; $ i ++ ) { for ( $ j = 0 ; $ j < sizeof ( $ a ) ; $ j ++ ) { if ( $ a [ $ i ] < $ a [ $ j ] ) $ count ++ ; } } return $ count ; } $ a = array ( 2 , 4 , 3 , 1 ) ; echo getPairs ( $ a ) ; ? >
Composite numbers with digit sum 1 | Function that returns true if number n is a composite number ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if the eventual digit sum of number nm is 1 ; Loop till the sum is not single digit number ; Intitialize the sum as zero ; Find the sum of digits ; If sum is eventually 1 ; Function to print the required numbers from the given range ; If i is one of the required numbers ; Driver code
< ? php function isComposite ( $ n ) { if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return false ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return true ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return true ; return false ; } function isDigitSumOne ( $ nm ) { while ( $ nm > 9 ) { $ sum_digit = 0 ; while ( $ nm > 0 ) { $ digit = $ nm % 10 ; $ sum_digit = $ sum_digit + $ digit ; $ nm = floor ( $ nm / 10 ) ; } $ nm = $ sum_digit ; } if ( $ nm == 1 ) return true ; else return false ; } function printValidNums ( $ l , $ r ) { for ( $ i = $ l ; $ i <= $ r ; $ i ++ ) { if ( isComposite ( $ i ) && isDigitSumOne ( $ i ) ) echo $ i , " ▁ " ; } } $ l = 10 ; $ r = 100 ; printValidNums ( $ l , $ r ) ; ? >
Determine the count of Leaf nodes in an N | Function to calculate leaf nodes in n - ary tree ; Driver code
< ? php function calcNodes ( $ N , $ I ) { $ result = 0 ; $ result = $ I * ( $ N - 1 ) + 1 ; return $ result ; } $ N = 5 ; $ I = 2 ; echo " Leaf ▁ nodes ▁ = ▁ " . calcNodes ( $ N , $ I ) ; ? >
An application on Bertrand 's ballot theorem | PHP implementation of the approach ; Function to calculate factorial of a number mod 1000000007 ; Factorial of i = factorial of ( i - 1 ) * i ; ; Taking mod aint with calculation . ; Function for modular exponentiation ; If p is odd ; If p is even ; Function to return the count of required permutations ; Calculating multiplicative modular inverse for x ! and multiplying with ans ; Calculating multiplicative modular inverse for y ! and multiplying with ans ; Pre - compute factorials
< ? php $ mod = 1000000007 ; $ arr = array_fill ( 0 , 10001 , 0 ) ; function cal_factorial ( ) { global $ arr , $ mod ; $ arr [ 0 ] = 1 ; for ( $ i = 1 ; $ i <= 10000 ; $ i ++ ) { $ arr [ $ i ] = ( $ arr [ $ i - 1 ] * $ i ) % $ mod ; } } function mod_exponent ( $ num , $ p ) { global $ mod ; if ( $ p == 0 ) return 1 ; if ( ( $ p & 1 ) ) { return ( ( $ num % $ mod ) * ( mod_exponent ( ( $ num * $ num ) % $ mod , $ p / 2 ) ) % $ mod ) % $ mod ; } else if ( ! ( $ p & 1 ) ) return ( mod_exponent ( ( $ num * $ num ) % $ mod , $ p / 2 ) ) % $ mod ; } function getCount ( $ x , $ y ) { global $ arr , $ mod ; $ ans = $ arr [ $ x + $ y - 1 ] ; $ ans *= mod_exponent ( $ arr [ $ x ] , $ mod - 2 ) ; $ ans %= $ mod ; $ ans *= mod_exponent ( $ arr [ $ y ] , $ mod - 2 ) ; $ ans %= $ mod ; $ ans *= ( $ x - $ y ) ; $ ans %= $ mod ; return $ ans ; } cal_factorial ( ) ; $ x = 3 ; $ y = 1 ; print ( getCount ( $ x , $ y ) ) ; ? >
Find the values of X and Y in the Given Equations | Function to find the values of X and Y ; base condition ; required answer ; Driver Code
< ? php function findValues ( $ a , $ b ) { if ( ( $ a - $ b ) % 2 == 1 ) { echo " - 1" ; return ; } echo ( $ a - $ b ) / 2 , " ▁ " , ( $ a + $ b ) / 2 ; } $ a = 12 ; $ b = 8 ; findValues ( $ a , $ b ) ; ? >
Count triplet pairs ( A , B , C ) of points in 2 | Function to return the count of possible triplets ; Insert all the points in a set ; If the mid point exists in the set ; Return the count of valid triplets ; Driver code
< ? php function countTriplets ( $ n , $ points ) { $ pts = array ( ) ; $ ct = 0 ; for ( $ i = 0 ; $ i < count ( $ points ) ; $ i ++ ) { for ( $ j = 0 ; $ j < count ( $ points [ $ i ] ) ; $ j ++ ) { $ pts [ ] = $ points [ $ i ] [ $ j ] ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { $ x = $ points [ $ i ] [ 0 ] + $ points [ $ j ] [ 0 ] ; $ y = $ points [ $ i ] [ 1 ] + $ points [ $ j ] [ 1 ] ; if ( $ x % 2 == 0 and $ y % 2 == 0 ) if ( in_array ( ( int ) ( $ x / 2 ) , $ pts ) and in_array ( ( int ) ( $ y / 2 ) , $ pts ) ) $ ct += 1 ; } return $ ct ; } $ points = array ( array ( 1 , 1 ) , array ( 2 , 2 ) , array ( 3 , 3 ) ) ; $ n = count ( $ points ) ; print ( countTriplets ( $ n , $ points ) ) ; ? >
Concentration of juice after mixing n glasses in equal proportion | Function to return the concentration of the resultant mixture ; Driver code
< ? php function mixtureConcentration ( $ n , $ p ) { $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ res += $ p [ $ i ] ; $ res /= $ n ; return $ res ; } $ arr = array ( 0 , 20 , 20 ) ; $ n = count ( $ arr ) ; print ( round ( mixtureConcentration ( $ n , $ arr ) , 4 ) ) ; ? >
Number of elements with even factors in the given range | Function to count the perfect squares ; Driver code
< ? php function countOddSquares ( $ n , $ m ) { return ( int ) pow ( $ m , 0.5 ) - ( int ) pow ( $ n - 1 , 0.5 ) ; } $ n = 5 ; $ m = 100 ; echo " Count ▁ is ▁ " , ( $ m - $ n + 1 ) - countOddSquares ( $ n , $ m ) ; ? >
Total position where king can reach on a chessboard in exactly M moves | Function to return the number of squares that the king can reach in the given number of moves ; Calculate initial and final coordinates ; Since chessboard is of size 8 X8 so if any coordinate is less than 1 or greater than 8 make it 1 or 8. ; Calculate total positions ; Driver code
< ? php function Square ( $ row , $ column , $ moves ) { $ a = 0 ; $ b = 0 ; $ c = 0 ; $ d = 0 ; $ total = 0 ; $ a = $ row - $ moves ; $ b = $ row + $ moves ; $ c = $ column - $ moves ; $ d = $ column + $ moves ; if ( $ a < 1 ) $ a = 1 ; if ( $ c < 1 ) $ c = 1 ; if ( $ b > 8 ) $ b = 8 ; if ( $ d > 8 ) $ d = 8 ; $ total = ( $ b - $ a + 1 ) * ( $ d - $ c + 1 ) - 1 ; return $ total ; } $ R = 4 ; $ C = 5 ; $ M = 2 ; echo Square ( $ R , $ C , $ M ) ; ? >
Find M | number whosesum till one digit is N ; Driver Code
< ? php function findNumber ( $ n , $ m ) { $ num = ( $ m - 1 ) * 9 + $ n ; return $ num ; } $ n = 2 ; $ m = 5 ; echo findNumber ( $ n , $ m ) ; ? >
Check if a number can be represented as a sum of 2 triangular numbers | Function to check if it is possible or not ; Store all triangular numbers up to N in a Set ; Check if a pair exists ; Driver Code
< ? php function checkTriangularSumRepresentation ( $ n ) { $ tri = array ( ) ; $ i = 1 ; while ( true ) { $ x = $ i * ( $ i + 1 ) ; if ( $ x >= $ n ) break ; array_push ( $ tri , $ x ) ; $ i += 1 ; } foreach ( $ tri as $ tm ) if ( in_array ( $ n - $ tm , $ tri ) ) return true ; return false ; } $ n = 24 ; if ( checkTriangularSumRepresentation ( $ n ) ) print ( " Yes " ) ; else print ( " No " ) ; ? >
Absolute difference between the first X and last X Digits of N | Function to find the number of digits in the integer ; Function to find the absolute difference ; Store the last x digits in last ; Count the no . of digits in N ; Remove the digits except the first x ; Store the first x digits in first ; Return the absolute difference between the first and last ; Driver code
< ? php function digitsCount ( $ n ) { $ len = 0 ; while ( $ n > 0 ) { $ len ++ ; $ n = ( int ) ( $ n / 10 ) ; } return $ len ; } function absoluteFirstLast ( $ n , $ x ) { $ i = 0 ; $ mod = 1 ; while ( $ i < $ x ) { $ mod *= 10 ; $ i ++ ; } $ last = $ n % $ mod ; $ len = digitsCount ( $ n ) ; while ( $ len != $ x ) { $ n = ( int ) ( $ n / 10 ) ; $ len -- ; } $ first = $ n ; return abs ( $ first - $ last ) ; } $ n = 21546 ; $ x = 2 ; echo absoluteFirstLast ( $ n , $ x ) ; ? >
Generate minimum sum sequence of integers with even elements greater | Function to print the required sequence ; arr [ ] will hold the sequence sum variable will store the sum of the sequence ; If sum of the sequence is odd ; Print the sequence ; Driver Code
< ? php function make_sequence ( $ N ) { $ arr = array ( ) ; $ sum = 0 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) { if ( $ i % 2 == 1 ) $ arr [ $ i ] = 1 ; else $ arr [ $ i ] = 2 ; $ sum += $ arr [ $ i ] ; } if ( $ sum % 2 == 1 ) $ arr [ 2 ] = 3 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) echo $ arr [ $ i ] , " ▁ " ; } $ N = 9 ; make_sequence ( $ N ) ; ? >
Count Odd and Even numbers in a range from L to R | Return the number of odd numbers in the range [ L , R ] ; if either R or L is odd ; Driver code
< ? php function countOdd ( $ L , $ R ) { $ N = ( $ R - $ L ) / 2 ; if ( $ R % 2 != 0 $ L % 2 != 0 ) $ N ++ ; return $ N ; } $ L = 3 ; $ R = 7 ; $ odds = countOdd ( $ L , $ R ) ; $ evens = ( $ R - $ L + 1 ) - $ odds ; echo " Count ▁ of ▁ odd ▁ numbers ▁ is ▁ " . $ odds . " STRNEWLINE " ; echo " Count ▁ of ▁ even ▁ numbers ▁ is ▁ " . $ evens ; ? >
Cost of painting n * m grid | Function to return the minimum cost ; Driver code
< ? php function getMinCost ( $ n , $ m ) { $ cost = ( $ n - 1 ) * $ m + ( $ m - 1 ) * $ n ; return $ cost ; } $ n = 4 ; $ m = 5 ; echo getMinCost ( $ n , $ m ) ; ? >
Minimum operations required to make all the array elements equal | Function to return the minimum number of given operation required to make all the array elements equal ; Check if all the elements from kth index to last are equal ; Finding the 1 st element which is not equal to the kth element ; Driver code
< ? php function minOperation ( $ n , $ k , & $ a ) { for ( $ i = $ k ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] != $ a [ $ k - 1 ] ) return -1 ; } for ( $ i = $ k - 2 ; $ i > -1 ; $ i -- ) { if ( $ a [ $ i ] != $ a [ $ k - 1 ] ) return ( $ i + 1 ) ; } } $ n = 5 ; $ k = 3 ; $ a = array ( 2 , 1 , 1 , 1 , 1 ) ; echo ( minOperation ( $ n , $ k , $ a ) ) ; ? >
Number of Simple Graph with N Vertices and M Edges | Function to return the value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate the value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * ... * 1 ] ; Driver Code
< ? php function binomialCoeff ( $ n , $ k ) { if ( $ k > $ n ) return 0 ; $ res = 1 ; if ( $ k > $ n - $ k ) $ k = $ n - $ k ; for ( $ i = 0 ; $ i < $ k ; ++ $ i ) { $ res *= ( $ n - $ i ) ; $ res /= ( $ i + 1 ) ; } return $ res ; } $ N = 5 ; $ M = 1 ; $ P = floor ( ( $ N * ( $ N - 1 ) ) / 2 ) ; echo binomialCoeff ( $ P , $ M ) ; ? >
Increasing sequence with given GCD | Function to print the required sequence ; Driver Code
< ? php function generateSequence ( $ n , $ g ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) echo $ i * $ g . " ▁ " ; } $ n = 6 ; $ g = 5 ; generateSequence ( $ n , $ g ) ; ? >
Program to find LCM of two Fibonnaci Numbers | PHP Program to find LCM of Fib ( a ) and Fib ( b ) ; Create an array for memoization ; Function to return the n 'th Fibonacci number using table f[]. ; Base cases ; If fib ( n ) is already computed ; Applying recursive formula Note value n & 1 is 1 if n is odd , else 0. ; Function to return gcd of a and b ; Function to return the LCM of Fib ( a ) and Fib ( a ) ; Driver code
< ? php $ GLOBALS [ ' MAX ' ] = 1000 ; $ GLOBALS [ ' f ' ] = array ( ) ; for ( $ i = 0 ; $ i < $ GLOBALS [ ' MAX ' ] ; $ i ++ ) $ GLOBALS [ ' f ' ] [ $ i ] = 0 ; function fib ( $ n ) { if ( $ n == 0 ) return 0 ; if ( $ n == 1 $ n == 2 ) return ( $ GLOBALS [ ' f ' ] [ $ n ] = 1 ) ; if ( $ GLOBALS [ ' f ' ] [ $ n ] ) return $ GLOBALS [ ' f ' ] [ $ n ] ; $ k = ( $ n & 1 ) ? ( $ n + 1 ) / 2 : $ n / 2 ; $ GLOBALS [ ' f ' ] [ $ n ] = ( $ n & 1 ) ? ( fib ( $ k ) * fib ( $ k ) + fib ( $ k - 1 ) * fib ( $ k - 1 ) ) : ( 2 * fib ( $ k - 1 ) + fib ( $ k ) ) * fib ( $ k ) ; return $ GLOBALS [ ' f ' ] [ $ n ] ; } function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function findLCMFibonacci ( $ a , $ b ) { return ( fib ( $ a ) * fib ( $ b ) ) / fib ( gcd ( $ a , $ b ) ) ; } $ a = 3 ; $ b = 12 ; echo findLCMFibonacci ( $ a , $ b ) ; ? >
Check whether XOR of all numbers in a given range is even or odd | Function to check if XOR of all numbers in range [ L , R ] is Even or Odd ; Count odd Numbers in range [ L , R ] ; Check if count of odd Numbers is even or odd ; Driver Code
< ? php function isEvenOrOdd ( $ L , $ R ) { $ oddCount = floor ( ( $ R - $ L ) / 2 ) ; if ( $ R % 2 == 1 $ L % 2 == 1 ) $ oddCount ++ ; if ( $ oddCount % 2 == 0 ) return " Even " ; else return " Odd " ; } $ L = 5 ; $ R = 15 ; echo isEvenOrOdd ( $ L , $ R ) ; ? >
Count number of trailing zeros in ( 1 ^ 1 ) * ( 2 ^ 2 ) * ( 3 ^ 3 ) * ( 4 ^ 4 ) * . . | Function to return the number of trailing zeros ; To store the number of 2 s and 5 s ; If we get a factor 2 then we have i number of 2 s because the power of the number is raised to i ; If we get a factor 5 then we have i number of 5 s because the power of the number is raised to i ; Take the minimum of them ; Driver code
< ? php function trailing_zeros ( $ N ) { $ count_of_two = 0 ; $ count_of_five = 0 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) { $ val = $ i ; while ( $ val % 2 == 0 && $ val > 0 ) { $ val /= 2 ; $ count_of_two += $ i ; } while ( $ val % 5 == 0 && $ val > 0 ) { $ val /= 5 ; $ count_of_five += $ i ; } } $ ans = min ( $ count_of_two , $ count_of_five ) ; return $ ans ; } $ N = 12 ; echo trailing_zeros ( $ N ) ; ? >
Midpoint ellipse drawing algorithm | PHP program for implementing Mid - Point Ellipse Drawing Algorithm ; Initial decision parameter of region 1 ; For region 1 ; Print points based on 4 - way symmetry ; Checking and updating value of decision parameter based on algorithm ; Decision parameter of region 2 ; Plotting points of region 2 ; printing points based on 4 - way symmetry ; Checking and updating parameter value based on algorithm ; To draw a ellipse of major and minor radius 15 , 10 centred at ( 50 , 50 )
< ? php function midptellipse ( $ rx , $ ry , $ xc , $ yc ) { $ x = 0 ; $ y = $ ry ; $ d1 = ( $ ry * $ ry ) - ( $ rx * $ rx * $ ry ) + ( 0.25 * $ rx * $ rx ) ; $ dx = 2 * $ ry * $ ry * $ x ; $ dy = 2 * $ rx * $ rx * $ y ; while ( $ dx < $ dy ) { echo " ( ▁ " , $ x + $ xc , " , ▁ " , $ y + $ yc , " ▁ ) STRNEWLINE " ; echo " ( ▁ " , - $ x + $ xc , " , ▁ " , $ y + $ yc , " ▁ ) STRNEWLINE " ; echo " ( ▁ " , $ x + $ xc , " , ▁ " , - $ y + $ yc , " ▁ ) STRNEWLINE " ; echo " ( ▁ " , - $ x + $ xc , " , ▁ " , - $ y + $ yc , " ▁ ) STRNEWLINE " ; if ( $ d1 < 0 ) { $ x ++ ; $ dx = $ dx + ( 2 * $ ry * $ ry ) ; $ d1 = $ d1 + $ dx + ( $ ry * $ ry ) ; } else { $ x ++ ; $ y -- ; $ dx = $ dx + ( 2 * $ ry * $ ry ) ; $ dy = $ dy - ( 2 * $ rx * $ rx ) ; $ d1 = $ d1 + $ dx - $ dy + ( $ ry * $ ry ) ; } } $ d2 = ( ( $ ry * $ ry ) * ( ( $ x + 0.5 ) * ( $ x + 0.5 ) ) ) + ( ( $ rx * $ rx ) * ( ( $ y - 1 ) * ( $ y - 1 ) ) ) - ( $ rx * $ rx * $ ry * $ ry ) ; while ( $ y >= 0 ) { echo " ( ▁ " , $ x + $ xc , " , ▁ " , $ y + $ yc , " ▁ ) STRNEWLINE " ; echo " ( ▁ " , - $ x + $ xc , " , ▁ " , $ y + $ yc , " ▁ ) STRNEWLINE " ; echo " ( ▁ " , $ x + $ xc , " , ▁ " , - $ y + $ yc , " ▁ ) STRNEWLINE " ; echo " ( ▁ " , - $ x + $ xc , " , ▁ " , - $ y + $ yc , " ▁ ) STRNEWLINE " ; if ( $ d2 > 0 ) { $ y -- ; $ dy = $ dy - ( 2 * $ rx * $ rx ) ; $ d2 = $ d2 + ( $ rx * $ rx ) - $ dy ; } else { $ y -- ; $ x ++ ; $ dx = $ dx + ( 2 * $ ry * $ ry ) ; $ dy = $ dy - ( 2 * $ rx * $ rx ) ; $ d2 = $ d2 + $ dx - $ dy + ( $ rx * $ rx ) ; } } } midptellipse ( 10 , 15 , 50 , 50 ) ; ? >
Program to check if a number is divisible by sum of its digits | Function to check if the given number is divisible by sum of its digits ; Find sum of digits ; check if sum of digits divides n ; Driver Code
< ? php function isDivisible ( $ n ) { $ temp = $ n ; $ sum = 0 ; while ( $ n ) { $ k = $ n % 10 ; $ sum += $ k ; $ n = ( int ) ( $ n / 10 ) ; } if ( $ temp % $ sum == 0 ) return " YES " ; return " NO " ; } $ n = 123 ; print ( isDivisible ( $ n ) ) ; ? >
Find the final X and Y when they are Altering under given condition | Function to get final value of X and Y ; Following the sequence but by replacing minus with modulo ; Step 1 ; Step 2 ; Step 3 ; Otherwise terminate ; Get the initial X and Y values ; Find the result
< ? php function alter ( $ x , $ y ) { while ( true ) { if ( $ x == 0 $ y == 0 ) break ; if ( $ x >= 2 * $ y ) $ x = $ x % ( 2 * $ y ) ; else if ( $ y >= 2 * $ x ) $ y = $ y % ( 2 * $ x ) ; else break ; } echo " X = " , ▁ $ x , ▁ " , " , ▁ " Y = " } $ x = 12 ; $ y = 5 ; alter ( $ x , $ y ) ; ? >
Print all multiplicative primes <= N | Function to return the digit product of n ; Function to print all multiplicative primes <= n ; Create a boolean array " prime [ 0 . . n + 1 ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; If i is prime and its digit sum is also prime i . e . i is a multiplicative prime ; Driver code
< ? php function digitProduct ( $ n ) { $ prod = 1 ; while ( $ n ) { $ prod = $ prod * ( $ n % 10 ) ; $ n = floor ( $ n / 10 ) ; } return $ prod ; } function printMultiplicativePrimes ( $ n ) { $ prime = array_fill ( 0 , $ n + 1 , true ) ; $ prime [ 0 ] = $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = false ; } } for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { if ( $ prime [ $ i ] && $ prime [ digitProduct ( $ i ) ] ) echo $ i , " ▁ " ; } } $ n = 10 ; printMultiplicativePrimes ( $ n ) ; ? >
Largest number less than or equal to N / 2 which is coprime to N | Function to calculate gcd of two number ; Function to check if two numbers are coprime or not ; two numbers are coprime if their gcd is 1 ; Function to find largest integer less than or equal to N / 2 and coprime with N ; Check one by one all numbers less than or equal to N / 2 ; Driver code
< ? php function gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; else return gcd ( $ b , $ a % $ b ) ; } function coPrime ( $ n1 , $ n2 ) { if ( gcd ( $ n1 , $ n2 ) == 1 ) return true ; else return false ; } function largestCoprime ( $ N ) { $ half = floor ( $ N / 2 ) ; while ( coPrime ( $ N , $ half ) == false ) $ half -- ; return $ half ; } $ n = 50 ; echo largestCoprime ( $ n ) ;
Largest number less than or equal to N / 2 which is coprime to N | Function to find largest integer less than or equal to N / 2 and is coprime with N ; Handle the case for N = 6 ; Driver code
< ? php function largestCoprime ( $ N ) { if ( $ N == 6 ) return 1 ; else if ( $ N % 4 == 0 ) return ( $ N / 2 ) - 1 ; else if ( $ N % 2 == 0 ) return ( $ N / 2 ) - 2 ; else return ( ( $ N - 1 ) / 2 ) ; } $ n = 50 ; echo largestCoprime ( $ n ) ; ? >
Print all safe primes below N | Function to print first n safe primes ; Initialize all entries of integer array as 1. A value in prime [ i ] will finally be 0 if i is Not a prime , else 1 ; 0 and 1 are not primes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; If i is prime ; 2 p + 1 ; If 2 p + 1 is also a prime then set prime [ 2 p + 1 ] = 2 ; i is a safe prime ; Driver code
< ? php function printSafePrimes ( $ n ) { $ prime = array ( ) ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ prime [ $ i ] = 1 ; $ prime [ 0 ] = $ prime [ 1 ] = 0 ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] == 1 ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = 0 ; } } for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { if ( $ prime [ $ i ] != 0 ) { $ temp = ( 2 * $ i ) + 1 ; if ( $ temp <= $ n && $ prime [ $ temp ] != 0 ) $ prime [ $ temp ] = 2 ; } } for ( $ i = 5 ; $ i <= $ n ; $ i ++ ) if ( $ prime [ $ i ] == 2 ) echo $ i , " ▁ " ; } $ n = 20 ; printSafePrimes ( $ n ) ; ? >
Minimum multiplications with { 2 , 3 , 7 } to make two numbers equal | Function to find powers of 2 , 3 and 7 in x ; To keep count of each divisor ; To store the result ; Count powers of 2 in x ; Count powers of 3 in x ; Count powers of 7 in x ; Remaining number which is not divisible by 2 , 3 or 7 ; Function to return the minimum number of given operations required to make a and b equal ; a = x * 2 ^ a1 * 3 ^ a2 * 7 ^ a3 va [ 0 ] = a1 va [ 1 ] = a2 va [ 2 ] = a3 va [ 3 ] = x ; Similarly for b ; If a and b cannot be made equal with the given operation . Note that va [ 3 ] and vb [ 3 ] contain remaining numbers after repeated divisions with 2 , 3 and 7. If remaining numbers are not same then we cannot make them equal . ; Minimum number of operations required ; Driver code
< ? php function Divisors ( $ x ) { $ c = 0 ; $ v = array ( ) ; while ( $ x % 2 == 0 ) { $ c ++ ; $ x = floor ( $ x / 2 ) ; } array_push ( $ v , $ c ) ; $ c = 0 ; while ( $ x % 3 == 0 ) { $ c ++ ; $ x = floor ( $ x / 3 ) ; } array_push ( $ v , $ c ) ; $ c = 0 ; while ( $ x % 7 == 0 ) { $ c ++ ; $ x = floor ( $ x / 7 ) ; } array_push ( $ v , $ c ) ; array_push ( $ v , $ x ) ; return $ v ; } function MinOperations ( $ a , $ b ) { $ va = Divisors ( $ a ) ; $ vb = Divisors ( $ b ) ; if ( $ va [ 3 ] != $ vb [ 3 ] ) return -1 ; $ minOperations = abs ( $ va [ 0 ] - $ vb [ 0 ] ) + abs ( $ va [ 1 ] - $ vb [ 1 ] ) + abs ( $ va [ 2 ] - $ vb [ 2 ] ) ; return $ minOperations ; } $ a = 14 ; $ b = 28 ; echo MinOperations ( $ a , $ b ) ; ? >
Product of N with its largest odd digit | Function to return the largest odd digit in n ; If all digits are even then - 1 will be returned ; Last digit from n ; If current digit is odd and > maxOdd ; Remove last digit ; Return the maximum odd digit ; Function to return the product of n with its largest odd digit ; If there are no odd digits in n ; Product of n with its largest odd digit ; Driver code
< ? php function largestOddDigit ( $ n ) { $ maxOdd = -1 ; while ( $ n > 0 ) { $ digit = $ n % 10 ; if ( $ digit % 2 == 1 && $ digit > $ maxOdd ) $ maxOdd = $ digit ; $ n = $ n / 10 ; } return $ maxOdd ; } function getProduct ( $ n ) { $ maxOdd = largestOddDigit ( $ n ) ; if ( $ maxOdd == -1 ) return -1 ; return ( $ n * $ maxOdd ) ; } $ n = 12345 ; echo getProduct ( $ n ) ; ? >