codeparrot/xlcost-text-to-code huggingface dataset:
Find element in array that divides all array elements | Returns gcd of two numbers ; Function to return the desired number if exists ; Find GCD of array ; Check if GCD is present in array ; Driver Code
< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function findNumber ( $ arr , $ n ) { $ ans = $ arr [ 0 ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ ans = gcd ( $ ans , $ arr [ $ i ] ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] == $ ans ) return $ ans ; return -1 ; } $ arr = array ( 2 , 2 , 4 ) ; $ n = sizeof ( $ arr ) ; echo findNumber ( $ arr , $ n ) , " STRNEWLINE " ; ? >
Special prime numbers | PHP program to check whether there exist at least k or not in range [ 2. . n ] ; Generating all the prime numbers from 2 to n . ; If a prime number is Special prime number , then we increments the value of k . ; If at least k Special prime numbers are present , then we return 1. else we return 0 from outside of the outer loop . ; Driver Code
< ? php $ primes = array ( ) ; function SieveofEratosthenes ( $ n ) { global $ primes ; $ visited = array_fill ( 0 , $ n , false ) ; for ( $ i = 2 ; $ i <= $ n + 1 ; $ i ++ ) if ( ! $ visited [ $ i ] ) { for ( $ j = $ i * $ i ; $ j <= $ n + 1 ; $ j += $ i ) $ visited [ $ j ] = true ; array_push ( $ primes , $ i ) ; } } function specialPrimeNumbers ( $ n , $ k ) { global $ primes ; SieveofEratosthenes ( $ n ) ; $ count = 0 ; for ( $ i = 0 ; $ i < count ( $ primes ) ; $ i ++ ) { for ( $ j = 0 ; $ j < $ i - 1 ; $ j ++ ) { if ( $ primes [ $ j ] + $ primes [ $ j + 1 ] + 1 == $ primes [ $ i ] ) { $ count ++ ; break ; } } if ( $ count == $ k ) return true ; } return false ; } $ n = 27 ; $ k = 2 ; if ( specialPrimeNumbers ( $ n , $ k ) ) echo " YES STRNEWLINE " ; else echo " NO STRNEWLINE " ; ? >
Prime factors of a big number | function to calculate all the prime factors and count of every prime factor ; count the number of times 2 divides ; equivalent to n = n / 2 ; ; if 2 divides it ; check for all the possible numbers that can divide it ; if n at the end is a prime number . ; Driver Code
< ? php function factorize ( $ n ) { $ count = 0 ; while ( ! ( $ n % 2 ) ) { $ n >>= 1 ; $ count ++ ; } if ( $ count ) echo ( 2 . " " ▁ . ▁ $ count ▁ . ▁ " " for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i += 2 ) { $ count = 0 ; while ( $ n % $ i == 0 ) { $ count ++ ; $ n = $ n / $ i ; } if ( $ count ) echo ( $ i . " ▁ " . $ count ) ; } if ( $ n > 2 ) echo ( $ n . " ▁ " . 1 ) ; } $ n = 1000000000000000000 ; factorize ( $ n ) ; ? >
Minimum gcd operations to make all array elements one | __gcd function ; Function to count number of moves . ; Counting Number of ones . ; If there is a one ; Find smallest subarray with GCD equals to one . ; to calculate GCD ; Not Possible ; Final answer ; Driver code
< ? php function __gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return __gcd ( $ b % $ a , $ a ) ; } function minimumMoves ( $ A , $ N ) { $ one = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) if ( $ A [ $ i ] == 1 ) $ one ++ ; if ( $ one != 0 ) return $ N - $ one ; $ minimum = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ g = $ A [ $ i ] ; for ( $ j = $ i + 1 ; $ j < $ N ; $ j ++ ) { $ g = __gcd ( $ A [ $ j ] , $ g ) ; if ( $ g == 1 ) { $ minimum = min ( $ minimum , $ j - $ i ) ; break ; } } } if ( $ minimum == PHP_INT_MAX ) return -1 ; else return $ N + $ minimum - 1 ; } $ A = array ( 2 , 4 , 3 , 9 ) ; $ N = sizeof ( $ A ) ; echo ( minimumMoves ( $ A , $ N ) ) ; ? >
Given N and Standard Deviation , find N elements | function to print series of n elements ; if S . D . is 0 then print all elements as 0. ; print n 0 's ; if S . D . is even ; print - SD , + SD , - SD , + SD ; if odd ; convert n to a float integer ; print one element to be 0 ; print ( n - 1 ) elements as xi derived from the formula ; Driver Code
< ? php function series ( $ n , $ d ) { if ( $ d == 0 ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo "0 ▁ " ; echo " STRNEWLINE " ; return ; } if ( $ n % 2 == 0 ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { echo pow ( -1 , $ i ) * $ d , " " ; } echo " STRNEWLINE " ; } else { $ m = $ n ; $ r = ( $ m / ( $ m - 1 ) ) ; $ g = ( $ d * sqrt ( $ r ) ) ; echo "0 ▁ " ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { echo pow ( -1 , $ i ) * $ g , " " ; } echo " STRNEWLINE " ; } } $ n = 3 ; $ d = 3 ; series ( $ n , $ d ) ; ? >
Total no of 1 's in numbers | PHP code to count the frequency of 1 in numbers less than or equal to the given number . ; Driver Code
< ? php function countDigitOne ( $ n ) { $ countr = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ str = strval ( $ i ) ; $ countr += substr_count ( $ str , '1' ) ; } return $ countr ; } $ n = 13 ; echo countDigitOne ( $ n ) . " STRNEWLINE " ; $ n = 131 ; echo countDigitOne ( $ n ) . " STRNEWLINE " ; $ n = 159 ; echo countDigitOne ( $ n ) . " STRNEWLINE " ; ? >
Exponential Squaring ( Fast Modulo Multiplication ) | prime modulo value ; for cases where exponent is not an even value ; Driver Code
< ? php $ N = 1000000007 ; function exponentiation ( $ bas , $ exp ) { global $ N ; $ t = 1 ; while ( $ exp > 0 ) { if ( $ exp % 2 != 0 ) $ t = ( $ t * $ bas ) % $ N ; $ bas = ( $ bas * $ bas ) % $ N ; $ exp = ( int ) $ exp / 2 ; } return $ t % $ N ; } $ bas = 5 ; $ exp = 100000 ; $ modulo = exponentiation ( $ bas , $ exp ) ; echo ( $ modulo ) ; ? >
GCD of factorials of two numbers | PHP program to find GCD of factorial of two numbers . ; Driver Code
< ? php function factorial ( $ x ) { if ( $ x <= 1 ) return 1 ; $ res = 2 ; for ( $ i = 3 ; $ i <= $ x ; $ i ++ ) $ res = $ res * $ i ; return $ res ; } function gcdOfFactorial ( $ m , $ n ) { return factorial ( min ( $ m , $ n ) ) ; } $ m = 5 ; $ n = 9 ; echo gcdOfFactorial ( $ m , $ n ) ; ? >
Recursive sum of digits of a number is prime or not | Function for recursive digit sum ; function to check if prime or not the single digit ; calls function which returns sum till single digit ; checking prime ; Driver code
< ? php function recDigSum ( $ n ) { if ( $ n == 0 ) return 0 ; else { if ( $ n % 9 == 0 ) return 9 ; else return $ n % 9 ; } } function check ( $ n ) { $ n = recDigSum ( $ n ) ; if ( $ n == 2 or $ n == 3 or $ n == 5 or $ n == 7 ) echo " Yes " ; else echo " No " ; } $ n = 5602 ; check ( $ n ) ; ? >
Find n | Definition of findNumber function ; Finding x from equation n = x ( x + 1 ) / 2 + 1 ; Base of current block ; Value of n - th element ; Driver code
< ? php function findNumber ( $ n ) { $ x = floor ( ( -1 + sqrt ( 1 + 8 * $ n - 8 ) ) / 2 ) ; $ base = ( $ x * ( $ x + 1 ) ) / 2 + 1 ; return $ n - $ base + 1 ; } $ n = 55 ; echo findNumber ( $ n ) ; ? >
Program for weighted mean of natural numbers . | Returns weighted mean assuming for numbers { 1 , 2 , . . n } and weights { 1 , 2 , . . n } ; Driver Code
< ? php function weightedMean ( $ n ) { return ( 2 * $ n + 1 ) / 3 ; } $ n = 10 ; echo ( weightedMean ( $ n ) ) ; ? >
Divide every element of one array by other array elements | Function to calculate the quotient of every element of the array ; Calculate the product of all elements ; To calculate the quotient of every array element ; Driver code
< ? php function calculate ( $ a , $ b , $ n , $ m ) { $ mul = 1 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) if ( $ b [ $ i ] != 0 ) $ mul = $ mul * $ b [ $ i ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ x = floor ( $ a [ $ i ] / $ mul ) ; echo $ x , " " ; } } $ a = array ( 5 , 100 , 8 ) ; $ b = array ( 2 , 3 ) ; $ n = count ( $ a ) ; $ m = count ( $ b ) ; calculate ( $ a , $ b , $ n , $ m ) ; ? >
Minimum number of bombs | function to print where to shoot ; no . of bombs required ; bomb all the even positions ; bomb all the odd positions ; bomb all the even positions again ; Driver Code
< ? php function p_rint ( $ n ) { echo floor ( $ n + $ n / 2 ) , " STRNEWLINE " ; for ( $ i = 2 ; $ i <= $ n ; $ i += 2 ) echo $ i , " ▁ " ; for ( $ i = 1 ; $ i <= $ n ; $ i += 2 ) echo $ i , " ▁ " ; for ( $ i = 2 ; $ i <= $ n ; $ i += 2 ) echo $ i , " ▁ " ; } $ n = 3 ; p_rint ( $ n ) ; ? >
LCM of digits of a given number | define lcm function ; If at any point LCM become 0. return it ; Driver code
< ? php function lcm_fun ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return lcm_fun ( $ b , $ a % $ b ) ; } function digitLCM ( $ n ) { $ lcm = 1 ; while ( $ n > 0 ) { $ lcm = ( int ) ( ( $ n % 10 * $ lcm ) / lcm_fun ( $ n % 10 , $ lcm ) ) ; if ( $ lcm == 0 ) return 0 ; $ n = ( int ) ( $ n / 10 ) ; } return $ lcm ; } $ n = 397 ; echo digitLCM ( $ n ) ; ? >
Secretary Problem ( A Optimal Stopping Problem ) | PHP Program to test 1 / e law for Secretary Problem : ; To find closest integer of num . ; Finds best candidate using n / e rule . candidate [ ] represents talents of n candidates . ; Calculating sample size for benchmarking . ; Finding best candidate in sample size ; Finding the first best candidate that is better than benchmark set . ; Driver code ; n = 8 candidates and candidate array contains talents of n candidate where the largest number means highest talented candidate . ; generating random numbers between 1 to 8 for talent of candidate
< ? php $ e = 2.71828 ; function roundNo ( $ num ) { return $ num < 0 ? $ num - 0.5 : $ num + 0.5 ; } function printBestCandidate ( $ candidate , $ n ) { global $ e ; $ sample_size = roundNo ( $ n / $ e ) ; echo " Sample size is " floor ( $ sample_size ) . " STRNEWLINE " ; $ best = 0 ; for ( $ i = 1 ; $ i < $ sample_size ; $ i ++ ) if ( $ candidate [ $ i ] > $ candidate [ $ best ] ) $ best = $ i ; for ( $ i = $ sample_size ; $ i < $ n ; $ i ++ ) if ( $ candidate [ $ i ] >= $ candidate [ $ best ] ) { $ best = $ i ; break ; } if ( $ best >= $ sample_size ) echo " Best candidate found is " . floor ( $ best + 1 ) . " ▁ with ▁ talent ▁ " . floor ( $ candidate [ $ best ] ) . " STRNEWLINE " ; else echo " Couldn ' t ▁ find ▁ a ▁ best ▁ candidate STRNEWLINE " ; } $ n = 8 ; $ candidate = array_fill ( 0 , $ n , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ candidate [ $ i ] = 1 + rand ( 1 , 8 ) ; echo " Candidate ▁ : ▁ " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo ( $ i + 1 ) . " ▁ " ; echo " Talents : " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ candidate [ $ i ] . " ▁ " ; printBestCandidate ( $ candidate , $ n ) ; ? >
Newton Forward And Backward Interpolation | Calculation of u mentioned in formula ; Calculating factorial of given n ; number of values given ; y [ ] [ ] is used for difference table and y [ ] [ 0 ] used for input ; Calculating the backward difference table ; Displaying the backward difference table ; Value to interpolate at ; Initializing u and sum
< ? php function u_cal ( $ u , $ n ) { $ temp = $ u ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ temp = $ temp * ( $ u + $ i ) ; return $ temp ; } function fact ( $ n ) { $ f = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ f *= $ i ; return $ f ; } $ n = 5 ; $ x = array ( 1891 , 1901 , 1911 , 1921 , 1931 ) ; $ y = array_fill ( 0 , $ n , array_fill ( 0 , $ n , 0 ) ) ; $ y [ 0 ] [ 0 ] = 46 ; $ y [ 1 ] [ 0 ] = 66 ; $ y [ 2 ] [ 0 ] = 81 ; $ y [ 3 ] [ 0 ] = 93 ; $ y [ 4 ] [ 0 ] = 101 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ n - 1 ; $ j >= $ i ; $ j -- ) $ y [ $ j ] [ $ i ] = $ y [ $ j ] [ $ i - 1 ] - $ y [ $ j - 1 ] [ $ i - 1 ] ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ i ; $ j ++ ) print ( $ y [ $ i ] [ $ j ] . " TABSYMBOL " ) ; print ( " STRNEWLINE " ) ; } $ value = 1925 ; $ sum = $ y [ $ n - 1 ] [ 0 ] ; $ u = ( $ value - $ x [ $ n - 1 ] ) / ( $ x [ 1 ] - $ x [ 0 ] ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ sum = $ sum + ( u_cal ( $ u , $ i ) * $ y [ $ n - 1 ] [ $ i ] ) / fact ( $ i ) ; } print ( " Value at " ▁ . ▁ $ value ▁ . STRNEWLINE TABSYMBOL " is " ? >
Happy Numbers | Returns sum of squares of digits of a number n . For example for n = 12 it returns 1 + 4 = 5 ; Returns true if n is Happy number else returns false . ; Keep replacing n with sum of squares of digits until we either reach 1 or we end up in a cycle ; Number is Happy if we reach 1 ; Replace n with sum of squares of digits ; Number is not Happy if we reach 4 ; Driver code
< ? php function sumDigitSquare ( $ n ) { $ sq = 0 ; while ( $ n ) { $ digit = $ n % 10 ; $ sq += $ digit * $ digit ; $ n = $ n / 10 ; } return $ sq ; } function isHappy ( $ n ) { while ( 1 ) { if ( $ n == 1 ) return true ; $ n = sumDigitSquare ( $ n ) ; if ( $ n == 4 ) return false ; } return false ; } $ n = 23 ; if ( isHappy ( $ n ) ) echo " Yes " ; else echo " No " ; ? >
Sum of all subsets of a set formed by first n natural numbers | PHP program to find sum of all subsets of a set ; sum of subsets is ( n * ( n + 1 ) / 2 ) * pow ( 2 , n - 1 ) ; Driver Code
< ? php function findSumSubsets ( $ n ) { return ( $ n * ( $ n + 1 ) / 2 ) * ( 1 << ( $ n - 1 ) ) ; } $ n = 3 ; echo findSumSubsets ( $ n ) ; ? >
Minimum element whose n | function to find the minimum element ; loop to traverse and store the sum of log ; computes sum ; calculates the elements according to formula . ; returns the minimal element ; initialised array ; computes the size of array ; prints out the minimal element
< ? php function findMin ( $ a , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += log ( $ a [ $ i ] ) ; $ x = exp ( $ sum / $ n ) ; return ( int ) ( $ x + 1 ) ; } $ a = array ( 3 , 2 , 1 , 4 ) ; $ n = sizeof ( $ a ) ; echo ( findMin ( $ a , $ n ) ) ; ? >
Generate all cyclic permutations of a number | Function to count the total number of digits in a number . ; Function to generate all cyclic permutations of a number ; Following three lines generates a circular pirmutation of a number . ; If all the permutations are checked and we obtain original number exit from loop . ; Driver Code
< ? php function countdigits ( $ N ) { $ count = 0 ; while ( $ N ) { $ count ++ ; $ N = floor ( $ N / 10 ) ; } return $ count ; } function cyclic ( $ N ) { $ num = $ N ; $ n = countdigits ( $ N ) ; while ( 1 ) { echo ( $ num ) ; echo " STRNEWLINE " ; $ rem = $ num % 10 ; $ div = floor ( $ num / 10 ) ; $ num = ( pow ( 10 , $ n - 1 ) ) * $ rem + $ div ; if ( $ num == $ N ) break ; } } $ N = 5674 ; cyclic ( $ N ) ; ? >
Check whether a number is circular prime or not | Function to check if a number is prime or not . ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check if the number is circular prime or not . ; Count digits . ; Following three lines generate the next circular permutation of a number . We move last digit to first position . ; If all the permutations are checked and we obtain original number exit from loop . ; Driver Code
< ? 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 -1 ; return true ; } function checkCircular ( $ N ) { $ count = 0 ; $ temp = $ N ; while ( $ temp ) { $ count ++ ; $ temp = ( int ) $ temp / 10 ; } $ num = $ N ; while ( isPrime ( $ num ) ) { $ rem = $ num % 10 ; $ div = ( int ) $ num / 10 ; $ num = ( pow ( 10 , $ count - 1 ) ) * $ rem + $ div ; if ( $ num == $ N ) return true ; } return -1 ; } $ N = 1193 ; if ( checkCircular ( $ N ) ) echo " Yes " , " STRNEWLINE " ; else echo " No " , " STRNEWLINE " ; ? >
Find if two people ever meet after same number of jumps | function to find if any one of them can overtake the other ; Since starting points are always different , they will meet if following conditions are met . ( 1 ) Speeds are not same ( 2 ) Difference between speeds divide the total distance between initial points . ; Driver Code
< ? php function sackRace ( $ p1 , $ s1 , $ p2 , $ s2 ) { return ( ( $ s1 > $ s2 && ( $ p2 - $ p1 ) % ( $ s1 - $ s2 ) == 0 ) || ( $ s2 > $ s1 && ( $ p1 - $ p2 ) % ( $ s2 - $ s1 ) == 0 ) ) ; } $ p1 = 4 ; $ s1 = 4 ; $ p2 = 8 ; $ s2 = 2 ; if ( sackRace ( $ p1 , $ s1 , $ p2 , $ s2 ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; ? >
Largest proper fraction with sum of numerator and denominator equal to a given number | PHP program to find the largest fraction a / b such that a + b is equal to given number and a < b . ; Calculate N / 2 ; ; Check if N is odd or even ; If N is odd answer will be ceil ( n / 2 ) - 1 and floor ( n / 2 ) + 1 ; If N is even check if N / 2 i . e a is even or odd ; If N / 2 is even apply the previous formula ; If N / 2 is odd answer will be ceil ( N / 2 ) - 2 and floor ( N / 2 ) + 2 ; driver function
< ? php function solve ( $ n ) { $ a = ( float ) $ n / 2 ; if ( $ n % 2 != 0 ) echo ceil ( $ a ) - 1 , " ▁ " , floor ( $ a ) + 1 , " STRNEWLINE " ; else { if ( $ a % 2 == 0 ) { echo ceil ( $ a ) - 1 , " ▁ " , floor ( $ a ) + 1 , " STRNEWLINE " ; } else { echo ceil ( $ a ) - 2 , " ▁ " , floor ( $ a ) + 2 , " STRNEWLINE " ; } } } $ n = 34 ; solve ( $ n ) ; ? >
Program to find simple interest | We can change values here for different inputs ; Calculate simple interest ; Print the resultant value of SI
< ? php $ P = 1 ; $ R = 1 ; $ T = 1 ; $ SI = ( $ P * $ T * $ R ) / 100 ; echo " Simple ▁ Interest ▁ = ▁ " . $ SI ; ? >
Number of digits in the product of two numbers | function to count number of digits in the product of two numbers ; absolute value of the product of two numbers ; if product is 0 ; count number of digits in the product ' p ' ; required count of digits ; Driver Code
< ? php function countDigits ( $ a , $ b ) { $ count = 0 ; $ p = abs ( $ a * $ b ) ; if ( $ p == 0 ) return 1 ; while ( $ p > 0 ) { $ count ++ ; $ p = ( int ) ( $ p / 10 ) ; } return $ count ; } $ a = 33 ; $ b = -24 ; echo " Number ▁ of ▁ digits ▁ = ▁ " . countDigits ( $ a , $ b ) ; ? >
Find multiple of x closest to or a ^ b ( a raised to power b ) | function to find closest multiple of x to a ^ b ; calculate a ^ b / x ; Answer is either ( ans * x ) or ( ans + 1 ) * x ; Printing nearest answer ; Driver Code
< ? php function multiple ( $ a , $ b , $ x ) { if ( $ b < 0 ) { if ( $ a == 1 && $ x == 1 ) echo "1" ; else echo "0" ; } $ mul = pow ( $ a , $ b ) ; $ ans = $ mul / $ x ; $ ans1 = $ x * $ ans ; $ ans2 = $ x * ( $ ans + 1 ) ; $ k = ( ( ( $ mul - $ ans1 ) <= ( $ ans2 - $ mul ) ) ? $ ans1 : $ ans2 ) ; echo ( $ k ) ; } $ a = 348 ; $ b = 1 ; $ x = 4 ; multiple ( $ a , $ b , $ x ) ; ? >
Maximum sum of difference of adjacent elements | To find max sum of permutation ; Base case ; Otherwise max sum will be ( n * ( n - 1 ) / 2 ) - 1 + n / 2 ; Driver Code
< ? php function maxSum ( $ n ) { if ( $ n == 1 ) return 1 ; else return ( $ n * ( $ n - 1 ) / 2 ) - 1 + $ n / 2 ; } $ n = 3 ; echo intval ( maxSum ( $ n ) ) ; ? >
Add minimum number to an array so that the sum becomes even | Function to find out minimum number ; Count odd number of terms in array ; Driver code
< ? php function minNum ( $ arr , $ n ) { $ odd = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] % 2 ) $ odd = ! $ odd ; if ( $ odd ) return 1 ; return 2 ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) ; $ n = sizeof ( $ arr ) ; echo minNum ( $ arr , $ n ) , " STRNEWLINE " ; ? >
Check if a number is jumbled or not | Function to check if a number is jumbled or not ; Single digit number ; Checking every digit through a loop ; All digits were checked ; Digit at index i ; Digit at index i - 1 ; If difference is greater than 1 ; Number checked ; - 1234 to be checked ; 287 to be checked
< ? php function checkJumbled ( $ num ) { if ( $ num / 10 == 0 ) return true ; while ( $ num != 0 ) { if ( $ num / 10 == 0 ) return true ; $ digit1 = $ num % 10 ; $ digit2 = ( $ num / 10 ) % 10 ; if ( abs ( $ digit2 - $ digit1 ) > 1 ) return false ; $ num = $ num / 10 ; } return true ; } $ num = -1234 ; if ( checkJumbled ( $ num ) ) echo " True ▁ STRNEWLINE " ; else echo " False ▁ STRNEWLINE " ; $ num = -1247 ; if ( checkJumbled ( $ num ) ) echo " True ▁ STRNEWLINE " ; else echo " False ▁ STRNEWLINE " ; ? >
Josephus Problem Using Bit Magic | function to find the position of the Most Significant Bit ; keeps shifting bits to the right until we are left with 0 ; function to return at which place Josephus should sit to avoid being killed ; Getting the position of the Most Significant Bit ( MSB ) . The leftmost '1' . If the number is '41' then its binary is '101001' . So msbPos ( 41 ) = 6 ; ' j ' stores the number with which to XOR the number ' n ' . Since we need '100000' . We will do 1 << 6 - 1 to get '100000' ; Toggling the Most Significant Bit . Changing the leftmost '1' to '0' . 101001 ^ 100000 = 001001 ( 9 ) ; Left - shifting once to add an extra '0' to the right end of the binary number 001001 = 010010 ( 18 ) ; Toggling the '0' at the end to '1' which is essentially the same as putting the MSB at the rightmost place . 010010 | 1 = 010011 ( 19 ) ; Driver Code
< ? php function msbPos ( $ n ) { $ pos = 0 ; while ( $ n != 0 ) { $ pos ++ ; $ n = $ n >> 1 ; } return $ pos ; } function josephify ( $ n ) { $ position = msbPos ( $ n ) ; $ j = 1 << ( $ position - 1 ) ; $ n = $ n ^ $ j ; $ n = $ n << 1 ; $ n = $ n | 1 ; return $ n ; } $ n = 41 ; print ( josephify ( $ n ) ) ; ? >
Count pairs with Odd XOR | A function will return number of pair whose XOR is odd ; To store count of XOR pair ; If XOR is odd increase count ; Return count ; Driver Code
< ? php function countXorPair ( $ arr , $ n ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( ( $ arr [ $ i ] ^ $ arr [ $ j ] ) % 2 == 1 ) $ count ++ ; } return $ count ; } $ arr = array ( 1 , 2 , 3 ) ; $ n = count ( $ arr ) ; echo countXorPair ( $ arr , $ n ) ; ? >
Discrete logarithm ( Find an integer k such that a ^ k is congruent modulo b ) | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; $x = $x % $p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now $y = $y >> 1 ; y = y / 2 ; Function to calculate k for given a , b , m ; Store all values of a ^ ( n * i ) of LHS ; Calculate ( a ^ j ) * b and check for collision ; If collision occurs i . e . , LHS = RHS ; Check whether ans lies below m or not ; Driver code
< ? php function powmod ( $ x , $ y , $ p ) { while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function discreteLogarithm ( $ a , $ b , $ m ) { $ n = ( int ) sqrt ( $ m ) + 1 ; $ value = array_fill ( 0 , $ m , NULL ) ; for ( $ i = $ n ; $ i >= 1 ; -- $ i ) $ value [ powmod ( $ a , $ i * $ n , $ m ) ] = $ i ; for ( $ j = 0 ; $ j < $ n ; ++ $ j ) { $ cur = ( powmod ( $ a , $ j , $ m ) * $ b ) % $ m ; if ( $ value [ $ cur ] ) { $ ans = $ value [ $ cur ] * $ n - $ j ; if ( $ ans < $ m ) return $ ans ; } } return -1 ; } $ a = 2 ; $ b = 3 ; $ m = 5 ; echo discreteLogarithm ( $ a , $ b , $ m ) , " " ; $ a = 3 ; $ b = 7 ; $ m = 11 ; echo discreteLogarithm ( $ a , $ b , $ m ) , " " ; ? >
Discrete logarithm ( Find an integer k such that a ^ k is congruent modulo b ) | PHP program to calculate discrete logarithm ; Calculate a ^ n ; Store all values of a ^ ( n * i ) of LHS ; Calculate ( a ^ j ) * b and check for collision ; Driver code
< ? php function discreteLogarithm ( $ a , $ b , $ m ) { $ n = ( int ) sqrt ( $ m ) + 1 ; $ an = 1 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) $ an = ( $ an * $ a ) % $ m ; $ value = array_fill ( 0 , $ m , NULL ) ; for ( $ i = 1 , $ cur = $ an ; $ i <= $ n ; ++ $ i ) { if ( ! $ value [ $ cur ] ) $ value [ $ cur ] = $ i ; $ cur = ( $ cur * $ an ) % $ m ; } for ( $ i = 0 , $ cur = $ b ; $ i <= $ n ; ++ $ i ) { if ( $ value [ $ cur ] ) { $ ans = $ value [ $ cur ] * $ n - $ i ; if ( $ ans < $ m ) return $ ans ; } $ cur = ( $ cur * $ a ) % $ m ; } return -1 ; } $ a = 2 ; $ b = 3 ; $ m = 5 ; echo discreteLogarithm ( $ a , $ b , $ m ) , " " ; $ a = 3 ; $ b = 7 ; $ m = 11 ; echo discreteLogarithm ( $ a , $ b , $ m ) ; ? >