Equidigital Numbers | PHP Program to find Equidigital Numbers till n ; Array to store all prime less than and equal to MAX . ; Utility function for sieve of sundaram ; In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x . Since we want primes smaller than MAX , we reduce MAX to half This array is used to separate numbers of the form i + j + 2 ij from others where 1 <= i <= j ; Main logic of Sundaram . Mark all numbers which do not generate prime number by doing 2 * i + 1 ; Since 2 is a prime number ; Print other primes . Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; Returns true if n is a Equidigital number , else false . ; Count digits in original number ; Count all digits in prime factors of n pDigit is going to hold this value . ; Count powers of p in n ; If primes [ i ] is a prime factor , ; Count the power of prime factors ; Add its digits to pDigit . ; Add digits of power of prime factors to pDigit . ; If n != 1 then one prime factor still to be summed up ; ; If digits in prime factors and digits in original number are same , then return true . Else return false . ; Finding all prime numbers before limit . These numbers are used to find prime factors .
< ? php $ MAX = 10000 ; $ primes = array ( ) ; function sieveSundaram ( ) { global $ primes , $ MAX ; $ marked = array_fill ( 0 , ( $ MAX / 2 + 1 ) , false ) ; for ( $ i = 1 ; $ i <= ( ( int ) sqrt ( $ MAX ) - 1 ) / 2 ; $ i ++ ) for ( $ j = ( $ i * ( $ i + 1 ) ) << 1 ; $ j <= ( int ) ( $ MAX / 2 ) ; $ j = $ j + 2 * $ i + 1 ) $ marked [ $ j ] = true ; array_push ( $ primes , 2 ) ; for ( $ i = 1 ; $ i <= ( int ) ( $ MAX / 2 ) ; $ i ++ ) if ( $ marked [ $ i ] == false ) array_push ( $ primes , 2 * $ i + 1 ) ; } function isEquidigital ( $ n ) { global $ primes , $ MAX ; if ( $ n == 1 ) return true ; $ original_no = $ n ; $ sumDigits = 0 ; while ( $ original_no > 0 ) { $ sumDigits ++ ; $ original_no = ( int ) ( $ original_no / 10 ) ; } $ pDigit = 0 ; $ count_exp = 0 ; $ p = 0 ; for ( $ i = 0 ; $ primes [ $ i ] <= ( int ) ( $ n / 2 ) ; $ i ++ ) { while ( $ n % $ primes [ $ i ] == 0 ) { $ p = $ primes [ $ i ] ; $ n = ( int ) ( $ n / $ p ) ; $ count_exp ++ ; } while ( $ p > 0 ) { $ pDigit ++ ; $ p = ( int ) ( $ p / 10 ) ; } while ( $ count_exp > 1 ) { $ pDigit ++ ; $ count_exp = ( int ) ( $ count_exp / 10 ) ; } } if ( $ n != 1 ) { while ( $ n > 0 ) { $ pDigit ++ ; $ n = ( int ) ( $ n / 10 ) ; } } return ( $ pDigit == $ sumDigits ) ; } sieveSundaram ( ) ; echo " Printing ▁ first ▁ few ▁ Equidigital ▁ Numbers ▁ using ▁ isEquidigital ( ) STRNEWLINE " ; for ( $ i = 1 ; $ i < 20 ; $ i ++ ) if ( isEquidigital ( $ i ) ) echo $ i . " " ; ? >