Piprimes: Difference between revisions

Content added Content deleted
Line 479: Line 479:
<syntaxhighlight futurebasic"j">
<syntaxhighlight futurebasic"j">
local fn IsPrime( n as NSUInteger ) as BOOL
local fn IsPrime( n as NSUInteger ) as BOOL
BOOL isPrime = YES
BOOL isPrime = YES
NSUInteger i
NSUInteger i

if n < 2 then exit fn = NO
if n < 2 then exit fn = NO
if n = 2 then exit fn = YES
if n = 2 then exit fn = YES
if n mod 2 == 0 then exit fn = NO
if n mod 2 == 0 then exit fn = NO
for i = 3 to int(n^.5) step 2
for i = 3 to int(n^.5) step 2
if n mod i == 0 then exit fn = NO
if n mod i == 0 then exit fn = NO
next
next
end fn = isPrime
end fn = isPrime



local fn Piprimes( limit as NSUInteger )
local fn Piprimes( limit as NSUInteger )
NSUInteger n = 0, p = 1
NSUInteger n = 0, p = 1

printf @"Piprimes from 1 through %lu:\n", limit
printf @"Piprimes from 1 through %lu:\n", limit
while ( n < limit )
while ( n < limit )
printf @"%2lu \b", n
printf @"%2lu \b", n
if p mod 10 == 0 then print
if p mod 10 == 0 then print
p++
p++
if ( fn IsPrime(p) ) then n++
if ( fn IsPrime(p) ) then n++
wend
wend
end fn
end fn