42 lines
944 B
Plaintext
42 lines
944 B
Plaintext
include "NSLog.incl"
|
|
|
|
local fn Factorial( n as NSUInteger ) as NSUInteger
|
|
NSUInteger factorial = 1
|
|
|
|
if n > 1 then factorial = n * fn Factorial( n -1 )
|
|
end fn = factorial
|
|
|
|
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 FactorialPrimes( n as long )
|
|
NSUInteger found = 0, i = 1
|
|
|
|
NSLog( @"First %lu factorial primes:", n )
|
|
while ( found < n )
|
|
NSUInteger fct = fn Factorial( i )
|
|
if ( fn IsPrime( fct - 1 ) )
|
|
found++
|
|
NSLog( @"%2lu: %3lu! - 1 = %-lu", found, i, fct - 1 )
|
|
end if
|
|
if ( fn IsPrime( fct + 1 ) )
|
|
found++
|
|
NSLog( @"%2lu: %3lu! + 1 = %-lu", found, i, fct + 1 )
|
|
end if
|
|
i++
|
|
wend
|
|
end fn
|
|
|
|
fn FactorialPrimes( 10 )
|
|
|
|
HandleEvents
|