Duffinian numbers: Difference between revisions

Applesoft BASIC implementation and moved FreeBASIC under the BASIC heading
m (→‎{{header|RPL}}: formatting)
(Applesoft BASIC implementation and moved FreeBASIC under the BASIC heading)
Line 250:
12481 12482 12483
13447 13448 13449"</syntaxhighlight>
 
=={{header|Arturo}}==
 
Line 301 ⟶ 300:
[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]</pre>
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic"> 100 DEF FN MOD(NUM) = NUM - INT (NUM / DIV) * DIV: REM NUM MOD DIV
110 M = 50:N = 4
120 PRINT "FIRST "M" DUFFINIAN NUMBERS:"
130 FOR C = 0 TO M STEP 0
140 GOSUB 600"DUFF
150 IF DUFF THEN PRINT RIGHT$ (" " + STR$ (N),4);:C = C + 1
160 N = N + 1
170 NEXT C
180 M = 15:S = 4:M$ = CHR$ (13)
190 PRINT M$M$"FIRST "M" DUFFINIAN TRIPLETS:"
200 FOR C = 0 TO M STEP 0
210 FOR D = 2 TO 0 STEP - 1:N = S + D: GOSUB 600: IF DUFF THEN NEXT D
220 IF D < 0 THEN C = C + 1: PRINT RIGHT$ (" " + STR$ (S) + "-",5) LEFT$ ( STR$ (S + 2) + " ",5);:D = 0
230 S = S + D + 1
240 NEXT C
250 END
 
REM ISPRIME V RETURNS ISPRIME
260 ISPRIME = FALSE: IF V < 2 THEN RETURN
270 DIV = 2:ISPRIME = FN MOD(V): IF NOT ISPRIME THEN ISPRIME = V = 2: RETURN
280 LIMIT = SQR (V): IF LIMIT > = 3 THEN FOR DIV = 3 TO LIMIT STEP 2:ISPRIME = FN MOD(V): IF ISPRIME THEN NEXT DIV
290 RETURN
 
REM GREATEST COMMON DIVISOR (GCD) A B RETURNS GCD
300 A = ABS ( INT (A))
310 B = ABS ( INT (B))
320 GCD = A * NOT NOT B
330 FOR B = B + A * NOT B TO 0 STEP 0
340 A = GCD
350 GCD = B
360 B = A - INT (A / GCD) * GCD
370 NEXT B
380 RETURN
 
REM SUMDIV NUM RETURNS SUM
400 DIV = 2
410 SUM = 0
420 QUOT = NUM / DIV
430 IF DIV > QUOT THEN SUM = 1: RETURN
440 FOR LOOP = 0 TO 1 STEP 0
450 IF FN MOD(NUM) = 0 THEN SUM = SUM + DIV: IF DIV < > QUOT THEN SUM = SUM + QUOT
460 DIV = DIV + 1
470 QUOT = NUM / DIV
480 LOOP = DIV > QUOT
500 NEXT LOOP
510 SUM = SUM + 1
520 RETURN
 
REM DUFF N RETURNS DUFF
600 DUFF = FALSE
610 V = N: GOSUB 260"ISPRIME
620 IF ISPRIME THEN RETURN
630 NUM = N: GOSUB 400"SUMDIV
640 A = SUM:B = N: GOSUB 300"GCD
650 DUFF = GCD = 1
660 RETURN</syntaxhighlight>
{{out}}
<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 15 DUFFINIAN TRIPLETS:
63-65 323-325 511-513 721-723 899-901 1443-1445 2303-2305 2449-2451 3599-3601 3871-3873 5183-5185 5617-5619 6049-6051 6399-6401 8449-8451
</pre>
==={{header|FreeBASIC}}===
{{trans|XPL0}}
<syntaxhighlight lang="vb">#include "isprime.bas"
 
Function GCD(p As Integer, q As Integer) As Integer
Return Iif(q = 0, p, GCD(q, p Mod q))
End Function
 
Function SumDiv(Num As Uinteger) As Uinteger
Dim As Uinteger Div = 2, Sum = 0, Quot
Do
Quot = Num / Div
If Div > Quot Then Exit Do
If Num Mod Div = 0 Then
Sum += Div
If Div <> Quot Then Sum += Quot
End If
Div += 1
Loop
Return Sum+1
End Function
 
Function Duff(N As Uinteger) As Boolean
Return Iif(isPrime(N), False, GCD(SumDiv(N), N) = 1)
End Function
 
Dim As Integer C = 0, N = 4
Print "First 50 Duffinian numbers:"
Do
If Duff(N) Then
Print Using "####"; N;
C += 1
If C Mod 20 = 0 Then Print
End If
N += 1
Loop Until C >= 50
 
C = 0 : N = 4
Print !"\n\nFirst 50 Duffinian triplets:"
Do
If Duff(N) And Duff(N+1) And Duff(N+2) Then
Print Using !" [###### ###### ######]\t"; N; N+1; N+2;
C += 1
If C Mod 4 = 0 Then Print
End If
N += 1
Loop Until C >= 50
 
Sleep</syntaxhighlight>
 
{{out}}
<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 50 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]</pre>
 
=={{header|C++}}==
Line 545 ⟶ 680:
8449 8450 8451
</pre>
 
=={{header|FreeBASIC}}==
{{trans|XPL0}}
<syntaxhighlight lang="vb">#include "isprime.bas"
 
Function GCD(p As Integer, q As Integer) As Integer
Return Iif(q = 0, p, GCD(q, p Mod q))
End Function
 
Function SumDiv(Num As Uinteger) As Uinteger
Dim As Uinteger Div = 2, Sum = 0, Quot
Do
Quot = Num / Div
If Div > Quot Then Exit Do
If Num Mod Div = 0 Then
Sum += Div
If Div <> Quot Then Sum += Quot
End If
Div += 1
Loop
Return Sum+1
End Function
 
Function Duff(N As Uinteger) As Boolean
Return Iif(isPrime(N), False, GCD(SumDiv(N), N) = 1)
End Function
 
Dim As Integer C = 0, N = 4
Print "First 50 Duffinian numbers:"
Do
If Duff(N) Then
Print Using "####"; N;
C += 1
If C Mod 20 = 0 Then Print
End If
N += 1
Loop Until C >= 50
 
C = 0 : N = 4
Print !"\n\nFirst 50 Duffinian triplets:"
Do
If Duff(N) And Duff(N+1) And Duff(N+2) Then
Print Using !" [###### ###### ######]\t"; N; N+1; N+2;
C += 1
If C Mod 4 = 0 Then Print
End If
N += 1
Loop Until C >= 50
 
Sleep</syntaxhighlight>
 
{{out}}
<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 50 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]</pre>
 
=={{header|Go}}==
413

edits