codeparrot/xlcost-text-to-code huggingface dataset:
Number of digits in the nth number made of given four digits | Efficient function to calculate number of digits in the nth number constructed by using 6 , 1 , 4 and 9 as digits in the ascending order . ; Number of digits increase after every i - th number where i increases in powers of 4. ; Driver Code
< ? php function number_of_digits ( $ n ) { $ i ; $ res ; $ sum = 0 ; for ( $ i = 4 , $ res = 1 ; ; $ i *= 4 , $ res ++ ) { $ sum += $ i ; if ( $ sum >= $ n ) break ; } return $ res ; } $ n = 21 ; echo number_of_digits ( $ n ) , " STRNEWLINE " ; ? >
Print prime numbers from 1 to N in reverse order | PHP program to print all primes between 1 to N in reverse order using Sieve of Eratosthenes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Print all prime numbers in reverse order ; static input ; to display ; Reverseorder ( $N ) ; calling the function
< ? php function Reverseorder ( $ n ) { $ prime = array_fill ( 0 , $ n + 1 , true ) ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = false ; } } for ( $ p = $ n ; $ p >= 2 ; $ p -- ) if ( $ prime [ $ p ] ) echo $ p . " " ; } $ N = 25 ; echo " Prime ▁ number ▁ in ▁ reverse ▁ order STRNEWLINE " ; if ( $ N == 1 ) echo " No ▁ prime ▁ no ▁ exist ▁ in ▁ this ▁ range " ; else ? >
Finding n | Function to generate a fixed number ; Driver Method
< ? php function magicOfSequence ( $ N ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) $ sum += ( $ i * $ i * $ i + $ i * 2 ) ; return $ sum ; } $ N = 4 ; echo magicOfSequence ( $ N ) ; ? >
Expressing a number as sum of consecutive | Set 2 ( Using odd factors ) | returns the number of odd factors ; If i is an odd factor and n is a perfect square ; If n is not perfect square ; N as sum of consecutive numbers
< ? php function countOddFactors ( $ n ) { $ odd_factors = 0 ; for ( $ i = 1 ; 1 * $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( 1 * $ i * $ i == $ n ) { if ( $ i & 1 ) $ odd_factors ++ ; } else { if ( $ i & 1 ) $ odd_factors ++ ; $ factor = $ n / $ i ; if ( $ factor & 1 ) $ odd_factors ++ ; } } } return $ odd_factors - 1 ; } $ N = 15 ; echo ( countOddFactors ( $ N ) . ( " " ) ) ; $ N = 10 ; echo ( countOddFactors ( $ N ) ) ; ? >
Making zero array by decrementing pairs of adjacent | PHP program to find if it is possible to make all array elements 0 by decrement operations . ; used for storing the sum of even and odd position element in array . ; if position is odd , store sum value of odd position in odd ; if position is even , store sum value of even position in even ; Driver Code
< ? php function isPossibleToZero ( $ a , $ n ) { $ even = 0 ; $ odd = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ i & 1 ) $ odd += $ a [ $ i ] ; else $ even += $ a [ $ i ] ; } return ( $ odd == $ even ) ; } $ arr = array ( 0 , 1 , 1 , 0 ) ; $ n = sizeof ( $ arr ) ; if ( isPossibleToZero ( $ arr , $ n ) ) echo " YES " ; else echo " NO " ; ? >
Program for sum of cos ( x ) series | PHP program to find the sum of cos ( x ) series ; here x is in degree . we have to convert it to radian for using it with series formula , as in series expansion angle is in radian ; Driver Code
< ? php $ PI = 3.142 ; function cosXSertiesSum ( $ x , $ n ) { global $ PI ; $ x = $ x * ( $ PI / 180.0 ) ; $ res = 1 ; $ sign = 1 ; $ fact = 1 ; $ pow = 1 ; for ( $ i = 1 ; $ i < 5 ; $ i ++ ) { $ sign = $ sign * -1 ; $ fact = $ fact * ( 2 * $ i - 1 ) * ( 2 * $ i ) ; $ pow = $ pow * $ x * $ x ; $ res = $ res + $ sign * $ pow / $ fact ; } return $ res ; } $ x = 50 ; $ n = 5 ; echo cosXSertiesSum ( $ x , 5 ) ; ? >
Sum of digits written in different bases from 2 to n | function to calculate sum of digit for a given base ; Sum of digits ; Calculating the number ( n ) by taking mod with the base and adding remainder to the result and parallelly reducing the num value . ; returning the result ; function calling for multiple bases ; Driver code
< ? php function solve ( $ n , $ base ) { $ result = 0 ; while ( $ n > 0 ) { $ remainder = $ n % $ base ; $ result = $ result + $ remainder ; $ n = $ n / $ base ; } return $ result ; } function printSumsOfDigits ( $ n ) { for ( $ base = 2 ; $ base < $ n ; ++ $ base ) { echo ( solve ( $ n , $ base ) ) ; echo ( " ▁ " ) ; } } $ n = 8 ; printSumsOfDigits ( $ n ) ; ? >
Possible two sets from first N natural numbers difference of sums as D | Function returns true if it is possible to split into two sets otherwise returns false ; Driver code
< ? php function check ( $ N , $ D ) { $ temp = ( $ N * ( $ N + 1 ) ) / 2 + $ D ; return ( $ temp % 2 == 0 ) ; } $ N = 5 ; $ M = 7 ; if ( check ( $ N , $ M ) ) echo ( " yes " ) ; else echo ( " no " ) ;
Minimum digits to remove to make a number Perfect Square | function to check minimum number of digits should be removed to make this number a perfect square ; size of the string ; our final answer ; to store string which is perfect square . ; We make all possible subsequences ; to check jth bit is set or not . ; we do not consider a number with leading zeros ; convert our temporary string into integer ; checking temp is perfect square or not . ; taking maximum sized string ; print PerfectSquare ; Driver code
< ? php function perfectSquare ( $ s ) { $ n = strlen ( $ s ) ; $ ans = -1 ; $ num = " " ; for ( $ i = 1 ; $ i < ( 1 << $ n ) ; $ i ++ ) { $ str = " " ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( ( $ i >> $ j ) & 1 ) { $ str = $ str . $ s [ $ j ] ; } } if ( $ str [ 0 ] != '0' ) { $ temp = 0 ; for ( $ j = 0 ; $ j < strlen ( $ str ) ; $ j ++ ) $ temp = $ temp * 10 + ( ord ( $ str [ $ j ] ) - ord ( '0' ) ) ; $ k = ( int ) ( sqrt ( $ temp ) ) ; if ( ( $ k * $ k ) == $ temp ) { if ( $ ans < strlen ( $ str ) ) { $ ans = strlen ( $ str ) ; $ num = $ str ; } } } } if ( $ ans == -1 ) return $ ans ; else { echo ( $ num . " " ) ; return ( $ n - $ ans ) ; } } echo ( perfectSquare ( "8314" ) . " STRNEWLINE " ) ; echo ( perfectSquare ( "753" ) . " STRNEWLINE " ) ; ? >
Lagrange 's four square theorem | Prints all the possible combinations 4 numbers whose sum of squares is equal to the given no . ; loops checking the sum of squares ; if sum of four squares equals the given no . ; printing the numbers ; Driver Code ; 74 = 0 * 0 + 0 * 0 + 5 * 5 + 7 * 7 74 = 0 * 0 + 1 * 1 + 3 * 3 + 8 * 8 74 = 0 * 0 + 3 * 3 + 4 * 4 + 7 * 7 74 = 1 * 1 + 1 * 1 + 6 * 6 + 6 * 6 74 = 2 * 2 + 3 * 3 + 5 * 5 + 6 * 6
< ? php function printFourSquares ( $ a ) { for ( $ i = 0 ; $ i * $ i <= $ a ; $ i ++ ) { for ( $ j = $ i ; $ j * $ j <= $ a ; $ j ++ ) { for ( $ k = $ j ; $ k * $ k <= $ a ; $ k ++ ) { for ( $ l = $ k ; $ l * $ l <= $ a ; $ l ++ ) { if ( $ i * $ i + $ j * $ j + $ k * $ k + $ l * $ l == $ a ) { echo $ a , " = " ▁ , ▁ $ i ▁ , ▁ " * " ▁ , $ i , STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL " + " ▁ , ▁ $ j ▁ , ▁ " * " ▁ , ▁ $ j ▁ , ▁ " + " echo $ k , " * " ▁ , ▁ $ k ▁ , ▁ " + " $ l , " * " , $ l , " STRNEWLINE " ; } } } } } } $ a = 74 ; printFourSquares ( $ a ) ; ? >
Hardy | A function to count prime factors of a given number n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; This condition is to handle the case when n is a prime number greater than 2 ; Driver Code
< ? php function exactPrimeFactorCount ( $ n ) { $ count = 0 ; if ( $ n % 2 == 0 ) { $ count ++ ; while ( $ n % 2 == 0 ) $ n = $ n / 2 ; } for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i = $ i + 2 ) { if ( $ n % $ i == 0 ) { $ count ++ ; while ( $ n % $ i == 0 ) $ n = $ n / $ i ; } } if ( $ n > 2 ) $ count ++ ; return $ count ; } $ n = 51242183 ; echo " The ▁ number ▁ of ▁ distinct ▁ prime " . " ▁ factors ▁ is / are ▁ " , exactPrimeFactorCount ( $ n ) , " STRNEWLINE " ; echo " The ▁ value ▁ of ▁ log ( log ( n ) ) ▁ " . " is ▁ " , log ( log ( $ n ) ) , " STRNEWLINE " ; ? >
Number of Digits in a ^ b | function to calculate number of digits in a ^ b ; Driver Code
< ? php function no_of_digit ( $ a , $ b ) { return ( ( int ) ( $ b * log10 ( $ a ) ) + 1 ) ; } $ a = 2 ; $ b = 100 ; echo ( " no . ▁ of ▁ digits ▁ = ▁ " . no_of_digit ( $ a , $ b ) ) ; ? >
Check whether a number is Emirpimes or not | Checking whether a number is semi - prime or not ; Increment count of prime numbers ; If number is still greater than 1 , after exiting the for loop add it to the count variable as it indicates the number is a prime number ; Return '1' if count is equal to '2' else return '0' ; Checking whether a number is emirpimes or not ; Number itself is not semiprime . ; Finding reverse of n . ; The definition of emirpimes excludes palindromes , hence we do not check further , if the number entered is a palindrome ; Checking whether the reverse of the semi prime number entered is also a semi prime number or not ; Driver Code
< ? php function checkSemiprime ( $ num ) { $ cnt = 0 ; for ( $ i = 2 ; $ cnt < 2 && $ i * $ i <= $ num ; ++ $ i ) { while ( $ num % $ i == 0 ) { $ num /= $ i ; ++ $ cnt ; } } if ( $ num > 1 ) ++ $ cnt ; return $ cnt == 2 ; } function isEmirpimes ( $ n ) { if ( checkSemiprime ( $ n ) == false ) return false ; $ r = 0 ; for ( $ t = $ n ; $ t != 0 ; $ t = $ t / $ n ) $ r = $ r * 10 + $ t % 10 ; if ( $ r == $ n ) return false ; return ( checkSemiprime ( $ r ) ) ; } $ n = 15 ; if ( isEmirpimes ( $ n ) ) echo " No " ; else echo " Yes " ; ? >
Generate a list of n consecutive composite numbers ( An interesting method ) | function to find factorial of given number ; Prints n consecutive numbers . ; Driver Code
< ? php function factorial ( $ n ) { $ res = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res *= $ i ; return $ res ; } function printNComposite ( int $ n ) { $ fact = factorial ( $ n + 1 ) ; for ( $ i = 2 ; $ i <= $ n + 1 ; ++ $ i ) echo $ fact + $ i , " ▁ " ; } $ n = 4 ; printNComposite ( $ n ) ; ? >
Frugal Number | Finding primes upto entered number ; Finding primes by Sieve of Eratosthenes method ; If prime [ i ] is not changed , then it is prime ; Update all multiples of p ; Forming array of the prime numbers found ; Returns number of digits in n ; Checking whether a number is Frugal or not ; Finding number of digits in prime factorization of the number ; Exponent for current factor ; Counting number of times this prime factor divides ( Finding exponent ) ; Finding number of digits in the exponent Avoiding exponents of value 1 ; Checking condition for frugal number ; Driver Code
< ? php function primes ( $ n ) { $ prime = array ( ) ; for ( $ i = 0 ; $ i < $ n + 1 ; $ i ++ ) $ prime [ $ i ] = true ; for ( $ i = 2 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ prime [ $ i ] == true ) { for ( $ j = $ i * 2 ; $ j <= $ n ; $ j += $ i ) $ prime [ $ j ] = false ; } } $ arr = array ( ) ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) if ( $ prime [ $ i ] ) array_push ( $ arr , $ i ) ; return $ arr ; } function countDigits ( $ n ) { $ temp = $ n ; $ c = 0 ; while ( $ temp != 0 ) { $ temp = intval ( $ temp / 10 ) ; $ c ++ ; } return $ c ; } function frugal ( $ n ) { $ r = primes ( $ n ) ; $ t = $ n ; $ s = 0 ; for ( $ i = 0 ; $ i < count ( $ r ) ; $ i ++ ) { if ( $ t % $ r [ $ i ] == 0 ) { $ k = 0 ; while ( $ t % $ r [ $ i ] == 0 ) { $ t = intval ( $ t / $ r [ $ i ] ) ; $ k ++ ; } if ( $ k == 1 ) $ s = $ s + countDigits ( $ r [ $ i ] ) ; else if ( $ k != 1 ) $ s = $ s + countDigits ( $ r [ $ i ] ) + countDigits ( $ k ) ; } } return ( countDigits ( $ n ) > $ s && $ s != 0 ) ; } $ n = 343 ; if ( frugal ( $ n ) ) echo ( " A ▁ Frugal ▁ number STRNEWLINE " ) ; else echo ( " Not ▁ a ▁ frugal ▁ number STRNEWLINE " ) ; ? >
N | PHP program to find n - th number which is both square and cube . ; Driver code
< ? php function nthSquareCube ( $ n ) { return $ n * $ n * $ n * $ n * $ n * $ n ; } $ n = 5 ; echo ( nthSquareCube ( $ n ) ) ; ? >
Squared triangular number ( Sum of cubes ) | Function to find if the given number is sum of the cubes of first n natural numbers ; Start adding cubes of the numbers from 1 ; If sum becomes equal to s return n ; Driver code
< ? php function findS ( $ s ) { $ sum = 0 ; for ( $ n = 1 ; $ sum < $ s ; $ n ++ ) { $ sum += $ n * $ n * $ n ; if ( $ sum == $ s ) return $ n ; } return -1 ; } $ s = 9 ; $ n = findS ( $ s ) ; if ( $ n == -1 ) echo ( " - 1" ) ; else echo ( $ n ) ; ? >
Number with even sum of digits | Function to find kth good number . ; Find the last digit of n . ; If last digit is between 0 to 4 then return 2 * n . ; If last digit is between 5 to 9 then return 2 * n + 1. ; Driver code
< ? php function findKthGoodNo ( $ n ) { $ lastDig = $ n % 10 ; if ( $ lastDig >= 0 && $ lastDig <= 4 ) return $ n << 1 ; else return ( $ n << 1 ) + 1 ; } $ n = 10 ; echo ( findKthGoodNo ( $ n ) ) ; ? >
Nicomachu 's Theorem | PHP program to verify Nicomachu 's Theorem ; Compute sum of cubes ; Check if sum is equal to given formula . ; Driver Code
< ? php function NicomachuTheorum_sum ( $ n ) { $ sum = 0 ; for ( $ k = 1 ; $ k <= $ n ; $ k ++ ) $ sum += $ k * $ k * $ k ; $ triNo = $ n * ( $ n + 1 ) / 2 ; if ( $ sum == $ triNo * $ triNo ) echo " Yes " ; else echo " No " ; } $ n = 5 ; NicomachuTheorum_sum ( $ n ) ; ? >
Largest even digit number not greater than N | function to check if all digits are even of a given number ; iterate for all digits ; if digit is odd ; all digits are even ; function to return the largest number with all digits even ; Iterate till we find a number with all digits even ; Driver Code
< ? php function checkDigits ( $ n ) { while ( $ n ) { if ( ( $ n % 10 ) % 2 ) return 0 ; $ n /= 10 ; } return 1 ; } function largestNumber ( $ n ) { for ( $ i = $ n ; ; $ i -- ) if ( checkDigits ( $ i ) ) return $ i ; } $ N = 23 ; echo ( largestNumber ( $ N ) ) ; ? >
Largest even digit number not greater than N | function to return the largest number with all digits even ; convert the number to a string for easy operations ; find first odd digit ; if no digit , then N is the answer ; till first odd digit , add all even numbers ; decrease 1 from the odd digit ; add 0 in the rest of the digits ; Driver Code
< ? php function largestNumber ( $ n ) { $ s = " " ; $ duplicate = $ n ; while ( $ n ) { $ s = chr ( $ n % 10 + 48 ) . $ s ; $ n = ( int ) ( $ n / 10 ) ; } $ index = -1 ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( ord ( $ s [ $ i ] - '0' ) % 2 & 1 ) { $ index = $ i ; break ; } } if ( $ index == -1 ) return $ duplicate ; $ num = 0 ; for ( $ i = 0 ; $ i < $ index ; $ i ++ ) $ num = $ num * 10 + ( ord ( $ s [ $ i ] ) - ord ( '0' ) ) ; $ num = $ num * 10 + ( ( ord ( $ s [ $ i ] ) - ord ( '0' ) ) - 1 ) ; for ( $ i = $ index + 1 ; $ i < strlen ( $ s ) ; $ i ++ ) $ num = $ num * 10 + 8 ; return $ num ; } $ N = 24578 ; echo largestNumber ( $ N ) ; ? >
Number of digits in 2 raised to power n | Function to find number of digits in 2 ^ n ; Driver code
< ? php function countDigits ( $ n ) { return intval ( $ n * log10 ( 2 ) + 1 ) ; } $ n = 5 ; echo ( countDigits ( $ n ) ) ; ? >
Smallest even digits number not less than N | function to check if all digits are even of a given number ; iterate for all digits ; if digit is odd ; all digits are even ; function to return the smallest number with all digits even ; iterate till we find a number with all digits even ; Driver Code
< ? php function check_digits ( $ n ) { while ( $ n ) { if ( ( $ n % 10 ) % 2 ) return 0 ; $ n /= 10 ; } return 1 ; } function smallest_number ( $ n ) { for ( $ i = $ n ; ; $ i ++ ) if ( check_digits ( $ i ) ) return $ i ; } $ N = 2397 ; echo smallest_number ( $ N ) ; ? >
Smallest triangular number larger than p | PHP code to find the bucket to choose for picking flowers out of it ; Driver code
< ? php function findBucketNo ( $ p ) { return ceil ( ( sqrt ( 8 * $ p + 1 ) - 1 ) / 2 ) ; } $ p = 10 ; echo ( findBucketNo ( $ p ) ) ; ? >
LCM of factorial and its neighbors | function to calculate the factorial ; returning the factorial of the largest number in the given three consecutive numbers ; Driver code
< ? php function factorial ( $ n ) { if ( $ n == 0 ) return 1 ; return $ n * factorial ( $ n - 1 ) ; } function LCMOfNeighbourFact ( $ n ) { return factorial ( $ n + 1 ) ; } $ N = 5 ; echo ( LCMOfNeighbourFact ( $ N ) ) ; ? >
Check if the given two numbers are friendly pair or not | Returns sum of all factors of n . ; Traversing through all prime factors . ; THE BELOW STATEMENT MAKES IT BETTER THAN ABOVE METHOD AS WE REDUCE VALUE OF n . ; This condition is to handle the case when n is a prime number greater than 2. ; Function to return gcd of a and b ; Function to check if the given two number are friendly pair or not . ; Finding the sum of factors of n and m ; finding gcd of n and sum of its factors . ; finding gcd of m and sum of its factors . ; checking is numerator and denominator of abundancy index of both number are equal or not . ; Driver code
< ? php function sumofFactors ( $ n ) { $ res = 1 ; for ( $ i = 2 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { $ count = 0 ; $ curr_sum = 1 ; $ curr_term = 1 ; while ( $ n % $ i == 0 ) { $ count ++ ; $ n = $ n / $ i ; $ curr_term *= $ i ; $ curr_sum += $ curr_term ; } $ res *= $ curr_sum ; } if ( $ n >= 2 ) $ res *= ( 1 + $ n ) ; return $ res ; } function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function checkFriendly ( $ n , $ m ) { $ sumFactors_n = sumofFactors ( $ n ) ; $ sumFactors_m = sumofFactors ( $ m ) ; $ gcd_n = gcd ( $ n , $ sumFactors_n ) ; $ gcd_m = gcd ( $ m , $ sumFactors_m ) ; if ( $ n / $ gcd_n == $ m / $ gcd_m and $ sumFactors_n / $ gcd_n == $ sumFactors_m / $ gcd_m ) return true ; else return false ; } $ n = 6 ; $ m = 28 ; if ( checkFriendly ( $ n , $ m ) ) echo " Yes " ; else echo " No " ; ? >
Find n | PHP program to find n - th Fortunate number ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to Find primorial of order n ( product of first n prime numbers ) . ; Function to find next prime number greater than n ; Note that difference ( or m ) should be greater than 1. ; loop continuously until isPrime returns true for a number above n ; Ignoring the prime number that is 1 greater than n ; Returns n - th Fortunate number ; 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 primorial ( $ n ) { $ p = 2 ; $ n -- ; for ( $ i = 3 ; $ n != 0 ; $ i ++ ) { if ( isPrime ( $ i ) ) { $ p = $ p * $ i ; $ n -- ; } $ i ++ ; } return $ p ; } function findNextPrime ( $ n ) { $ nextPrime = $ n + 2 ; while ( true ) { if ( isPrime ( $ nextPrime ) ) break ; $ nextPrime ++ ; } return $ nextPrime ; } function fortunateNumber ( $ n ) { $ p = primorial ( $ n ) ; return findNextPrime ( $ p ) - $ p ; } $ n = 5 ; echo fortunateNumber ( $ n ) , " STRNEWLINE " ; ? >
Probability for three randomly chosen numbers to be in AP | function to calculate probability ; Driver code
< ? php function procal ( $ n ) { return ( 3.0 * $ n ) / ( 4.0 * ( $ n * $ n ) - 1 ) ; } $ a = array ( 1 , 2 , 3 , 4 , 5 ) ; $ n = sizeof ( $ a ) ; echo procal ( $ n ) ; ? >
Fermat 's Last Theorem | PHP program to verify fermat 's last theorem for a given range and n. ; Check if there exists a triplet such that a ^ n + b ^ n = c ^ n ; Driver Code
< ? php function testSomeNumbers ( $ limit , $ n ) { if ( $ n < 3 ) for ( $ a = 1 ; $ a <= $ limit ; $ a ++ ) for ( $ b = $ a ; $ b <= $ limit ; $ b ++ ) { $ pow_sum = pow ( $ a , $ n ) + pow ( $ b , $ n ) ; $ c = pow ( $ pow_sum , 1.0 / $ n ) ; $ c_pow = pow ( $ c , $ n ) ; if ( $ c_pow != $ pow_sum ) { echo " Count ▁ example ▁ found " ; return ; } } echo " No ▁ counter ▁ example ▁ within ▁ " . " given ▁ range ▁ and ▁ data " ; } testSomeNumbers ( 10 , 3 ) ; ? >
Product of given N fractions in reduced form | Function to return gcd of a and b ; Print the Product of N fraction in Reduced Form . ; finding the product of all N numerators and denominators . ; Finding GCD of new numerator and denominator ; Converting into reduced form . ; Driver Code
< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function productReduce ( $ n , $ num , $ den ) { $ new_num = 1 ; $ new_den = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ new_num *= $ num [ $ i ] ; $ new_den *= $ den [ $ i ] ; } $ GCD = gcd ( $ new_num , $ new_den ) ; $ new_num /= $ GCD ; $ new_den /= $ GCD ; echo $ new_num , " / " , $ new_den , " STRNEWLINE " ; } $ n = 3 ; $ num = array ( 1 , 2 , 5 ) ; $ den = array ( 2 , 1 , 6 ) ; productReduce ( $ n , $ num , $ den ) ; ? >
Find value of ( n ^ 1 + n ^ 2 + n ^ 3 + n ^ 4 ) mod 5 for given n | function for f ( n ) mod 5 ; if n % 5 == 1 return 4 ; else return 0 ; Driver Code
< ? php function fnMod ( $ n ) { if ( $ n % 5 == 1 ) return 4 ; else return 0 ; } $ n = 10 ; echo fnMod ( $ n ) , " STRNEWLINE " ; $ n = 11 ; echo fnMod ( $ n ) ; ? >
Recursive sum of digits of a number formed by repeated appends | return single digit sum of a number . ; Returns recursive sum of digits of a number formed by repeating a number X number of times until sum become single digit . ; Driver Code
< ? php function digSum ( $ n ) { if ( $ n == 0 ) return 0 ; return ( $ n % 9 == 0 ) ? 9 : ( $ n % 9 ) ; } function repeatedNumberSum ( $ n , $ x ) { $ sum = $ x * digSum ( $ n ) ; return digSum ( $ sum ) ; } $ n = 24 ; $ x = 3 ; echo repeatedNumberSum ( $ n , $ x ) ; ? >
Sum of n digit numbers divisible by a given number | Returns sum of n digit numbers divisible by ' number ' ; compute the first and last term ; sum of number which having n digit and divisible by number ; Driver code
< ? php function totalSumDivisibleByNum ( $ n , $ number ) { $ firstnum = pow ( 10 , $ n - 1 ) ; $ lastnum = pow ( 10 , $ n ) ; $ sum = 0 ; for ( $ i = $ firstnum ; $ i < $ lastnum ; $ i ++ ) if ( $ i % $ number == 0 ) $ sum += $ i ; return $ sum ; } $ n = 3 ; $ num = 7 ; echo totalSumDivisibleByNum ( $ n , $ num ) , " " ; ? >
Count n digit numbers divisible by given number | Returns count of n digit numbers divisible by ' number ' ; compute the first and last term ; count total number of which having n digit and divisible by number ; Driver code
< ? php function numberofterm ( $ n , $ number ) { $ firstnum = pow ( 10 , $ n - 1 ) ; $ lastnum = pow ( 10 , $ n ) ; $ count = 0 ; for ( $ i = $ firstnum ; $ i < $ lastnum ; $ i ++ ) if ( $ i % $ number == 0 ) $ count ++ ; return $ count ; } $ n = 3 ; $ num = 7 ; echo numberofterm ( $ n , $ num ) ; ? >
N | PHP program to find N - th term in George Cantor set of rational numbers ; let i = numerator ; let j = denominator ; to keep the check of no . of terms ; loop till k is not equal to n ; check if k is already equal to N then the first term is the required rational number ; loop for traversing from right to left downwards diagonally ; loop for traversing from left to right upwards diagonally ; Driver Code
< ? php function georgeCantor ( $ n ) { $ i = 1 ; $ j = 1 ; $ k = 1 ; while ( $ k < $ n ) { $ j ++ ; $ k ++ ; if ( $ k == $ n ) break ; while ( $ j > 1 && $ k < $ n ) { $ i ++ ; $ j -- ; $ k ++ ; } if ( $ k == $ n ) break ; $ i ++ ; $ k ++ ; if ( $ k == $ n ) break ; while ( $ i > 1 && $ k < $ n ) { $ i -- ; $ j ++ ; $ k ++ ; } } echo " N - th ▁ term ▁ : ▁ " , $ i , " / " , $ j ; } $ n = 15 ; georgeCantor ( $ n ) ; ? >
Number is divisible by 29 or not | Returns true if n is divisible by 29 else returns false . ; add the lastdigit * 3 to remaining number until number becomes of only 2 digit ; return true if number is divisible by 29 another ; Driver Code
< ? php function isDivisible ( $ n ) { while ( intval ( $ n / 100 ) ) { $ last_digit = $ n % 10 ; $ n = intval ( $ n / 10 ) ; $ n += $ last_digit * 3 ; } return ( $ n % 29 == 0 ) ; } $ n = 348 ; if ( isDivisible ( $ n ) ) echo " Yes " ; else echo " No " ; ? >