48 lines
1.0 KiB
Plaintext
48 lines
1.0 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
|
|
|
|
void local fn DesecendingPrimes( limit as long )
|
|
long i, n, mask, num, count = 0
|
|
|
|
for i = 0 to limit -1
|
|
n = 0 : mask = i : num = 9
|
|
while ( mask )
|
|
if mask & 1 then n = n * 10 + num
|
|
mask = mask >> 1
|
|
num--
|
|
wend
|
|
mda(i) = n
|
|
next
|
|
|
|
mda_sort @"compare:"
|
|
|
|
for i = 1 to mda_count (0) - 1
|
|
n = mda_integer(i)
|
|
if ( fn IsPrime( n ) )
|
|
printf @"%10ld\b", n
|
|
count++
|
|
if count mod 10 == 0 then print
|
|
end if
|
|
next
|
|
printf @"\n\n\tThere are %ld descending primes.", count
|
|
end fn
|
|
|
|
window 1, @"Desecending Primes", ( 0, 0, 780, 230 )
|
|
print
|
|
|
|
CFTimeInterval t
|
|
t = fn CACurrentMediaTime
|
|
fn DesecendingPrimes( 512 )
|
|
printf @"\n\tCompute time: %.3f ms\n",(fn CACurrentMediaTime-t)*1000
|
|
|
|
HandleEvents
|