codeparrot/xlcost-text-to-code huggingface dataset:
Check if a number can be expressed as sum two abundant numbers | Function to return all abundant numbers This function will be helpful for multiple queries ; To store abundant numbers ; to store sum of the divisors include 1 in the sum ; if j is proper divisor ; if i is not a perfect square ; if sum is greater than i then i is a abundant number ; Check if number n is expressed as sum of two abundant numbers ; if both i and n - i are abundant numbers ; can not be expressed ; Driver code
< ? php function ABUNDANT ( ) { $ N = 100005 ; $ v = array ( ) ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) { $ sum = 1 ; for ( $ j = 2 ; $ j * $ j <= $ i ; $ j ++ ) { if ( $ i % $ j == 0 ) { $ sum += $ j ; if ( $ i / $ j != $ j ) $ sum += $ i / $ j ; } } if ( $ sum > $ i ) array_push ( $ v , $ i ) ; } $ v = array_unique ( $ v ) ; return $ v ; } function SumOfAbundant ( $ n ) { $ v = ABUNDANT ( ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( in_array ( $ i , $ v ) && in_array ( $ n - $ i , $ v ) ) { echo $ i , " ▁ " , $ n - $ i ; return ; } } echo - 1 ; } $ n = 24 ; SumOfAbundant ( $ n ) ; ? >
Find nth term of the series 5 2 13 41 | function to calculate nth term of the series ; if n is even number ; if n is odd number ; return nth term ; Driver code
< ? php function nthTermOfTheSeries ( $ n ) { if ( $ n % 2 == 0 ) $ nthTerm = pow ( $ n - 1 , 2 ) + $ n ; else $ nthTerm = pow ( $ n + 1 , 2 ) + $ n ; return $ nthTerm ; } $ n = 8 ; echo nthTermOfTheSeries ( $ n ) . " STRNEWLINE " ; $ n = 12 ; echo nthTermOfTheSeries ( $ n ) . " STRNEWLINE " ; $ n = 102 ; echo nthTermOfTheSeries ( $ n ) . " STRNEWLINE " ; $ n = 999 ; echo nthTermOfTheSeries ( $ n ) . " STRNEWLINE " ; $ n = 9999 ; echo nthTermOfTheSeries ( $ n ) . " STRNEWLINE " ; ? >
Find cost price from given selling price and profit or loss percentage | Function to calculate cost price with profit ; required formula to calculate CP with profit ; Function to calculate cost price with loss ; required formula to calculate CP with loss ; Driver code
< ? php function CPwithProfit ( $ sellingPrice , $ profit ) { $ costPrice = ( $ sellingPrice * 100.0 ) / ( 100 + $ profit ) ; return $ costPrice ; } function CPwithLoss ( $ sellingPrice , $ loss ) { $ costPrice = ( $ sellingPrice * 100.0 ) / ( 100 - $ loss ) ; return $ costPrice ; } $ SP = 1020 ; $ profit = 20 ; echo ( " Cost ▁ Price ▁ = ▁ " ) ; echo ( CPwithProfit ( $ SP , $ profit ) ) ; echo ( " STRNEWLINE " ) ; $ SP = 900 ; $ loss = 10 ; echo ( " Cost ▁ Price ▁ = ▁ " ) ; echo ( CPwithLoss ( $ SP , $ loss ) ) ; echo ( " STRNEWLINE " ) ; $ SP = 42039 ; $ profit = 8 ; echo ( " Cost ▁ Price ▁ = ▁ " ) ; echo ( CPwithProfit ( $ SP , $ profit ) ) ; echo ( " STRNEWLINE " ) ; ? >
Check whether a number is Non | Function to find prime factor and check if it is of the form 4 k + 1 or not ; 2 is a prime number but not of the form 4 k + 1 so , keep Dividing n by 2 until n is divisible by 2 ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; if i divides n check if i is of the form 4 k + 1 or not ; while i divides n divide n by i and update n ; This condition is to handle the case when n is a prime number greater than 2 ; Test function ; Driver code
< ? php function isNonHypotenuse ( $ n ) { while ( $ n % 2 == 0 ) { $ n = $ n / 2 ; } for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i = $ i + 2 ) { if ( $ n % $ i == 0 ) { if ( ( $ i - 1 ) % 4 == 0 ) return false ; while ( $ n % $ i == 0 ) { $ n = $ n / $ i ; } } } if ( $ n > 2 && ( $ n - 1 ) % 4 == 0 ) return false ; else return true ; } function test ( $ n ) { echo " Testing ▁ for ▁ " , $ n , " ▁ : ▁ " ; if ( isNonHypotenuse ( $ n ) ) echo " YES " . " STRNEWLINE " ; else echo " NO " . " STRNEWLINE " ; } $ n = 11 ; test ( $ n ) ; $ n = 10 ; test ( $ n ) ; ? >
Find the n | Function to return the nth string in the required sequence ; Length of the resultant string ; Relative index ; Initial string of length len consists of all a 's since the  list is sorted ; Convert relative index to Binary form and set 0 = a and 1 = b ; Reverse and return the string ; Driver Code
< ? php function obtain_str ( $ n ) { $ len = ( int ) ( log ( $ n + 1 ) / log ( 2 ) ) ; $ rel_ind = $ n + 1 - pow ( 2 , $ len ) ; $ i = 0 ; $ str = " " ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { $ str . = ' a ' ; } $ i = 0 ; while ( $ rel_ind > 0 ) { if ( $ rel_ind % 2 == 1 ) $ str [ $ i ] = ' b ' ; $ rel_ind = ( int ) ( $ rel_ind / 2 ) ; $ i ++ ; } return strrev ( $ str ) ; } $ n = 11 ; echo obtain_str ( $ n ) ; ? >
Program to find the Nth term of the series 0 , 3 / 1 , 8 / 3 , 15 / 5. . ... ... | Function to return the nth term of the given series ; nth term ; Driver code
< ? php function Nthterm ( $ n ) { $ numerator = ( pow ( $ n , 2 ) ) -1 ; $ denomenator = 2 * $ n - 3 ; echo $ numerator , " / " , $ denomenator ; return $ Tn ; } $ n = 3 ; Nthterm ( $ n ) ; ? >
Sum of each element raised to ( prime | Function to return the required sum ; Driver code
< ? php function getSum ( $ arr , $ p ) { return count ( $ arr ) ; } $ arr = array ( 5 , 6 , 8 ) ; $ p = 7 ; echo ( getSum ( $ arr , $ p ) ) ; ? >
Count numbers upto N which are both perfect square and perfect cube | Function to return required count ; Driver Code ; function call to print required answer
< ? php function SquareCube ( $ N ) { $ cnt = 0 ; $ i = 1 ; while ( ( pow ( $ i , 6 ) ) <= $ N ) { ++ $ cnt ; ++ $ i ; } return $ cnt ; } $ N = 100000 ; echo SquareCube ( $ N ) ; ? >
Sum of integers upto N with given unit digit | Function to return the required sum ; Driver code
< ? php function getSum ( $ n , $ d ) { $ sum = 0 ; while ( $ d <= $ n ) { $ sum += $ d ; $ d += 10 ; } return $ sum ; } $ n = 30 ; $ d = 3 ; echo ( getSum ( $ n , $ d ) ) ; ? >
Summing the sum series | Function to calculate twice of sum of first N natural numbers ; Function to calculate the terms of summing of sum series ; Driver Code
< ? php function sum ( $ N ) { $ MOD = 1000000007 ; $ val = $ N * ( $ N + 1 ) ; $ val = $ val % $ MOD ; return $ val ; } function sumX ( $ N , $ M , $ K ) { $ MOD = 1000000007 ; for ( $ i = 0 ; $ i < $ M ; $ i ++ ) { $ N = sum ( $ K + $ N ) ; } $ N = $ N % $ MOD ; return $ N ; } $ N = 1 ; $ M = 2 ; $ K = 3 ; echo ( sumX ( $ N , $ M , $ K ) ) ; ? >
Logarithm | PHP program to find log ( n ) using Recursion ; Drive main
< ? php function Log2n ( $ n ) { return ( $ n > 1 ) ? 1 + Log2n ( $ n / 2 ) : 0 ; } $ n = 32 ; echo Log2n ( $ n ) ; ? >
Harmonic Progression | PHP program to check if a given array can form harmonic progression ; Find reciprocal of arr [ ] ; After finding reciprocal , check if the reciprocal is in A . P . To check for A . P . , first Sort the reciprocal array , then check difference between consecutive elements ; series to check whether it is in H . P ; Checking a series is in H . P or not
< ? php function checkIsHP ( $ arr ) { $ n = count ( $ arr ) ; if ( $ n == 1 ) return true ; $ rec = array ( ) ; for ( $ i = 0 ; $ i < count ( $ arr ) ; $ i ++ ) { $ a = 1 / $ arr [ $ i ] ; array_push ( $ rec , $ a ) ; } return ( $ rec ) ; sort ( $ rec ) ; $ d = $ rec [ 1 ] - $ rec [ 0 ] ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) if ( $ rec [ $ i ] - $ rec [ $ i - 1 ] != $ d ) return false ; return true ; } $ arr = array ( 1 / 5 , 1 / 10 , 1 / 15 , 1 / 20 , 1 / 25 ) ; if ( checkIsHP ( $ arr ) ) print ( " Yes " ) ; else print ( " No " ) ; ? >
Arithmetic Mean | Prints N arithmetic means between A and B . ; calculate common difference ( d ) ; for finding N the arithmetic mean between A and B ; Driver Code
< ? php function printAMeans ( $ A , $ B , $ N ) { $ d = ( $ B - $ A ) / ( $ N + 1 ) ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) echo ( $ A + $ i * $ d ) , " ▁ " ; } $ A = 20 ; $ B = 32 ; $ N = 5 ; printAMeans ( $ A , $ B , $ N ) ;
Prime Factor | function to print all prime factors of a given number n ; Print the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Driver Code
< ? php function primeFactors ( $ n ) { while ( $ n % 2 == 0 ) { echo 2 , " ▁ " ; $ n = $ n / 2 ; } for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i = $ i + 2 ) { while ( $ n % $ i == 0 ) { echo $ i , " " ; $ n = $ n / $ i ; } } if ( $ n > 2 ) echo $ n , " ▁ " ; } $ n = 315 ; primeFactors ( $ n ) ; ? >
Time taken by two persons to meet on a circular track | PHP implementation of above approach ; Function to return the time when both the persons will meet at the starting point ; Time to cover 1 round by both ; Finding LCM to get the meeting point ; Function to return the time when both the persons will meet for the first time ; Driver Code ; Calling function
< ? php function gcd ( $ a , $ b ) { return $ b ? gcd ( $ b , $ a % $ b ) : $ a ; } function startingPoint ( $ Length , $ Speed1 , $ Speed2 ) { $ result1 = 0 ; $ result2 = 0 ; $ time1 = $ Length / $ Speed1 ; $ time2 = $ Length / $ Speed2 ; $ result1 = gcd ( $ time1 , $ time2 ) ; $ result2 = $ time1 * $ time2 / ( $ result1 ) ; return $ result2 ; } function firstTime ( $ Length , $ Speed1 , $ Speed2 ) { $ result = 0 ; $ relativeSpeed = abs ( $ Speed1 - $ Speed2 ) ; $ result = ( ( float ) $ Length / $ relativeSpeed ) ; return $ result ; } $ L = 30 ; $ S1 = 5 ; $ S2 = 2 ; $ first_Time = firstTime ( $ L , $ S1 , $ S2 ) ; $ starting_Point = startingPoint ( $ L , $ S1 , $ S2 ) ; echo " Met ▁ first ▁ time ▁ after ▁ " . $ first_Time . " ▁ hrs " . " STRNEWLINE " ; echo " Met ▁ at ▁ starting ▁ point ▁ after ▁ " . $ starting_Point . " ▁ hrs " . " STRNEWLINE " ; ? >
Check if the array has an element which is equal to product of remaining elements | Function to Check if the array has an element which is equal to product of all the remaining elements ; Calculate the product of all the elements ; Return true if any such element is found ; If no element is found ; Driver Code
< ? php function CheckArray ( $ arr , $ n ) { $ prod = 1 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) $ prod *= $ arr [ $ i ] ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) if ( $ arr [ $ i ] == $ prod / $ arr [ $ i ] ) return true ; return false ; } $ arr = array ( 1 , 2 , 12 , 3 , 2 ) ; $ n = sizeof ( $ arr ) ; if ( CheckArray ( $ arr , $ n ) ) echo " YES " ; else echo " NO " ;
Sum of common divisors of two numbers A and B | print the sum of common factors ; sum of common factors ; iterate from 1 to minimum of a and b ; if i is the common factor of both the numbers ; Driver code ; print the sum of common factors
< ? php function sum ( $ a , $ b ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= min ( $ a , $ b ) ; $ i ++ ) if ( $ a % $ i == 0 && $ b % $ i == 0 ) $ sum += $ i ; return $ sum ; } $ A = 10 ; $ B = 15 ; echo " Sum = " ? >
Minimum number of cuts required to make circle segments equal sized | Recursive function to return gcd of two nos ; Function to find the minimum number of additional cuts required to make circle segments are equal sized ; Sort the array ; Initial gcd value ; Including the last segment ; Driver Code
< ? php function findgcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return findgcd ( $ b , $ a % $ b ) ; } function minimumCuts ( $ a , $ n ) { sort ( $ a ) ; $ gcd = $ a [ 1 ] - $ a [ 0 ] ; $ s = $ gcd ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) { $ gcd = findgcd ( $ gcd , $ a [ $ i ] - $ a [ $ i - 1 ] ) ; $ s += $ a [ $ i ] - $ a [ $ i - 1 ] ; } if ( 360 - $ s > 0 ) $ gcd = findgcd ( $ gcd , 360 - $ s ) ; return ( 360 / $ gcd ) - $ n ; } $ arr = array ( 30 , 60 , 180 ) ; $ n = sizeof ( $ arr ) ; echo ( minimumCuts ( $ arr , $ n ) ) ; ? >
Find Selling Price from given Profit Percentage and Cost | Function to calculate the Selling Price ; Decimal Equivalent of Profit Percentage ; Find the Selling Price ; return the calculated Selling Price ; Get the CP and Profit % ; Printing the returned value
< ? php function SellingPrice ( $ CP , $ PP ) { $ P_decimal = 1 + ( $ PP / 100 ) ; $ res = $ P_decimal * $ CP ; return $ res ; } $ C = 720 ; $ P = 13 ; echo SellingPrice ( $ C , $ P ) ; ? >
Product of all the Composite Numbers in an array | Function that returns the the product of all composite numbers ; Find maximum value in the array ; Use sieve to find all prime numbers less than or equal to max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Set 0 and 1 as primes as they don 't need to be  counted as composite numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Find the product of all composite numbers in the arr [ ] ; Driver code
< ? php function compositeProduct ( $ arr , $ n ) { $ max_val = max ( $ arr ) ; $ prime = array_fill ( 0 , $ max_val + 1 , true ) ; $ prime [ 0 ] = true ; $ prime [ 1 ] = true ; for ( $ p = 2 ; $ p * $ p <= $ max_val ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ max_val ; $ i += $ p ) $ prime [ $ i ] = false ; } } $ product = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( ! $ prime [ $ arr [ $ i ] ] ) { $ product *= $ arr [ $ i ] ; } return $ product ; } $ arr = array ( 2 , 3 , 4 , 5 , 6 , 7 ) ; $ n = count ( $ arr ) ; echo compositeProduct ( $ arr , $ n ) ; ? >
Primality test for the sum of digits at odd places of a number | Function that return sum of the digits at odd places ; Function that returns true if the number is prime else false ; Corner cases ; This condition is checked so that we can skip middle five numbers in the below loop ; Driver code ; Get the sum of the digits at odd places
< ? php function sum_odd ( $ n ) { $ sum = 0 ; $ pos = 1 ; while ( $ n ) { if ( $ pos % 2 == 1 ) $ sum += $ n % 10 ; $ n = ( int ) ( $ n / 10 ) ; $ pos ++ ; } return $ sum ; } function check_prime ( $ n ) { if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = ( $ i + 6 ) ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return false ; return true ; } $ n = 223 ; $ sum = sum_odd ( $ n ) ; if ( check_prime ( $ sum ) ) echo " YES " ; else echo " NO " ; ? >
Find amount to be added to achieve target ratio in a given mixture | PHP program to find amount of water to be added to achieve given target ratio . ; Driver Code
< ? php function findAmount ( $ X , $ W , $ Y ) { return ( $ X * ( $ Y - $ W ) ) / ( 100 - $ Y ) ; } $ X = 100 ; $ W = 50 ; $ Y = 60 ; echo " Water ▁ to ▁ be ▁ added ▁ = ▁ " . findAmount ( $ X , $ W , $ Y ) ; ? >
Check if a number is a Mystery Number | Finds reverse of given num x . ; if found print the pair , return
< ? php function reverseNum ( $ x ) { $ s = ( string ) $ x ; $ s = strrev ( $ s ) ; $ rev = ( int ) $ s ; return $ rev ; } function isMysteryNumber ( $ n ) { for ( $ i = 1 ; $ i <= $ n / 2 ; $ i ++ ) { $ j = reverseNum ( $ i ) ; if ( $ i + $ j == $ n ) { echo $ i . " ▁ " . $ j ; return true ; } } echo " Not ▁ a ▁ Mystery ▁ Number " ; return false ; } $ n = 121 ; isMysteryNumber ( $ n ) ; return 0 ; ? >
Replace every element of the array by product of all other elements | PHP program to Replace every element by the product of all other elements ; Calculate the product of all the elements ; Replace every element product of all other elements ; Driver Code ; Print the modified array .
< ? php function ReplaceElements ( $ arr , $ n ) { $ prod = 1 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ prod *= $ arr [ $ i ] ; } for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ arr [ $ i ] = ( int ) ( $ prod / $ arr [ $ i ] ) ; } return $ arr ; } $ arr = array ( 2 , 3 , 3 , 5 , 7 ) ; $ n = sizeof ( $ arr ) ; $ arr1 = ReplaceElements ( $ arr , $ n ) ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { echo $ arr1 [ $ i ] . " " ; } ? >
Check if there is any pair in a given range with GCD is divisible by k | function to count such possible numbers ; if i is divisible by k ; if count of such numbers is greater than one ; Driver code
< ? php function Check_is_possible ( $ l , $ r , $ k ) { $ count = 0 ; for ( $ i = $ l ; $ i <= $ r ; $ i ++ ) { if ( $ i % $ k == 0 ) $ count ++ ; } return ( $ count > 1 ) ; } $ l = 4 ; $ r = 12 ; $ k = 5 ; if ( Check_is_possible ( $ l , $ r , $ k ) ) echo " YES STRNEWLINE " ; else echo " NO STRNEWLINE " ; ? >
Sum of first N natural numbers which are divisible by 2 and 7 | Function to calculate the sum of numbers divisible by 2 or 7 ; Driver code
< ? php function sum ( $ N ) { $ S1 = ( int ) ( ( $ N / 2 ) ) * ( int ) ( 2 * 2 + ( int ) ( $ N / 2 - 1 ) * 2 ) / 2 ; $ S2 = ( int ) ( ( $ N / 7 ) ) * ( int ) ( 2 * 7 + ( int ) ( $ N / 7 - 1 ) * 7 ) / 2 ; $ S3 = ( int ) ( ( $ N / 14 ) ) * ( int ) ( 2 * 14 + ( int ) ( $ N / 14 - 1 ) * 14 ) / 2 ; return ( $ S1 + $ S2 ) - $ S3 ; } $ N = 20 ; echo sum ( $ N ) ;
Ways to color a skewed tree such that parent and child have different colors | fast_way is recursive method to calculate power ; Driver Code
< ? php function fastPow ( $ N , $ K ) { if ( $ K == 0 ) return 1 ; $ temp = fastPow ( $ N , $ K / 2 ) ; if ( $ K % 2 == 0 ) return $ temp * $ temp ; else return $ N * $ temp * $ temp ; } function countWays ( $ N , $ K ) { return $ K * fastPow ( $ K - 1 , $ N - 1 ) ; } $ N = 3 ; $ K = 3 ; echo countWays ( $ N , $ K ) ; ? >
Sum of nth terms of Modified Fibonacci series made by every pair of two arrays | PHP program to find sum of n - th terms of a Fibonacci like series formed using first two terms of two arrays . ; if sum of first term is required ; if sum of second term is required ; fibonacci series used to find the nth term of every series ; as every b [ i ] term appears m times and every a [ i ] term also appears m times ; m is the size of the array
< ? php function sumNth ( & $ A , & $ B , & $ m , & $ n ) { $ res = 0 ; if ( $ n == 1 ) { for ( $ i = 0 ; $ i < $ m ; $ i ++ ) $ res = $ res + $ A [ $ i ] ; } else if ( $ n == 2 ) { for ( $ i = 0 ; $ i < $ m ; $ i ++ ) $ res = $ res + $ B [ $ i ] * $ m ; } else { $ f = array ( ) ; $ f [ 0 ] = 0 ; $ f [ 1 ] = 1 ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) $ f [ $ i ] = $ f [ $ i - 1 ] + $ f [ $ i - 2 ] ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { $ res = $ res + ( $ m * ( $ B [ $ i ] * $ f [ $ n - 1 ] ) ) + ( $ m * ( $ A [ $ i ] * $ f [ $ n - 2 ] ) ) ; } } return $ res ; } $ A = array ( 1 , 2 , 3 ) ; $ B = array ( 4 , 5 , 6 ) ; $ n = 3 ; $ m = sizeof ( $ A ) ; echo ( sumNth ( $ A , $ B , $ m , $ n ) ) ; ? >
Puzzle | Minimum distance for Lizard | side of cube ; understand from diagram ; understand from diagram ; minimum distance
< ? php $ a = 5 ; $ AC = $ a ; $ CE = 2 * $ a ; $ shortestDistance = ( double ) ( sqrt ( $ AC * $ AC + $ CE * $ CE ) ) ; echo $ shortestDistance . " STRNEWLINE " ; ? >
Find Sum of Series 1 ^ 2 | Function to find sum of series ; If n is even ; If n is odd ; return the result ; Get n ; Find the sum ; Get n ; Find the sum
< ? php function sum_of_series ( $ n ) { $ result = 0 ; if ( $ n % 2 == 0 ) { $ result = - ( $ n * ( $ n + 1 ) ) / 2 ; } else { $ result = ( $ n * ( $ n + 1 ) ) / 2 ; } return $ result ; } $ n = 3 ; echo sum_of_series ( $ n ) ; echo ( " STRNEWLINE " ) ; $ n = 10 ; echo sum_of_series ( $ n ) ; echo ( " STRNEWLINE " ) ; ? >
Check whether the given numbers are Cousin prime or not | Function to check if the given Number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Returns true if n1 and n2 are Cousin primes ; Check if the given number differ by 4 or not ; Check if both numbers are prime or not ; Get the 2 numbers ; Check the numbers for cousin prime
< ? php function isPrime ( $ n ) { if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return false ; return true ; } function isCousinPrime ( $ n1 , $ n2 ) { if ( abs ( $ n1 - $ n2 ) != 4 ) return false ; else return ( isPrime ( $ n1 ) && isPrime ( $ n2 ) ) ; } $ n1 = 7 ; $ n2 = 11 ; if ( isCousinPrime ( $ n1 , $ n2 ) ) echo " YES " ; else echo " NO " ; ? >
Thabit number | Utility function to check power of two ; function to check if the given number is Thabit Number ; Add 1 to the number ; Divide the number by 3 ; Check if the given number is power of 2 ; Driver Code ; Check if number is thabit number
< ? php function isPowerOfTwo ( $ n ) { return ( $ n && ! ( $ n & ( $ n - 1 ) ) ) ; } function isThabitNumber ( $ n ) { $ n = $ n + 1 ; if ( $ n % 3 == 0 ) $ n = $ n / 3 ; else return false ; if ( isPowerOfTwo ( $ n ) ) return true ; else return false ; } $ n = 47 ; if ( isThabitNumber ( $ n ) ) echo " YES " ; else echo " NO " ; ? >
Smallest Integer to be inserted to have equal sums | Function to find the minimum value to be added ; Variable to store entire array sum ; Variables to store sum of subarray1 and subarray 2 ; minimum value to be added ; Traverse through the array ; Sum of both halves ; Calculate minimum number to be added ; Driver code ; Length of array
< ? php function findMinEqualSums ( $ a , $ N ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ sum += $ a [ $ i ] ; } $ sum1 = 0 ; $ sum2 = 0 ; $ min = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ sum1 += $ a [ $ i ] ; $ sum2 = $ sum - $ sum1 ; if ( abs ( $ sum1 - $ sum2 ) < $ min ) { $ min = abs ( $ sum1 - $ sum2 ) ; } if ( $ min == 0 ) { break ; } } return $ min ; } $ a = array ( 3 , 2 , 1 , 5 , 7 , 8 ) ; $ N = count ( $ a ) ; echo ( findMinEqualSums ( $ a , $ N ) ) ; ? >
Find the total Number of Digits in ( N ! ) N | Function to find the total Number of Digits in ( N ! ) ^ N ; Finding X ; Calculating N * X ; Floor ( N * X ) + 1 return ceil ( $sum ) ; equivalent to floor ( sum ) + 1 ; Driver code
< ? php function CountDigits ( $ n ) { if ( $ n == 1 ) return 1 ; $ sum = 0 ; for ( $ i = 2 ; $ i <= $ n ; ++ $ i ) { $ sum += log ( $ i ) / log ( 10 ) ; } $ sum *= $ n ; } $ N = 5 ; echo CountDigits ( $ N ) ; ? >
Find the value of max ( f ( x ) ) | PHP implementation of above approach ; Function to calculate the value ; forming the prefix sum arrays ; Taking the query ; finding the sum in the range l to r in array a ; finding the sum in the range l to r in array b ; Finding the max value of the function ; Finding the min value of the function ; Driver code
< ? php $ MAX = 200006 ; $ CONS = 32766 ; function calc ( $ a , $ b , $ lr , $ q , $ n ) { global $ MAX ; global $ CONS ; $ M ; $ m ; $ i ; $ j ; $ k ; $ l ; $ r ; $ suma ; $ sumb ; $ cc ; $ cc = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; ++ $ i ) { $ a [ $ i + 1 ] += $ a [ $ i ] ; $ b [ $ i + 1 ] += $ b [ $ i ] ; } while ( $ q -- ) { $ l = $ lr [ $ cc ++ ] ; $ r = $ lr [ $ cc ++ ] ; $ l -= 2 ; $ r -= 1 ; $ suma = $ a [ $ r ] ; $ sumb = $ b [ $ r ] ; if ( $ l >= 0 ) { $ suma -= $ a [ $ l ] ; $ sumb -= $ b [ $ l ] ; } $ M = max ( $ CONS * $ suma + $ CONS * $ sumb , - $ CONS * $ suma - $ CONS * $ sumb ) ; $ M = max ( $ M , max ( $ CONS * $ suma - $ CONS * $ sumb , - $ CONS * $ suma + $ CONS * $ sumb ) ) ; $ m = min ( $ CONS * $ suma + $ CONS * $ sumb , - $ CONS * $ suma - $ CONS * $ sumb ) ; $ m = min ( $ m , min ( $ CONS * $ suma - $ CONS * $ sumb , - $ CONS * $ suma + $ CONS * $ sumb ) ) ; echo ( $ M - $ m ) , " STRNEWLINE " ; } } $ n = 5 ; $ q = 2 ; $ a = array ( 0 , 7 , 3 , 4 , 5 ) ; $ b = array ( 0 , 3 , 1 , 2 , 3 ) ; $ lr [ 0 ] = 1 ; $ lr [ 1 ] = 1 ; $ lr [ 2 ] = 1 ; $ lr [ 3 ] = 3 ; calc ( $ a , $ b , $ lr , $ q , $ n ) ; ? >
Program to find the Nth number of the series 2 , 10 , 24 , 44 , 70. ... . | function to return Nth term of the series ; Taking n as 4 ; Get Nth term
< ? php function NthTerm ( $ n ) { $ mod = 1000000009 ; $ x = ( 3 * $ n * $ n ) % $ mod ; return ( $ x - $ n + $ mod ) % $ mod ; } let N = 4 ; echo NthTerm ( $ N ) ; ? >