codeparrot/xlcost-text-to-code huggingface dataset:
Lexicographically first palindromic string | PHP program to find first palindromic permutation of given string ; Function to count frequency of each char in the string . freq [ 0 ] for ' a ' , ... . , freq [ 25 ] for ' z ' ; Cases to check whether a palindr0mic string can be formed or not ; count_odd to count no of chars with odd frequency ; For even length string no odd freq character ; For odd length string one odd freq character ; Function to find odd freq char and reducing its freq by 1 returns " " if odd freq char is not present ; To find lexicographically first palindromic string . ; Assigning odd freq character if present else empty string . ; Traverse characters in increasing order ; Divide all occurrences into two halves . Note that odd character is removed by findOddAndRemoveItsFreq ( ) ; creating front string ; creating rear string ; Final palindromic string which is lexicographically first ; Driver code
< ? php $ MAX_CHAR = 26 ; function countFreq ( $ str , & $ freq , $ len ) { for ( $ i = 0 ; $ i < $ len ; $ i ++ ) $ freq [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ++ ; } function canMakePalindrome ( $ freq , $ len ) { global $ MAX_CHAR ; $ count_odd = 0 ; for ( $ i = 0 ; $ i < $ MAX_CHAR ; $ i ++ ) if ( $ freq [ $ i ] % 2 != 0 ) $ count_odd ++ ; if ( $ len % 2 == 0 ) { if ( $ count_odd > 0 ) return false ; else return true ; } if ( $ count_odd != 1 ) return false ; return true ; } function findOddAndRemoveItsFreq ( $ freq ) { global $ MAX_CHAR ; $ odd_str = " " ; for ( $ i = 0 ; $ i < $ MAX_CHAR ; $ i ++ ) { if ( $ freq [ $ i ] % 2 != 0 ) { $ freq [ $ i ] -- ; $ odd_str . = chr ( $ i + ord ( ' a ' ) ) ; return $ odd_str ; } } return $ odd_str ; } function findPalindromicString ( $ str ) { global $ MAX_CHAR ; $ len = strlen ( $ str ) ; $ freq = array_fill ( 0 , $ MAX_CHAR , 0 ) ; countFreq ( $ str , $ freq , $ len ) ; if ( ! canMakePalindrome ( $ freq , $ len ) ) return " No ▁ Palindromic ▁ String " ; $ odd_str = findOddAndRemoveItsFreq ( $ freq ) ; $ front_str = " " ; $ rear_str = " ▁ " ; for ( $ i = 0 ; $ i < $ MAX_CHAR ; $ i ++ ) { $ temp = " " ; if ( $ freq [ $ i ] != 0 ) { $ ch = chr ( $ i + ord ( ' a ' ) ) ; for ( $ j = 1 ; $ j <= ( int ) ( $ freq [ $ i ] / 2 ) ; $ j ++ ) $ temp . = $ ch ; $ front_str . = $ temp ; $ rear_str = $ temp . $ rear_str ; } } return ( $ front_str . $ odd_str . $ rear_str ) ; } $ str = " malayalam " ; echo findPalindromicString ( $ str ) ; ? >
Count substrings with same first and last characters | Returns true if first and last characters of s are same . ; Starting point of substring ; Length of substring ; Check if current substring has same starting and ending characters . ; Driver Code
< ? php function checkEquality ( $ s ) { return ( $ s [ 0 ] == $ s [ strlen ( $ s ) - 1 ] ) ; } function countSubstringWithEqualEnds ( $ s ) { $ result = 0 ; $ n = strlen ( $ s ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ len = 1 ; $ len <= $ n - $ i ; $ len ++ ) if ( checkEquality ( substr ( $ s , $ i , $ len ) ) ) $ result ++ ; return $ result ; } $ s = " abcab " ; print ( countSubstringWithEqualEnds ( $ s ) ) ; ? >
Count substrings with same first and last characters | Space efficient PHP program to count all substrings with same first and last characters . ; Iterating through all substrings in way so that we can find first and last character easily ; Driver Code
< ? php function countSubstringWithEqualEnds ( $ s ) { $ result = 0 ; $ n = strlen ( $ s ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = $ i ; $ j < $ n ; $ j ++ ) if ( $ s [ $ i ] == $ s [ $ j ] ) $ result ++ ; return $ result ; } $ s = " abcab " ; echo countSubstringWithEqualEnds ( $ s ) ;
Maximum consecutive repeating character in string | function to find out the maximum repeating character in given string ; Find the maximum repeating character starting from str [ i ] ; Update result if required ; Driver code
< ? php function maxRepeating ( $ str ) { $ len = strlen ( $ str ) ; $ count = 0 ; $ res = $ str [ 0 ] ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { $ cur_count = 1 ; for ( $ j = $ i + 1 ; $ j < $ len ; $ j ++ ) { if ( $ str [ $ i ] != $ str [ $ j ] ) break ; $ cur_count ++ ; } if ( $ cur_count > $ count ) { $ count = $ cur_count ; $ res = $ str [ $ i ] ; } } return $ res ; } $ str = " aaaabbaaccde " ; echo maxRepeating ( $ str ) ; ? >
Sum of two large numbers | Function for finding sum of larger numbers ; Before proceeding further , make sure length of str2 is larger . ; Take an empty string for storing result ; Calculate length of both string ; Reverse both of strings ; Do school mathematics , compute sum of current digits and carry ; Calculate carry for next step ; Add remaining digits of larger number ; Add remaining carry ; reverse resultant string ; Driver code
< ? php function findSum ( $ str1 , $ str2 ) { if ( strlen ( $ str1 ) > strlen ( $ str2 ) ) { $ t = $ str1 ; $ str1 = $ str2 ; $ str2 = $ t ; } $ str = " " ; $ n1 = strlen ( $ str1 ) ; $ n2 = strlen ( $ str2 ) ; $ str1 = strrev ( $ str1 ) ; $ str2 = strrev ( $ str2 ) ; $ carry = 0 ; for ( $ i = 0 ; $ i < $ n1 ; $ i ++ ) { $ sum = ( ( ord ( $ str1 [ $ i ] ) -48 ) + ( ( ord ( $ str2 [ $ i ] ) -48 ) + $ carry ) ) ; $ str . = chr ( $ sum % 10 + 48 ) ; $ carry = ( int ) ( $ sum / 10 ) ; } for ( $ i = $ n1 ; $ i < $ n2 ; $ i ++ ) { $ sum = ( ( ord ( $ str2 [ $ i ] ) -48 ) + $ carry ) ; $ str . = chr ( $ sum % 10 + 48 ) ; $ carry = ( int ) ( $ sum / 10 ) ; } if ( $ carry ) $ str . = chr ( $ carry + 48 ) ; $ str = strrev ( $ str ) ; return $ str ; } $ str1 = "12" ; $ str2 = "198111" ; echo findSum ( $ str1 , $ str2 ) ; ? >
Sum of two large numbers | Function for finding sum of larger numbers ; Before proceeding further , make sure length of str2 is larger . ; Take an empty string for storing result ; Calculate length of both string ; Initially take carry zero ; Traverse from end of both strings ; Do school mathematics , compute sum of current digits and carry ; Add remaining digits of str2 [ ] ; Add remaining carry ; reverse resultant string ; Driver code
< ? php function findSum ( $ str1 , $ str2 ) { if ( strlen ( $ str1 ) > strlen ( $ str2 ) ) { $ temp = $ str1 ; $ str1 = $ str2 ; $ str2 = $ temp ; } $ str3 = " " ; $ n1 = strlen ( $ str1 ) ; $ n2 = strlen ( $ str2 ) ; $ diff = $ n2 - $ n1 ; $ carry = 0 ; for ( $ i = $ n1 - 1 ; $ i >= 0 ; $ i -- ) { $ sum = ( ( ord ( $ str1 [ $ i ] ) - ord ( '0' ) ) + ( ( ord ( $ str2 [ $ i + $ diff ] ) - ord ( '0' ) ) ) + $ carry ) ; $ str3 . = chr ( $ sum % 10 + ord ( '0' ) ) ; $ carry = ( int ) ( $ sum / 10 ) ; } for ( $ i = $ n2 - $ n1 - 1 ; $ i >= 0 ; $ i -- ) { $ sum = ( ( ord ( $ str2 [ $ i ] ) - ord ( '0' ) ) + $ carry ) ; $ str3 . = chr ( $ sum % 10 + ord ( '0' ) ) ; $ carry = ( int ) ( $ sum / 10 ) ; } if ( $ carry ) $ str3 . = chr ( $ carry + ord ( '0' ) ) ; return strrev ( $ str3 ) ; } $ str1 = "12" ; $ str2 = "198111" ; print ( findSum ( $ str1 , $ str2 ) ) ; ? >
Efficiently check if a string has all unique characters without using any additional data structure | Returns true if all characters of str are unique . Assumptions : ( 1 ) str contains only characters from ' a ' to ' z ' ( 2 ) integers are stored using 32 bits ; An integer to store presence / absence of 26 characters using its 32 bits . ; If bit corresponding to current character is already set ; set bit in checker ; Driver code
< ? php function areChractersUnique ( $ str ) { $ checker = 0 ; for ( $ i = 0 ; $ i < $ len = strlen ( $ str ) ; ++ $ i ) { $ val = ( $ str [ $ i ] - ' a ' ) ; if ( ( $ checker & ( 1 << $ val ) ) > 0 ) return false ; $ checker |= ( 1 << $ val ) ; } return true ; } $ s = " aaabbccdaa " ; if ( areChractersUnique ( $ s ) ) echo " Yes " ; else echo " No " ; ? >
Count of words whose i | Return the count of words . ; If word contain single letter , return 1. ; Checking for first letter . ; Traversing the string and multiplying for combinations . ; If all three letters are same . ; If two letter are distinct . ; If all three letter are distinct . ; Checking for last letter . ; Driver Code
< ? php function countWords ( $ str , $ len ) { $ count = 1 ; if ( $ len == 1 ) return $ count ; if ( $ str [ 0 ] == $ str [ 1 ] ) $ count *= 1 ; else $ count *= 2 ; for ( $ j = 1 ; $ j < $ len - 1 ; $ j ++ ) { if ( $ str [ $ j ] == $ str [ $ j - 1 ] && $ str [ $ j ] == $ str [ $ j + 1 ] ) $ count *= 1 ; else if ( $ str [ $ j ] == $ str [ $ j - 1 ] $ str [ $ j ] == $ str [ $ j + 1 ] $ str [ $ j - 1 ] == $ str [ $ j + 1 ] ) $ count *= 2 ; else $ count *= 3 ; } if ( $ str [ $ len - 1 ] == $ str [ $ len - 2 ] ) $ count *= 1 ; else $ count *= 2 ; return $ count ; } $ str = " abc " ; $ len = strlen ( $ str ) ; echo countWords ( $ str , $ len ) ; ? >
Maximum and minimum sums from two numbers with digit replacements | Find new value of x after replacing digit " from " to " to " ; Required digit found , replace it ; Returns maximum and minimum possible sums of x1 and x2 if digit replacements are allowed . ; We always get minimum sum if we replace 6 with 5. ; We always get maximum sum if we replace 5 with 6. ; Driver code
< ? php function replaceDig ( $ x , $ from , $ to ) { $ result = 0 ; $ multiply = 1 ; while ( $ x > 0 ) { $ reminder = $ x % 10 ; if ( $ reminder == $ from ) $ result = $ result + $ to * $ multiply ; else $ result = $ result + $ reminder * $ multiply ; $ multiply *= 10 ; $ x = $ x / 10 ; } return $ result ; } function calculateMinMaxSum ( $ x1 , $ x2 ) { $ minSum = replaceDig ( $ x1 , 6 , 5 ) + replaceDig ( $ x2 , 6 , 5 ) ; $ maxSum = replaceDig ( $ x1 , 5 , 6 ) + replaceDig ( $ x2 , 5 , 6 ) ; echo " Minimum sum = " ▁ , ▁ $ minSum , " " ; STRNEWLINE TABSYMBOL echo ▁ " Maximum sum = " } $ x1 = 5466 ; $ x2 = 4555 ; calculateMinMaxSum ( $ x1 , $ x2 ) ; ? >
Queries on substring palindrome formation | Query type 1 : update string position i with character x . ; Print " Yes " if range [ L . . R ] can form palindrome , else print " No " . ; Find the frequency of each character in S [ L ... R ] . ; Checking if more than one character have frequency greater than 1. ; Driver code
< ? php function qType1 ( $ l , $ x , & $ str ) { $ str [ $ l - 1 ] = $ x ; } function qType2 ( $ l , $ r , $ str ) { $ freq = array_fill ( 0 , 27 , 0 ) ; for ( $ i = $ l - 1 ; $ i <= $ r - 1 ; $ i ++ ) $ freq [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ++ ; $ count = 0 ; for ( $ j = 0 ; $ j < 26 ; $ j ++ ) if ( $ freq [ $ j ] % 2 ) $ count ++ ; ( $ count <= 1 ) ? ( print ( " Yes STRNEWLINE " ) ) : ( print ( " No STRNEWLINE " ) ) ; } $ str = " geeksforgeeks " ; $ n = strlen ( $ str ) ; qType1 ( 4 , ' g ' , $ str ) ; qType2 ( 1 , 4 , $ str ) ; qType2 ( 2 , 3 , $ str ) ; qType1 ( 10 , ' t ' , $ str ) ; qType2 ( 10 , 11 , $ str ) ; ? >
Efficient method for 2 's complement of a binary string | Function to find two 's complement ; Traverse the string to get first '1' from the last of string ; If there exists no '1' concatenate 1 at the starting of string ; Continue traversal after the position of first '1' ; Just flip the values ; return the modified string ; Driver code
< ? php function findTwoscomplement ( $ str ) { $ n = strlen ( $ str ) ; $ i ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) if ( $ str [ $ i ] == '1' ) break ; if ( $ i == -1 ) return '1' + $ str ; for ( $ k = $ i - 1 ; $ k >= 0 ; $ k -- ) { if ( $ str [ $ k ] == '1' ) $ str [ $ k ] = '0' ; else $ str [ $ k ] = '1' ; } return $ str ; ; } $ str = "00000101" ; echo findTwoscomplement ( $ str ) ; ? >
Check length of a string is equal to the number appended at its last | Function to find if given number is equal to length or not ; Traverse string from end and find the number stored at the end . x is used to store power of 10. ; Check if number is equal to string length except that number 's digits ; Driver code
< ? php function isequal ( $ str ) { $ n = strlen ( $ str ) ; $ num = 0 ; $ x = 1 ; $ i = $ n - 1 ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { if ( '0' <= $ str [ $ i ] && $ str [ $ i ] <= '9' ) { $ num = ( $ str [ $ i ] - '0' ) * $ x + $ num ; $ x = $ x * 10 ; if ( $ num >= $ n ) return false ; } else break ; } return $ num == $ i + 1 ; } $ str = " geeksforgeeks13" ; if ( isequal ( $ str ) ) echo " Yes " ; else echo " No " ; return 0 ; ? >
Minimum number of characters to be removed to make a binary string alternate | Returns count of minimum characters to be removed to make s alternate . ; if two alternating characters of string are same ; then need to delete a character ; Driver code
< ? php function countToMake0lternate ( $ s ) { $ result = 0 ; for ( $ i = 0 ; $ i < ( strlen ( $ s ) - 1 ) ; $ i ++ ) if ( $ s [ $ i ] == $ s [ $ i + 1 ] ) $ result ++ ; return $ result ; } echo countToMake0lternate ( " 000111 " ) , " " ; echo countToMake0lternate ( " 11111 " ) , " " ; echo countToMake0lternate ( " 01010101 " ) ; ? >
Smallest Palindrome after replacement | Utility method to check str is possible palindrome after ignoring ; If both left and right character are not dot and they are not equal also , then it is not possible to make this string a palindrome ; Returns lexicographically smallest palindrome string , if possible ; loop through character of string ; if one of character is dot , replace dot with other character ; if both character are dot , then replace them with smallest character ' a ' ; return the result ; Driver code
< ? php function isPossiblePalindrome ( $ str ) { $ n = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ n / 2 ; $ i ++ ) { if ( $ str [ $ i ] != ' . ' && $ str [ $ n - $ i - 1 ] != ' . ' && $ str [ $ i ] != $ str [ $ n - $ i - 1 ] ) return false ; } return true ; } function smallestPalindrome ( $ str ) { if ( ! isPossiblePalindrome ( $ str ) ) return " Not ▁ Possible " ; $ n = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] == ' . ' ) { if ( $ str [ $ n - $ i - 1 ] != ' . ' ) $ str [ $ i ] = $ str [ $ n - $ i - 1 ] ; else $ str [ $ i ] = $ str [ $ n - $ i - 1 ] = ' a ' ; } } return $ str ; } $ str = " ab . . e . c . a " ; echo smallestPalindrome ( $ str ) ; ? >
Number of flips to make binary string alternate | Set 1 | Utility method to flip a character ; Utility method to get minimum flips when alternate string starts with expected char ; if current character is not expected , increase flip count ; flip expected character each time ; method return minimum flip to make binary string alternate ; return minimum of following two 1 ) flips when alternate string starts with 0 2 ) flips when alternate string starts with 1 ; Driver code to test above method
< ? php function flip ( $ ch ) { return ( $ ch == '0' ) ? '1' : '0' ; } function getFlipWithStartingCharcter ( $ str , $ expected ) { $ flipCount = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i ] != $ expected ) $ flipCount ++ ; $ expected = flip ( $ expected ) ; } return $ flipCount ; } function minFlipToMakeStringAlternate ( $ str ) { return min ( getFlipWithStartingCharcter ( $ str , '0' ) , getFlipWithStartingCharcter ( $ str , '1' ) ) ; } $ str = "0001010111" ; echo minFlipToMakeStringAlternate ( $ str ) ; ? >
Remainder with 7 for large numbers | Function which return Remainder after dividing the number by 7 ; This series is used to find remainder with 7 ; Index of next element in series ; Initialize result ; Traverse num from end ; Find current digit of num ; Add next term to result ; Move to next term in series ; Make sure that result never goes beyond 7. ; Make sure that remainder is positive ; Driver Code
< ? php function remainderWith7 ( $ num ) { $ series = array ( 1 , 3 , 2 , -1 , -3 , -2 ) ; $ series_index = 0 ; $ result = 0 ; for ( $ i = strlen ( $ num ) - 1 ; $ i >= 0 ; $ i -- ) { $ digit = $ num [ $ i ] - '0' ; $ result += $ digit * $ series [ $ series_index ] ; $ series_index = ( $ series_index + 1 ) % 6 ; $ result %= 7 ; } if ( $ result < 0 ) $ result = ( $ result + 7 ) % 7 ; return $ result ; } { $ str = "12345" ; echo " Remainder ▁ with ▁ 7 ▁ is ▁ " , ( remainderWith7 ( $ str ) ) ; return 0 ; } ? >
Converting Decimal Number lying between 1 to 3999 to Roman Numerals | Function to calculate roman equivalent ; storing roman values of digits from 0 - 9 when placed at different places ; Converting to roman ; Driver Code
< ? php function intToRoman ( $ num ) { $ m = array ( " " , " M " , " MM " , " MMM " ) ; $ c = array ( " " , " C " , " CC " , " CCC " , " CD " , " D " , " DC " , " DCC " , " DCCC " , " CM " ) ; $ x = array ( " " , " X " , " XX " , " XXX " , " XL " , " L " , " LX " , " LXX " , " LXXX " , " XC " ) ; $ i = array ( " " , " I " , " II " , " III " , " IV " , " V " , " VI " , " VII " , " VIII " , " IX " ) ; $ thousands = $ m [ $ num / 1000 ] ; $ hundereds = $ c [ ( $ num % 1000 ) / 100 ] ; $ tens = $ x [ ( $ num % 100 ) / 10 ] ; $ ones = $ i [ $ num % 10 ] ; $ ans = $ thousands . $ hundereds . $ tens . $ ones ; return $ ans ; } $ number = 3549 ; echo intToRoman ( $ number ) ;
Print string of odd length in ' X ' format | Function to print given string in cross pattern ; i and j are the indexes of characters to be displayed in the ith iteration i = 0 initially and go upto length of string j = length of string initially in each iteration of i , we increment i and decrement j , we print character only of k == i or k == j ; Driver code
< ? php function pattern ( $ str , $ len ) { for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { $ j = $ len - 1 - $ i ; for ( $ k = 0 ; $ k < $ len ; $ k ++ ) { if ( $ k == $ i $ k == $ j ) echo $ str [ $ k ] ; else echo " ▁ " ; } echo " STRNEWLINE " ; } } $ str = " geeksforgeeks " ; $ len = strlen ( $ str ) ; pattern ( $ str , $ len ) ; ? >
Find the smallest window in a string containing all characters of another string | PHP program to find smallest window containing all characters of a pattern . ; Function to find smallest window containing all characters of ' pat ' ; check if string ' s ▁ length ▁ is ▁ less ▁ ▁ than ▁ pattern ' s length . If yes then no such window can exist ; store occurrence ofs characters of pattern ; start traversing the string $count = 0 ; count of characters ; count occurrence of characters of string ; If string ' s ▁ char ▁ matches ▁ with ▁ ▁ pattern ' s char then increment count ; if all the characters are matched ; Try to minimize the window i . e . , check if any character is occurring more no . of times than its occurrence in pattern , if yes then remove it from starting and also remove the useless characters . ; update window size ; If no window found ; Return substring starting from start_index and length min_len ; Driver code
< ? php define ( " no _ of _ chars " , 256 ) ; function findSubString ( & $ str , & $ pat ) { $ len1 = strlen ( $ str ) ; $ len2 = strlen ( $ pat ) ; if ( $ len1 < $ len2 ) { echo " No ▁ such ▁ window ▁ exists " ; return " " ; } $ hash_pat = array_fill ( 0 , no_of_chars , 0 ) ; $ hash_str = array_fill ( 0 , no_of_chars , 0 ) ; for ( $ i = 0 ; $ i < $ len2 ; $ i ++ ) $ hash_pat [ ord ( $ pat [ $ i ] ) ] ++ ; $ start = 0 ; $ start_index = -1 ; $ min_len = PHP_INT_MAX ; for ( $ j = 0 ; $ j < $ len1 ; $ j ++ ) { $ hash_str [ ord ( $ str [ $ j ] ) ] ++ ; if ( $ hash_str [ ord ( $ str [ $ j ] ) ] <= $ hash_pat [ ord ( $ str [ $ j ] ) ] ) $ count ++ ; if ( $ count == $ len2 ) { while ( $ hash_str [ ord ( $ str [ $ start ] ) ] > $ hash_pat [ ord ( $ str [ $ start ] ) ] || $ hash_pat [ ord ( $ str [ $ start ] ) ] == 0 ) { if ( $ hash_str [ ord ( $ str [ $ start ] ) ] > $ hash_pat [ ord ( $ str [ $ start ] ) ] ) $ hash_str [ ord ( $ str [ $ start ] ) ] -- ; $ start ++ ; } $ len_window = $ j - $ start + 1 ; if ( $ min_len > $ len_window ) { $ min_len = $ len_window ; $ start_index = $ start ; } } } if ( $ start_index == -1 ) { echo " No ▁ such ▁ window ▁ exists " ; return " " ; } return substr ( $ str , $ start_index , $ min_len ) ; } $ str = " this ▁ is ▁ a ▁ test ▁ string " ; $ pat = " tist " ; echo " Smallest ▁ window ▁ is ▁ : ▁ STRNEWLINE " . findSubString ( $ str , $ pat ) ; ? >
Prime numbers after prime P with sum S | vector to store prime and N primes whose sum equals given S ; function to check prime number ; square root of x ; since 1 is not prime number ; if any factor is found return false ; no factor found ; function to display N primes whose sum equals S ; function to evaluate all possible N primes whose sum equals S ; if total equals S And total is reached using N primes ; display the N primes ; if total is greater than S or if index has reached last element ; add prime [ index ] to set vector ; include the ( index ) th prime to total ; remove element from set vector ; exclude ( index ) th prime ; function to generate all primes ; all primes less than S itself ; if i is prime add it to prime vector ; if primes are less than N ; Driver Code
< ? php $ set = array ( ) ; $ prime = array ( ) ; function isPrime ( $ x ) { $ sqroot = sqrt ( $ x ) ; $ flag = true ; if ( $ x == 1 ) return false ; for ( $ i = 2 ; $ i <= $ sqroot ; $ i ++ ) if ( $ x % $ i == 0 ) return false ; return true ; } function display ( ) { global $ set , $ prime ; $ length = count ( $ set ) ; for ( $ i = 0 ; $ i < $ length ; $ i ++ ) echo ( $ set [ $ i ] . " ▁ " ) ; echo ( " STRNEWLINE " ) ; } function primeSum ( $ total , $ N , $ S , $ index ) { global $ set , $ prime ; if ( $ total == $ S && count ( $ set ) == $ N ) { display ( ) ; return ; } if ( $ total > $ S || $ index == count ( $ prime ) ) return ; array_push ( $ set , $ prime [ $ index ] ) ; primeSum ( $ total + $ prime [ $ index ] , $ N , $ S , $ index + 1 ) ; array_pop ( $ set ) ; primeSum ( $ total , $ N , $ S , $ index + 1 ) ; } function allPrime ( $ N , $ S , $ P ) { global $ set , $ prime ; for ( $ i = $ P + 1 ; $ i <= $ S ; $ i ++ ) { if ( isPrime ( $ i ) ) array_push ( $ prime , $ i ) ; } if ( count ( $ prime ) < $ N ) return ; primeSum ( 0 , $ N , $ S , 0 ) ; } $ S = 54 ; $ N = 2 ; $ P = 3 ; allPrime ( $ N , $ S , $ P ) ; ? >
Find 2 ^ ( 2 ^ A ) % B | Function to return 2 ^ ( 2 ^ A ) % B ; $Base case , 2 ^ ( 2 ^ 1 ) % B = 4 % B ; Driver code ; Print 2 ^ ( 2 ^ $A ) % $B
< ? php function F ( $ A , $ B ) { if ( $ A == 1 ) return ( 4 % $ B ) ; else { $ temp = F ( $ A - 1 , $ B ) ; return ( $ temp * $ temp ) % $ B ; } } $ A = 25 ; $ B = 50 ; echo F ( $ A , $ B ) ;
Sum of i * countDigits ( i ) ^ 2 for all i in range [ L , R ] | PHP implementation of the approach ; Function to return the required sum ; If range is valid ; Sum of AP ; Driver code
< ? php $ MOD = 1000000007 ; function rangeSum ( $ l , $ r ) { global $ MOD ; $ a = 1 ; $ b = 9 ; $ res = 0 ; for ( $ i = 1 ; $ i <= 10 ; $ i ++ ) { $ L = max ( $ l , $ a ) ; $ R = min ( $ r , $ b ) ; if ( $ L <= $ R ) { $ sum = ( $ L + $ R ) * ( $ R - $ L + 1 ) / 2 ; $ res += ( $ i * $ i ) * ( $ sum % $ MOD ) ; $ res %= $ MOD ; } $ a = $ a * 10 ; $ b = $ b * 10 + 9 ; } return $ res ; } $ l = 98 ; $ r = 102 ; echo rangeSum ( $ l , $ r ) ; ? >
Generate a random permutation of elements from range [ L , R ] ( Divide and Conquer ) | To store the random permutation ; Utility function to print the generated permutation ; Function to return a random number between x and y ; Recursive function to generate the random permutation ; Base condition ; Random number returned from the function ; Inserting random number in vector ; Recursion call for [ l , n - 1 ] ; Recursion call for [ n + 1 , r ] ; Driver code ; Generate permutation ; Print the generated permutation
< ? php $ permutation = array ( ) ; function printPermutation ( ) { global $ permutation ; foreach ( $ permutation as $ i ) echo $ i . " " ; } function give_random_number ( $ l , $ r ) { $ x = rand ( ) % ( $ r - $ l + 1 ) + $ l ; return $ x ; } function generate_random_permutation ( $ l , $ r ) { global $ permutation ; if ( $ l > $ r ) return ; $ n = give_random_number ( $ l , $ r ) ; array_push ( $ permutation , $ n ) ; generate_random_permutation ( $ l , $ n - 1 ) ; generate_random_permutation ( $ n + 1 , $ r ) ; } $ l = 5 ; $ r = 15 ; generate_random_permutation ( $ l , $ r ) ; printPermutation ( ) ; ? >