codeparrot/xlcost-text-to-code huggingface dataset:
Program to Convert Hexadecimal Number to Binary | function to convert Hexadecimal to Binary Number ; Get the Hexadecimal number ; Convert HexaDecimal to Binary
< ? php function HexToBin ( $ hexdec ) { $ i = 0 ; while ( $ hexdec [ $ i ] ) { switch ( $ hexdec [ $ i ] ) { case '0' : echo "0000" ; break ; case '1' : echo "0001" ; break ; case '2' : echo "0010" ; break ; case '3' : echo "0011" ; break ; case '4' : echo "0100" ; break ; case '5' : echo "0101" ; break ; case '6' : echo "0110" ; break ; case '7' : echo "0111" ; break ; case '8' : echo "1000" ; break ; case '9' : echo "1001" ; break ; case ' A ' : case ' a ' : echo "1010" ; break ; case ' B ' : case ' b ' : echo "1011" ; break ; case ' C ' : case ' c ' : echo "1100" ; break ; case ' D ' : case ' d ' : echo "1101" ; break ; case ' E ' : case ' e ' : echo "1110" ; break ; case ' F ' : case ' f ' : echo "1111" ; break ; default : echo " Invalid hexadecimal digit " $ hexdec [ $ i ] ; } $ i ++ ; } } $ hexdec = "1AC5" ; echo " Equivalent Binary value is : " HexToBin ( $ hexdec ) ;
Count unordered pairs ( i , j ) such that product of a [ i ] and a [ j ] is power of two | Function to check if x is power of 2 ; First x in the below expression is for the case when x is 0 ; Function to Count unordered pairs ; is a number can be expressed as power of two ; count total number of unordered pairs ; Driver code
< ? php function isPowerOfTwo ( $ x ) { return ( $ x && ( ! ( $ x & ( $ x - 1 ) ) ) ) ; } function Count_pairs ( $ a , $ n ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( isPowerOfTwo ( $ a [ $ i ] ) ) $ count ++ ; } $ ans = ( $ count * ( $ count - 1 ) ) / 2 ; echo $ ans , " STRNEWLINE " ; } $ a = array ( 2 , 5 , 8 , 16 , 128 ) ; $ n = sizeof ( $ a ) ; Count_pairs ( $ a , $ n ) ; ? >
Ways of dividing a group into two halves such that two elements are in different groups | This function will return the factorial of a given number ; This function will calculate nCr of given n and r ; This function will Calculate number of ways ; Driver Code
< ? php function factorial ( $ n ) { $ result = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ result = $ result * $ i ; return $ result ; } function nCr ( $ n , $ r ) { return factorial ( $ n ) / ( factorial ( $ r ) * factorial ( $ n - $ r ) ) ; } function calculate_result ( $ n ) { $ result = 2 * nCr ( ( $ n - 2 ) , ( $ n / 2 - 1 ) ) ; return $ result ; } $ a = 2 ; $ b = 4 ; echo calculate_result ( 2 * $ a ) . " STRNEWLINE " ; echo calculate_result ( 2 * $ b ) . " STRNEWLINE " ; ? >
Print values of ' a ' in equation ( a + b ) <= n and a + b is divisible by x | function to Find values of a , in equation ( a + b ) <= n and a + b is divisible by x . ; least possible which is divisible by x ; run a loop to get required answer ; increase value by x ; answer is possible ; Driver code ; function call
< ? php function PossibleValues ( $ b , $ x , $ n ) { $ leastdivisible = ( intval ( $ b / $ x ) + 1 ) * $ x ; $ flag = 1 ; while ( $ leastdivisible <= $ n ) { if ( $ leastdivisible - $ b >= 1 ) { echo $ leastdivisible - $ b . " " ; $ leastdivisible += $ x ; $ flag = 0 ; } else break ; } if ( $ flag ) echo " - 1" ; } $ b = 10 ; $ x = 6 ; $ n = 40 ; PossibleValues ( $ b , $ x , $ n ) ; ? >
Check if all sub | Function to calculate product of digits between given indexes ; Function to check if all sub - numbers have distinct Digit product ; Length of number N ; Digit array ; to maintain digit products ; Finding all possible subarrays ; Driver code
< ? php function digitProduct ( $ digits , $ start , $ end ) { $ pro = 1 ; for ( $ i = $ start ; $ i <= $ end ; $ i ++ ) { $ pro *= $ digits [ $ i ] ; } return $ pro ; } function isDistinct ( $ N ) { $ s = " $ N " ; $ len = sizeof ( $ s ) ; $ digits = array ( ) ; $ products = array ( ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { $ digits [ $ i ] = $ s [ $ i ] - '0' ; } for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { for ( $ j = $ i ; $ j < $ len ; $ j ++ ) { $ val = digitProduct ( $ digits , $ i , $ j ) ; if ( in_array ( $ val , $ products ) ) return false ; else array_push ( $ products , $ val ) ; } } return true ; } $ N = 324 ; if ( isDistinct ( $ N ) ) echo " YES " ; else echo " NO " ; ? >
Hilbert Number | Utility function to return Nth Hilbert Number ; Driver code
< ? php function nthHilbertNumber ( $ n ) { return 4 * ( $ n - 1 ) + 1 ; } $ n = 5 ; echo nthHilbertNumber ( $ n ) ; ? >
Program to find the nth Kynea number | Function to calculate nth kynea number ; Firstly calculate 2 ^ n + 1 ; Now calculate ( 2 ^ n + 1 ) ^ 2 ; Now calculate ( ( 2 ^ n + 1 ) ^ 2 ) - 2 ; return nth Kynea number ; Driver Code ; print nth kynea number
< ? php function nthKyneaNumber ( $ n ) { $ n = ( 1 << $ n ) + 1 ; $ n = $ n * $ n ; $ n = $ n - 2 ; return $ n ; } $ n = 8 ; echo nthKyneaNumber ( $ n ) ;
Program to find the nth Kynea number | Function to calculate nth kynea number ; Calculate nth kynea number ; Driver Code ; print nth kynea number
< ? php function nthKyneaNumber ( $ n ) { return ( ( 1 << ( 2 * $ n ) ) + ( 1 << ( $ n + 1 ) ) - 1 ) ; } $ n = 2 ; echo nthKyneaNumber ( $ n ) ; ? >
Program to check whether a number is Proth number or not | Utility function to check power of two ; Function to check if the Given number is Proth number or not ; check if k divides n or not ; Check if n / k is power of 2 or not ; update k to next odd number ; If we reach here means there exists no value of K Such that k is odd number and n / k is a power of 2 greater than k ; Get n ; Check n for Proth Number
< ? php function isPowerOfTwo ( $ n ) { return ( $ n && ! ( $ n & ( $ n - 1 ) ) ) ; } function isProthNumber ( $ n ) { $ k = 1 ; while ( $ k < ( $ n / $ k ) ) { if ( $ n % $ k == 0 ) { if ( isPowerOfTwo ( $ n / $ k ) ) return true ; } $ k = $ k + 2 ; } return false ; } $ n = 25 ; if ( isProthNumber ( $ n - 1 ) ) echo " YES " ; else echo " NO " ; ? >
Find last two digits of sum of N factorials | Function to find the unit ' s ▁ and ▁ ten ' s place digit ; Let us write for cases when N is smaller than or equal to 10. ; We know following ( 1 ! + 2 ! + 3 ! + 4 ! ... + 10 ! ) % 100 = 13 else ( N >= 10 ) ; Driver code
< ? php function get_last_two_digit ( $ N ) { if ( $ N <= 10 ) { $ ans = 0 ; $ fac = 1 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) { $ fac = $ fac * $ i ; $ ans += $ fac ; } return $ ans % 100 ; } return 13 ; } $ N = 1 ; for ( $ N = 1 ; $ N <= 10 ; $ N ++ ) echo " For ▁ N ▁ = ▁ " . $ N . " ▁ : ▁ " . get_last_two_digit ( $ N ) . " STRNEWLINE " ;
Check whether product of ' n ' numbers is even or odd | function to check whether product of ' n ' numbers is even or odd ; if a single even number is found , then final product will be an even number ; product is an odd number ; Driver code
< ? php function isProductEven ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( ( $ arr [ $ i ] & 1 ) == 0 ) return true ; return false ; } $ arr = array ( 2 , 4 , 3 , 5 ) ; $ n = sizeof ( $ arr ) ; if ( isProductEven ( $ arr , $ n ) ) echo " Even " ; else echo " Odd " ; ? >
Sum of squares of Fibonacci numbers | Function to calculate sum of squares of Fibonacci numbers ; Initialize result ; Add remaining terms ; Driver Code
< ? php function calculateSquareSum ( $ n ) { if ( $ n <= 0 ) return 0 ; $ fibo [ 0 ] = 0 ; $ fibo [ 1 ] = 1 ; $ sum = ( $ fibo [ 0 ] * $ fibo [ 0 ] ) + ( $ fibo [ 1 ] * $ fibo [ 1 ] ) ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ fibo [ $ i ] = $ fibo [ $ i - 1 ] + $ fibo [ $ i - 2 ] ; $ sum += ( $ fibo [ $ i ] * $ fibo [ $ i ] ) ; } return $ sum ; } $ n = 6 ; echo " Sum ▁ of ▁ squares ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : ▁ " , calculateSquareSum ( $ n ) ; ? >
Value of the series ( 1 ^ 3 + 2 ^ 3 + 3 ^ 3 + ... + n ^ 3 ) mod 4 for a given n | function for obtaining the value of f ( n ) mod 4 ; Find the remainder of n when divided by 4 ; If n is of the form 4 k or 4 k + 3 ; If n is of the form 4 k + 1 or 4 k + 2 ; Driver code
< ? php function fnMod ( $ n ) { $ rem = $ n % 4 ; if ( $ rem == 0 or $ rem == 3 ) return 0 ; else if ( $ rem == 1 or $ rem == 2 ) return 1 ; } $ n = 6 ; echo fnMod ( $ n ) ; ? >
Minimum increment operations to make the array in increasing order | function to find minimum moves required to make the array in increasing order ; to store answer ; iterate over an array ; non - increasing order ; add moves to answer ; increase the element ; return required answer ; Driver code
< ? php function MinimumMoves ( & $ a , $ n , $ x ) { $ ans = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] <= $ a [ $ i - 1 ] ) { $ p = ( $ a [ $ i - 1 ] - $ a [ $ i ] ) / $ x + 1 ; $ ans += $ p ; $ a [ $ i ] += $ p * $ x ; } } return $ ans ; } $ arr = array ( 1 , 3 , 3 , 2 ) ; $ x = 2 ; $ n = sizeof ( $ arr ) ; echo ( ( int ) MinimumMoves ( $ arr , $ n , $ x ) ) ; ? >
Check if a large number is divisible by 2 , 3 and 5 or not | function to return sum of digits of a number ; function to Check if a large number is divisible by 2 , 3 and 5 or not ; Driver code
< ? php function SumOfDigits ( $ str , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += ( int ) ( $ str [ $ i ] - '0' ) ; return $ sum ; } function Divisible ( $ str , $ n ) { if ( SumOfDigits ( $ str , $ n ) % 3 == 0 and $ str [ $ n - 1 ] == '0' ) return true ; return false ; } $ str = "263730746028908374890" ; $ n = strlen ( $ str ) ; if ( Divisible ( $ str , $ n ) ) echo " YES " ; else echo " NO " ; ? >
Count all the numbers in a range with smallest factor as K | Function to check if k is a prime number or not ; Corner case ; Check from 2 to n - 1 ; Function to check if a number is not divisible by any number between 2 and K - 1 ; to check if the num is divisible by any numbers between 2 and k - 1 ; if not divisible by any number between 2 and k - 1 but divisible by k ; Function to find count of numbers in range [ a , b ] with smallest factor as K ; a number can be divisible only by k and not by any number less than k only if k is a prime ; to check if a number has smallest factor as K ; Driver code
< ? php function isPrime ( $ k ) { if ( $ k <= 1 ) return false ; for ( $ i = 2 ; $ i < $ k ; $ i ++ ) if ( $ k % $ i == 0 ) return false ; return true ; } function check ( $ num , $ k ) { $ flag = 1 ; for ( $ i = 2 ; $ i < $ k ; $ i ++ ) { if ( $ num % $ i == 0 ) $ flag = 0 ; } if ( $ flag == 1 ) { if ( $ num % $ k == 0 ) return 1 ; else return 0 ; } else return 0 ; } function findCount ( $ a , $ b , $ k ) { $ count = 0 ; if ( ! isPrime ( $ k ) ) return 0 ; else { for ( $ i = $ a ; $ i <= $ b ; $ i ++ ) { $ ans = check ( $ i , $ k ) ; if ( $ ans == 1 ) $ count ++ ; else continue ; } } return $ count ; } $ a = 2020 ; $ b = 6300 ; $ k = 29 ; echo ( findCount ( $ a , $ b , $ k ) ) ; ? >
Number of solutions for x < y , where a <= x <= b and c <= y <= d and x , y are integers | function to Find the number of solutions for x < y , where a <= x <= b and c <= y <= d and x , y integers . ; to store answer ; iterate explicitly over all possible values of x ; return answer ; Driver code ; function call
< ? php function NumberOfSolutions ( $ a , $ b , $ c , $ d ) { $ ans = 0 ; for ( $ i = $ a ; $ i <= $ b ; $ i ++ ) if ( $ d >= max ( $ c , $ i + 1 ) ) $ ans += $ d - max ( $ c , $ i + 1 ) + 1 ; return $ ans ; } $ a = 2 ; $ b = 3 ; $ c = 3 ; $ d = 4 ; echo NumberOfSolutions ( $ a , $ b , $ c , $ d ) ; ? >
Minimum value possible of a given function from the given set | Function to find the value of F ( n ) ; Driver code
< ? php function findF_N ( $ n ) { $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) $ ans += ( $ i + 1 ) * ( $ n - $ i - 1 ) ; return $ ans ; } $ n = 3 ; echo findF_N ( $ n ) ;
Find N digits number which is divisible by D | Function to return N digits number which is divisible by D ; to store answer ; Driver code
< ? php function findNumber ( $ n , $ d ) { $ ans = " " ; if ( $ d != 10 ) { $ ans . = strval ( $ d ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ ans . = '0' ; } else { if ( n == 1 ) $ ans . = " Impossible " ; else $ ans . = '1' ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ ans . = '0' ; } return $ ans ; } $ n = 12 ; $ d = 3 ; print ( findNumber ( $ n , $ d ) ) ;
Count all the numbers less than 10 ^ 6 whose minimum prime factor is N | PHP implementation of above approach ; the sieve of prime number and count of minimum prime factor ; form the prime sieve ; 1 is not a prime number ; form the sieve ; if i is prime ; if i is the least prime factor ; mark the number j as non prime ; count the numbers whose least prime factor is i ; form the sieve ; display ; display
< ? php $ MAX = 1000000 ; $ sieve_Prime = array_fill ( 0 , $ MAX + 4 , NULL ) ; $ sieve_count = array_fill ( 0 , $ MAX + 4 , NULL ) ; function form_sieve ( ) { global $ sieve_Prime , $ sieve_count , $ MAX ; $ sieve_Prime [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ MAX ; $ i ++ ) { if ( $ sieve_Prime [ $ i ] == 0 ) { for ( $ j = $ i * 2 ; $ j <= $ MAX ; $ j += $ i ) { if ( $ sieve_Prime [ $ j ] == 0 ) { $ sieve_Prime [ $ j ] = 1 ; $ sieve_count [ $ i ] ++ ; } } } } } form_sieve ( ) ; $ n = 2 ; echo " Count = " ▁ . ▁ ( $ sieve _ count [ $ n ] ▁ + ▁ 1 ) ▁ . ▁ " " $ n = 3 ; echo " Count = " ▁ . ▁ ( $ sieve _ count [ $ n ] ▁ + ▁ 1 ) ▁ . ▁ " " ? >
Find the largest good number in the divisors of given number N | function to return distinct prime factors ; to store distinct prime factors ; run a loop upto sqrt ( n ) ; place this prime factor in vector ; This condition is to handle the case when n is a prime number greater than 1 ; function that returns good number ; distinct prime factors ; to store answer ; product of all distinct prime factors is required answer ; Driver code ; function call
< ? php function PrimeFactors ( $ n ) { $ v = array ( ) ; $ x = $ n ; for ( $ i = 2 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ x % $ i == 0 ) { array_push ( $ v , $ i ) ; while ( $ x % $ i == 0 ) $ x /= $ i ; } } if ( $ x > 1 ) array_push ( $ v , $ x ) ; return $ v ; } function GoodNumber ( $ n ) { $ v = PrimeFactors ( $ n ) ; $ ans = 1 ; for ( $ i = 0 ; $ i < count ( $ v ) ; $ i ++ ) $ ans *= $ v [ $ i ] ; return $ ans ; } $ n = 12 ; echo GoodNumber ( $ n ) ; ? >
Find Largest Special Prime which is less than or equal to a given number | Function to check whether the number is a special prime or not ; While number is not equal to zero ; If the number is not prime return false . ; Else remove the last digit by dividing the number by 10. ; If the number has become zero then the number is special prime , hence return true ; Function to find the Largest Special Prime which is less than or equal to a given number ; Initially all numbers are considered Primes . ; There is always an answer possible ; Checking if the number is a special prime or not ; If yes print the number and break the loop . ; Else decrement the number . ; Driver code
< ? php function checkSpecialPrime ( & $ sieve , $ num ) { while ( $ num ) { if ( ! $ sieve [ $ num ] ) { return false ; } $ num = ( int ) ( $ num / 10 ) ; } return true ; } function findSpecialPrime ( $ N ) { $ sieve = array_fill ( 0 , $ N + 10 , true ) ; $ sieve [ 0 ] = $ sieve [ 1 ] = false ; for ( $ i = 2 ; $ i <= $ N ; $ i ++ ) { if ( $ sieve [ $ i ] ) { for ( $ j = $ i * $ i ; $ j <= $ N ; $ j += $ i ) { $ sieve [ $ j ] = false ; } } } while ( true ) { if ( checkSpecialPrime ( $ sieve , $ N ) ) { echo $ N . " STRNEWLINE " ; break ; } else $ N -- ; } } findSpecialPrime ( 379 ) ; findSpecialPrime ( 100 ) ; ? >
Check if a number is a Pythagorean 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 ; Driver Code ; Check if number is prime and of the form 4 * n + 1
< ? php function isPrime ( $ n ) { if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ; if ( $ n % 2 == 0 or $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) { if ( $ n % $ i == 0 or $ n % ( $ i + 2 ) == 0 ) { return false ; } } return true ; } $ n = 13 ; if ( isPrime ( $ n ) && ( $ n % 4 == 1 ) ) { echo " YES " ; } else { echo " NO " ; } ? >
Divide an isosceles triangle in two parts with ratio of areas as n : m | Function to return the height ; type cast the n , m into float ; calculate the height for cut ; Driver code
< ? php function heightCalculate ( $ H , $ n , $ m ) { $ N = $ n * 1.0 ; $ M = $ m * 1.0 ; $ h = $ H * sqrt ( $ N / ( $ N + $ M ) ) ; return $ h ; } $ H = 10 ; $ n = 3 ; $ m = 4 ; echo heightCalculate ( $ H , $ n , $ m ) ; ? >
Check n ^ 2 | Check a number is prime or not ; run a loop upto square of given number ; Check if n ^ 2 - m ^ 2 is prime ; Driver code
< ? php function isprime ( $ x ) { for ( $ i = 2 ; $ i * $ i <= $ x ; $ i ++ ) if ( $ x % i == 0 ) return false ; return true ; } function isNSqMinusnMSqPrime ( $ m , $ n ) { if ( $ n - $ m == 1 and isprime ( $ m + $ n ) ) return true ; else return false ; } $ m = 13 ; $ n = 16 ; if ( isNSqMinusnMSqPrime ( $ m , $ n ) ) echo " YES " ; else echo " NO " ; ? >
Find ' N ' number of solutions with the given inequality equations | Function to calculate all the solutions ; there is no solutions ; print first element as y - n + 1 ; print rest n - 1 elements as 1 ; initialize the number of elements and the value of x an y
< ? php function findsolution ( $ n , $ x , $ y ) { if ( ( $ y - $ n + 1 ) * ( $ y - $ n + 1 ) + $ n - 1 < $ x $ y < $ n ) { echo " No ▁ solution " ; return ; } echo $ y - $ n + 1 ; while ( $ n -- > 1 ) echo " STRNEWLINE " . 1 ; } $ n = 5 ; $ x = 15 ; $ y = 15 ; findsolution ( $ n , $ x , $ y ) ;
Number of different positions where a person can stand | Function to find the position ; Driver code
< ? php function findPosition ( $ n , $ f , $ b ) { return $ n - max ( $ f + 1 , $ n - $ b ) + 1 ; } $ n = 5 ; $ f = 2 ; $ b = 3 ; echo findPosition ( $ n , $ f , $ b ) ; ? >
Program for n | Function to find the Nth odd number ; Driver code
< ? php function nthOdd ( $ n ) { return ( 2 * $ n - 1 ) ; } $ n = 10 ; echo nthOdd ( $ n ) ; ? >
Program for n | Function to find the nth even number ; Driver code
< ? php function nthEven ( $ n ) { return ( 2 * $ n ) ; } $ n = 10 ; echo nthEven ( $ n ) ; ? >
Program to find the Nth Harmonic Number | Function to find N - th Harmonic Number ; H1 = 1 ; loop to apply the forumula Hn = H1 + H2 + H3 ... + Hn - 1 + Hn - 1 + 1 / n ; Driver Code
< ? php function nthHarmonic ( $ N ) { $ harmonic = 1.00 ; for ( $ i = 2 ; $ i <= $ N ; $ i ++ ) { $ harmonic += ( float ) 1 / $ i ; } return $ harmonic ; } $ N = 8 ; echo nthHarmonic ( $ N ) ; ? >
Program to find Nth term of series 0 , 7 , 18 , 33 , 51 , 75 , 102 , 133 , ... . . | calculate Nth term of series ; Driver code
< ? php function nthTerm ( $ n ) { return 2 * pow ( $ n , 2 ) + $ n - 3 ; } $ N = 4 ; echo nthTerm ( $ N ) + " STRNEWLINE " ; ? >
Program to find Nth term of series 0 , 10 , 30 , 60 , 99 , 150 , 210 , 280. ... ... ... . | calculate Nth term of series ; Driver code
< ? php function nthTerm ( $ n ) { return 5 * pow ( $ n , 2 ) - 5 * $ n ; } $ N = 4 ; echo nthTerm ( $ N ) ; ? >
Program to find Nth term of series 2 , 12 , 28 , 50 , 77 , 112 , 152 , 198 , ... . . | calculate Nth term of series ; Driver code
< ? php function nthTerm ( $ n ) { return 3 * pow ( $ n , 2 ) + $ n - 2 ; } $ N = 4 ; echo nthTerm ( $ N ) ; ? >
Program to find Nth term of series 4 , 14 , 28 , 46 , 68 , 94 , 124 , 158 , ... . . | calculate Nth term of series ; Driver code
< ? php function nthTerm ( $ n ) { return 2 * pow ( $ n , 2 ) + 4 * $ n - 2 ; } $ N = 4 ; echo nthTerm ( $ N ) . " STRNEWLINE " ; ? >
Program to find Nth term of series 0 , 11 , 28 , 51 , 79 , 115 , 156 , 203 , ... . | calculate Nth term of series ; Driver code
< ? php function nthTerm ( $ n ) { return 3 * pow ( $ n , 2 ) + 2 * $ n - 5 ; } $ N = 4 ; echo nthTerm ( $ N ) . " STRNEWLINE " ;
Program to find Nth term of series 0 , 9 , 22 , 39 , 60 , 85 , 114 , 147 , ... . . | calculate Nth term of series ; Driver code
< ? php function nthTerm ( $ n ) { return 2 * pow ( $ n , 2 ) + 3 * $ n - 5 ; } $ N = 4 ; echo nthTerm ( $ N ) . " STRNEWLINE " ; ? >
Program to find Nth term of series 3 , 12 , 29 , 54 , 86 , 128 , 177 , 234 , ... . . | calculate Nth term of series ; Driver code
< ? php function nthTerm ( $ n ) { return 4 * pow ( $ n , 2 ) - 3 * $ n + 2 ; } $ N = 4 ; echo nthTerm ( $ N ) . " STRNEWLINE " ; ? >
Find other two sides and angles of a right angle triangle | PHP program to print all sides and angles of right angle triangle given one side ; Function to find angle A Angle in front of side a ; applied cosine rule ; convert into degrees ; Function to find angle B Angle in front of side b ; applied cosine rule ; convert into degrees and return ; Function to print all angles of the right angled triangle ; for calculate angle A ; for calculate angle B ; Function to find other two sides of the right angled triangle ; if n is odd ; case of n = 1 handled separately ; case of n = 2 handled separately ; Print angles of the triangle ; Driver Code
< ? php $ PI = 3.1415926535 ; function findAnglesA ( $ a , $ b , $ c ) { global $ PI ; $ A = acos ( ( $ b * $ b + $ c * $ c - $ a * $ a ) / ( 2 * $ b * $ c ) ) ; return $ A * 180 / $ PI ; } function findAnglesB ( $ a , $ b , $ c ) { global $ PI ; $ B = acos ( ( $ a * $ a + $ c * $ c - $ b * $ b ) / ( 2 * $ a * $ c ) ) ; return $ B * 180 / $ PI ; } function printAngles ( $ a , $ b , $ c ) { $ x = ( double ) $ a ; $ y = ( double ) $ b ; $ z = ( double ) $ c ; $ A = findAnglesA ( $ x , $ y , $ z ) ; $ B = findAnglesB ( $ x , $ y , $ z ) ; echo " Angles ▁ are ▁ A ▁ = ▁ " . $ A . " , B = " ▁ . ▁ $ B ▁ . ▁ " , C = 90 " ; } function printOtherSides ( $ n ) { if ( $ n & 1 ) { if ( $ n == 1 ) echo " - 1 STRNEWLINE " ; else { $ b = ( $ n * $ n - 1 ) / 2 ; $ c = ( $ n * $ n + 1 ) / 2 ; echo " Side ▁ b ▁ = ▁ " . $ b . " , ▁ Side ▁ c ▁ = ▁ " . $ c . " STRNEWLINE " ; } } else { if ( $ n == 2 ) echo " - 1 STRNEWLINE " ; else { $ b = $ n * $ n / 4 - 1 ; $ c = $ n * $ n / 4 + 1 ; echo " Side ▁ b ▁ = ▁ " . $ b . " , ▁ Side ▁ c ▁ = ▁ " . $ c . " STRNEWLINE " ; } } printAngles ( $ n , $ b , $ c ) ; } $ a = 12 ; printOtherSides ( $ a ) ; ? >
Sum of the first N terms of the series 2 , 6 , 12 , 20 , 30. ... | Function to calculate the sum ; number of terms to be included in the sum ; find the Sn
< ? php function calculateSum ( $ n ) { return $ n * ( $ n + 1 ) / 2 + $ n * ( $ n + 1 ) * ( 2 * $ n + 1 ) / 6 ; } $ n = 3 ; echo " Sum = " ? >
Program to find the Nth term of the series 0 , 5 , 14 , 27 , 44 , ... ... . . | Calculate Nth term of series ; Driver code
< ? php function nthTerm ( $ n ) { return 2 * pow ( $ n , 2 ) - $ n - 1 ; } $ N = 4 ; echo nthTerm ( $ N ) ; ? >
Program to find the Nth term of the series 0 , 5 , 18 , 39 , 67 , 105 , 150 , 203 , ... | calculate Nth term of series ; Driver code
< ? php function nthTerm ( $ n ) { return 4 * pow ( $ n , 2 ) - 7 * $ n + 3 ; } $ N = 4 ; echo nthTerm ( $ N ) ; ? >
Check whether a given Number is Power | PHP program to find whether a number is power - isolated or not ; for 2 as prime factor ; for odd prime factor ; calculate product of powers and prime factors ; check result for power - isolation ; driver code
< ? php function checkIfPowerIsolated ( $ num ) { $ input = $ num ; $ count = 0 ; $ factor = array ( ) ; if ( $ num % 2 == 0 ) { while ( $ num % 2 == 0 ) { ++ $ count ; $ num /= 2 ; } $ factor [ 2 ] = $ count ; } for ( $ i = 3 ; $ i * $ i <= $ num ; $ i += 2 ) { $ count = 0 ; while ( $ num % $ i == 0 ) { ++ $ count ; $ num /= $ i ; } if ( $ count ) $ factor [ $ i ] = $ count ; } if ( $ num > 1 ) $ factor [ $ num ] = 1 ; $ product = 1 ; foreach ( $ factor as $ primefactor = > $ power ) { $ product = $ product * $ primefactor * $ power ; } if ( $ product == $ input ) print_r ( " Power - isolated ▁ Integer STRNEWLINE " ) ; else print_r ( " Not ▁ a ▁ Power - isolated ▁ Integer STRNEWLINE " ) ; } checkIfPowerIsolated ( 12 ) ; checkIfPowerIsolated ( 18 ) ; checkIfPowerIsolated ( 35 ) ; ? >
Program to find the Nth term of the series 3 , 7 , 13 , 21 , 31. ... . | Function to calculate sum ; Return Nth term ; declaration of number of terms ; Get the Nth term
< ? php function getNthTerm ( $ N ) { return ( pow ( $ N , 2 ) + $ N + 1 ) ; } $ N = 11 ; echo getNthTerm ( $ N ) ; ? >