codeparrot/xlcost-text-to-code huggingface dataset:
Loss when two items are sold at same price and same percentage profit / loss | Function that will find loss ; Driver Code ; Calling Function
< ? php function Loss ( $ SP , $ P ) { $ loss = 0 ; $ loss = ( ( 2 * $ P * $ P * $ SP ) / ( 100 * 100 - $ P * $ P ) ) ; print ( " Loss ▁ = ▁ " . round ( $ loss , 3 ) ) ; } $ SP = 2400 ; $ P = 30 ; Loss ( $ SP , $ P ) ; ? >
Check for an array element that is co | PHP implementation of the approach ; Stores smallest prime factor for every number ; Hash to store prime factors count ; Function to calculate SPF ( Smallest Prime Factor ) for every number till MAXN ; Marking smallest prime factor for every number to be itself ; Separately marking spf for every even number as 2 ; Checking if i is prime ; Marking SPF for all numbers divisible by i ; Marking spf [ j ] if it is not previously marked ; Function to store the prime factors after dividing by the smallest prime factor at every step ; Storing the count of prime factors in hash ; Function that returns true if there are no common prime factors between x and other numbers of the array ; Checking whether it common prime factor with other numbers ; Function that returns true if there is an element in the array which is coprime with all the other elements of the array ; Using sieve for generating prime factors ; Checking the common prime factors with other numbers ; Driver code
< ? php $ MAXN = 10001 ; $ spf = array_fill ( 0 , $ MAXN , 0 ) ; $ hash1 = array_fill ( 0 , $ MAXN , 0 ) ; function sieve ( ) { global $ spf , $ MAXN , $ hash1 ; $ spf [ 1 ] = 1 ; for ( $ i = 2 ; $ i < $ MAXN ; $ i ++ ) $ spf [ $ i ] = $ i ; for ( $ i = 4 ; $ i < $ MAXN ; $ i += 2 ) $ spf [ $ i ] = 2 ; for ( $ i = 3 ; $ i * $ i < $ MAXN ; $ i ++ ) { if ( $ spf [ $ i ] == $ i ) { for ( $ j = $ i * $ i ; $ j < $ MAXN ; $ j += $ i ) if ( $ spf [ $ j ] == $ j ) $ spf [ $ j ] = $ i ; } } } function getFactorization ( $ x ) { global $ spf , $ MAXN , $ hash1 ; while ( $ x != 1 ) { $ temp = $ spf [ $ x ] ; if ( $ x % $ temp == 0 ) { $ hash1 [ $ spf [ $ x ] ] ++ ; $ x = ( int ) ( $ x / $ spf [ $ x ] ) ; } while ( $ x % $ temp == 0 ) $ x = ( int ) ( $ x / $ temp ) ; } } function check ( $ x ) { global $ spf , $ MAXN , $ hash1 ; while ( $ x != 1 ) { $ temp = $ spf [ $ x ] ; if ( $ x % $ temp == 0 && $ hash1 [ $ temp ] > 1 ) return false ; while ( $ x % $ temp == 0 ) $ x = ( int ) ( $ x / $ temp ) ; } return true ; } function hasValidNum ( $ arr , $ n ) { global $ spf , $ MAXN , $ hash1 ; sieve ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) getFactorization ( $ arr [ $ i ] ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( check ( $ arr [ $ i ] ) ) return true ; return false ; } $ arr = array ( 2 , 8 , 4 , 10 , 6 , 7 ) ; $ n = count ( $ arr ) ; if ( hasValidNum ( $ arr , $ n ) ) echo " Yes " ; else echo " No " ; ? >
Ways to Remove Edges from a Complete Graph to make Odd Edges | Function to return the number of ways to remove edges from the graph so that odd number of edges are left in the graph ; Total number of edges ; Driver code
< ? php function countWays ( $ N ) { $ E = ( $ N * ( $ N - 1 ) ) / 2 ; if ( $ N == 1 ) return 0 ; return ( int ) pow ( 2 , $ E - 1 ) ; } $ N = 4 ; echo ( countWays ( $ N ) ) ; ? >
Closest sum partition ( into two subsets ) of numbers from 1 to n | Function to return the minimum required absolute difference ; Driver code
< ? php function minAbsDiff ( $ n ) { $ mod = $ n % 4 ; if ( $ mod == 0 $ mod == 3 ) return 0 ; return 1 ; } $ n = 5 ; echo minAbsDiff ( $ n ) ; ? >
Check if the Xor of the frequency of all digits of a number N is zero or not | PHP implementation of the above approach ; creating a frequency array ; Finding the last digit of the number ; Dividing the number by 10 to eliminate last digit ; counting frequency of each digit ; checking if the xor of all frequency is zero or not ; Main Drive
< ? php function check ( $ s ) { $ freq = array_fill ( 0 , 10 , 0 ) ; while ( $ s != 0 ) { $ r = $ s % 10 ; $ s = ( int ) ( $ s / 10 ) ; $ freq [ $ r ] += 1 ; } $ xor = 0 ; for ( $ i = 0 ; $ i < 10 ; $ i ++ ) $ xor = $ xor ^ $ freq [ $ i ] ; if ( $ xor == 0 ) return true ; else return false ; } $ s = 122233 ; if ( check ( $ s ) ) print ( " Yes " ) ; else print ( " No " ) ; ? >
Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K | Function to print N lines ; Iterate N times to print N lines ; Driver Code
< ? php function printLines ( $ n , $ k ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { echo ( $ k * ( 6 * $ i + 1 ) ) ; echo ( " ▁ " ) ; echo ( $ k * ( 6 * $ i + 2 ) ) ; echo ( " ▁ " ) ; echo ( $ k * ( 6 * $ i + 3 ) ) ; echo ( " ▁ " ) ; echo ( $ k * ( 6 * $ i + 5 ) ) ; echo ( " STRNEWLINE " ) ; } } $ n = 2 ; $ k = 2 ; printLines ( $ n , $ k ) ; ? >
Sum of first n term of Series 3 , 5 , 9 , 17 , 33. ... | PHP program to find sum of first n terms ; Sn = n * ( 4 * n * n + 6 * n - 1 ) / 3 ; number of terms to be included in sum ; find the Sn
< ? php function calculateSum ( $ n ) { return ( pow ( 2 , $ n + 1 ) + $ n - 2 ) ; } $ n = 4 ; echo " Sum = " ? >
Ways to write N as sum of two or more positive integers | Set | Function to find the number of partitions of N ; Base case ; Driver Code
< ? php function partitions ( $ n ) { $ p = array_fill ( 0 , $ n + 1 , 0 ) ; $ p [ 0 ] = 1 ; for ( $ i = 1 ; $ i < $ n + 1 ; $ i ++ ) { $ k = 1 ; while ( ( $ k * ( 3 * $ k - 1 ) ) / 2 <= $ i ) { $ p [ $ i ] += ( ( $ k % 2 ? 1 : -1 ) * $ p [ $ i - ( $ k * ( 3 * $ k - 1 ) ) / 2 ] ) ; if ( $ k > 0 ) $ k *= -1 ; else $ k = 1 - $ k ; } } return $ p [ $ n ] ; } $ N = 20 ; print ( partitions ( $ N ) ) ; ? >
Counts paths from a point to reach Origin | Recursive function to count number of paths ; If we reach bottom or top left , we are have only one way to reach ( 0 , 0 ) ; Else count sum of both ways ; Driver Code
< ? php function countPaths ( $ n , $ m ) { if ( $ n == 0 $ m == 0 ) return 1 ; return ( countPaths ( $ n - 1 , $ m ) + countPaths ( $ n , $ m - 1 ) ) ; } $ n = 3 ; $ m = 2 ; echo " ▁ Number ▁ of ▁ Paths ▁ " , countPaths ( $ n , $ m ) ; ? >
Gold Mine Problem | Returns maximum amount of gold that can be collected when journey started from first column and moves allowed are right , right - up and right - down ; Create a table for storing intermediate results and initialize all cells to 0. The first row of goldMineTable gives the maximum gold that the miner can collect when starts that row ; Gold collected on going to the cell on the rigth ( -> ) ; Gold collected on going to the cell to right up ( / ) ; Gold collected on going to the cell to right down ( \ ) ; Max gold collected from taking either of the above 3 paths ; The max amount of gold collected will be the max value in first column of all rows ; Driver code
< ? php function getMaxGold ( $ gold , $ m , $ n ) { $ MAX = 100 ; $ goldTable = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ goldTable [ $ i ] [ $ j ] = 0 ; for ( $ col = $ n - 1 ; $ col >= 0 ; $ col -- ) { for ( $ row = 0 ; $ row < $ m ; $ row ++ ) { if ( $ col == $ n - 1 ) $ right = 0 ; else $ right = $ goldTable [ $ row ] [ $ col + 1 ] ; if ( $ row == 0 or $ col == $ n - 1 ) $ right_up = 0 ; else $ right_up = $ goldTable [ $ row - 1 ] [ $ col + 1 ] ; if ( $ row == $ m - 1 or $ col == $ n - 1 ) $ right_down = 0 ; else $ right_down = $ goldTable [ $ row + 1 ] [ $ col + 1 ] ; $ goldTable [ $ row ] [ $ col ] = $ gold [ $ row ] [ $ col ] + max ( $ right , $ right_up , $ right_down ) ; } } $ res = $ goldTable [ 0 ] [ 0 ] ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) $ res = max ( $ res , $ goldTable [ $ i ] [ 0 ] ) ; return $ res ; } $ gold = array ( array ( 1 , 3 , 1 , 5 ) , array ( 2 , 2 , 4 , 1 ) , array ( 5 , 0 , 2 , 3 ) , array ( 0 , 6 , 1 , 2 ) ) ; $ m = 4 ; $ n = 4 ; echo getMaxGold ( $ gold , $ m , $ n ) ; ? >
Find minimum adjustment cost of an array | PHP program to find minimum adjustment cost of an array ; Function to find minimum adjustment cost of an array ; dp [ i ] [ j ] stores minimal adjustment cost on changing A [ i ] to j ; handle first element of array separately ; do for rest elements of the array ; replace A [ i ] to j and calculate minimal adjustment cost dp [ i ] [ j ] ; initialize minimal adjustment cost to INT_MAX ; consider all k such that k >= max ( j - target , 0 ) and k <= min ( M , j + target ) and take minimum ; return minimum value from last row of dp table ; Driver Code
< ? php $ M = 100 ; function minAdjustmentCost ( $ A , $ n , $ target ) { global $ M ; $ dp = array ( array ( ) ) ; for ( $ j = 0 ; $ j <= $ M ; $ j ++ ) $ dp [ 0 ] [ $ j ] = abs ( $ j - $ A [ 0 ] ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ M ; $ j ++ ) { $ dp [ $ i ] [ $ j ] = PHP_INT_MAX ; for ( $ k = max ( $ j - $ target , 0 ) ; $ k <= min ( $ M , $ j + $ target ) ; $ k ++ ) $ dp [ $ i ] [ $ j ] = min ( $ dp [ $ i ] [ $ j ] , $ dp [ $ i - 1 ] [ $ k ] + abs ( $ A [ $ i ] - $ j ) ) ; } } $ res = PHP_INT_MAX ; for ( $ j = 0 ; $ j <= $ M ; $ j ++ ) $ res = min ( $ res , $ dp [ $ n - 1 ] [ $ j ] ) ; return $ res ; } $ arr = array ( 55 , 77 , 52 , 61 , 39 , 6 , 25 , 60 , 49 , 47 ) ; $ n = count ( $ arr ) ; $ target = 10 ; echo " Minimum ▁ adjustment ▁ cost ▁ is ▁ " , minAdjustmentCost ( $ arr , $ n , $ target ) ; ? >
Count occurrences of a character in a repeated string | Function to count the character ' a ' ; atleast k repetition are required ; if n is not the multiple of the string size check for the remaining repeating character . ; Driver code
< ? php function countChar ( $ str , $ x ) { $ count = 0 ; $ n = 10 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) if ( $ str [ $ i ] == $ x ) $ count ++ ; $ repetitions = ( int ) ( $ n / strlen ( $ str ) ) ; $ count = $ count * $ repetitions ; for ( $ i = 0 ; $ i < $ n % strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i ] == $ x ) $ count ++ ; } return $ count ; } $ str = " abcac " ; echo countChar ( $ str , ' a ' ) ; ? >
Check if a string has m consecutive 1 ' s ▁ or ▁ 0' s | Function that checks if the binary string contains m consecutive 1 ' s ▁ or ▁ 0' s ; length of binary string ; counts zeros ; counts 1 's ; count consecutive 0 's ; count consecutive 1 's ; Driver Code ; function call
< ? php function check ( $ s , $ m ) { $ l = count ( $ s ) ; $ c1 = 0 ; $ c2 = 0 ; for ( $ i = 0 ; $ i <= $ l ; $ i ++ ) { if ( $ s [ $ i ] == '0' ) { $ c2 = 0 ; $ c1 ++ ; } else { $ c1 = 0 ; $ c2 ++ ; } if ( $ c1 == $ m or $ c2 == $ m ) return true ; } return false ; } $ s = "001001" ; $ m = 2 ; if ( check ( $ s , $ m ) ) echo " YES " ; else echo " NO " ; ? >
Product of nodes at k | Function to find product of digits of elements at k - th level ; $product = 1 ; Initialize result ; increasing level number ; decreasing level number ; check if current level is the desired level or not ; required product ; Driver Code
< ? php function productAtKthLevel ( $ tree , $ k ) { $ level = -1 ; $ n = strlen ( $ tree ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ tree [ $ i ] == ' ( ' ) $ level ++ ; else if ( $ tree [ $ i ] == ' ) ' ) $ level -- ; else { if ( $ level == $ k ) $ product *= ( ord ( $ tree [ $ i ] ) - ord ( '0' ) ) ; } } return $ product ; } $ tree = " ( 0(5(6 ( ) ( ) ) ( 4 ( ) (9 ( ) ( ) ) ) ) ( 7(1 ( ) ( ) ) ( 3 ( ) ( ) ) ) ) " ; $ k = 2 ; echo productAtKthLevel ( $ tree , $ k ) ; ? >
Program to check for ISBN | PHP program to check if a given ISBN is valid or not . ; length must be 10 ; Computing weighted sum of first 9 digits ; Checking last digit . ; If last digit is ' X ' , add 10 to sum , else add its value . ; Return true if weighted sum of digits is divisible by 11. ; Driver code
< ? php function isValidISBN ( $ isbn ) { $ n = strlen ( $ isbn ) ; if ( $ n != 10 ) return -1 ; $ sum = 0 ; for ( $ i = 0 ; $ i < 9 ; $ i ++ ) { $ digit = $ isbn [ $ i ] - '0' ; if ( 0 > $ digit 9 < $ digit ) return -1 ; $ sum += ( $ digit * ( 10 - $ i ) ) ; } $ last = $ isbn [ 9 ] ; if ( $ last != ' X ' && ( $ last < '0' $ last > '9' ) ) return -1 ; $ sum += ( ( $ last == ' X ' ) ? 10 : ( $ last - '0' ) ) ; return ( $ sum % 11 == 0 ) ; } $ isbn = "007462542X " ; if ( isValidISBN ( $ isbn ) ) echo " Valid " ; else echo " Invalid " ; ? >
Program to find the angles of a quadrilateral | Driver code ; according to formula derived above ; print all the angles
< ? php $ d = 10 ; $ a = ( 360 - ( 6 * $ d ) ) / 4 ; echo $ a , " , ▁ " , $ a + $ d , " , ▁ " , $ a + ( 2 * $ d ) , " , ▁ " , $ a + ( 3 * $ d ) ; ? >
Distance between two parallel Planes in 3 | Function to find distance ; Driver Code
< ? php function distance ( $ a1 , $ b1 , $ c1 , $ d1 , $ a2 , $ b2 , $ c2 , $ d2 ) { if ( $ a1 / $ a2 == $ b1 / $ b2 && $ b1 / $ b2 == $ c1 / $ c2 ) { $ x1 = $ y1 = 0 ; $ z1 = - $ d1 / $ c1 ; $ d = abs ( ( $ c2 * $ z1 + $ d2 ) ) / ( sqrt ( $ a2 * $ a2 + $ b2 * $ b2 + $ c2 * $ c2 ) ) ; echo " Perpendicular ▁ distance ▁ is ▁ " , $ d ; } else echo " Planes ▁ are ▁ not ▁ parallel " ; } $ a1 = 1 ; $ b1 = 2 ; $ c1 = -1 ; $ d1 = 1 ; $ a2 = 3 ; $ b2 = 6 ; $ c2 = -3 ; $ d2 = -4 ; distance ( $ a1 , $ b1 , $ c1 , $ d1 , $ a2 , $ b2 , $ c2 , $ d2 ) ; ? >
Form minimum number from given sequence | Prints the minimum number that can be formed from input sequence of I ' s ▁ and ▁ D ' s ; Initialize current_max ( to make sure that we don 't use repeated  character ; Initialize last_entry ( Keeps track for last printed digit ) ; Iterate over input array ; Initialize ' noOfNextD ' to get count of next D 's available ; If letter is ' I ' Calculate number of next consecutive D 's  available ; If ' I ' is first letter , print incremented sequence from 1 ; Set max digit reached ; If not first letter Get next digit to print ; Print digit for I ; For all next consecutive ' D ' print decremented sequence ; If letter is ' D ' ; If ' D ' is first letter in sequence . Find number of Next D 's available ; Calculate first digit to print based on number of consecutive D 's ; Print twice for the first time ; Store last entry ; If current ' D ' is not first letter Decrement last_entry ; Driver Code
< ? php function PrintMinNumberForPattern ( $ arr ) { $ curr_max = 0 ; $ last_entry = 0 ; $ j ; for ( $ i = 0 ; $ i < strlen ( $ arr ) ; $ i ++ ) { $ noOfNextD = 0 ; switch ( $ arr [ $ i ] ) { case ' I ' : $ j = $ i + 1 ; while ( $ arr [ $ j ] == ' D ' && $ j < strlen ( $ arr ) ) { $ noOfNextD ++ ; $ j ++ ; } if ( $ i == 0 ) { $ curr_max = $ noOfNextD + 2 ; echo " ▁ " , ++ $ last_entry ; echo " ▁ " , $ curr_max ; $ last_entry = $ curr_max ; } else { $ curr_max = $ curr_max + $ noOfNextD + 1 ; $ last_entry = $ curr_max ; echo " ▁ " , $ last_entry ; } for ( $ k = 0 ; $ k < $ noOfNextD ; $ k ++ ) { echo " ▁ " , -- $ last_entry ; $ i ++ ; } break ; case ' D ' : if ( $ i == 0 ) { $ j = $ i + 1 ; while ( ( $ arr [ $ j ] == ' D ' ) && ( $ j < strlen ( $ arr ) ) ) { $ noOfNextD ++ ; $ j ++ ; } $ curr_max = $ noOfNextD + 2 ; echo " ▁ " , $ curr_max , " ▁ " , $ curr_max - 1 ; $ last_entry = $ curr_max - 1 ; } else { echo " ▁ " , $ last_entry - 1 ; $ last_entry -- ; } break ; } } echo " STRNEWLINE " ; } PrintMinNumberForPattern ( " IDID " ) ; PrintMinNumberForPattern ( " I " ) ; PrintMinNumberForPattern ( " DD " ) ; PrintMinNumberForPattern ( " II " ) ; PrintMinNumberForPattern ( " DIDI " ) ; PrintMinNumberForPattern ( " IIDDD " ) ; PrintMinNumberForPattern ( " DDIDDIID " ) ; ? >
Check if the first and last digit of the smallest number forms a prime | function to check prime ; Function to generate smallest possible number with given digits ; Declare a hash array of size 10 and initialize all the elements to zero ; store the number of occurrences of the digits in the given array into the hash table ; Traverse the hash in ascending order to print the required number ; Print the number of times a digits occurs ; extracting the first digit ; extracting the last digit ; printing the prime combinations ; Driver Code
< ? php function isPrime ( $ n ) { $ c = 0 ; for ( $ i = 1 ; $ i < $ n / 2 ; $ i ++ ) { if ( $ n % $ i == 0 ) $ c ++ ; } if ( $ c == 1 ) return 1 ; else return 0 ; } function findMinNum ( $ arr , $ n ) { $ first = 0 ; $ last = 0 ; $ num ; $ rev ; $ i ; $ hash = array_fill ( 0 , 20 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ hash [ $ arr [ $ i ] ] ++ ; } echo " Minimum ▁ number : ▁ " ; for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { for ( $ j = 0 ; $ j < $ hash [ $ i ] ; $ j ++ ) echo $ i ; } for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { if ( $ hash [ $ i ] != 0 ) { $ first = $ i ; break ; } } for ( $ i = 9 ; $ i >= 0 ; $ i -- ) { if ( $ hash [ $ i ] != 0 ) { $ last = $ i ; break ; } } $ num = $ first * 10 + $ last ; $ rev = $ last * 10 + $ first ; echo " Prime combinations : " if ( isPrime ( $ num ) && isPrime ( $ rev ) ) echo $ num . " ▁ " . $ rev ; else if ( isPrime ( $ num ) ) echo $ num ; else if ( isPrime ( $ rev ) ) echo $ rev ; else echo " No ▁ combinations ▁ exist " ; } $ arr = array ( 1 , 2 , 4 , 7 , 8 ) ; findMinNum ( $ arr , 5 ) ; ? >
Finding a Non Transitive Co | Function to return gcd of a and b ; function to check for gcd ; a and b are coprime if their gcd is 1. ; Checks if any possible triplet ( a , b , c ) satifying the condition that ( a , b ) is coprime , ( b , c ) is coprime but ( a , c ) isnt ; Generate and check for all possible triplets between L and R ; if we find any such triplets set flag to true ; flag = True indicates that a pair exists between L and R ; Driver Code ; finding possible Triplet between 2 and 10 ; finding possible Triplet between 23 and 46
< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function coprime ( $ a , $ b ) { return ( gcd ( $ a , $ b ) == 1 ) ; } function possibleTripletInRange ( $ L , $ R ) { $ flag = false ; $ possibleA ; $ possibleB ; $ possibleC ; for ( $ a = $ L ; $ a <= $ R ; $ a ++ ) { for ( $ b = $ a + 1 ; $ b <= $ R ; $ b ++ ) { for ( $ c = $ b + 1 ; $ c <= $ R ; $ c ++ ) { if ( coprime ( $ a , $ b ) && coprime ( $ b , $ c ) && ! coprime ( $ a , $ c ) ) { $ flag = true ; $ possibleA = $ a ; $ possibleB = $ b ; $ possibleC = $ c ; break ; } } } } if ( $ flag == true ) { echo " ( " , $ possibleA , " , ▁ " , $ possibleB , " , ▁ " , $ possibleC , " ) " , " ▁ is ▁ one ▁ such ▁ possible ▁ triplet ▁ between ▁ " , $ L , " ▁ and ▁ " , $ R , " STRNEWLINE " ; } else { echo " No ▁ Such ▁ Triplet ▁ exists ▁ between ▁ " , $ L , " ▁ and ▁ " , $ R , " STRNEWLINE " ; } } $ L ; $ R ; $ L = 2 ; $ R = 10 ; possibleTripletInRange ( $ L , $ R ) ; $ L = 23 ; $ R = 46 ; possibleTripletInRange ( $ L , $ R ) ; ? >
Reach A and B by multiplying them with K and K ^ 2 at every step | function to check is it is possible to reach A and B starting from 1 and 1 ; find the cuberoot of the number ; divide the number by cuberoot ; if it is a perfect cuberoot and divides a and b ; Driver Code
< ? php function possibleToReach ( $ a , $ b ) { $ c = ( $ a * $ b ) ; $ re1 = $ a / $ c ; $ re2 = $ b / $ c ; if ( ( $ re1 * $ re1 * $ re2 == $ a ) && ( $ re2 * $ re2 * $ re1 == $ b ) ) return 1 ; else return -1 ; } $ A = 60 ; $ B = 450 ; if ( possibleToReach ( $ A , $ B ) ) echo " yes " ; else echo " no " ; ? >
Undulating numbers | PHP program to check whether a number is undulating or not ; Considering the definition with restriction that there should be at least 3 digits ; Check if all alternate digits are same or not . ; Driver code
< ? php function isUndulating ( $ n ) { if ( strlen ( $ n ) <= 2 ) return false ; for ( $ i = 2 ; $ i < strlen ( $ n ) ; $ i ++ ) if ( $ n [ $ i - 2 ] != $ n [ $ i ] ) false ; return true ; } $ n = "1212121" ; if ( isUndulating ( $ n ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >
Program to find the sum of a Series ( 1 * 1 ) + ( 2 * 2 ) + ( 3 * 3 ) + ( 4 * 4 ) + ( 5 * 5 ) + ... + ( n * n ) | Function to calculate the following series ; Driver Code
< ? php function Series ( $ n ) { $ i ; $ sums = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sums += ( $ i * $ i ) ; return $ sums ; } $ n = 3 ; $ res = Series ( $ n ) ; echo ( $ res ) ; ? >
Sum of all numbers divisible by 6 in a given range | function to calculate the sum of all numbers divisible by 6 in range L - R . . ; no of multiples of 6 upto r ; no of multiples of 6 upto l - 1 ; summation of all multiples of 6 upto r ; summation of all multiples of 6 upto l - 1 ; returns the answer ; Driver Code
< ? php function sum ( $ L , $ R ) { $ p = intval ( $ R / 6 ) ; $ q = intval ( ( $ L - 1 ) / 6 ) ; $ sumR = intval ( 3 * ( $ p * ( $ p + 1 ) ) ) ; $ sumL = intval ( ( $ q * ( $ q + 1 ) ) * 3 ) ; return $ sumR - $ sumL ; } $ L = 1 ; $ R = 20 ; echo sum ( $ L , $ R ) ; ? >
Largest smaller number possible using only one swap operation | PHP program to find the largest smaller number by swapping one digit . Returns largest possible number with one swap such that the number is smaller than str . It is assumed that there are leading 0 s . ; Traverse from right until we find a digit which is greater than its next digit . For example , in 34125 , our index is 4. ; We can also use binary search here as digits after index are sorted in increasing order . Find the biggest digit in the right of arr [ index ] which is smaller than arr [ index ] ; If index is - 1 i . e . digits are in increasing order . ; Swap both values ; swap ( str [ index ] , str [ smallGreatDgt ] ) ; ; Driver code
< ? php function prevNum ( $ str ) { $ len = strlen ( $ str ) ; $ index = -1 ; for ( $ i = $ len - 2 ; $ i >= 0 ; $ i -- ) { if ( $ str [ $ i ] > $ str [ $ i + 1 ] ) { $ index = $ i ; break ; } } $ smallGreatDgt = -1 ; for ( $ i = $ len - 1 ; $ i > $ index ; $ i -- ) { if ( $ str [ $ i ] < $ str [ $ index ] ) { if ( $ smallGreatDgt == -1 ) $ smallGreatDgt = $ i ; else if ( $ str [ $ i ] >= $ str [ $ smallGreatDgt ] ) $ smallGreatDgt = $ i ; } } if ( $ index == -1 ) return " - 1" ; if ( $ smallGreatDgt != -1 ) { list ( $ str [ $ index ] , $ str [ $ smallGreatDgt ] ) = array ( $ str [ $ smallGreatDgt ] , $ str [ $ index ] ) ; return $ str ; } return " - 1" ; } $ str = "34125" ; echo prevNum ( $ str ) ; ? >
Sgn value of a polynomial | returns value of poly [ 0 ] x ( n - 1 ) + poly [ 1 ] x ( n - 2 ) + . . + poly [ n - 1 ] ; Initialize result ; Evaluate value of polynomial using Horner 's method ; Returns sign value of polynomial ; Let us evaluate value of 2 x3 - 6 x2 + 2 x - 1 for x = 3
< ? php function horner ( $ poly , $ n , $ x ) { $ result = $ poly [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ result = $ result * $ x + $ poly [ $ i ] ; return $ result ; } function findSign ( $ poly , $ n , $ x ) { $ result = horner ( $ poly , $ n , $ x ) ; if ( $ result > 0 ) return 1 ; else if ( $ result < 0 ) return -1 ; return 0 ; } $ poly = array ( 2 , -6 , 2 , -1 ) ; $ x = 3 ; $ n = count ( $ poly ) ; echo " Sign ▁ of ▁ polynomial ▁ is ▁ " , findSign ( $ poly , $ n , $ x ) ; ? >
Insert minimum number in array so that sum of array becomes prime | PHP program to find minimum number to insert in array so their sum is prime ; function to calculate primes using sieve of eratosthenes ; Find prime number greater than a number ; To return prime number greater than n ; check if num is prime ; increment num ; To find number to be added so sum of array is prime ; call sieveOfEratostheneses to calculate primes ; To find sum of array elements ; To find prime number greater then sum ; Return difference of sum and num ; Driver Code
< ? php $ MAX = 100005 ; function sieveOfEratostheneses ( ) { $ isPrime = array_fill ( true , $ MAX , NULL ) ; $ isPrime [ 1 ] = false ; for ( $ i = 2 ; $ i * $ i < $ MAX ; $ i ++ ) { if ( $ isPrime [ $ i ] ) { for ( $ j = 2 * $ i ; $ j < $ MAX ; $ j += $ i ) $ isPrime [ $ j ] = false ; } } } function findPrime ( $ n ) { $ num = $ n + 1 ; while ( $ num ) { if ( $ isPrime [ $ num ] ) return $ num ; $ num = $ num + 1 ; } return 0 ; } function minNumber ( & $ arr , $ n ) { sieveOfEratostheneses ( ) ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] ; if ( $ isPrime [ $ sum ] ) return 0 ; $ num = findPrime ( $ sum ) ; return $ num - $ sum ; } $ arr = array ( 2 , 4 , 6 , 8 , 12 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo minNumber ( $ arr , $ n ) ; return 0 ; ? >