Recaman's sequence: Difference between revisions

Add Comal
m (→‎{{header|BASIC}}: syntax highlighting)
(Add Comal)
Line 769:
First duplicated number: A(24) = 42
Terms needed to generate [0..1000]: 328002</pre>
 
=={{header|Comal}}==
<lang comal>0010 DIM a#(0:100)
0020 //
0030 // Print the first 15 items
0040 PRINT "First 15 items: ",
0050 FOR i#:=0 TO 14 DO PRINT reca#(i#);
0060 PRINT
0070 //
0080 // Find and print the first repeated item
0090 i#:=15
0100 WHILE NOT find#(i#,reca#(i#)) DO i#:+1
0110 PRINT "First repeated item: A(",i#,") = ",a#(i#)
0120 //
0130 // Generate the n'th member of the Recaman sequence
0140 FUNC reca#(n#)
0150 IF n#=0 THEN RETURN 0
0160 a#(n#):=a#(n#-1)-n#
0180 IF a#(n#)<=0 OR find#(n#,a#(n#)) THEN a#(n#):=a#(n#-1)+n#
0190 RETURN a#(n#)
0200 ENDFUNC reca#
0210 //
0220 // See if a number occurs before the n'th member of the Recaman sequence
0230 FUNC find#(n#,num#)
0240 FOR x#:=0 TO n#-1 DO IF a#(x#)=num# THEN RETURN x#
0250 RETURN 0
0260 ENDFUNC find#
0270 END</lang>
{{out}}
<pre>First 15 items: 0 1 3 6 2 7 13 20 12 21 11 22 10 23 9
First repeated item: A(24) = 42</pre>
 
=={{header|COBOL}}==
2,114

edits