Recaman's sequence: Difference between revisions

m
typo
m (typo)
 
(7 intermediate revisions by 6 users not shown)
Line 2:
The '''[[wp:Recamán's sequence|Recamán's sequence]]''' generates Natural numbers.
 
Starting from a(0)=0, the n'th term <code>a(n)</code>, where n>0, is the previous term minus <code>n</code> i.e <code>a(n) = a(n-1) - n</code> but only if this is '''both''' positive ''and'' has not been previouselypreviously generated.<br>
 
If the conditions ''don't'' hold then <code>a(n) = a(n-1) + n</code>.
Line 502:
150 A(N)=X: RETURN
160 A(N)=A(N-1)+N: RETURN</syntaxhighlight>
 
{{out}}
 
<pre>First 15 terms:
0 1 3 6 2 7 13 20 12 21 11 22 10 23 9
First repeated term:
A( 24 ) = 42</pre>
 
==={{header|Applesoft BASIC}}===
{{trans|BASIC}}
{{works with|Chipmunk Basic}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">10 DIM A(100)
20 PRINT "First 15 terms:"
30 FOR N=0 TO 14: GOSUB 100: PRINT A(N); " ";: NEXT
35 PRINT
40 PRINT "First repeated term:"
50 GOSUB 100
55 FOR M=0 TO N-1
56 IF A(M)=A(N) THEN 70
57 NEXT
60 N=N+1: GOTO 50
70 PRINT "A(";N;") = ";A(N)
80 END
100 IF N=0 THEN A(0)=0: RETURN
110 X = A(N-1)-N: IF X<0 THEN 160
120 FOR M=0 TO N-1
130 IF A(M)=X THEN 160
140 NEXT
150 A(N)=X: RETURN
160 A(N)=A(N-1)+N: RETURN</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
The [[#BASIC|BASIC]] solution works without any changes.
 
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
The [[#BASIC|BASIC]] solution works without any changes.
 
==={{header|Minimal BASIC}}===
{{trans|BASIC}}
{{works with|Quite BASIC}}
<syntaxhighlight lang="qbasic">10 DIM A(100)
20 PRINT "FIRST 15 TERMS:"
30 FOR N=0 TO 14
40 GOSUB 170
50 PRINT A(N);" ";
60 NEXT N
70 PRINT
80 PRINT "FIRST REPEATED TERM:"
90 GOSUB 170
100 FOR M=0 TO N-1
110 IF A(M)=A(N) THEN 150
120 NEXT M
130 LET N=N+1
140 GOTO 90
150 PRINT "A(";N;") = ";A(N)
160 STOP
170 IF N=0 THEN 280
180 LET X = A(N-1)-N
190 IF X<0 THEN 250
200 FOR M=0 TO N-1
210 IF A(M)=X THEN 250
220 NEXT M
230 LET A(N)=X
240 RETURN
250 LET A(N)=A(N-1)+N
260 RETURN
270 STOP
280 LET A(0)=0
290 RETURN
300 END</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#BASIC|BASIC]] solution works without any changes.
 
==={{header|Quite BASIC}}===
The [[#Minimal BASIC|Minimal BASIC]] solution works without any changes.
 
=={{header|BCPL}}==
Line 991 ⟶ 1,063:
<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|EasyLang}}==
<syntaxhighlight lang="easylang">
arrbase a[] 0
arrbase seen[] 0
len seen[] 100
#
a[] &= 0
seen[0] = 1
i = 1
repeat
h = a[i - 1] - i
if h <= 0 or seen[h] = 1
h = a[i - 1] + i
.
until seen[h] = 1
seen[h] = 1
a[] &= h
if i = 14
print a[]
.
i += 1
.
print h
</syntaxhighlight>
 
=={{header|Forth}}==
Line 1,034 ⟶ 1,131:
<pre>0 1 3 6 2 7 13 20 12 21 11 22 10 23 9
first repeated : a[20 ]=a[24 ]=42 ok</pre>
 
 
=={{header|FreeBASIC}}==
Line 1,143 ⟶ 1,239:
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Recam%C3%A1n%27s_sequence}}
 
'''Solution'''
 
The following snippet generates the Recaman's sequence of a given number of terms:
 
[[File:Fōrmulæ - Recamán's sequence 01.png]]
 
'''Case 1'''
 
* Generate and show here the first 15 members of the sequence.
* Find and show here, the first duplicated number in the sequence.
* Optionally. Find and show here, How many terms of the sequence are needed until all the integers 0..1000, inclusive, are generated.
 
[[File:Fōrmulæ - Recamán's sequence 02.png]]
 
[[File:Fōrmulæ - Recamán's sequence 03.png]]
 
[[File:Fōrmulæ - Recamán's sequence 04.png]]
 
'''Case 2. Plotting the sequence'''
 
[[File:Fōrmulæ - Recamán's sequence 05.png]]
 
[[File:Fōrmulæ - Recamán's sequence 06.png]]
 
'''Case 3. Drawing the sequence as it was shown in the Numberphile video'''
 
[[File:Fōrmulæ - Recamán's sequence 07.png]]
 
[[File:Fōrmulæ - Recamán's sequence 08.png]]
 
[[File:Fōrmulæ - Recamán's sequence 09.png]]
 
=={{header|Go}}==
Line 2,978 ⟶ 3,106:
The first duplicated term is a[24] = 42
Terms up to a[328002] are needed to generate 0 to 1000</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
use std::collections::HashSet ;
 
fn main() {
let mut recamans : Vec<i32> = Vec::new( ) ;
let mut reca_set : HashSet<i32> = HashSet::new() ;
let mut first_nums : HashSet<i32> = HashSet::new( ) ;
for i in 0i32..=1000 {
first_nums.insert( i ) ;
}
recamans.push( 0 ) ;
reca_set.insert( 0 ) ;
let mut current : i32 = 0 ;
while ! first_nums.is_subset( &reca_set ) {
current += 1 ;
let mut nextnum : i32 = recamans[( current as usize ) - 1] - current ;
if nextnum < 0 || reca_set.contains( &nextnum ) {
nextnum = recamans[(current as usize ) - 1 ] + current ;
}
recamans.push( nextnum ) ;
reca_set.insert( nextnum ) ;
if current == 15 {
println!("The first 15 numbers of the Recaman sequence are:" ) ;
println!("{:?}" , recamans ) ;
}
}
println!("To generate all numbers from 0 to 1000 , one has to go to element {}" , current) ;
}</syntaxhighlight>
{{Out}}
<pre>
The first 15 numbers of the Recaman sequence are:
[0, 1, 3, 6, 2, 7, 13, 20, 12, 21, 11, 22, 10, 23, 9, 24]
To generate all numbers from 0 to 1000 , one has to go to element 328002
</pre>
 
=={{header|Scala}}==
Line 3,105 ⟶ 3,269:
Terms of Recaman's sequence to generate all integers 0..1000, inclusive: 328003</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program recaman;
a := {[0,0]};
 
loop for i in [1..14] do
extend(a);
end loop;
 
print("First 15:", [a(n) : n in [0..14]]);
 
loop
doing n := extend(a);
until #(rept:=[[r,i] : r = a(i) | r=n]) > 1
do pass;
end loop;
 
print("First repetition:", n, "at", {x:x in rept}{n});
 
proc extend(rw a);
n := max/ domain a;
t := a(n) - n-1;
if t<0 or t in range a then
t := a(n) + n+1;
end if;
return a(n+1) := t;
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>First 15: [0 1 3 6 2 7 13 20 12 21 11 22 10 23 9]
First repetition: 42 at {20 24}</pre>
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func recamans_generator() {
Line 3,284 ⟶ 3,478:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">var a = [0]
var used = { 0: true }
var used1000 = { 0: true }
62

edits