40 lines
963 B
Text
40 lines
963 B
Text
BBOOL local fn IsPrime( p as UInt64 )
|
|
if ( p < 2 ) then return NO
|
|
if ( p % 2 == 0 ) then return ( p == 2 )
|
|
UInt64 limit = fn sqrt( (double)p )
|
|
for UInt64 i = 3 to limit step 2
|
|
if ( p % i == 0 ) then return NO
|
|
next
|
|
end fn = YES
|
|
|
|
local fn CubanPrimes( howMany as NSUInteger )
|
|
CFMutableArrayRef cubanPrimes = fn MutableArrayNew
|
|
UInt64 n = 1
|
|
|
|
while ( fn ArrayCount( cubanPrimes ) < howMany )
|
|
UInt64 p = 3 * n * n + 3 * n + 1
|
|
if ( fn IsPrime( p ) )
|
|
MutableArrayAddObject( cubanPrimes, @(p) )
|
|
end if
|
|
n++
|
|
wend
|
|
|
|
printf @"\nFirst 200 Cuban primes:\n"
|
|
for NSUInteger i = 0 to fn ArrayCount( cubanPrimes ) - 1
|
|
printf @"%7lu\b", fn NumberUnsignedLongValue( cubanPrimes[i] )
|
|
if ( i < fn ArrayCount( cubanPrimes ) - 1 )
|
|
if ( i % 8 == 7 )
|
|
print
|
|
else
|
|
print @"\t"
|
|
end if
|
|
end if
|
|
next
|
|
print
|
|
end fn
|
|
|
|
window 1, @"cuban Primes", ( 0, 0, 630, 440 )
|
|
|
|
fn CubanPrimes( 200 )
|
|
|
|
HandleEvents
|