codeparrot/xlcost-text-to-code huggingface dataset:
Numbers having difference with digit sum more than s | function for digit sum ; function to calculate count of integer s . t . integer - digSum > s ; if n < s no integer possible ; iterate for s range and then calculate total count of such integer if starting integer is found ; if no integer found return 0 ; Driver Code
< ? php function digitSum ( $ n ) { $ digSum = 0 ; while ( $ n ) { $ digSum += $ n % 10 ; $ n /= 10 ; } return $ digSum ; } function countInteger ( $ n , $ s ) { if ( $ n < $ s ) return 0 ; for ( $ i = $ s ; $ i <= min ( $ n , $ s + 163 ) ; $ i ++ ) if ( ( $ i - digitSum ( $ i ) ) > $ s ) return ( $ n - $ i + 1 ) ; return 0 ; } $ n = 1000 ; $ s = 100 ; echo countInteger ( $ n , $ s ) ; ? >
Division without using ' / ' operator | Function to find division without using ' / ' operator ; Handling negative numbers ; if num1 is greater than equal to num2 subtract num2 from num1 and increase quotient by one . ; checking if neg equals to 1 then making quotient negative ; Driver program
< ? php function division ( $ num1 , $ num2 ) { if ( $ num1 == 0 ) return 0 ; if ( $ num2 == 0 ) return INT_MAX ; $ negResult = false ; if ( $ num1 < 0 ) { $ num1 = - $ num1 ; if ( $ num2 < 0 ) $ num2 = - $ num2 ; else $ negResult = true ; } else if ( $ num2 < 0 ) { $ num2 = - $ num2 ; $ negResult = true ; } $ quotient = 0 ; while ( $ num1 >= $ num2 ) { $ num1 = $ num1 - $ num2 ; $ quotient ++ ; } if ( $ negResult ) $ quotient = - $ quotient ; return $ quotient ; } $ num1 = 13 ; $ num2 = 2 ; echo division ( $ num1 , $ num2 ) ; ? >
Nonagonal number | Function to find nonagonal number series . ; Driver Code
< ? php function Nonagonal ( $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { echo $ i * ( 7 * $ i - 5 ) / 2 ; echo " ▁ " ; } } $ n = 10 ; Nonagonal ( $ n ) ; ? >
Find n | func for calualtion ; for summation of square of first n - natural nos . ; summation of first n natural nos . ; return result ; Driver Code
< ? php function seriesFunc ( $ n ) { $ sumSquare = ( $ n * ( $ n + 1 ) * ( 2 * $ n + 1 ) ) / 6 ; $ sumNatural = ( $ n * ( $ n + 1 ) / 2 ) ; return ( $ sumSquare + $ sumNatural + 1 ) ; } $ n = 8 ; echo ( seriesFunc ( $ n ) . " " ) ; $ n = 13 ; echo ( seriesFunc ( $ n ) . " " ) ; ? >
Program to check Plus Perfect Number | function to check plus perfect number ; calculating number of digits ; calculating plus perfect number ; checking whether number is plus perfect or not ; Driver Code
< ? php function checkplusperfect ( $ x ) { $ temp = $ x ; $ n = 0 ; while ( $ x != 0 ) { $ x /= 10 ; $ n ++ ; } $ x = $ temp ; $ sum = 0 ; while ( $ x != 0 ) { $ sum += pow ( $ x % 10 , $ n ) ; $ x /= 10 ; } return ( $ sum == $ temp ) ; } $ x = 9474 ; if ( checkplusperfect ( ! $ x ) ) echo " Yes " ; else echo " No " ; ? >
Number of distinct subsets of a set | Returns 2 ^ n ; Driver Code
< ? php function subsetCount ( $ arr , $ n ) { return 1 << $ n ; } $ A = array ( 1 , 2 , 3 ) ; $ n = sizeof ( $ A ) ; echo ( subsetCount ( $ A , $ n ) ) ; ? >
Program to calculate GST from original and net prices | PHP Program to compute GST from original and net prices . ; return value after calculate GST % ; Driver Code
< ? php function Calculate_GST ( $ org_cost , $ N_price ) { return ( ( ( $ N_price - $ org_cost ) * 100 ) / $ org_cost ) ; } $ org_cost = 100 ; $ N_price = 120 ; echo ( " GST ▁ = ▁ " ) ; echo ( Calculate_GST ( $ org_cost , $ N_price ) ) ; echo ( " ▁ % ▁ " ) ; ? >
Centered hexagonal number | Function to find centered hexadecimal number . ; Formula to calculate nth centered hexadecimal number and return it into main function . ; Driver Code
< ? php function centeredHexagonalNumber ( $ n ) { return 3 * $ n * ( $ n - 1 ) + 1 ; } $ n = 10 ; echo $ n , " th ▁ centered ▁ hexagonal ▁ number : ▁ " ; echo centeredHexagonalNumber ( $ n ) ; ? >
Find the distance covered to collect items at equal distances | function to calculate the distance ; Driver program
< ? php function find_distance ( $ n ) { return $ n * ( ( 3 * $ n ) + 7 ) ; } $ n = 5 ; echo " Distance = " ? >
Twin Prime Numbers | Please refer below post for details of this function https : goo . gl / Wv3fGv ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Returns true if n1 and n2 are twin primes ; 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 false ; return true ; } function twinPrime ( $ n1 , $ n2 ) { return ( isPrime ( $ n1 ) && isPrime ( $ n2 ) && abs ( $ n1 - $ n2 ) == 2 ) ; } $ n1 = 11 ; $ n2 = 13 ; if ( twinPrime ( $ n1 , $ n2 ) ) echo " Twin ▁ Prime " , " STRNEWLINE " ; else echo " STRNEWLINE " , " Not ▁ Twin ▁ Prime " , " STRNEWLINE " ; ? >
Sum of the sequence 2 , 22 , 222 , ... ... ... | function which return the the sum of series ; Driver Code
< ? php function sumOfSeries ( $ n ) { return 0.0246 * ( pow ( 10 , $ n ) - 1 - ( 9 * $ n ) ) ; } $ n = 3 ; echo ( sumOfSeries ( $ n ) ) ; ? >
Find sum of even index binomial coefficients | Returns value of even indexed Binomial Coefficient Sum which is 2 raised to power n - 1. ; Driver Code
< ? php function evenbinomialCoeffSum ( $ n ) { return ( 1 << ( $ n - 1 ) ) ; } $ n = 4 ; echo evenbinomialCoeffSum ( $ n ) ; ? >
Program to print triangular number series till n | Function to find triangular number ; For each iteration increase j by 1 and add it into k ; Increasing j by 1 ; Add value of j into k and update k ; Driver Code
< ? php function triangular_series ( $ n ) { $ i ; $ j = 1 ; $ k = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { echo ( " ▁ " . $ k . " ▁ " ) ; $ j = $ j + 1 ; $ k = $ k + $ j ; } } $ n = 5 ; triangular_series ( $ n ) ; ? >
Sum of the series 1 + ( 1 + 3 ) + ( 1 + 3 + 5 ) + ( 1 + 3 + 5 + 7 ) + Ã ¢ â ‚¬¦ Ã ¢ â ‚¬¦ + ( 1 + 3 + 5 + 7 + Ã ¢ â ‚¬¦ + ( 2 n | functionn to find the sum of the given series ; required sum ; Driver Code
< ? php function sumOfTheSeries ( $ n ) { return ( $ n * ( $ n + 1 ) / 2 ) * ( 2 * $ n + 1 ) / 3 ; } $ n = 5 ; echo " Sum ▁ = ▁ " . sumOfTheSeries ( $ n ) ; ? >
Sum of the series 1 + ( 1 + 2 ) + ( 1 + 2 + 3 ) + ( 1 + 2 + 3 + 4 ) + ... ... + ( 1 + 2 + 3 + 4 + ... + n ) | Function to find sum of given series ; Driver Code
< ? php function sumOfSeries ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) for ( $ j = 1 ; $ j <= $ i ; $ j ++ ) $ sum += $ j ; return $ sum ; } $ n = 10 ; echo ( sumOfSeries ( $ n ) ) ; ? >
Number of triangles after N moves | function to calculate number of triangles in Nth step ; Driver Code
< ? php function numberOfTriangles ( $ n ) { $ ans = 2 * ( pow ( 3 , $ n ) ) - 1 ; return $ ans ; } $ n = 2 ; echo numberOfTriangles ( $ n ) ; ? >
Motzkin number | Return the nth Motzkin Number . ; Base case ; Finding i - th Motzkin number . ; Driven Code
< ? php function motzkin ( $ n ) { $ dp [ 0 ] = $ dp [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ dp [ $ i ] = ( ( 2 * $ i + 1 ) * $ dp [ $ i - 1 ] + ( 3 * $ i - 3 ) * $ dp [ $ i - 2 ] ) / ( $ i + 2 ) ; return $ dp [ $ n ] ; } $ n = 8 ; echo ( motzkin ( $ n ) ) ; ? >
NicomachusÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¾¢ s Theorem ( Sum of k | Return the sum of kth group of positive odd integer . ; Driven Code
< ? php function kthgroupsum ( $ k ) { return $ k * $ k * $ k ; } $ k = 3 ; echo kthgroupsum ( $ k ) ; ? >
Find x , y , z that satisfy 2 / n = 1 / x + 1 / y + 1 / z | function to find x y and z that satisfy given equation . ; Driver Code
< ? php function printXYZ ( $ n ) { if ( $ n == 1 ) echo - 1 ; else echo " x ▁ is ▁ " , $ n , " STRNEWLINE y ▁ is ▁ " , $ n + 1 , " STRNEWLINE z ▁ is ▁ " , $ n * ( $ n + 1 ) ; } $ n = 7 ; printXYZ ( $ n ) ; ? >
Find n | Function to find nth term ; Driver code
< ? php function term ( $ n ) { return $ n * ( $ n + 1 ) / 2 ; } $ n = 4 ; echo ( term ( $ n ) ) ; ? >
Find Harmonic mean using Arithmetic mean and Geometric mean | Function to calculate arithmetic mean , geometric mean and harmonic mean ; Driver Code
< ? php function compute ( $ a , $ b ) { $ AM ; $ GM ; $ HM ; $ AM = ( $ a + $ b ) / 2 ; $ GM = sqrt ( $ a * $ b ) ; $ HM = ( $ GM * $ GM ) / $ AM ; return $ HM ; } $ a = 5 ; $ b = 15 ; $ HM = compute ( $ a , $ b ) ; echo " Harmonic ▁ Mean ▁ between ▁ " . $ a . " ▁ and ▁ " . $ b . " ▁ is ▁ " . $ HM ; return 0 ; ? >
Find n | Returns n - th element of the series ; Driver Code
< ? php function series ( $ n ) { return ( 8 * $ n * $ n ) + 1 ; } $ n = 5 ; echo ( series ( $ n ) ) ; ? >
Check if a number is divisible by all prime divisors of another number | PHP program to find if all prime factors of y divide x . ; Returns true if all prime factors of y divide x . ; Driver Code
< ? php function gcd ( $ a , $ b ) { return $ b == 0 ? $ a : gcd ( $ b , $ a % $ b ) ; } function isDivisible ( $ x , $ y ) { if ( $ y == 1 ) return true ; $ z = gcd ( $ x , $ y ) ; if ( $ z == 1 ) return false ; return isDivisible ( $ x , $ y / $ z ) ; } $ x = 18 ; $ y = 12 ; if ( isDivisible ( $ x , $ y ) ) echo " Yes " ; else echo " No " ; ? >
Program to find Sum of a Series a ^ 1 / 1 ! + a ^ 2 / 2 ! + a ^ 3 / 3 ! + a ^ 4 / 4 ! + Ã ¢ â ‚¬¦ Ã ¢ â ‚¬¦ . + a ^ n / n ! | Function to calculate sum of given series ; multiply ( a / i ) to previous term ; store result in res ; Driver Code
< ? php function sumOfSeries ( $ a , $ num ) { $ res = 0 ; $ prev = 1 ; for ( $ i = 1 ; $ i <= $ num ; $ i ++ ) { $ prev *= ( $ a / $ i ) ; $ res = $ res + $ prev ; } return ( $ res ) ; } $ n = 5 ; $ a = 2 ; echo ( sumOfSeries ( $ a , $ n ) ) ; ? >
Program for Celsius To Fahrenheit conversion | function to convert Celsius scale to Fahrenheit scale ; Driver Code
< ? php function Cel_To_Fah ( $ n ) { return ( ( $ n * 9.0 / 5.0 ) + 32.0 ) ; } $ n = 20.0 ; echo Cel_To_Fah ( $ n ) ; ? >
Series with largest GCD and sum equals to n | function to generate and print the sequence ; stores the maximum gcd that can be possible of sequence . ; if maximum gcd comes out to be zero then not possible ; the smallest gcd possible is 1 ; traverse the array to find out the max gcd possible ; checks if the number is divisible or not ; checks if x is smaller then the max gcd possible and x is greater then the resultant gcd till now , then r = x ; checks if n / x is smaller than the max gcd possible and n / x is greater then the resultant gcd till now , then r = x ; traverses and prints d , 2d , 3d , ... , ( k - 1 ) d , ; computes the last element of the sequence n - s . ; prints the last element ; Driver Code
< ? php function print_sequence ( $ n , $ k ) { $ b = ( int ) ( $ n / ( $ k * ( $ k + 1 ) / 2 ) ) ; if ( $ b == 0 ) { echo ( -1 ) ; } else { $ r = 1 ; for ( $ x = 1 ; $ x * $ x <= $ n ; $ x ++ ) { if ( $ n % $ x != 0 ) continue ; if ( $ x <= $ b && $ x > $ r ) $ r = $ x ; if ( $ n / $ x <= $ b && $ n / $ x > $ r ) $ r = $ n / $ x ; } for ( $ i = 1 ; $ i < $ k ; $ i ++ ) echo ( $ r * $ i . " ▁ " ) ; $ res = $ n - ( $ r * ( $ k * ( $ k - 1 ) / 2 ) ) ; echo ( $ res . " " ) ; } } $ n = 24 ; $ k = 4 ; print_sequence ( $ n , $ k ) ; $ n = 24 ; $ k = 5 ; print_sequence ( $ n , $ k ) ; $ n = 6 ; $ k = 4 ; print_sequence ( $ n , $ k ) ; ? >
Number of compositions of a natural number | PHP program to find the total number of compositions of a natural number ; Return 2 raised to power ( n - 1 ) ; Driver Code
< ? php function countCompositions ( $ n ) { return ( ( 1 ) << ( $ n - 1 ) ) ; } $ n = 4 ; echo countCompositions ( $ n ) , " STRNEWLINE " ; ? >
Program to count digits in an integer ( 4 Different Methods ) | Recursive PHP program to count number of digits in a number ; Driver Code
< ? php function countDigit ( $ n ) { if ( $ n / 10 == 0 ) return 1 ; return 1 + countDigit ( ( int ) ( $ n / 10 ) ) ; } $ n = 345289467 ; print ( " Number ▁ of ▁ digits ▁ : ▁ " . ( countDigit ( $ n ) ) ) ; ? >
Tribonacci Numbers | A DP based PHP program to print first n Tribonacci numbers . ; Driver code
< ? php function printTrib ( $ n ) { $ dp [ 0 ] = $ dp [ 1 ] = 0 ; $ dp [ 2 ] = 1 ; for ( $ i = 3 ; $ i < $ n ; $ i ++ ) $ dp [ $ i ] = $ dp [ $ i - 1 ] + $ dp [ $ i - 2 ] + $ dp [ $ i - 3 ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ dp [ $ i ] , " ▁ " ; } $ n = 10 ; printTrib ( $ n ) ; ? >
Tribonacci Numbers | Program to print first n tribonacci numbers Matrix Multiplication function for 3 * 3 matrix ; Recursive function to raise the matrix T to the power n ; base condition . ; recursively call to square the matrix ; calculating square of the matrix T ; if n is odd multiply it one time with M ; base condition ; $T [ 0 ] [ 0 ] contains the tribonacci number so return it ; Driver Code
< ? php function multiply ( & $ T , $ M ) { $ a = $ T [ 0 ] [ 0 ] * $ M [ 0 ] [ 0 ] + $ T [ 0 ] [ 1 ] * $ M [ 1 ] [ 0 ] + $ T [ 0 ] [ 2 ] * $ M [ 2 ] [ 0 ] ; $ b = $ T [ 0 ] [ 0 ] * $ M [ 0 ] [ 1 ] + $ T [ 0 ] [ 1 ] * $ M [ 1 ] [ 1 ] + $ T [ 0 ] [ 2 ] * $ M [ 2 ] [ 1 ] ; $ c = $ T [ 0 ] [ 0 ] * $ M [ 0 ] [ 2 ] + $ T [ 0 ] [ 1 ] * $ M [ 1 ] [ 2 ] + $ T [ 0 ] [ 2 ] * $ M [ 2 ] [ 2 ] ; $ d = $ T [ 1 ] [ 0 ] * $ M [ 0 ] [ 0 ] + $ T [ 1 ] [ 1 ] * $ M [ 1 ] [ 0 ] + $ T [ 1 ] [ 2 ] * $ M [ 2 ] [ 0 ] ; $ e = $ T [ 1 ] [ 0 ] * $ M [ 0 ] [ 1 ] + $ T [ 1 ] [ 1 ] * $ M [ 1 ] [ 1 ] + $ T [ 1 ] [ 2 ] * $ M [ 2 ] [ 1 ] ; $ f = $ T [ 1 ] [ 0 ] * $ M [ 0 ] [ 2 ] + $ T [ 1 ] [ 1 ] * $ M [ 1 ] [ 2 ] + $ T [ 1 ] [ 2 ] * $ M [ 2 ] [ 2 ] ; $ g = $ T [ 2 ] [ 0 ] * $ M [ 0 ] [ 0 ] + $ T [ 2 ] [ 1 ] * $ M [ 1 ] [ 0 ] + $ T [ 2 ] [ 2 ] * $ M [ 2 ] [ 0 ] ; $ h = $ T [ 2 ] [ 0 ] * $ M [ 0 ] [ 1 ] + $ T [ 2 ] [ 1 ] * $ M [ 1 ] [ 1 ] + $ T [ 2 ] [ 2 ] * $ M [ 2 ] [ 1 ] ; $ i = $ T [ 2 ] [ 0 ] * $ M [ 0 ] [ 2 ] + $ T [ 2 ] [ 1 ] * $ M [ 1 ] [ 2 ] + $ T [ 2 ] [ 2 ] * $ M [ 2 ] [ 2 ] ; $ T [ 0 ] [ 0 ] = $ a ; $ T [ 0 ] [ 1 ] = $ b ; $ T [ 0 ] [ 2 ] = $ c ; $ T [ 1 ] [ 0 ] = $ d ; $ T [ 1 ] [ 1 ] = $ e ; $ T [ 1 ] [ 2 ] = $ f ; $ T [ 2 ] [ 0 ] = $ g ; $ T [ 2 ] [ 1 ] = $ h ; $ T [ 2 ] [ 2 ] = $ i ; } function power ( & $ T , $ n ) { if ( $ n == 0 $ n == 1 ) return ; $ M = array ( array ( 1 , 1 , 1 ) , array ( 1 , 0 , 0 ) , array ( 0 , 1 , 0 ) ) ; power ( $ T , ( int ) ( $ n / 2 ) ) ; multiply ( $ T , $ T ) ; if ( $ n % 2 ) multiply ( $ T , $ M ) ; } function tribonacci ( $ n ) { $ T = array ( array ( 1 , 1 , 1 ) , array ( 1 , 0 , 0 ) , array ( 0 , 1 , 0 ) ) ; if ( $ n == 0 $ n == 1 ) return 0 ; else power ( $ T , $ n - 2 ) ; return $ T [ 0 ] [ 0 ] ; } $ n = 10 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo tribonacci ( $ i ) . " ▁ " ; echo " STRNEWLINE " ; ? >
Geometric mean ( Two Methods ) | function to calculate geometric mean and return float value . ; declare sum variable and initialize it to 1. ; Compute the sum of all the elements in the array . ; compute geometric mean through formula antilog ( ( ( log ( 1 ) + log ( 2 ) + . . . + log ( n ) ) / n ) and return the value ; Driver Code ; function call
< ? php function geometricMean ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum = $ sum + log ( $ arr [ $ i ] ) ; $ sum = $ sum / $ n ; return exp ( $ sum ) ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ) ; $ n = count ( $ arr ) ; echo geometricMean ( $ arr , $ n ) ; ? >
Smallest number k such that the product of digits of k is equal to n | function to find smallest number k such that the product of digits of k is equal to n ; if ' n ' is a single digit number , then it is the required number ; stack the store the digits ; repeatedly divide ' n ' by the numbers from 9 to 2 until all the numbers are used or ' n ' > 1 ; save the digit ' i ' that divides ' n ' onto the stack ; if true , then no number ' k ' can be formed ; pop digits from the stack ' digits ' and add them to ' k ' ; required smallest number ; Driver code
< ? php function smallestNumber ( $ n ) { if ( $ n >= 0 && $ n <= 9 ) return $ n ; $ digits = array ( ) ; for ( $ i = 9 ; $ i >= 2 && $ n > 1 ; $ i -- ) { while ( $ n % $ i == 0 ) { array_push ( $ digits , $ i ) ; $ n = ( int ) ( $ n / $ i ) ; } } if ( $ n != 1 ) return -1 ; $ k = 0 ; while ( ! empty ( $ digits ) ) $ k = $ k * 10 + array_pop ( $ digits ) ; return $ k ; } $ n = 100 ; echo smallestNumber ( $ n ) ; ? >
Check if a number is magic ( Recursive sum of digits is 1 ) | PHP program to check if a number is Magic number . ; Note that the loop continues if n is 0 and sum is non - zero . It stops when n becomes 0 and sum becomes single digit . ; Return true if sum becomes 1. ; Driver code
< ? php function isMagic ( $ n ) { $ sum = 0 ; while ( $ n > 0 $ sum > 9 ) { if ( $ n == 0 ) { $ n = $ sum ; $ sum = 0 ; } $ sum += $ n % 10 ; $ n /= 10 ; } return ( $ sum == 1 ) ; } $ n = 1234 ; if ( isMagic ( $ n ) ) echo " Magic ▁ Number " ; else echo " Not ▁ a ▁ magic ▁ Number " ; ? >
Sylvester 's sequence | PHP program to print terms of Sylvester 's sequence ; To store the product . ; To store the current number . ; Loop till n . ; Driver Code
< ? php $ N = 1000000007 ; function printSequence ( $ n ) { global $ N ; $ a = 1 ; $ ans = 2 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { echo $ ans , " " ; $ ans = ( ( $ a % $ N ) * ( $ ans % $ N ) ) % $ N ; $ a = $ ans ; $ ans = ( $ ans + 1 ) % $ N ; } } $ n = 6 ; printSequence ( $ n ) ; ? >
Program to find sum of first n natural numbers | Returns sum of first n natural numbers ; Driver code
< ? php function findSum ( $ n ) { $ sum = 0 ; for ( $ x = 1 ; $ x <= $ n ; $ x ++ ) $ sum = $ sum + $ x ; return $ sum ; } $ n = 5 ; echo findSum ( $ n ) ; ? >
Hailstone Numbers | function to print hailstone numbers and to calculate the number of steps required ; N is initially 1. ; N is reduced to 1. ; If N is Even . ; N is Odd . ; Driver Code ; Function to generate Hailstone Numbers ; Output : Number of Steps
< ? php function HailstoneNumbers ( $ N ) { static $ c ; echo $ N . " " ; if ( $ N == 1 && $ c == 0 ) { return $ c ; } else if ( $ N == 1 && $ c != 0 ) { $ c ++ ; return $ c ; } else if ( $ N % 2 == 0 ) { $ c ++ ; HailstoneNumbers ( ( int ) ( $ N / 2 ) ) ; } else if ( $ N % 2 != 0 ) { $ c ++ ; HailstoneNumbers ( 3 * $ N + 1 ) ; } return $ c ; } $ N = 7 ; $ x = HailstoneNumbers ( $ N ) ; echo " Number of Steps : " ? >
Find m | Function to return mth summation ; base case ; Driver Code
< ? php function SUM ( $ n , $ m ) { if ( $ m == 1 ) return ( $ n * ( $ n + 1 ) / 2 ) ; $ sum = SUM ( $ n , $ m - 1 ) ; return ( $ sum * ( $ sum + 1 ) / 2 ) ; } $ n = 5 ; $ m = 3 ; echo " SUM ( " , $ n , " , ▁ " , $ m , " ) : ▁ " , SUM ( $ n , $ m ) ; ? >
Find smallest number n such that n XOR n + 1 equals to given k . | function to return the required n ; if k is of form 2 ^ i - 1 ; Driver Code
< ? php function xorCalc ( $ k ) { if ( $ k == 1 ) return 2 ; if ( ( ( $ k + 1 ) & $ k ) == 0 ) return floor ( $ k / 2 ) ; return 1 ; } $ k = 31 ; echo xorCalc ( $ k ) ; ? >
Find n | PHP program to find n - th number containing only 4 and 7. ; If n is odd , append 4 and move to parent ; If n is even , append 7 and move to parent ; Reverse res and return . ; Driver code
< ? php function findNthNo ( $ n ) { $ res = " " ; while ( $ n >= 1 ) { if ( $ n & 1 ) { $ res = $ res . "4" ; $ n = ( int ) ( ( $ n - 1 ) / 2 ) ; } else { $ res = $ res . "7" ; $ n = ( int ) ( ( $ n - 2 ) / 2 ) ; } } return strrev ( $ res ) ; } $ n = 13 ; echo findNthNo ( $ n ) ; ? >
Total number of divisors for a given number | program for finding no . of divisors ; sieve method for prime calculation ; Traversing through all prime numbers ; calculate number of divisor with formula total div = ( p1 + 1 ) * ( p2 + 1 ) * ... . . * ( pn + 1 ) where n = ( a1 ^ p1 ) * ( a2 ^ p2 ) . ... * ( an ^ pn ) ai being prime divisor for n and pi are their respective power in factorization ; Driver Code
< ? php function divCount ( $ n ) { $ hash = array_fill ( 0 , $ n + 1 , 1 ) ; for ( $ p = 2 ; ( $ p * $ p ) < $ n ; $ p ++ ) if ( $ hash [ $ p ] == 1 ) for ( $ i = ( $ p * 2 ) ; $ i < $ n ; $ i = ( $ i + $ p ) ) $ hash [ $ i ] = 0 ; $ total = 1 ; for ( $ p = 2 ; $ p <= $ n ; $ p ++ ) { if ( $ hash [ $ p ] == 1 ) { $ count = 0 ; if ( $ n % $ p == 0 ) { while ( $ n % $ p == 0 ) { $ n = ( $ n / $ p ) ; $ count ++ ; } $ total = $ total * ( $ count + 1 ) ; } } } return $ total ; } $ n = 24 ; echo divCount ( $ n ) ; ? >
Maximum number of unique prime factors | Return smallest number having maximum prime factors . ; Sieve of eratosthenes method to count number of unique prime factors . ; Return maximum element in arr [ ] ; Driver Code
< ? php function maxPrimefactorNum ( $ N ) { $ arr = array_fill ( 0 , $ N + 1 , 0 ) ; for ( $ i = 2 ; $ i * $ i <= $ N ; $ i ++ ) { if ( ! $ arr [ $ i ] ) for ( $ j = 2 * $ i ; $ j <= $ N ; $ j += $ i ) $ arr [ $ j ] ++ ; $ arr [ $ i ] = 1 ; } return max ( $ arr ) ; } $ N = 40 ; echo maxPrimefactorNum ( $ N ) ; ? >
Decimal to binary conversion without using arithmetic operators | function for decimal to binary conversion without using arithmetic operators ; to store the binary equivalent of decimal ; to get the last binary digit of the number ' n ' and accumulate it at the beginning of ' bin ' ; right shift ' n ' by 1 ; required binary number ; Driver Code
< ? php function decToBin ( $ n ) { if ( $ n == 0 ) return "0" ; $ bin = " " ; while ( $ n > 0 ) { $ bin = ( ( $ n & 1 ) == 0 ? '0' : '1' ) . $ bin ; $ n >>= 1 ; } return $ bin ; } $ n = 38 ; echo decToBin ( $ n ) ; ? >
Sum of array elements that is first continuously increasing then decreasing | Efficient PHP method to find sum of the elements of array that is halfway increasing and then halfway decreassing ; Driver code
< ? php function arraySum ( $ arr , $ n ) { $ x = ( $ n + 1 ) / 2 ; return ( $ arr [ 0 ] - 1 ) * $ n + $ x * $ x ; } $ arr = array ( 10 , 11 , 12 , 13 , 12 , 11 , 10 ) ; $ n = sizeof ( $ arr ) ; echo ( arraySum ( $ arr , $ n ) ) ; ? >
Balance pans using given weights that are powers of a number | method returns true if balancing of scale is possible ; baseForm vector will store T 's  representation on base a in reverse order ; convert T to representation on base a ; make first digit of representation as 0 ; loop over base representation of T ; if any digit is not 0 , 1 , ( a - 1 ) or a then balancing is not possible ; if digit is a or ( a - 1 ) then increase left index ' s ▁ count / ▁ ( case , ▁ when ▁ this ▁ weight ▁ is ▁ transferred ▁ to ▁ T ' s side ) ; if representation is processed then balancing is possible ; Driver Code
< ? php function isBalancePossible ( $ T , $ a ) { $ baseForm = array ( ) ; while ( $ T ) { array_push ( $ baseForm , $ T % $ a ) ; $ T = ( int ) ( $ T / $ a ) ; } array_push ( $ baseForm , 0 ) ; for ( $ i = 0 ; $ i < count ( $ baseForm ) ; $ i ++ ) { if ( $ baseForm [ $ i ] != 0 && $ baseForm [ $ i ] != 1 && $ baseForm [ $ i ] != ( $ a - 1 ) && $ baseForm [ $ i ] != $ a ) return false ; if ( $ baseForm [ $ i ] == $ a || $ baseForm [ $ i ] == ( $ a - 1 ) ) $ baseForm [ $ i + 1 ] += 1 ; } return true ; } $ T = 11 ; $ a = 4 ; $ balancePossible = isBalancePossible ( $ T , $ a ) ; if ( $ balancePossible ) echo " Balance ▁ is ▁ possible STRNEWLINE " ; else echo " Balance ▁ is ▁ not ▁ possible STRNEWLINE " ; ? >
Number of digits in the product of two numbers | function to count number of digits in the product of two numbers ; if either of the number is 0 , then product will be 0 ; required count of digits ; Driver Code
< ? php function countDigits ( $ a , $ b ) { if ( $ a == 0 or $ b == 0 ) return 1 ; return floor ( log10 ( abs ( $ a ) ) + log10 ( abs ( $ b ) ) ) + 1 ; } $ a = 33 ; $ b = -24 ; echo countDigits ( $ a , $ b ) ; ? >
Distributing M items in a circle of size N starting from K | n == > Size of circle m == > Number of items k == > Initial position ; n - k + 1 is number of positions before we reach beginning of circle . If m is less than this value , then we can simply return ( m - 1 ) th position ; Let us compute remaining items before we reach beginning . ; We compute m % n to skip all complete rounds . If we reach end , we return n else we return m % n ; Driver code
< ? php function lastPosition ( $ n , $ m , $ k ) { if ( $ m <= $ n - $ k + 1 ) return $ m + $ k - 1 ; $ m = $ m - ( $ n - $ k + 1 ) ; return ( $ m % $ n == 0 ) ? $ n : ( $ m % $ n ) ; } $ n = 5 ; $ m = 8 ; $ k = 2 ; echo lastPosition ( $ n , $ m , $ k ) ; ? >