61 lines
1.2 KiB
Plaintext
61 lines
1.2 KiB
Plaintext
local fn IsPrime( n as NSUInteger ) as BOOL
|
|
BOOL isPrime = YES
|
|
NSUInteger i
|
|
|
|
if n < 2 then exit fn = NO
|
|
if n = 2 then exit fn = YES
|
|
if n mod 2 == 0 then exit fn = NO
|
|
for i = 3 to int(n^.5) step 2
|
|
if n mod i == 0 then exit fn = NO
|
|
next
|
|
end fn = isPrime
|
|
|
|
|
|
local fn ReverseNumber( n as NSUInteger ) as NSUInteger
|
|
NSInteger sum = 0
|
|
|
|
if n < 10 then exit fn = n
|
|
while ( n > 0 )
|
|
sum = 10 * sum + ( n mod 10 )
|
|
n /= 10
|
|
wend
|
|
end fn = sum
|
|
|
|
|
|
local fn IsEmirp( n as NSUInteger ) as BOOL
|
|
BOOL result = NO
|
|
NSUInteger r = fn ReverseNumber(n)
|
|
if r != n and fn IsPrime(n) and fn IsPrime(r) then result = YES
|
|
end fn = result
|
|
|
|
|
|
local fn GetEmirpPrimes
|
|
NSUInteger count = 0, i = 13
|
|
|
|
printf @"\nThe first 20 Emirp primes are:"
|
|
do
|
|
if fn IsEmirp(i) then printf @"%4lu\b", i : count++
|
|
i += 2
|
|
until ( count == 20 )
|
|
|
|
printf @"\n\nThe Emirp primes between 7700 and 8000 are:"
|
|
i = 7701
|
|
while ( i < 8000 )
|
|
if fn IsEmirp(i) then printf @"%5lu\b", i
|
|
i += 2
|
|
wend
|
|
|
|
i = 13 : count = 0
|
|
while (1)
|
|
if fn IsEmirp(i) then count++
|
|
if count = 10000 then exit while
|
|
i += 2
|
|
wend
|
|
printf @"\n\nThe 10,000th Emirp prime is: %lu", i
|
|
|
|
end fn
|
|
|
|
fn GetEmirpPrimes
|
|
|
|
HandleEvents
|