RosettaCodeData/Task/Brilliant-numbers/FutureBasic/brilliant-numbers-1.basic
2024-10-16 18:07:41 -07:00

70 lines
1.9 KiB
Text

#build Optimization 3
#build Architecture "Apple Silicon"
// #build Architecture "Intel"
// #build Architecture "Universal"
local fn isPrime( v as Uint64 ) as bool
if v < 2 then exit fn = NO
select 0
if ( ( v - 2 ) == 0 || ( v - 3 ) == 0 ) then exit fn = YES
if ( ( v % 2 ) == 0 || ( v % 3 ) == 0 ) then exit fn = NO
end select
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
local fn FirstPrimeFactor( n as UInt64 ) as UInt64
UInt64 i
if n mod 2 == 0 then exit fn = 2
if n mod 3 == 0 then exit fn = 3
i = 5
while i*i <= n
if n mod i == 0 then exit fn = i
i += 2
if n mod i == 0 then exit fn = i
i += 4
wend
end fn = n
local fn DoBrilliants( expoLimit as UInt64 ) as CFMutableStringRef
UInt64 count = 0, n = 0, ff, sf, expo = 0
CFMutableStringRef mutStr = fn MutableStringWithString( @"\nFirst 100 brilliant numbers:\n" )
do
ff = fn FirstPrimeFactor( n )
sf = n/ff
if fn IsPrime( sf ) && len( str(ff) ) == len( str(sf) )
MutableStringAppendFormat( mutStr, @"%7u\b", n )
count++
if count mod 10 == 0 then MutableStringAppendFormat( mutStr, @"\n" )
end if
n++
until count == 100
MutableStringAppendFormat( mutStr, @"\n" ) : count = 0 : n = 0
while ( expo != expoLimit )
ff = fn FirstPrimeFactor( n )
sf = n/ff
if fn IsPrime( sf ) && len( str(ff) ) = len( str(sf) )
count++
if ( n >= 10^expo )
MutableStringAppendFormat( mutStr, @"%10u (%6d * %-6d) => 10^%d is brilliant at Position No. %d\n", n, ff, sf, expo, count )
expo++
end if
end if
n++
wend
end fn = mutStr
CFTimeInterval t : t = fn CACurrentMediaTime
CFStringRef brilliants : brilliants = fn DoBrilliants( 7 )
printf @"%@\n\t\tTime = %.f milliseconds", brilliants, (fn CACurrentMediaTime - t) * 1000
HandleEvents