127 lines
3.7 KiB
Plaintext
127 lines
3.7 KiB
Plaintext
begin globals
|
|
CFTimeInterval t
|
|
long n(16), clops(52), primes(52), blinds(52), pals(52)
|
|
long num, iClop, iPrime, iBlind, iPal
|
|
short first1, last1, midPt
|
|
xref show(50) as long
|
|
midPt = 8
|
|
end globals
|
|
|
|
local fn convertToNumber as long
|
|
long p10 = 1, nr = 0
|
|
short c2 = last1
|
|
do
|
|
nr += n(c2) * p10
|
|
if c2 == midPt then p10 *= 100 else p10 *= 10 //Add 0 if at midPoint
|
|
c2--
|
|
until c2 < first1
|
|
end fn = nr
|
|
|
|
void local fn increment( dgt as short )
|
|
if n(dgt) < 9 then n(dgt)++ : exit fn
|
|
n(dgt) = 1
|
|
if dgt > first1 then fn increment( dgt - 1 ) : exit fn //Carry
|
|
first1-- : last1 ++
|
|
for dgt = first1 to last1 //New width: set all digits to 1
|
|
n(dgt) = 1
|
|
next
|
|
end fn
|
|
|
|
local fn isPrime( v as long ) as bool
|
|
//Skip check for 2 and 3 because we're starting at 101
|
|
if v mod 2 == 0 then exit fn = no
|
|
if v mod 3 == 0 then exit fn = no
|
|
long 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
|
|
|
|
local fn isBlind as bool
|
|
short temp = 0
|
|
swap temp, midPt //Keep fn convertToNumber from adding 0 at midPt
|
|
bool rslt = fn isPrime( fn convertToNumber )
|
|
swap temp, midPt
|
|
end fn = rslt
|
|
|
|
local fn isPalindrome as bool
|
|
short a = first1, b = last1
|
|
while b > a
|
|
if n(a) <> n(b) then exit fn = no
|
|
a++ : b--
|
|
wend
|
|
end fn = yes
|
|
|
|
void local fn print50( title as cfstringref, addr as ptr )
|
|
short r = 0
|
|
show = addr //Set array address to param
|
|
print : print title
|
|
while r < 50
|
|
printf @"%9ld\b",show(r)
|
|
r++ : if r mod 10 == 0 then print
|
|
wend
|
|
end fn
|
|
|
|
void local fn display
|
|
window 1, @"Cyclopean numbers", (0, 0, 680, 515 )
|
|
fn print50( @" First 50 Cyclopean numbers:", @clops( 0))
|
|
fn print50( @" First 50 Cyclopean primes:", @primes(0))
|
|
fn print50( @" First 50 blind Cyclopean primes:", @blinds(0))
|
|
fn print50( @" First 50 palindromic Cyclopean primes:",@pals( 0))
|
|
print
|
|
printf @" First Cyclopean number above 10,000,000 is %ld at index %ld", clops(50), clops(51)
|
|
printf @" First Cyclopean prime above 10,000,000 is %ld at index %ld", primes(50),primes(51)
|
|
printf @" First blind Cyclopean prime above 10,000,000 is %ld at index %ld", blinds(50),blinds(51)
|
|
printf @" First palindromic Cyclopean prime above 10,000,000 is %ld at index %ld", pals(50), pals(51)
|
|
print
|
|
printf @" Compute time: %.3f sec", t
|
|
end fn
|
|
|
|
void local fn cyclops
|
|
clops( 0 ) = 0 : iClop = 1 : iPrime = 0 : iBlind = 0 : iPal = 0
|
|
first1 = midPt-1 : last1 = midPt : n(first1) = 1 : n(last1) = 1
|
|
|
|
// Record first 50 numbers in each category
|
|
while ( iPal ) < 50
|
|
num = fn convertToNumber
|
|
if iClop < 50 then clops(iClop) = num
|
|
iClop++
|
|
if fn isPrime( num )
|
|
if iPrime < 50 then primes(iPrime) = num : iPrime++
|
|
if iBlind < 50 then if fn isBlind then blinds(iBlind) = num : iBlind++
|
|
if iPal < 50 then if fn isPalindrome then pals(iPal) = num : iPal++
|
|
end if
|
|
num++
|
|
fn increment( last1 )
|
|
wend
|
|
|
|
// Keep counting Cyclops numbers until 10,000,000
|
|
while fn convertToNumber < 10000000
|
|
fn increment( last1 ) : iClop++
|
|
wend
|
|
|
|
// Find next number in each category
|
|
clops(50) = fn convertToNumber : clops(51) = iClop
|
|
iPrime = 1 : iBlind = 1 : iPal = 1
|
|
while (iPrime or iBlind or iPal)
|
|
num = fn convertToNumber
|
|
iClop++
|
|
if fn isPrime( num )
|
|
if iPrime then primes(50) = num : primes(51) = iClop : iPrime--
|
|
if iBlind then if fn isBlind then blinds(50) = num : blinds(51) = iClop : iBlind--
|
|
if iPal then if fn isPalindrome then pals(50) = num : pals(51) = iClop : ipal--
|
|
end if
|
|
fn increment( last1 )
|
|
wend
|
|
end fn
|
|
|
|
t = fn CACurrentMediaTime
|
|
fn cyclops
|
|
t = fn CACurrentMediaTime - t
|
|
fn display
|
|
|
|
handleevents
|