codeparrot/xlcost-text-to-code huggingface dataset:
Find the other number when LCM and HCF given | Function that will calculates the zeroes at the end ; Driver code ; Calling function .
< ? php function otherNumber ( $ A , $ Lcm , $ Hcf ) { return ( $ Lcm * $ Hcf ) / $ A ; } $ A = 8 ; $ Lcm = 8 ; $ Hcf = 1 ; $ result = otherNumber ( $ A , $ Lcm , $ Hcf ) ; echo " B = "
Overall percentage change from successive changes | PHP implementation of above approach ; Calculate successive change of 1 st 2 change ; Calculate successive change for rest of the value ; Driver code ; Calling function
< ? php function successiveChange ( $ arr , $ N ) { $ result = 0 ; $ var1 = $ arr [ 0 ] ; $ var2 = $ arr [ 1 ] ; $ result = $ var1 + $ var2 + ( ( $ var1 * $ var2 ) / 100 ) ; for ( $ i = 2 ; $ i < $ N ; $ i ++ ) $ result = $ result + $ arr [ $ i ] + ( ( $ result * $ arr [ $ i ] ) / 100 ) ; return $ result ; } $ arr = array ( 10 , 20 , 30 , 10 ) ; $ N = count ( $ arr ) ; $ result = successiveChange ( $ arr , $ N ) ; echo " Percentage ▁ change ▁ is ▁ = ▁ " , $ result , " ▁ % " ; ? >
Minimum numbers ( smaller than or equal to N ) with sum S | Function to find the minimum numbers required to get to S ; Driver Code
< ? php function minimumNumbers ( $ n , $ s ) { if ( $ s % $ n ) return round ( $ s / $ n + 1 ) ; else return round ( $ s / $ n ) ; } $ n = 5 ; $ s = 11 ; echo minimumNumbers ( $ n , $ s ) ; ? >
Sum of multiples of A and B less than N | PHP program to find the sum of all multiples of A and B below N ; Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of A and B below N ; Since , we need the sum of multiples less than N ; common factors of A and B ; Driver code
< ? php function __gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return __gcd ( $ b , $ a % $ b ) ; } function sumAP ( $ n , $ d ) { $ n = ( int ) ( $ n / $ d ) ; return ( $ n ) * ( 1 + $ n ) * $ d / 2 ; } function sumMultiples ( $ A , $ B , $ n ) { $ n -- ; $ common = ( int ) ( ( $ A * $ B ) / __gcd ( $ A , $ B ) ) ; return sumAP ( $ n , $ A ) + sumAP ( $ n , $ B ) - sumAP ( $ n , $ common ) ; } $ n = 100 ; $ A = 5 ; $ B = 10 ; echo " Sum = " ? >
Check if a prime number can be expressed as sum of two Prime Numbers | Function to check whether a number is prime or not ; Function to check if a prime number can be expressed as sum of two Prime Numbers ; if the number is prime , and number - 2 is also prime ; Driver code
< ? php function isPrime ( $ n ) { if ( $ n <= 1 ) return false ; for ( $ i = 2 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) return false ; } return true ; } function isPossible ( $ N ) { if ( isPrime ( $ N ) && isPrime ( $ N - 2 ) ) return true ; else return false ; } $ n = 13 ; if ( isPossible ( $ n ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >
Largest sub | function to return the length of the largest sub - array of an array every element of whose is a perfect square ; if both a and b are equal then arr [ i ] is a perfect square ; Driver code
< ? php function contiguousPerfectSquare ( $ arr , $ n ) { $ current_length = 0 ; $ max_length = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ b = ( float ) sqrt ( $ arr [ $ i ] ) ; $ a = ( int ) $ b ; if ( $ a == $ b ) $ current_length = $ current_length + 1 ; else $ current_length = 0 ; $ max_length = max ( $ max_length , $ current_length ) ; } return $ max_length ; } $ arr = array ( 9 , 75 , 4 , 64 , 121 , 25 ) ; $ n = sizeof ( $ arr ) ; echo contiguousPerfectSquare ( $ arr , $ n ) ; ? >
Count pairs of numbers from 1 to N with Product divisible by their Sum | Function to count pairs ; variable to store count ; Generate all possible pairs such that 1 <= x < y < n ; Driver code
< ? php function countPairs ( $ n ) { $ count = 0 ; for ( $ x = 1 ; $ x < $ n ; $ x ++ ) { for ( $ y = $ x + 1 ; $ y <= $ n ; $ y ++ ) { if ( ( $ y * $ x ) % ( $ y + $ x ) == 0 ) $ count ++ ; } } return $ count ; } $ n = 15 ; echo countPairs ( $ n ) ; ? >
Find the index of the left pointer after possible moves in the array | Function that returns the index of the left pointer ; there 's only one element  in the array ; initially both are at end ; Driver code
< ? php function getIndex ( $ a , $ n ) { if ( $ n == 1 ) return 0 ; $ ptrL = 0 ; $ ptrR = $ n - 1 ; $ sumL = $ a [ 0 ] ; $ sumR = $ a [ $ n - 1 ] ; while ( $ ptrR - $ ptrL > 1 ) { if ( $ sumL < $ sumR ) { $ ptrL ++ ; $ sumL += $ a [ $ ptrL ] ; } else if ( $ sumL > $ sumR ) { $ ptrR -- ; $ sumR += $ a [ $ ptrR ] ; } else { break ; } } return $ ptrL ; } $ a = array ( 2 , 7 , 9 , 8 , 7 ) ; $ n = count ( $ a ) ; echo getIndex ( $ a , $ n ) ; ? >
Find the position of the last removed element from the array | Function to find the original position of the element which will be removed last ; take ceil of every number ; Since position is index + 1 ; Driver code
< ? php function getPosition ( $ a , $ n , $ m ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ a [ $ i ] = ( $ a [ $ i ] / $ m + ( $ a [ $ i ] % $ m != 0 ) ) ; } $ ans = -1 ; $ max = -1 ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { if ( $ max < $ a [ $ i ] ) { $ max = $ a [ $ i ] ; $ ans = $ i ; } } return $ ans + 1 ; } $ a = array ( 2 , 5 , 4 ) ; $ n = sizeof ( $ a ) ; $ m = 2 ; echo getPosition ( $ a , $ n , $ m ) ; ? >
Find the value of f ( n ) / f ( r ) * f ( n | Function to find value of given F ( n ) ; iterate over n ; calculate result ; return the result ; Driver code
< ? php function calcFunction ( $ n , $ r ) { $ finalDenominator = 1 ; $ mx = max ( $ r , $ n - $ r ) ; for ( $ i = $ mx + 1 ; $ i <= $ n ; $ i ++ ) { $ denominator = pow ( $ i , $ i ) ; $ numerator = pow ( $ i - $ mx , $ i - $ mx ) ; $ finalDenominator = ( $ finalDenominator * $ denominator ) / $ numerator ; } return $ finalDenominator ; } $ n = 6 ; $ r = 2 ; echo "1 / " , calcFunction ( $ n , $ r ) ; ? >
Find the number after successive division | Function to find the number ; Driver Code
< ? php function findNum ( $ div , $ rem , $ N ) { $ num = $ rem [ $ N - 1 ] ; for ( $ i = $ N - 2 ; $ i >= 0 ; $ i -- ) { $ num = $ num * $ div [ $ i ] + $ rem [ $ i ] ; } return $ num ; } $ div = array ( 8 , 3 ) ; $ rem = array ( 2 , 2 ) ; $ N = sizeof ( $ div ) ; echo findNum ( $ div , $ rem , $ N ) ; ? >
Program to find the profit or loss when CP of N items is equal to SP of M items | Function to calculate Profit or loss ; Driver Code
< ? php function profitLoss ( $ N , $ M ) { if ( $ N == $ M ) echo " No ▁ Profit ▁ nor ▁ Loss " ; else { $ result = 0.0 ; $ result = ( abs ( $ N - $ M ) ) / $ M ; if ( $ N - $ M < 0 ) echo " Loss ▁ = ▁ - " , $ result * 100 , " % " ; else echo " Profit ▁ = ▁ " , $ result * 100 , " % " ; } } $ N = 8 ; $ M = 9 ; profitLoss ( $ N , $ M ) ; ? >
Count pieces of circle after N cuts | Function to find number of pieces of circle after N cuts ; Driver Code
< ? php function countPieces ( $ N ) { return 2 * $ N ; } $ N = 100 ; echo countPieces ( $ N ) ; ? >
Sum of all the multiples of 3 and 7 below N | Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of 3 and 7 below N ; Since , we need the sum of multiples less than N ; Driver code
< ? php function sumAP ( $ n , $ d ) { $ n = ( int ) ( $ n / $ d ) ; return ( $ n ) * ( 1 + $ n ) * ( $ d / 2 ) ; } function sumMultiples ( $ n ) { $ n -- ; return sumAP ( $ n , 3 ) + sumAP ( $ n , 7 ) - sumAP ( $ n , 21 ) ; } $ n = 24 ; echo sumMultiples ( $ n ) ; ? >
Check whether product of digits at even places is divisible by sum of digits at odd place of a number | Below function checks whether product of digits at even places is divisible by sum of digits at odd places ; if size is even ; if size is odd ; Driver code
< ? php function productSumDivisible ( $ n , $ size ) { $ sum = 0 ; $ product = 1 ; while ( $ n > 0 ) { if ( $ size % 2 == 0 ) { $ product *= $ n % 10 ; } else { $ sum += $ n % 10 ; } $ n = $ n / 10 ; $ size -- ; } if ( $ product % $ sum == 0 ) return true ; return false ; } $ n = 1234 ; $ len = 4 ; if ( productSumDivisible ( $ n , $ len ) ) echo " TRUE " ; else echo " FALSE " ; ? >
GCD of a number raised to some power and another number | PHP program to find GCD of a ^ n and b ; Returns GCD of a ^ n and b . ; Driver code
< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function powGCD ( $ a , $ n , $ b ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ a = $ a * $ a ; return gcd ( $ a , $ b ) ; } $ a = 10 ; $ b = 5 ; $ n = 2 ; echo powGCD ( $ a , $ n , $ b ) ; ? >
Position after taking N steps to the right and left in an alternate manner | Function to return the last destination ; Driver Code
< ? php function lastCoordinate ( $ n , $ a , $ b ) { return ( ( $ n + 1 ) / 2 ) * $ a - ( int ) ( $ n / 2 ) * $ b ; } $ n = 3 ; $ a = 5 ; $ b = 2 ; echo lastCoordinate ( $ n , $ a , $ b ) ; ? >
Smallest number greater than or equal to N divisible by K | Function to find the smallest number greater than or equal to N that is divisible by k ; Driver code
< ? php function findNum ( $ N , $ K ) { $ rem = ( $ N + $ K ) % $ K ; if ( $ rem == 0 ) return $ N ; else return $ N + $ K - $ rem ; } $ N = 45 ; $ K = 6 ; echo " Smallest ▁ number ▁ greater ▁ than ▁ " . " or ▁ equal ▁ to ▁ " , $ N ; echo " that is divisible by " ▁ , ▁ $ K ▁ , STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL " is " ? >
Sum and Product of digits in a number that divide the number | Print the sum and product of digits that divides the number . ; Fetching each digit of the number ; Checking if digit is greater than 0 and can divides n . ; Driver code
< ? php function countDigit ( $ n ) { $ temp = $ n ; $ sum = 0 ; $ product = 1 ; while ( $ temp != 0 ) { $ d = $ temp % 10 ; $ temp = ( int ) ( $ temp / 10 ) ; if ( $ d > 0 && $ n % $ d == 0 ) { $ sum += $ d ; $ product *= $ d ; } } echo " Sum = " . $ sum ; STRNEWLINE TABSYMBOL echo ▁ " Product = " } $ n = 1012 ; countDigit ( $ n ) ; ? >
Largest number smaller than or equal to N divisible by K | Function to find the largest number smaller than or equal to N that is divisible by k ; Driver code
< ? php function findNum ( $ N , $ K ) { $ rem = $ N % $ K ; if ( $ rem == 0 ) return $ N ; else return $ N - $ rem ; } $ N = 45 ; $ K = 6 ; echo " Largest ▁ number ▁ smaller ▁ than ▁ or ▁ equal ▁ to ▁ " , $ N , " that is divisible by " , ▁ $ K , ▁ " is " findNum ( $ N , $ K ) ; ? >
Check if any permutation of a number is divisible by 3 and is Palindromic | Function to check if any permutation of a number is divisible by 3 and is Palindromic ; Hash array to store frequency of digits of n ; traverse the digits of integer and store their frequency ; Calculate the sum of digits simultaneously ; Check if number is not divisible by 3 ; If more than one digits have odd frequency , palindromic permutation not possible ; Driver Code
< ? php function isDivisiblePalindrome ( $ n ) { $ hash = array ( 0 ) ; $ digitSum = 0 ; while ( $ n ) { $ digitSum += $ n % 10 ; $ hash ++ ; $ n /= 10 ; } if ( $ digitSum % 3 != 0 ) return false ; $ oddCount = 0 ; for ( $ i = 0 ; $ i < 10 ; $ i ++ ) { if ( $ hash % 2 != 0 ) $ oddCount ++ ; } if ( $ oddCount > 1 ) return true ; else return false ; } $ n = 34734 ; if ( isDivisiblePalindrome ( $ n ) ) echo " True " ; else echo " False " ; # This  Code is contributed by Tushill. NEW_LINE ? >
Check whether product of digits at even places of a number is divisible by K | Below function checks whether product of digits at even places is divisible by K ; if position is even ; Driver code
< ? php function productDivisible ( $ n , $ k ) { $ product = 1 ; $ position = 1 ; while ( $ n > 0 ) { if ( $ position % 2 == 0 ) $ product *= $ n % 10 ; $ n = ( int ) ( $ n / 10 ) ; $ position ++ ; } if ( $ product % $ k == 0 ) return true ; return false ; } $ n = 321922 ; $ k = 3 ; if ( productDivisible ( $ n , $ k ) ) echo " YES " ; else echo " NO " ; ? >
Permutations of n things taken r at a time with k things together | Function to find factorial of a number ; Function to calculate p ( n , r ) ; Function to find the number of permutations of n different things taken r at a time with k things grouped together ; Driver code
< ? php function factorial ( $ n ) { $ fact = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ fact = $ fact * $ i ; return $ fact ; } function npr ( $ n , $ r ) { $ pnr = factorial ( $ n ) / factorial ( $ n - $ r ) ; return $ pnr ; } function countPermutations ( $ n , $ r , $ k ) { return factorial ( $ k ) * ( $ r - $ k + 1 ) * npr ( $ n - $ k , $ r - $ k ) ; } $ n = 8 ; $ r = 5 ; $ k = 2 ; echo countPermutations ( $ n , $ r , $ k ) ; ? >
Greatest Integer Function | Function to calculate the GIF value of a number ; GIF is the floor of a number ; Driver code
< ? php function GIF ( $ n ) { return floor ( $ n ) ; } $ n = 2.3 ; echo GIF ( $ n ) ; ? >
Number of triangles formed from a set of points on three lines | Returns factorial of a number ; calculate c ( n , r ) ; Driver code
< ? php function factorial ( $ n ) { $ fact = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ fact = $ fact * $ i ; return $ fact ; } function ncr ( $ n , $ r ) { return factorial ( $ n ) / ( factorial ( $ r ) * factorial ( $ n - $ r ) ) ; } $ m = 3 ; $ n = 4 ; $ k = 5 ; $ totalTriangles = ncr ( $ m + $ n + $ k , 3 ) - ncr ( $ m , 3 ) - ncr ( $ n , 3 ) - ncr ( $ k , 3 ) ; echo $ totalTriangles . " STRNEWLINE " ;
Check whether sum of digits at odd places of a number is divisible by K | function that checks the divisibility of the sum of the digits at odd places of the given number ; if position is odd ; Driver code
< ? php function SumDivisible ( $ n , $ k ) { $ sum = 0 ; $ position = 1 ; while ( $ n > 0 ) { if ( $ position % 2 == 1 ) $ sum += $ n % 10 ; $ n = ( int ) $ n / 10 ; $ position ++ ; } if ( $ sum % $ k == 0 ) return true ; return false ; } $ n = 592452 ; $ k = 3 ; if ( SumDivisible ( $ n , $ k ) ) echo " YES " ; else echo " NO " ; ? >
Check if a triangle of positive area is possible with the given angles | PHP program to check if a triangle of positive area is possible with the given angles ; Checking if the sum of three angles is 180 and none of the angles is zero ; Checking if sum of any two angles is greater than equal to the third one ; Driver Code
< ? php function isTriangleExists ( $ a , $ b , $ c ) { if ( $ a != 0 && $ b != 0 && $ c != 0 && ( $ a + $ b + $ c ) == 180 ) if ( ( $ a + $ b ) >= $ c || ( $ b + $ c ) >= $ a || ( $ a + $ c ) >= $ b ) return " YES " ; else return " NO " ; else return " NO " ; } $ a = 50 ; $ b = 60 ; $ c = 70 ; echo isTriangleExists ( $ a , $ b , $ c ) ; ? >
Find maximum value of x such that n ! % ( k ^ x ) = 0 | Function to maximize the value of x such that n ! % ( k ^ x ) = 0 ; Find square root of k and add 1 to it ; Run the loop from 2 to m and k should be greater than 1 ; optimize the value of k ; Minimum store ; Driver Code
< ? php function findX ( $ n , $ k ) { $ r = $ n ; $ m = ( int ) sqrt ( $ k ) + 1 ; for ( $ i = 2 ; $ i <= $ m && $ k > 1 ; $ i ++ ) { if ( $ i == $ m ) { $ i = $ k ; } for ( $ u = $ v = 0 ; $ k % $ i == 0 ; $ v ++ ) { $ k = ( int ) ( $ k / $ i ) ; } if ( $ v > 0 ) { $ t = $ n ; while ( $ t > 0 ) { $ t = ( int ) ( $ t / $ i ) ; $ u = $ u + $ t ; } $ r = min ( $ r , ( int ) ( $ u / $ v ) ) ; } } return $ r ; } $ n = 5 ; $ k = 2 ; echo findX ( $ n , $ k ) ; ? >
Ways of selecting men and women from a group to make a team | Returns factorial of the number ; Function to calculate ncr ; Function to calculate the total possible ways ; Driver code
< ? php function fact ( $ n ) { $ fact = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ fact *= $ i ; return $ fact ; } function ncr ( $ n , $ r ) { $ ncr = ( int ) ( fact ( $ n ) / ( fact ( $ r ) * fact ( $ n - $ r ) ) ) ; return $ ncr ; } function ways ( $ m , $ w , $ n , $ k ) { $ ans = 0 ; while ( $ m >= $ k ) { $ ans += ncr ( $ m , $ k ) * ncr ( $ w , $ n - $ k ) ; $ k += 1 ; } return $ ans ; } $ m = 7 ; $ w = 6 ; $ n = 5 ; $ k = 3 ; echo ways ( $ m , $ w , $ n , $ k ) ;
Sum of greatest odd divisor of numbers in given range | Function to return sum of first n odd numbers ; Recursive function to return sum of greatest odd divisor of numbers in range [ 1 , n ] ; { Odd n ; { Even n ; Function to return sum of greatest odd divisor of numbers in range [ a , b ] ; Driver code
< ? php function square ( $ n ) { return $ n * $ n ; } function sum ( $ n ) { if ( $ n == 0 ) return 0 ; if ( $ n % 2 == 1 ) return square ( ( int ) ( ( $ n + 1 ) / 2 ) ) + sum ( ( int ) ( $ n / 2 ) ) ; } else return square ( ( int ) ( $ n / 2 ) ) + sum ( ( int ) ( $ n / 2 ) ) ; } } function oddDivSum ( $ a , $ b ) { return sum ( $ b ) - sum ( $ a - 1 ) ; } $ a = 3 ; $ b = 9 ; echo oddDivSum ( $ a , $ b ) ; ? >
Minimum numbers needed to express every integer below N as a sum | function to count length of binary expression of n ; Driver code
< ? php function countBits ( $ n ) { $ count = 0 ; while ( $ n ) { $ count ++ ; $ n >>= 1 ; } return $ count ; } $ n = 32 ; echo " Minimum ▁ value ▁ of ▁ K ▁ is ▁ = ▁ " , countBits ( $ n ) , " STRNEWLINE " ; ? >
Check if a number is an Achilles number or not | Function to check if the number is powerful number ; First divide the number repeatedly by 2 ; If only 2 ^ 1 divides n ( not higher powers ) , then return false ; if n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n ( not higher powers ) , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Utility function to check if number is a perfect power or not ; Function to check Achilles Number ; Driver Code
< ? php function isPowerful ( $ n ) { while ( $ n % 2 == 0 ) { $ power = 0 ; while ( $ n % 2 == 0 ) { $ n /= 2 ; $ power ++ ; } if ( $ power == 1 ) return false ; } for ( $ factor = 3 ; $ factor <= sqrt ( $ n ) ; $ factor += 2 ) { $ power = 0 ; while ( $ n % $ factor == 0 ) { $ n = $ n / $ factor ; $ power ++ ; } if ( $ power == 1 ) return false ; } return ( $ n == 1 ) ; } function isPower ( $ a ) { if ( $ a == 1 ) return true ; for ( $ i = 2 ; $ i * $ i <= $ a ; $ i ++ ) { $ val = log ( $ a ) / log ( $ i ) ; if ( ( $ val - ( int ) $ val ) < 0.00000001 ) return true ; } return false ; } function isAchillesNumber ( $ n ) { if ( isPowerful ( $ n ) && ! isPower ( $ n ) ) return true ; else return false ; } $ n = 72 ; if ( isAchillesNumber ( $ n ) ) echo " YES " , " STRNEWLINE " ; else echo " NO " , " STRNEWLINE " ; $ n = 36 ; if ( isAchillesNumber ( $ n ) ) echo " YES " , " STRNEWLINE " ; else echo " NO " , " STRNEWLINE " ; ? >
Program to find count of numbers having odd number of divisors in given range | Function to count numbers having odd number of divisors in range [ A , B ] ; variable to odd divisor count ; iterate from a to b and count their number of divisors ; variable to divisor count ; if count of divisor is odd then increase res by 1 ; Driver code
< ? php function OddDivCount ( $ a , $ b ) { $ res = 0 ; for ( $ i = $ a ; $ i <= $ b ; ++ $ i ) { $ divCount = 0 ; for ( $ j = 1 ; $ j <= $ i ; ++ $ j ) { if ( $ i % $ j == 0 ) { ++ $ divCount ; } } if ( $ divCount % 2 ) { ++ $ res ; } } return $ res ; } $ a = 1 ; $ b = 10 ; echo OddDivCount ( $ a , $ b ) ; ? >
Largest factor of a given number which is a perfect square | Function to find the largest factor of a given number which is a perfect square ; Initialise the answer to 1 ; Finding the prime factors till sqrt ( num ) ; Frequency of the prime factor in the factorisation initialised to 0 ; If the frequency is odd then multiply i frequency - 1 times to the answer ; Else if it is even , multiply it frequency times ; Driver Code
< ? php function largestSquareFactor ( $ num ) { $ answer = 1 ; for ( $ i = 2 ; $ i < sqrt ( $ num ) ; ++ $ i ) { $ cnt = 0 ; $ j = $ i ; while ( $ num % $ j == 0 ) { $ cnt ++ ; $ j *= $ i ; } if ( $ cnt & 1 ) { $ cnt -- ; $ answer *= pow ( $ i , $ cnt ) ; } else { $ answer *= pow ( $ i , $ cnt ) ; } } return $ answer ; } $ N = 420 ; echo largestSquareFactor ( $ N ) ; ? >
Find the Nth term of the series 2 + 6 + 13 + 23 + . . . | calculate Nth term of given series ; Driver code
< ? php function Nth_Term ( $ n ) { return ( 3 * pow ( $ n , 2 ) - $ n + 2 ) / ( 2 ) ; } $ N = 5 ; echo ( Nth_Term ( $ N ) ) ; ? >
All possible numbers of N digits and base B without leading zeros | function to count all permutations ; count of all permutations ; count of permutations with leading zeros ; Return the permutations without leading zeros ; Driver code
< ? php function countPermutations ( $ N , $ B ) { $ x = pow ( $ B , $ N ) ; $ y = pow ( $ B , $ N - 1 ) ; echo ( $ x - $ y ) , " STRNEWLINE " ; } $ N = 6 ; $ B = 4 ; countPermutations ( $ N , $ B ) ; ? >
Absolute difference between the Product of Non | Function to find the difference between the product of non - primes and the product of primes of an array . ; 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 . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store the product of primes in P1 and the product of non primes in P2 ; the number is prime ; the number is non - prime ; Return the absolute difference ; Driver Code ; Find the absolute difference
< ? php function calculateDifference ( $ arr , $ n ) { $ max_val = max ( $ arr ) ; $ prime = array_fill ( 0 , $ max_val , true ) ; $ prime [ 0 ] = false ; $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ max_val ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ max_val ; $ i += $ p ) $ prime [ $ i ] = false ; } } $ P1 = 1 ; $ P2 = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ prime [ $ arr [ $ i ] ] ) { $ P1 *= $ arr [ $ i ] ; } else if ( $ arr [ $ i ] != 1 ) { $ P2 *= $ arr [ $ i ] ; } } return abs ( $ P2 - $ P1 ) ; } $ arr = array ( 1 , 3 , 5 , 10 , 15 , 7 ) ; $ n = count ( $ arr , COUNT_NORMAL ) ; echo CalculateDifference ( $ arr , $ n ) ; ? >
Maximum count of equal numbers in an array after performing given operations | Function to find the maximum number of equal numbers in an array ; to store sum of elements ; if sum of numbers is not divisible by n ; Driver Code ; size of an array
< ? php function EqualNumbers ( $ a , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ a [ $ i ] ; if ( $ sum % $ n ) return $ n - 1 ; return $ n ; } $ a = array ( 1 , 4 , 1 ) ; $ n = sizeof ( $ a ) ; echo EqualNumbers ( $ a , $ n ) ;
Count number of ordered pairs with Even and Odd Sums | function to count odd sum pair ; if number is even ; if number is odd ; count of ordered pairs ; function to count even sum pair ; Driver code
< ? php function count_odd_pair ( $ n , $ a ) { $ odd = 0 ; $ even = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] % 2 == 0 ) $ even ++ ; else $ odd ++ ; } $ ans = $ odd * $ even * 2 ; return $ ans ; } function count_even_pair ( $ odd_sum_pairs , $ n ) { $ total_pairs = ( $ n * ( $ n - 1 ) ) ; $ ans = $ total_pairs - $ odd_sum_pairs ; return $ ans ; } $ n = 6 ; $ a = array ( 2 , 4 , 5 , 9 , 1 , 8 ) ; $ odd_sum_pairs = count_odd_pair ( $ n , $ a ) ; $ even_sum_pairs = count_even_pair ( $ odd_sum_pairs , $ n ) ; echo " Even ▁ Sum ▁ Pairs ▁ = ▁ $ even _ sum _ pairs ▁ STRNEWLINE " ; echo " Odd ▁ Sum ▁ Pairs = ▁ $ odd _ sum _ pairs ▁ STRNEWLINE " ; ? >
Steps required to visit M points in order on a circular ring of N points | Function to count the steps required ; Start at 1 ; Initialize steps ; If nxt is greater than cur ; Now we are at a [ i ] ; Driver code
< ? php function findSteps ( $ n , $ m , $ a ) { $ cur = 1 ; $ steps = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { if ( $ a [ $ i ] >= $ cur ) $ steps += ( $ a [ $ i ] - $ cur ) ; else $ steps += ( $ n - $ cur + $ a [ $ i ] ) ; $ cur = $ a [ $ i ] ; } return $ steps ; } $ n = 3 ; $ m = 3 ; $ a = array ( 2 , 1 , 2 ) ; echo findSteps ( $ n , $ m , $ a ) ; ? >