codeparrot/xlcost-text-to-code huggingface dataset:
Check if a large number is divisible by 13 or not | Returns true if number is divisible by 13 else returns false ; Append required 0 s at the beginning . ; Same as strcat ( num , "00" ) ; in c . ; Same as strcat ( num , "0" ) ; in c . ; Alternatively add / subtract digits in group of three to result . ; Store group of three numbers in group variable . ; Generate alternate series of plus and minus ; Driver code
< ? php function checkDivisibility ( $ num ) { $ length = strlen ( $ num ) ; if ( $ length == 1 && $ num [ 0 ] == '0' ) return true ; if ( $ length % 3 == 1 ) { $ num += "00" ; $ length += 2 ; } else if ( $ length % 3 == 2 ) { $ num += "0" ; $ length += 1 ; } $ sum = 0 ; $ p = 1 ; for ( $ i = $ length - 1 ; $ i >= 0 ; $ i -- ) { $ group = 0 ; $ group += $ num [ $ i -- ] - '0' ; $ group += ( $ num [ $ i -- ] - '0' ) * 10 ; $ group += ( $ num [ $ i ] - '0' ) * 100 ; $ sum = $ sum + $ group * $ p ; $ p *= ( -1 ) ; } $ sum = abs ( $ sum ) ; return ( $ sum % 13 == 0 ) ; } $ number = "83959092724" ; if ( checkDivisibility ( $ number ) ) echo ( $ number . " ▁ is ▁ divisible ▁ by ▁ 13 . " ) ; else echo ( $ number . " ▁ is ▁ not ▁ divisible ▁ by ▁ 13 . " ) ; ? >
Given two numbers a and b find all x such that a % x = b | PHP program to find x such that a % x is equal to b . ; if a is less than b then no solution ; if a is equal to b then every number greater than a will be the solution so its infinity ; count variable store the number of values possible ; checking for both divisor and quotient whether they divide ( a - b ) completely and greater than b . ; Here y is added twice in the last iteration so 1 y should be decremented to get correct solution ; Driver Code
< ? php function modularEquation ( $ a , $ b ) { if ( $ a < $ b ) { echo " No ▁ solution ▁ possible ▁ " ; return ; } if ( $ a == $ b ) { echo " Infinite ▁ Solution ▁ possible ▁ " ; return ; } $ count = 0 ; $ n = $ a - $ b ; $ y = sqrt ( $ a - $ b ) ; for ( $ i = 1 ; $ i <= $ y ; ++ $ i ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i > $ b ) $ count ++ ; if ( $ i > $ b ) $ count ++ ; } } if ( $ y * $ y == $ n && $ y > $ b ) $ count -- ; echo $ count ; } $ a = 21 ; $ b = 5 ; modularEquation ( $ a , $ b ) ; ? >
Count different numbers that can be generated such that there digits sum is equal to ' n ' | Function to count ' num ' as sum of digits ( 1 , 2 , 3 , 4 ) ; Initialize dp [ ] array ; Base case ; Initialize the current dp [ ] array as '0' ; if i == j then there is only one way to write with element itself ' i ' ; If j == 1 , then there exist two ways , one from '1' and other from '4' ; if i - j is positive then pick the element from ' i - j ' element of dp [ ] array ; Check for modulas ; return the final answer ; Driver code
< ? php function countWays ( $ num ) { $ dp [ $ num + 1 ] = array ( ) ; $ MOD = 100000000 + 7 ; $ dp [ 1 ] = 2 ; for ( $ i = 2 ; $ i <= $ num ; ++ $ i ) { $ dp [ $ i ] = 0 ; for ( $ j = 1 ; $ j <= 3 ; ++ $ j ) { if ( $ i - $ j == 0 ) $ dp [ $ i ] += 1 ; else if ( $ j == 1 ) $ dp [ $ i ] += $ dp [ $ i - $ j ] * 2 ; else if ( $ i - $ j > 0 ) $ dp [ $ i ] += $ dp [ $ i - $ j ] ; if ( $ dp [ $ i ] >= $ MOD ) $ dp [ $ i ] %= $ MOD ; } } return $ dp [ $ num ] ; } $ n = 3 ; echo countWays ( $ n ) ; ? >
Check whether a number can be represented by sum of two squares | Check whether a number can be represented by sum of two squares using Fermat Theorem . ; Count all the prime factors . ; Ifany prime factor of the form ( 4 k + 3 ) ( 4 k + 3 ) occurs an odd number of times . ; If n itself is a x prime number and can be expressed in the form of 4 k + 3 we return false . ; Driver Code
< ? php function judgeSquareSum ( $ n ) { for ( $ i = 2 ; $ i * $ i <= $ n ; $ i ++ ) { $ count = 0 ; if ( $ n % $ i == 0 ) { while ( $ n % $ i == 0 ) { $ count ++ ; $ n = ( int ) $ n / $ i ; } if ( $ i % 4 == 3 && $ count % 2 != 0 ) return false ; } } return $ n % 4 != 3 ; } $ n = 17 ; if ( judgeSquareSum ( $ n ) ) echo " Yes " ; else echo " No " ; ? >
Total no of 1 's in numbers | function to count the frequency of 1. ; Driver Code
< ? php function countDigitOne ( $ n ) { $ countr = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i *= 10 ) { $ divider = $ i * 10 ; $ countr += ( int ) ( $ n / $ divider ) * $ i + min ( max ( $ n % $ divider - $ i + 1 , 0 ) , $ i ) ; } return $ countr ; } $ n = 13 ; echo countDigitOne ( $ n ) , " STRNEWLINE " ; $ n = 113 ; echo countDigitOne ( $ n ) , " STRNEWLINE " ; $ n = 205 ; echo countDigitOne ( $ n ) , " STRNEWLINE " ; ? >
Largest number with prime digits | check if character is prime ; replace with previous prime character ; if 2 erase s [ i ] and replace next with 7 ; find first non prime char ; find first char greater than 2 ; like 20 ; like 7721 ; replace remaining with 7 ; Driver code
< ? php function isPrime ( $ c ) { return ( $ c == '2' $ c == '3' $ c == '5' $ c == '7' ) ? 1 : 0 ; } function decrease ( $ s , $ i ) { if ( $ s [ $ i ] <= '2' ) { $ s [ $ i ] = ' * ' ; $ a = str_split ( $ s ) ; $ s = " " ; for ( $ h = 0 ; $ h < count ( $ a ) ; $ h ++ ) if ( $ a [ $ h ] != ' * ' ) $ s = $ s . $ a [ $ h ] ; $ s [ $ i ] = '7' ; } else if ( $ s [ $ i ] == '3' ) $ s [ $ i ] = '2' ; else if ( $ s [ $ i ] <= '5' ) $ s [ $ i ] = '3' ; else if ( $ s [ $ i ] <= '7' ) $ s [ $ i ] = '5' ; else $ s [ $ i ] = '7' ; return $ s ; } function primeDigits ( $ s ) { for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( isPrime ( $ s [ $ i ] ) == 0 ) { while ( $ i >= 0 && $ s [ $ i ] <= '2' ) -- $ i ; if ( $ i < 0 ) { $ i = 0 ; $ s = decrease ( $ s , $ i ) ; } else $ s = decrease ( $ s , $ i ) ; for ( $ j = $ i + 1 ; $ j < strlen ( $ s ) ; $ j ++ ) $ s [ $ j ] = '7' ; break ; } } return $ s ; } $ s = "45" ; echo primeDigits ( $ s ) . " STRNEWLINE " ; $ s = "1000" ; echo primeDigits ( $ s ) . " STRNEWLINE " ; $ s = "7721" ; echo primeDigits ( $ s ) . " STRNEWLINE " ; $ s = "7221" ; echo primeDigits ( $ s ) . " STRNEWLINE " ; $ s = "74545678912345689748593275897894708927680" ; echo primeDigits ( $ s ) ; ? >
Print digit 's position to be removed to make a number divisible by 6 | Function to print the number divisible by 6 after exactly removing a digit ; stores the sum of all elements ; traverses the string and converts string to number array and sums up ; if ( $a [ $n - 1 ] % 2 ) ODD CHECK ; if second last is odd or sum of n - 1 elements are not divisible by 3. ; second last is even and print n - 1 elements removing last digit ; last digit removed ; counter to check if any element after removing , its sum % 3 == 0 ; traverse till second last element ; to check if any element after removing , its sum % 3 == 0 ; the leftmost element ; break at the leftmost element ; stores the right most element ; if no element has been found as a [ i + 1 ] > a [ i ] ; if second last is even , then remove last if ( sum - last ) % 3 == 0 ; if no element which on removing gives sum % 3 == 0 ; Driver Code
< ? php function greatest ( $ s ) { $ n = strlen ( $ s ) ; $ a [ $ n ] = array ( ) ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ a [ $ i ] = $ s [ $ i ] - '0' ; $ sum += $ a [ $ i ] ; } { if ( $ a [ $ n - 2 ] % 2 != 0 or ( $ sum - $ a [ $ n - 1 ] ) % 3 != 0 ) { echo " - 1" , " STRNEWLINE " ; } else { echo $ n , " STRNEWLINE " ; } } else { $ re = $ sum % 3 ; $ del = -1 ; $ flag = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( ( $ a [ $ i ] ) % 3 == $ re ) { if ( $ a [ $ i + 1 ] > $ a [ $ i ] ) { $ del = $ i ; $ flag = 1 ; break ; } else { $ del = $ i ; } } } if ( $ flag == 0 ) { if ( $ a [ $ n - 2 ] % 2 == 0 and $ re == $ a [ $ n - 1 ] % 3 ) $ del = $ n - 1 ; } if ( $ del == -1 ) echo - 1 , " STRNEWLINE " ; else { echo $ del + 1 , " STRNEWLINE " ; } } } $ s = "7510222" ; greatest ( $ s ) ; ? >
Representation of a number in powers of other | PHP program to check if m can be represented as powers of w . ; break ; None of 3 worked . ; If m is not zero means , it can 't be  represented in terms of powers of w. ; Driver code
< ? php function asPowerSum ( $ w , $ m ) { while ( $ m ) { if ( ( $ m - 1 ) % $ w == 0 ) $ m = ( $ m - 1 ) / $ w ; else if ( ( $ m + 1 ) % $ w == 0 ) $ m = ( $ m + 1 ) / $ w ; else if ( $ m % $ w == 0 ) $ m = $ m / $ w ; else } return ( $ m == 0 ) ; } $ w = 3 ; $ m = 7 ; if ( asPowerSum ( $ w , $ m ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; ? >
Number of digits to be removed to make a number divisible by 3 | function to count the no of removal of digits to make a very large number divisible by 3 ; add up all the digits of num ; if num is already is divisible by 3 then no digits are to be removed ; if there is single digit , then it is not possible to remove one digit . ; traverse through the number and find out if any number on removal makes the sum divisible by 3 ; if there are two numbers then it is not possible to remove two digits . ; Otherwise we can always make a number multiple of 2 by removing 2 digits . ; Driver Code
< ? php function divisible ( $ num ) { $ n = strlen ( $ num ) ; $ sum = ( $ num ) ; ( $ num ) ; 0 - '0' ; if ( $ sum % 3 == 0 ) return 0 ; if ( $ n == 1 ) return -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ sum % 3 == ( $ num [ $ i ] - '0' ) % 3 ) return 1 ; if ( $ n == 2 ) return -1 ; return 2 ; } $ num = "1234" ; echo divisible ( $ num ) ; ? >
Program for dot product and cross product of two vectors | PHP implementation for dot product and cross product of two vector . ; Function that return dot product of two vector array . ; Loop for calculate cot product ; Function to find cross product of two vector array . ; Driver Code ; dotproduct function call ; crossproduct function call ; Loop that print cross product of two vector array .
< ? php $ n = 3 ; function dotproduct ( $ vect_A , $ vect_B ) { global $ n ; $ product = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ product = $ product + $ vect_A [ $ i ] * $ vect_B [ $ i ] ; return $ product ; } function crossproduct ( $ vect_A , $ vect_B , $ cross_P ) { $ cross_P [ 0 ] = $ vect_A [ 1 ] * $ vect_B [ 2 ] - $ vect_A [ 2 ] * $ vect_B [ 1 ] ; $ cross_P [ 1 ] = $ vect_A [ 2 ] * $ vect_B [ 0 ] - $ vect_A [ 0 ] * $ vect_B [ 2 ] ; $ cross_P [ 2 ] = $ vect_A [ 0 ] * $ vect_B [ 1 ] - $ vect_A [ 1 ] * $ vect_B [ 0 ] ; return $ cross_P ; } $ vect_A = array ( 3 , -5 , 4 ) ; $ vect_B = array ( 2 , 6 , 5 ) ; $ cross_P = array_fill ( 0 , $ n , 0 ) ; echo " Dot ▁ product : " ; echo dotproduct ( $ vect_A , $ vect_B ) ; echo " Cross product : " $ cross_P = crossproduct ( $ vect_A , $ vect_B , $ cross_P ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ cross_P [ $ i ] . " ▁ " ; ? >
Count ways to express even number â €˜ nâ €™ as sum of even integers | Initialize mod variable as constant ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; 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 ; Return number of ways to write ' n ' as sum of even integers ; Driver code
< ? php $ MOD = 1000000000.0 ; function power ( $ x , $ y , $ p ) { $ res = 1 ; $ x = $ x % $ p ; while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( 1 * $ res * $ x ) % $ p ; $ x = ( 1 * $ x * $ x ) % $ p ; } return $ res ; } function countEvenWays ( $ n ) { global $ MOD ; return power ( 2 , $ n / 2 - 1 , $ MOD ) ; } $ n = 6 ; echo countEvenWays ( $ n ) , " STRNEWLINE " ; $ n = 8 ; echo countEvenWays ( $ n ) ; ? >
Number of steps to convert to prime factors | PHP program to count number of steps required to convert an integer array to array of factors . ; array to store prime factors ; function to generate all prime factors of numbers from 1 to 10 ^ 6 ; Initializes all the positions with their value . ; Initializes all multiples of 2 with 2 ; A modified version of Sieve of Eratosthenes to store the smallest prime factor that divides every number . ; check if it has no prime factor . ; Initializes of j starting from i * i ; if it has no prime factor before , then stores the smallest prime divisor ; function to calculate the number of representations ; keep an count of prime factors ; traverse for every element ; count the no of factors ; subtract 1 if Ai is not 1 as the last step wont be taken into count ; call sieve to calculate the factors
< ? php $ MAX = 100001 ; $ factor = array_fill ( 0 , $ MAX + 1 , 0 ) ; function cal_factor ( ) { global $ factor , $ MAX ; $ factor [ 1 ] = 1 ; for ( $ i = 2 ; $ i < $ MAX ; $ i ++ ) $ factor [ $ i ] = $ i ; for ( $ i = 4 ; $ i < $ MAX ; $ i += 2 ) $ factor [ $ i ] = 2 ; for ( $ i = 3 ; $ i * $ i < $ MAX ; $ i ++ ) { if ( $ factor [ $ i ] == $ i ) { for ( $ j = $ i * $ i ; $ j < $ MAX ; $ j += $ i ) { if ( $ factor [ $ j ] == $ j ) $ factor [ $ j ] = $ i ; } } } } function no_of_representations ( $ a , $ n ) { global $ factor , $ MAX ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ temp = $ a [ $ i ] ; $ flag = 0 ; while ( $ factor [ $ temp ] != 1 ) { $ flag = -1 ; $ count ++ ; $ temp = ( int ) ( $ temp / $ factor [ $ temp ] ) ; } $ count += $ flag ; } return $ count ; } cal_factor ( ) ; $ a = array ( 4 , 4 , 4 ) ; $ n = count ( $ a ) ; echo no_of_representations ( $ a , $ n ) ; ? >
Subsequences of size three in an array whose sum is divisible by m | Brute Force Implementation ( Time Complexity : O ( N ^ 3 ) ) PHP program to find count of subsequences of size three divisible by M . ; Three nested loop to find all the sub sequences of length three in the given array A [ ] . ; checking if the sum of the chosen three number is divisible by m . ; Driver code
< ? php function coutSubSeq ( $ A , $ N , $ M ) { $ sum = 0 ; $ ans = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ N ; $ j ++ ) { for ( $ k = $ j + 1 ; $ k < $ N ; $ k ++ ) { $ sum = $ A [ $ i ] + $ A [ $ j ] + $ A [ $ k ] ; if ( $ sum % $ M == 0 ) $ ans ++ ; } } } return $ ans ; } $ M = 3 ; $ A = array ( 1 , 2 , 4 , 3 ) ; $ N = count ( $ A ) ; echo coutSubSeq ( $ A , $ N , $ M ) ; ? >
Subsequences of size three in an array whose sum is divisible by m | O ( M ^ 2 ) time complexity PHP program to find count of subsequences of size three divisible by M . ; Storing frequencies of all remainders when divided by M . ; including i and j in the sum rem calculate the remainder required to make the sum divisible by M ; if the required number is less than j , we skip as we have already calculated for that value before . As j here starts with i and rem is less than j . ; if satisfies the first case . ; if satisfies the second case ; if satisfies the third case ; Driver code
< ? php function countSubSeq ( $ A , $ N , $ M ) { $ ans = 0 ; $ h = array_fill ( 0 , $ M , 0 ) ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ A [ $ i ] = $ A [ $ i ] % $ M ; $ h [ $ A [ $ i ] ] ++ ; } for ( $ i = 0 ; $ i < $ M ; $ i ++ ) { for ( $ j = $ i ; $ j < $ M ; $ j ++ ) { $ rem = ( $ M - ( $ i + $ j ) % $ M ) % $ M ; if ( $ rem < $ j ) continue ; if ( $ i == $ j && $ rem == $ j ) $ ans += $ h [ $ i ] * ( $ h [ $ i ] - 1 ) * ( $ h [ $ i ] - 2 ) / 6 ; else if ( $ i == $ j ) $ ans += $ h [ $ i ] * ( $ h [ $ i ] - 1 ) * $ h [ $ rem ] / 2 ; else if ( $ i == $ rem ) $ ans += $ h [ $ i ] * ( $ h [ $ i ] - 1 ) * $ h [ $ j ] / 2 ; else if ( $ rem == $ j ) $ ans += $ h [ $ j ] * ( $ h [ $ j ] - 1 ) * $ h [ $ i ] / 2 ; else $ ans = $ ans + $ h [ $ i ] * $ h [ $ j ] * $ h [ $ rem ] ; } } return $ ans ; } $ M = 3 ; $ A = array ( 1 , 2 , 4 , 3 ) ; $ N = count ( $ A ) ; echo countSubSeq ( $ A , $ N , $ M ) ; ? >
Find n | utility function ; since first element of the series is 7 , we initialise a variable with 7 ; Using iteration to find nth term ; Driver Code
< ? php function findTerm ( $ n ) { if ( $ n == 1 ) return $ n ; else { $ term = 7 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ term = $ term * 2 + ( $ i - 1 ) ; return $ term ; } } $ n = 5 ; echo ( findTerm ( $ n ) ) ; ? >
Find n | Returns n - th number in sequence 1 , 1 , 2 , 1 , 2 , 3 , 1 , 2 , 4 , ... ; One by one subtract counts elements in different blocks ; Driver code
< ? php function findNumber ( $ n ) { $ n -- ; $ i = 1 ; while ( $ n >= 0 ) { $ n -= $ i ; ++ $ i ; } return ( $ n + $ i ) ; } $ n = 3 ; echo findNumber ( $ n ) ; ? >
Program to find correlation coefficient | function that returns correlation coefficient . ; sum of elements of array X . ; sum of elements of array Y . ; sum of X [ i ] * Y [ i ] . ; sum of square of array elements . ; use formula for calculating correlation coefficient . ; Driver Code ; Find the size of array . ; Function call to correlationCoefficient .
< ? php function correlationCoefficient ( $ X , $ Y , $ n ) { $ sum_X = 0 ; $ sum_Y = 0 ; $ sum_XY = 0 ; $ squareSum_X = 0 ; $ squareSum_Y = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum_X = $ sum_X + $ X [ $ i ] ; $ sum_Y = $ sum_Y + $ Y [ $ i ] ; $ sum_XY = $ sum_XY + $ X [ $ i ] * $ Y [ $ i ] ; $ squareSum_X = $ squareSum_X + $ X [ $ i ] * $ X [ $ i ] ; $ squareSum_Y = $ squareSum_Y + $ Y [ $ i ] * $ Y [ $ i ] ; } $ corr = ( float ) ( $ n * $ sum_XY - $ sum_X * $ sum_Y ) / sqrt ( ( $ n * $ squareSum_X - $ sum_X * $ sum_X ) * ( $ n * $ squareSum_Y - $ sum_Y * $ sum_Y ) ) ; return $ corr ; } $ X = array ( 15 , 18 , 21 , 24 , 27 ) ; $ Y = array ( 25 , 25 , 27 , 31 , 32 ) ; $ n = sizeof ( $ X ) ; echo correlationCoefficient ( $ X , $ Y , $ n ) ; ? >
Find the number of spectators standing in the stadium at time t | PHP program to find number of spectators standing at a time ; If the time is less than k then we can print directly t time . ; If the time is n then k spectators are standing . ; Otherwise we calculate the spectators standing . ; Stores the value of n , k and t t is time n & k is the number of specators
< ? php function result ( $ n , $ k , $ t ) { if ( $ t <= $ k ) echo t ; else if ( $ t <= $ n ) echo k ; else { $ temp = $ t - $ n ; $ temp = $ k - $ temp ; echo $ temp ; } } $ n = 10 ; $ k = 5 ; $ t = 12 ; result ( $ n , $ k , $ t ) ; ? >
Program for weighted mean of natural numbers . | Function to calculate weighted mean . ; Take num array and corresponding weight array and initialize it . ; Calculate the size of array . ; Check the size of both array is equal or not .
< ? php function weightedMean ( $ X , $ W , $ n ) { $ sum = 0 ; $ numWeight = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ numWeight = $ numWeight + $ X [ $ i ] * $ W [ $ i ] ; $ sum = $ sum + $ W [ $ i ] ; } return ( float ) ( $ numWeight / $ sum ) ; } $ X = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ) ; $ W = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ) ; $ n = sizeof ( $ X ) ; $ m = sizeof ( $ W ) ; if ( $ n == $ m ) echo ( weightedMean ( $ X , $ W , $ n ) ) ; else echo ( " - 1" ) ; ? >
Program to find GCD of floating point numbers | Recursive function to return gcd of a and b ; base case ; Driver Code
< ? php function gcd ( $ a , $ b ) { if ( $ a < $ b ) return gcd ( $ b , $ a ) ; if ( abs ( $ b ) < 0.001 ) return $ a ; else return ( gcd ( $ b , $ a - floor ( $ a / $ b ) * $ b ) ) ; } $ a = 1.20 ; $ b = 22.5 ; echo gcd ( $ a , $ b ) ; ? >
Program for harmonic mean of numbers | Function that returns harmonic mean . ; Declare sum variables and initialize with zero ; Driver code
< ? php function harmonicMean ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum = $ sum + ( float ) ( 1 / $ arr [ $ i ] ) ; return ( float ) ( $ n / $ sum ) ; } $ arr = array ( 13.5 , 14.5 , 14.8 , 15.2 , 16.1 ) ; $ n = sizeof ( $ arr ) ; echo ( harmonicMean ( $ arr , $ n ) ) ; ? >
Program for harmonic mean of numbers | Function that returns harmonic mean . ; Driver code
< ? php function harmonicMean ( $ arr , $ freq , $ n ) { $ sum = 0 ; $ frequency_sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum = $ sum + ( float ) ( $ freq [ $ i ] / $ arr [ $ i ] ) ; $ frequency_sum = $ frequency_sum + $ freq [ $ i ] ; } return ( $ frequency_sum / $ sum ) ; } $ num = array ( 13 , 14 , 15 , 16 , 17 ) ; $ freq = array ( 2 , 5 , 13 , 7 , 3 ) ; $ n = sizeof ( $ num ) ; echo ( harmonicMean ( $ num , $ freq , $ n ) ) ; ? >
First collision point of two series | PHP program to calculate the colliding point of two series ; Iterating through n terms of the first series ; x is i - th term of first series ; d is first element of second series and c is common difference for second series . ; If no term of first series is found ; Driver Code
< ? php function point ( $ a , $ b , $ c , $ d , $ n ) { $ x ; $ flag = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ x = $ b + $ i * $ a ; if ( ( $ x - $ d ) % $ c == 0 and $ x - $ d >= 0 ) { echo $ x ; $ flag = 1 ; break ; } } if ( $ flag == 0 ) { echo " No ▁ collision ▁ po $ " ; } } $ a = 20 ; $ b = 2 ; $ c = 9 ; $ d = 19 ; $ n = 20 ; point ( $ a , $ b , $ c , $ d , $ n ) ; ? >
Armstrong Numbers between two integers | Prints Armstrong Numbers in given range ; number of digits calculation ; compute sum of nth power of its digits ; checks if number i is equal to the sum of nth power of its digits ; Driver code
< ? php function findArmstrong ( $ low , $ high ) { for ( $ i = $ low + 1 ; $ i < $ high ; ++ $ i ) { $ x = $ i ; $ n = 0 ; while ( $ x != 0 ) { $ x = ( int ) ( $ x / 10 ) ; ++ $ n ; } $ pow_sum = 0 ; $ x = $ i ; while ( $ x != 0 ) { $ digit = $ x % 10 ; $ pow_sum += ( int ) ( pow ( $ digit , $ n ) ) ; $ x = ( int ) ( $ x / 10 ) ; } if ( $ pow_sum == $ i ) echo $ i . " " ; } } $ num1 = 100 ; $ num2 = 400 ; findArmstrong ( $ num1 , $ num2 ) ; ? >
Pair with maximum GCD from two arrays | Find the maximum GCD pair with maximum sum ; array to keep a count of existing elements ; first [ i ] and second [ i ] are going to store maximum multiples of i in a [ ] and b [ ] respectively . ; traverse through the first array to mark the elements in cnt ; Find maximum multiple of every number in first array ; Find maximum multiple of every number in second array We re - initialise cnt [ ] and traverse through the second array to mark the elements in cnt ; if the multiple is present in the second array then store the max of number or the pre - existing element ; traverse for every elements and checks the maximum N that is present in both the arrays ; Driver code ; Maximum possible value of elements in both arrays .
< ? php function gcdMax ( $ a , $ b , $ n , $ N ) { $ cnt = array_fill ( 0 , $ N , 0 ) ; $ first = array_fill ( 0 , $ N , 0 ) ; $ second = array_fill ( 0 , $ N , 0 ) ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) $ cnt [ $ a [ $ i ] ] = 1 ; for ( $ i = 1 ; $ i < $ N ; ++ $ i ) for ( $ j = $ i ; $ j < $ N ; $ j += $ i ) if ( $ cnt [ $ j ] ) $ first [ $ i ] = max ( $ first [ $ i ] , $ j ) ; $ cnt = array_fill ( 0 , $ N , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ cnt [ $ b [ $ i ] ] = 1 ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) for ( $ j = $ i ; $ j < $ N ; $ j += $ i ) if ( $ cnt [ $ j ] ) $ second [ $ i ] = max ( $ second [ $ i ] , $ j ) ; $ x = $ N - 1 ; for ( ; $ x >= 0 ; $ x -- ) if ( $ first [ $ x ] && $ second [ $ x ] ) break ; echo $ first [ $ x ] . " ▁ " . $ second [ $ x ] . " STRNEWLINE " ; } $ a = array ( 3 , 1 , 4 , 2 , 8 ) ; $ b = array ( 5 , 2 , 12 , 8 , 3 ) ; $ n = sizeof ( $ a ) ; $ N = 20 ; gcdMax ( $ a , $ b , $ n , $ N ) ; ? >
Pierpont Prime | PHP program to print Pierpont prime numbers smaller than n . ; Finding all numbers having factor power of 2 and 3 Using sieve ; Storing number of the form 2 ^ i . 3 ^ k + 1. ; Finding prime number using sieve of Eratosthenes . Reusing same array as result of above computations in v . ; Printing n pierpont primes smaller than n ; Driver Code
< ? php function printPierpont ( $ n ) { $ arr = array_fill ( 0 , $ n + 1 , false ) ; $ two = 1 ; $ three = 1 ; while ( $ two + 1 < $ n ) { $ arr [ $ two ] = true ; while ( $ two * $ three + 1 < $ n ) { $ arr [ $ three ] = true ; $ arr [ $ two * $ three ] = true ; $ three *= 3 ; } $ three = 1 ; $ two *= 2 ; } $ v ; $ x = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] ) $ v [ $ x ++ ] = $ i + 1 ; $ arr1 = array_fill ( 0 , count ( $ arr ) , false ) ; for ( $ p = 2 ; $ p * $ p < $ n ; $ p ++ ) { if ( $ arr1 [ $ p ] == false ) for ( $ i = $ p * 2 ; $ i < $ n ; $ i += $ p ) $ arr1 [ $ i ] = true ; } for ( $ i = 0 ; $ i < $ x ; $ i ++ ) if ( ! $ arr1 [ $ v [ $ i ] ] ) echo $ v [ $ i ] . " ▁ " ; } $ n = 200 ; printPierpont ( $ n ) ; ? >