68 lines
1.6 KiB
Plaintext
68 lines
1.6 KiB
Plaintext
begin globals
|
|
_last1 = 7
|
|
CFTimeInterval t
|
|
byte n(_last1 +1) //array of digits
|
|
uint64 list(20), p10 = 1
|
|
short count, digits, first1 = _last1
|
|
n(_last1) = 2 //First number to check
|
|
end globals
|
|
|
|
local fn convertToNumber as uint64
|
|
short i = first1
|
|
uint64 nr = n(i)
|
|
while i < _last1
|
|
i++ : nr = nr * 10 + n(i)
|
|
wend
|
|
end fn = nr
|
|
|
|
void local fn increment( dgt as short )
|
|
select n(dgt)
|
|
case 1, 7, 5 : n(dgt) += 2
|
|
case 3 : if digits then n(dgt) = 7 else n(dgt) = 5
|
|
case 9 : n(dgt) = 1
|
|
if dgt == first1 then first1-- : digits++ : p10 *= 10
|
|
fn increment( dgt -1 )
|
|
case else : n(dgt)++
|
|
end select
|
|
end fn
|
|
|
|
local fn isPrime( v as uint64 ) as bool
|
|
if v < 4 then exit fn = yes
|
|
if v mod 3 == 0 then exit fn = no
|
|
uint64 f = 5
|
|
while f*f <= v
|
|
if v mod f == 0 then exit fn = no
|
|
f += 2
|
|
if v mod f == 0 then exit fn = no
|
|
f += 4
|
|
wend
|
|
end fn = yes
|
|
|
|
void local fn circularPrimes
|
|
uint64 num, nr
|
|
short r
|
|
while ( count < 19 )
|
|
num = fn convertToNumber
|
|
if fn isPrime( num )
|
|
nr = num
|
|
for r = 1 to digits
|
|
nr = 10 * ( nr mod p10 ) + ( nr / p10 ) //Rotate
|
|
if nr < num then break //Rotation of lower number
|
|
if fn isPrime( nr ) == no then break //Not prime
|
|
next
|
|
if r > digits then list( count ) = num : count++
|
|
end if
|
|
fn increment( _last1 )
|
|
wend
|
|
end fn
|
|
|
|
window 1, @"Circular Primes in FutureBasic", (0, 0, 280, 320)
|
|
t = fn CACurrentMediaTime
|
|
fn circularPrimes
|
|
t = fn CACurrentMediaTime - t
|
|
for count = 0 to 18
|
|
printf @"%22u",list(count)
|
|
next
|
|
printf @"\n Compute time: %.3f ms", t * 1000
|
|
handleevents
|