Duffinian numbers: Difference between revisions

Initial FutureBasic task solution added
(Applesoft BASIC implementation and moved FreeBASIC under the BASIC heading)
(Initial FutureBasic task solution added)
Line 680:
8449 8450 8451
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn IsPrime( n as NSUInteger ) as BOOL
BOOL isPrime = YES
NSUInteger i
if n < 2 then exit fn = NO
if n = 2 then exit fn = YES
if n mod 2 == 0 then exit fn = NO
for i = 3 to int(n^.5) step 2
if n mod i == 0 then exit fn = NO
next
end fn = isPrime
 
 
local fn GCD( a as long, b as long ) as long
long result
if ( a == 0 ) then exit fn = b
result = fn GCD( b mod a, a )
end fn = result
 
 
local fn SumDiv( num as NSUInteger ) as NSUInteger
NSUInteger div = 2, sum = 0, quot, result
while (1)
quot = num / div
if ( div > quot ) then result = 0 : exit while
if ( num mod div == 0 )
sum += div
if ( div != quot ) then sum += quot
end if
div++
wend
result = sum + 1
end fn = result
 
 
local fn IsDuffinian( n as NSUInteger) as BOOL
BOOL result = NO
if ( fn IsPrime(n) == NO and fn GCD( fn SumDiv(n), n ) == 1 )
result = YES
else
result = NO
end if
end fn = result
 
 
local fn FindDuffinians
long c = 0, n = 4
print "First 50 Duffinian numbers:"
do
if ( fn IsDuffinian(n) )
printf @"%4d \b", n
c++
if ( c mod 10 == 0 ) then print
end if
n++
until ( c >= 50 )
c = 0 : n = 4
printf @"\n\nFirst 56 Duffinian triplets:"
do
if ( fn IsDuffinian(n) and fn IsDuffinian(n + 1) and fn IsDuffinian(n + 2) )
printf @" [%6ld %6ld %6ld] \b", n, n+1, n+2
c++
if ( c mod 4 == 0 ) then print
end if
n++
until ( c >= 56 )
end fn
 
CFTimeInterval t
 
t = fn CACurrentMediaTime
fn FindDuffinians
printf @"\nCompute time: %.3f ms",(fn CACurrentMediaTime-t)*1000
 
HandleEvents
</syntaxhighlight>
 
{{output}}
<pre>
First 50 Duffinian numbers:
4 8 9 16 21 25 27 32 35 36
39 49 50 55 57 63 64 65 75 77
81 85 93 98 100 111 115 119 121 125
128 129 133 143 144 155 161 169 171 175
183 185 187 189 201 203 205 209 215 217
 
First 56 Duffinian triplets:
[ 63 64 65] [ 323 324 325] [ 511 512 513] [ 721 722 723]
[ 899 900 901] [ 1443 1444 1445] [ 2303 2304 2305] [ 2449 2450 2451]
[ 3599 3600 3601] [ 3871 3872 3873] [ 5183 5184 5185] [ 5617 5618 5619]
[ 6049 6050 6051] [ 6399 6400 6401] [ 8449 8450 8451] [ 10081 10082 10083]
[ 10403 10404 10405] [ 11663 11664 11665] [ 12481 12482 12483] [ 13447 13448 13449]
[ 13777 13778 13779] [ 15841 15842 15843] [ 17423 17424 17425] [ 19043 19044 19045]
[ 26911 26912 26913] [ 30275 30276 30277] [ 36863 36864 36865] [ 42631 42632 42633]
[ 46655 46656 46657] [ 47523 47524 47525] [ 53137 53138 53139] [ 58563 58564 58565]
[ 72961 72962 72963] [ 76175 76176 76177] [ 79523 79524 79525] [ 84099 84100 84101]
[ 86527 86528 86529] [ 94177 94178 94179] [108899 108900 108901] [121103 121104 121105]
[125315 125316 125317] [128017 128018 128019] [129599 129600 129601] [137287 137288 137289]
[144399 144400 144401] [144721 144722 144723] [154567 154568 154569] [158403 158404 158405]
[166463 166464 166465] [167041 167042 167043] [175231 175232 175233] [177607 177608 177609]
[181475 181476 181477] [186623 186624 186625] [188497 188498 188499] [197191 197192 197193]
 
Compute time: 2963.753 ms
</pre>
 
 
=={{header|Go}}==
715

edits