RosettaCodeData/Task/Consecutive-primes-with-asc.../FutureBasic/consecutive-primes-with-asc...

56 lines
1.8 KiB
Plaintext

include "NSLog.incl"
_max = 1000000
_size = _max/16
uint8 oddIntBits(_size)
xref list(20) as int : list = fn calloc(20, 4)
xref up(20) as int : up = fn calloc(20, 4)
xref dn(20) as int : dn = fn calloc(20, 4)
clear local fn ascDecDifs
int dir
uint32 num, np, count=1, umax=3, dmax=3, dif1, dif2, n(2)
///////Build Sieve of Eratosthenes////////////////////////////////////
for num = 3 to _max step 2
if oddIntBits(num >> 4) & bit((num & 15) >> 1) then continue
np = num * num
while np < _max
oddIntBits(np >> 4) |= bit((np & 15) >> 1)
np += num << 1
wend
/////Build lists of ascending and descending prime gaps//////////////
dif2 = num - n(2)
if sgn(dif2 - dif1) == dir
if count then count++ : list(count) = num : exit if //add to list
//Start list
blockmove(@n(0), @list(0), 12) //Copy primes to list
list(3) = num : count = 3
else
if count > umax then if dir == 1 then umax = count : swap list, up
if count > dmax then if dir == -1 then dmax = count : swap list, dn
count = 0
end if
dir = sgn(dif2 - dif1)
dif1 = dif2
blockmove(@n(1), @n(0), 8) //Shift 1 lower in array
n(2) = num
next
///////Show results///////////////////////////////////////////////////
CFStringRef s = fn stringwithformat(@"\n Ascending: %d", up(0))
for num = 1 to umax
s = fn stringwithformat(@"%@, %d", s, up(num))
next
s = fn stringwithformat(@"%@\n\n Descending: %d", s, dn(0))
for num = 1 to dmax
s = fn stringwithformat(@"%@, %d", s, dn(num))
next
nsLogSetTitle(@"Consecutive primes with ascending or descending differences")
nslog( s )
end fn
CFTimeInterval t : t = fn CACurrentMediaTime
fn ascDecDifs
nslog(@"\n %.3f ms.",1000*(fn CACurrentMediaTime - t))
handleevents