codeparrot/xlcost-text-to-code huggingface dataset:
Check if a larger number divisible by 36 | Function to check whether a number is divisible by 36 or not ; null number cannot be divisible by 36 ; single digit number other than 0 is not divisible by 36 ; number formed by the last 2 digits ; if number is not divisible by 4 ; number is divisible by 4 calculate sum of digits ; sum of digits is not divisible by 9 ; number is divisible by 4 and 9 hence , number is divisible by 36 ; Driver Code
< ? php function divisibleBy36 ( $ num ) { $ l = strlen ( $ num ) ; if ( $ l == 0 ) return " No " ; if ( $ l == 1 && $ num [ 0 ] != '0' ) return " No " ; $ two_digit_num = ( $ num [ $ l - 2 ] - '0' ) * 10 + ( $ num [ $ l - 1 ] - '0' ) ; if ( $ two_digit_num % 4 != 0 ) return " No " ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) $ sum += ( $ num [ $ i ] - '0' ) ; if ( $ sum % 9 != 0 ) return " No " ; return " Yes " ; } $ num = "92567812197966231384" ; echo ( divisibleBy36 ( $ num ) ) ; ? >
Check if a large number is divisible by 8 or not | Function to find that number divisible by 8 or not ; Empty string ; If there is single digit ; If there is double digit ; If number formed by last three digits is divisible by 8. ; Driver code
< ? php function check ( $ str ) { $ n = strlen ( $ str ) ; if ( $ n == 0 ) return false ; if ( $ n == 1 ) return ( ( $ str [ 0 ] - '0' ) % 8 == 0 ) ; if ( $ n == 2 ) return ( ( ( $ str [ $ n - 2 ] - '0' ) * 10 + ( $ str [ $ n - 1 ] - '0' ) ) % 8 == 0 ) ; $ last = $ str [ $ n - 1 ] - '0' ; $ second_last = $ str [ $ n - 2 ] - '0' ; $ third_last = $ str [ $ n - 3 ] - '0' ; return ( ( $ third_last * 100 + $ second_last * 10 + $ last ) % 8 == 0 ) ; } $ str = "76952" ; $ x = check ( $ str ) ? " Yes " : " No ▁ " ; echo ( $ x ) ; ? >
Prime points ( Points that split a number into two primes ) | Function to count number of digits ; Function to check whether a number is prime or not . Returns 0 if prime else - 1 ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to print prime points ; counting digits ; As single and double digit numbers do not have left and right number pairs ; Finding all left and right pairs . Printing the prime points accordingly . Discarding first and last index point ; Calculating left number ; Calculating right number ; Prime point condition ; No prime point found ; Driver Code
< ? php function countDigits ( $ n ) { $ count = 0 ; while ( $ n > 0 ) { $ count ++ ; $ n = ( int ) ( $ n / 10 ) ; } return $ count ; } function checkPrime ( $ n ) { if ( $ n <= 1 ) return -1 ; if ( $ n <= 3 ) return 0 ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return -1 ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return -1 ; return 0 ; } function printPrimePoints ( $ n ) { $ count = countDigits ( $ n ) ; if ( $ count == 1 $ count == 2 ) { echo " - 1" ; return ; } $ found = false ; for ( $ i = 1 ; $ i < ( $ count - 1 ) ; $ i ++ ) { $ left = ( int ) ( $ n / ( ( int ) pow ( 10 , $ count - $ i ) ) ) ; $ right = $ n % ( ( int ) pow ( 10 , $ count - $ i - 1 ) ) ; if ( checkPrime ( $ left ) == 0 && checkPrime ( $ right ) == 0 ) { echo $ i , " " ; $ found = true ; } } if ( $ found == false ) echo " - 1" ; } $ n = 2317 ; printPrimePoints ( $ n ) ; ? >
Find ways an Integer can be expressed as sum of n | Function to calculate and return the power of any given number ; Function to check power representations recursively ; Initialize number of ways to express x as n - th powers of different natural numbers ; Calling power of ' i ' raised to ' n ' ; Recursively check all greater values of i ; If sum of powers is equal to x then increase the value of result . ; Return the final result ; Driver Code .
< ? php function power ( $ num , $ n ) { if ( $ n == 0 ) return 1 ; else if ( $ n % 2 == 0 ) return power ( $ num , ( int ) ( $ n / 2 ) ) * power ( $ num , ( int ) ( $ n / 2 ) ) ; else return $ num * power ( $ num , ( int ) ( $ n / 2 ) ) * power ( $ num , ( int ) ( $ n / 2 ) ) ; } function checkRecursive ( $ x , $ n , $ curr_num = 1 , $ curr_sum = 0 ) { $ results = 0 ; $ p = power ( $ curr_num , $ n ) ; while ( $ p + $ curr_sum < $ x ) { $ results += checkRecursive ( $ x , $ n , $ curr_num + 1 , $ p + $ curr_sum ) ; $ curr_num ++ ; $ p = power ( $ curr_num , $ n ) ; } if ( $ p + $ curr_sum == $ x ) $ results ++ ; return $ results ; } $ x = 10 ; $ n = 2 ; echo ( checkRecursive ( $ x , $ n ) ) ; ? >
Number Theory | Generators of finite cyclic group under addition | Function to return gcd of a and b ; Print generators of n ; 1 is always a generator ; A number x is generator of GCD is 1 ; Driver program to test above function
< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function printGenerators ( $ n ) { echo "1 ▁ " ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) if ( gcd ( $ i , $ n ) == 1 ) echo $ i , " ▁ " ; } $ n = 10 ; printGenerators ( $ n ) ; ? >
Check if a large number is divisible by 3 or not | Function to find that number divisible by 3 or not ; Compute sum of digits ; Check if sum of digits is divisible by 3. ; Driver code
< ? php function check ( $ str ) { $ n = strlen ( $ str ) ; $ digitSum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ digitSum += ( $ str [ $ i ] - '0' ) ; return ( $ digitSum % 3 == 0 ) ; } $ str = "1332" ; $ x = check ( $ str ) ? " Yes " : " No ▁ " ; echo ( $ x ) ; ? >
Count all perfect divisors of a number | Below is PHP code to count total perfect divisors ; Pre - compute counts of all perfect divisors of all numbers upto MAX . ; Iterate through all the multiples of i * i ; Increment all such multiples by 1 ; Returns count of perfect divisors of n . ; Driver code
< ? php $ MAX = 10001 ; $ perfectDiv = array_fill ( 0 , $ MAX , 0 ) ; function precomputeCounts ( ) { global $ MAX , $ perfectDiv ; for ( $ i = 1 ; $ i * $ i < $ MAX ; ++ $ i ) { for ( $ j = $ i * $ i ; $ j < $ MAX ; $ j += $ i * $ i ) ++ $ perfectDiv [ $ j ] ; } } function countPerfectDivisors ( $ n ) { global $ perfectDiv ; return $ perfectDiv [ $ n ] ; } precomputeCounts ( ) ; $ n = 16 ; echo " Total ▁ perfect ▁ divisors ▁ of ▁ " . $ n . " ▁ = ▁ " . countPerfectDivisors ( $ n ) . " STRNEWLINE " ; $ n = 12 ; echo " Total ▁ perfect ▁ divisors ▁ of ▁ " . $ n . " ▁ = ▁ " . countPerfectDivisors ( $ n ) ; ? >
Prime Factorization using Sieve O ( log n ) for multiple queries | PHP program to find prime factorization of a number n in O ( Log n ) time with precomputation allowed . ; stores smallest prime factor for every number ; Calculating SPF ( Smallest Prime Factor ) for every number till MAXN . Time Complexity : O ( nloglogn ) ; marking smallest prime factor for every number to be itself . ; separately marking spf for every even number as 2 ; checking if i is prime ; marking SPF for all numbers divisible by i ; marking spf [ j ] if it is not previously marked ; A O ( log n ) function returning primefactorization by dividing by smallest prime factor at every step ; precalculating Smallest Prime Factor ; calling getFactorization function
< ? php $ MAXN = 19999 ; $ spf = array_fill ( 0 , $ MAXN , 0 ) ; function sieve ( ) { global $ MAXN , $ spf ; $ spf [ 1 ] = 1 ; for ( $ i = 2 ; $ i < $ MAXN ; $ i ++ ) $ spf [ $ i ] = $ i ; for ( $ i = 4 ; $ i < $ MAXN ; $ i += 2 ) $ spf [ $ i ] = 2 ; for ( $ i = 3 ; $ i * $ i < $ MAXN ; $ i ++ ) { if ( $ spf [ $ i ] == $ i ) { for ( $ j = $ i * $ i ; $ j < $ MAXN ; $ j += $ i ) if ( $ spf [ $ j ] == $ j ) $ spf [ $ j ] = $ i ; } } } function getFactorization ( $ x ) { global $ spf ; $ ret = array ( ) ; while ( $ x != 1 ) { array_push ( $ ret , $ spf [ $ x ] ) ; if ( $ spf [ $ x ] ) $ x = ( int ) ( $ x / $ spf [ $ x ] ) ; } return $ ret ; } sieve ( ) ; $ x = 12246 ; echo " prime ▁ factorization ▁ for ▁ " . $ x . " ▁ : ▁ " ; $ p = getFactorization ( $ x ) ; for ( $ i = 0 ; $ i < count ( $ p ) ; $ i ++ ) echo $ p [ $ i ] . " ▁ " ; ? >
Largest subsequence having GCD greater than 1 | Returns length of the largest subsequence with GCD more than 1. ; Finding the Maximum value in arr [ ] ; Iterate from 2 to maximum possible divisor of all give values ; If we found divisor , increment count ; Driver code
< ? php function largestGCDSubsequence ( $ arr , $ n ) { $ ans = 0 ; $ maxele = max ( $ arr ) ; for ( $ i = 2 ; $ i <= $ maxele ; ++ $ i ) { $ count = 0 ; for ( $ j = 0 ; $ j < $ n ; ++ $ j ) { if ( $ arr [ $ j ] % $ i == 0 ) ++ $ count ; } $ ans = max ( $ ans , $ count ) ; } return $ ans ; } $ arr = array ( 3 , 6 , 2 , 5 , 4 ) ; $ size = count ( $ arr ) ; echo largestGCDSubsequence ( $ arr , $ size ) ; ? >
Queries on the sum of prime factor counts in a range | PHP program to find sum prime factors in given range . ; using sieve method to evaluating the prime factor of numbers ; if i is prime ; setting number of prime factor of a prime number . ; Returns sum of counts of prime factors in range from l to r . This function mainly uses count [ ] which is filled by Sieve ( ) ; finding the sum of number of prime factor of numbers in a range . ; Driver Code
< ? php $ MAX = 100006 ; function sieve ( & $ count ) { global $ MAX ; for ( $ i = 2 ; $ i * $ i <= $ MAX ; $ i ++ ) { if ( $ count [ $ i ] == 0 ) { for ( $ j = 2 * $ i ; $ j < $ MAX ; $ j += $ i ) $ count [ $ j ] ++ ; $ count [ $ i ] = 1 ; } } } function query ( $ count , $ l , $ r ) { $ sum = 0 ; for ( $ i = $ l ; $ i <= $ r ; $ i ++ ) $ sum += $ count [ $ i ] ; return $ sum ; } $ count = array_fill ( 0 , $ MAX , 0 ) ; sieve ( $ count ) ; echo query ( $ count , 6 , 10 ) . " " . query ( $ count , 1 , 5 ) ; ? >
Generation of n numbers with given set of factors | Generate n numbers with factors in factor [ ] ; array of k to store next multiples of given factors ; Prints n numbers $output = 0 ; Next number to print as output ; Find the next smallest multiple ; Printing minimum in each iteration print the value if output is not equal to current value ( to avoid the duplicates ) ; incrementing the current value by the respective factor ; Driver code
< ? php function generateNumbers ( $ factor , $ n , $ k ) { $ next = array_fill ( 0 , $ k , 0 ) ; for ( $ i = 0 ; $ i < $ n ; ) { $ toincrement = 0 ; for ( $ j = 0 ; $ j < $ k ; $ j ++ ) if ( $ next [ $ j ] < $ next [ $ toincrement ] ) $ toincrement = $ j ; if ( $ output != $ next [ $ toincrement ] ) { $ output = $ next [ $ toincrement ] ; echo $ next [ $ toincrement ] . " " ; $ i ++ ; } $ next [ $ toincrement ] += $ factor [ $ toincrement ] ; } } $ factor = array ( 3 , 5 , 7 ) ; $ n = 10 ; $ k = count ( $ factor ) ; generateNumbers ( $ factor , $ n , $ k ) ; ? >
Multiplicative order | function for GCD ; Function return smallest + ve integer that holds condition A ^ k ( mod N ) = 1 ; result store power of A that rised to the power N - 1 ; modular arithmetic ; return smallest + ve integer ; increment power ; Driver Code
< ? php function GCD ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return GCD ( $ b , $ a % $ b ) ; } function multiplicativeOrder ( $ A , $ N ) { if ( GCD ( $ A , $ N ) != 1 ) return -1 ; $ result = 1 ; $ K = 1 ; while ( $ K < $ N ) { $ result = ( $ result * $ A ) % $ N ; if ( $ result == 1 ) return $ K ; $ K ++ ; } return -1 ; } $ A = 4 ; $ N = 7 ; echo ( multiplicativeOrder ( $ A , $ N ) ) ; ? >
Sum of product of x and y such that floor ( n / x ) = y | Return the sum of natural number in a range . ; n * ( n + 1 ) / 2. ; Return the sum of product x * y . ; Iterating i from 1 to sqrt ( n ) ; Finding the upper limit . ; Finding the lower limit . ; Driven Code
< ? php function sumOfRange ( $ a , $ b ) { $ i = ( $ a * ( $ a + 1 ) ) >> 1 ; $ j = ( $ b * ( $ b + 1 ) ) >> 1 ; return ( $ i - $ j ) ; } function sumofproduct ( $ n ) { $ sum = 0 ; $ root = sqrt ( $ n ) ; for ( $ i = 1 ; $ i <= $ root ; $ i ++ ) { $ up = ( int ) ( $ n / $ i ) ; $ low = max ( ( int ) ( $ n / ( $ i + 1 ) ) , $ root ) ; $ sum += ( $ i * sumOfRange ( $ up , $ low ) ) ; $ sum += ( $ i * ( int ) ( $ n / $ i ) ) ; } return $ sum ; } $ n = 10 ; echo sumofproduct ( $ n ) . " STRNEWLINE " ; ? >
Minimum number of power terms with sum equal to n | Return minimum power terms of x required ; if x is 1 , return n since any power of 1 is 1 only . ; Consider n = a * x + b where a = n / x and b = n % x . ; Update count of powers for 1 's added ; Repeat the process for reduced n ; Driver Code
< ? php function minPower ( $ n , $ x ) { if ( $ x == 1 ) return $ n ; $ ans = 0 ; while ( $ n > 0 ) { $ ans += ( $ n % $ x ) ; $ n /= $ x ; } return $ ans ; } $ n = 5 ; $ x = 3 ; echo ( minPower ( $ n , $ x ) ) ; ? >
Sum of Perrin Numbers | function for sum of first n Perrin number . ; if ( $n == 0 ) n = 0 ; if ( $n == 1 ) n = 1 ; if ( $n == 2 ) n = 2 ; calculate k = 5 sum of three previous step . ; Sum remaining numbers ; calculate next term ; Driver code
< ? php function calSum ( $ n ) { $ a = 3 ; $ b = 0 ; $ c = 2 ; return 3 ; return 3 ; return 5 ; $ sum = 5 ; while ( $ n > 2 ) { $ d = $ a + $ b ; $ sum += $ d ; $ a = $ b ; $ b = $ c ; $ c = $ d ; $ n -- ; } return $ sum ; } $ n = 9 ; echo calSum ( $ n ) ; ? >
Print the kth common factor of two numbers | Returns k 'th common factor of x and y. ; Find smaller of two numbers ; Count common factors until we either reach small or count becomes k . ; If we reached small ; Driver code
< ? php function findKCF ( $ x , $ y , $ k ) { $ small = min ( $ x , $ y ) ; $ count = 1 ; for ( $ i = 2 ; $ i <= $ small ; $ i ++ ) { if ( $ x % $ i == 0 && $ y % $ i == 0 ) $ count ++ ; if ( $ count == $ k ) return $ i ; } return -1 ; } $ x = 4 ; $ y = 24 ; $ k = 3 ; echo findKCF ( $ x , $ y , $ k ) ; ? >
Find minimum number to be divided to make a number a perfect square | Return the minimum number to be divided to make n a perfect square . ; Since 2 is only even prime , compute its power separately . ; If count is odd , it must be removed by dividing n by prime number . ; If count is odd , it must be removed by dividing n by prime number . ; Driver Code
< ? php function findMinNumber ( $ n ) { $ count = 0 ; $ ans = 1 ; while ( $ n % 2 == 0 ) { $ count ++ ; $ n /= 2 ; } if ( $ count % 2 ) $ ans *= 2 ; for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i += 2 ) { $ count = 0 ; while ( $ n % $ i == 0 ) { $ count ++ ; $ n /= $ i ; } if ( $ count % 2 ) $ ans *= $ i ; } if ( $ n > 2 ) $ ans *= $ n ; return $ ans ; } $ n = 72 ; echo findMinNumber ( $ n ) , " STRNEWLINE " ; ? >
Program to implement Collatz Conjecture | Function to find if n reaches to 1 or not . ; Return true if n is positive ; Driver code
< ? php function isToOne ( $ n ) { if ( $ n > 0 ) return true ; return false ; } $ n = 5 ; isToOne ( $ n ) ? print ( " Yes " ) : print ( " No " ) ; ? >
GCD of two numbers formed by n repeating x and y times | Return the Greatest common Divisor of two numbers . ; Prints Greatest Common Divisor of number formed by n repeating x times and y times . ; Finding GCD of x and y . ; Print n , g times . ; Driver Code
< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function findgcd ( $ n , $ x , $ y ) { $ g = gcd ( $ x , $ y ) ; for ( $ i = 0 ; $ i < $ g ; $ i ++ ) echo ( $ n ) ; } $ n = 123 ; $ x = 5 ; $ y = 2 ; findgcd ( $ n , $ x , $ y ) ; ? >
Count natural numbers whose factorials are divisible by x but not y | GCD function to compute the greatest divisor among a and b ; Returns first number whose factorial is divisible by x . ; Result ; Remove common factors ; We found first i . ; Count of natural numbers whose factorials are divisible by x but not y . ; Return difference between first natural number whose factorial is divisible by y and first natural number whose factorial is divisible by x . ; Driver code
< ? php function gcd ( $ a , $ b ) { if ( ( $ a % $ b ) == 0 ) return $ b ; return gcd ( $ b , $ a % $ b ) ; } function firstFactorialDivisibleNumber ( $ x ) { $ i = 1 ; $ new_x = $ x ; for ( $ i = 1 ; $ i < $ x ; $ i ++ ) { $ new_x /= gcd ( $ i , $ new_x ) ; if ( $ new_x == 1 ) break ; } return $ i ; } function countFactorialXNotY ( $ x , $ y ) { return ( firstFactorialDivisibleNumber ( $ y ) - firstFactorialDivisibleNumber ( $ x ) ) ; } $ x = 15 ; $ y = 25 ; echo ( countFactorialXNotY ( $ x , $ y ) ) ; ? >
Find the first natural number whose factorial is divisible by x | Returns first number whose factorial divides x . ; Result ; Driver code
< ? php function firstFactorialDivisibleNumber ( $ x ) { $ i = 1 ; $ fact = 1 ; for ( $ i = 1 ; $ i < $ x ; $ i ++ ) { $ fact = $ fact * $ i ; if ( $ fact % $ x == 0 ) break ; } return $ i ; } $ x = 16 ; echo ( firstFactorialDivisibleNumber ( $ x ) ) ; ? >
Find two prime numbers with given sum | Generate all prime numbers less than n . ; Initialize all entries of boolean array as true . A value in isPrime [ i ] will finally be false if i is Not a prime , else true bool isPrime [ n + 1 ] ; ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Prints a prime pair with given sum ; Generating primes using Sieve ; Traversing all numbers to find first pair ; Driver Code
< ? php function SieveOfEratosthenes ( $ n , & $ isPrime ) { $ isPrime [ 0 ] = $ isPrime [ 1 ] = false ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ isPrime [ $ i ] = true ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ isPrime [ $ p ] == true ) { for ( $ i = $ p * $ p ; $ i <= $ n ; $ i += $ p ) $ isPrime [ $ i ] = false ; } } } function findPrimePair ( $ n ) { $ isPrime = array_fill ( 0 , $ n + 1 , NULL ) ; SieveOfEratosthenes ( $ n , $ isPrime ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ isPrime [ $ i ] && $ isPrime [ $ n - $ i ] ) { echo $ i . " ▁ " . ( $ n - $ i ) ; return ; } } } $ n = 74 ; findPrimePair ( $ n ) ; ? >
Count numbers with same first and last digits | method to get first digit of x ; method to return count of numbers with same starting and ending digit from 1 upto x ; get ten - spans from 1 to x ; add 9 to consider all 1 digit numbers ; Find first and last digits ; If last digit is greater than first digit then decrease count by 1 ; Method to return count of numbers with same starting and ending digit between start and end ; Driver Code
< ? php function getFirstDigit ( $ x ) { while ( $ x >= 10 ) $ x /= 10 ; return $ x ; } function getCountWithSameStartAndEndFrom1 ( $ x ) { if ( $ x < 10 ) return $ x ; $ tens = $ x / 10 ; $ res = $ tens + 9 ; $ firstDigit = getFirstDigit ( $ x ) ; $ lastDigit = $ x % 10 ; if ( $ lastDigit < $ firstDigit ) $ res -- ; return $ res ; } function getCountWithSameStartAndEnd ( $ start , $ end ) { return getCountWithSameStartAndEndFrom1 ( $ end ) - getCountWithSameStartAndEndFrom1 ( $ start - 1 ) ; } $ start = 5 ; $ end = 40 ; echo getCountWithSameStartAndEnd ( $ start , $ end ) ; ? >
Right | Generate all prime numbers less than n . ; Initialize all entries of boolean array as true . A value in isPrime [ i ] will finally be false if i is Not a prime , else true bool isPrime [ n + 1 ] ; ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Returns true if n is right - truncatable , else false ; Generating primes using Sieve ; Checking whether the number remains prime when the last ( " right " ) digit is successively removed ; Driver Code
< ? php function sieveOfEratosthenes ( $ n , & $ isPrime ) { $ isPrime [ 0 ] = $ isPrime [ 1 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ isPrime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ isPrime [ $ i ] = false ; } } } function rightTruPrime ( $ n ) { $ isPrime = array_fill ( 0 , $ n + 1 , true ) ; sieveOfEratosthenes ( $ n , $ isPrime ) ; while ( $ n ) { if ( $ isPrime [ $ n ] ) $ n = ( int ) ( $ n / 10 ) ; else return false ; } return true ; } $ n = 59399 ; if ( rightTruPrime ( $ n ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; ? >
Mersenne Prime | Generate all prime numbers less than n . ; Initialize all entries of boolean array 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 ; Function to generate mersenne primes less than or equal to n ; Generating primes using Sieve ; Generate all numbers of the form 2 ^ k - 1 and smaller than or equal to n . ; Checking whether number is prime and is one less then the power of 2 ; Driver Code
< ? php function SieveOf ( $ n ) { $ prime = array ( $ n + 1 ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ prime [ $ i ] = true ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = false ; } } return $ prime ; } function mersennePrimes ( $ n ) { $ prime = SieveOf ( $ n ) ; for ( $ k = 2 ; ( ( 1 << $ k ) - 1 ) <= $ n ; $ k ++ ) { $ num = ( 1 << $ k ) - 1 ; if ( $ prime [ $ num ] ) echo $ num . " " ; } } $ n = 31 ; echo " Mersenne ▁ prime ▁ numbers ▁ smaller ▁ " . " than ▁ or ▁ equal ▁ to ▁ $ n ▁ " . mersennePrimes ( $ n ) ; ? >
Find sum of modulo K of first N natural number | Return sum of modulo K of first N natural numbers . ; Iterate from 1 to N && evaluating and adding i % K . ; Driver Code
< ? php function findSum ( $ N , $ K ) { $ ans = 0 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) $ ans += ( $ i % $ K ) ; return $ ans ; } $ N = 10 ; $ K = 2 ; echo findSum ( $ N , $ K ) , " STRNEWLINE " ; ? >
Find sum of modulo K of first N natural number | Return sum of modulo K of first N natural numbers . ; Counting the number of times 1 , 2 , . . , K - 1 , 0 sequence occurs . ; Finding the number of elements left which are incomplete of sequence Leads to Case 1 type . ; adding multiplication of number of times 1 , 2 , . . , K - 1 , 0 sequence occurs and sum of first k natural number and sequence from case 1. ; Driver program
< ? php function findSum ( $ N , $ K ) { $ ans = 0 ; $ y = $ N / $ K ; $ x = $ N % $ K ; $ ans = ( $ K * ( $ K - 1 ) / 2 ) * $ y + ( $ x * ( $ x + 1 ) ) / 2 ; return $ ans ; } $ N = 10 ; $ K = 2 ; echo findSum ( $ N , $ K ) ; ? >
Smallest number to multiply to convert floating point to natural | Finding GCD of two number ; Returns smallest integer k such that k * str becomes natural . str is an input floating point number ; Find size of string representing a floating point number . ; Below is used to find denominator in fraction form . ; Used to find value of count_after_dot ; To find numerator in fraction form of given number . For example , for 30.25 , numerator would be 3025. ; If there was no dot , then number is already a natural . ; Find denominator in fraction form . For example , for 30.25 , denominator is 100 ; Result is denominator divided by GCD - of - numerator - and - denominator . For example , for 30.25 , result is 100 / GCD ( 3025 , 100 ) = 100 / 25 = 4 ; Driver Code
< ? php function gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return gcd ( $ b , $ a % $ b ) ; } function findnum ( $ str ) { $ n = strlen ( $ str ) ; $ count_after_dot = 0 ; $ dot_seen = false ; $ num = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] != ' . ' ) { $ num = $ num * 10 + ( $ str [ $ i ] - '0' ) ; if ( $ dot_seen == true ) $ count_after_dot ++ ; } else $ dot_seen = true ; } if ( $ dot_seen == false ) return 1 ; $ dem = pow ( 10 , $ count_after_dot ) ; return ( $ dem / gcd ( $ num , $ dem ) ) ; } { $ str = "5.125" ; echo findnum ( $ str ) ; return 0 ; } ? >
Find the maximum number of handshakes | Calculating the maximum number of handshake using derived formula . ; Driver Code
< ? php function maxHandshake ( $ n ) { return ( $ n * ( $ n - 1 ) ) / 2 ; } $ n = 10 ; echo maxHandshake ( $ n ) ; ? >