Van Eck sequence: Difference between revisions
m
→{{header|Uiua}}: Reformat
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
m (→{{header|Uiua}}: Reformat) |
||
(22 intermediate revisions by 11 users not shown) | |||
Line 365:
</pre>
=={{header|ABC}}==
<syntaxhighlight lang="ABC">HOW TO RETURN vaneck.sequence length:
PUT {[1]: 0} IN seq
WHILE #seq < length:
PUT #seq-1 IN i
WHILE i>0 AND seq[i]<>seq[#seq]: PUT i-1 IN i
SELECT:
i=0: PUT 0 IN seq[#seq+1]
ELSE: PUT #seq-i IN seq[#seq+1]
RETURN seq
PUT vaneck.sequence 1000 IN eck
FOR i IN {1..10}: WRITE eck[i]>>4
WRITE /
FOR i IN {991..1000}: WRITE eck[i]>>4
WRITE /</syntaxhighlight>
{{out}}
<pre> 0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136</pre>
=={{header|Action!}}==
<syntaxhighlight lang="action!">INT FUNC LastPos(INT ARRAY a INT count,value)
Line 557 ⟶ 576:
0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136
</pre>
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="c">
#include <basico.h>
algoritmo
decimales '0'
dimensionar(2) matriz de ceros (vaneck)
iterar para (i=2, #(i<=1000), ++i )
matriz.buscar en reversa(#(vaneck[i]),#(vaneck[1:i-1]))
---retener--- no es cero?, entonces{ menos'i' por'-1' }
meter en 'vaneck'
siguiente
imprimir ( "Van Eck: first 10 terms : ",#(vaneck[1:10]), NL,\
"Van Eck: terms 991 - 1000: ", #(vaneck[991:1000]), NL)
terminar
</syntaxhighlight>
{{out}}
<pre>
Van Eck: first 10 terms : 0,0,1,0,2,0,2,2,1,6
Van Eck: terms 991 - 1000: 4,7,30,25,67,225,488,0,10,136
</pre>
Line 958 ⟶ 1,003:
=={{header|BASIC}}==
{{works with|QBasic}}
<syntaxhighlight lang="basic">10 DEFINT A-Z
20 DIM E(1000)
Line 972 ⟶ 1,018:
<pre> 0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136</pre>
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">limite = 1001
dim a(limite) fill 0
for n = 0 to limite-1
for m = n-1 to 0 step -1
if a[m] = a[n] then
a[n+1] = n-m
exit for
end if
next m
next n
print "Secuencia de Van Eck:" & Chr(10)
print "Primeros 10 terminos: ";
for i = 0 to 9
print a[i]; " ";
next i
print chr(10) & "Terminos 991 al 1000: ";
for i = 990 to 999
print a[i]; " ";
next i</syntaxhighlight>
==={{header|Chipmunk Basic}}===
{{trans|FreeBASIC}}
{{works with|Chipmunk Basic|3.6.4}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">100 cls
110 limite = 1000
120 dim a(limite)
130 '
140 for n = 0 to limite-1
150 for m = n-1 to 0 step -1
160 if a(m) = a(n) then
170 a(n+1) = n-m
180 exit for
190 endif
200 next m
210 next n
220 '
230 print "Secuencia de Van Eck:";chr$(10)
240 print "Primeros 10 terminos: ";
250 for i = 0 to 9
260 print a(i);
270 next i
280 print chr$(10);"Terminos 991 al 1000: ";
290 for i = 990 to 999
300 print a(i);
310 next i
320 end</syntaxhighlight>
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">define limit = 1000
dim list[limit]
print "calculating van eck sequence..."
for n = 0 to limit - 1
for m = n - 1 to 0 step -1
if list[m] = list[n] then
let c = n + 1
let list[c] = n - m
break m
endif
wait
next m
next n
print "first 10 terms: "
for i = 0 to 9
print list[i]
next i
print "terms 991 to 1000: "
for i = 990 to 999
print list[i]
next i</syntaxhighlight>
{{out| Output}}<pre>calculating van eck sequence...
first 10 terms: 0 0 1 0 2 0 2 2 1 6
terms 991 to 1000: 4 7 30 25 67 225 488 0 10 136 </pre>
==={{header|FreeBASIC}}===
<syntaxhighlight lang="vb">Const limite = 1000
Dim As Integer a(limite), n, m, i
For n = 0 To limite-1
For m = n-1 To 0 Step -1
If a(m) = a(n) Then a(n+1) = n-m: Exit For
Next m
Next n
Print "Secuencia de Van Eck:" &Chr(10)
Print "Primeros 10 terminos: ";
For i = 0 To 9
Print a(i) &" ";
Next i
Print Chr(10) & "Terminos 991 al 1000: ";
For i = 990 To 999
Print a(i) &" ";
Next i
End</syntaxhighlight>
{{out}}
<pre>Secuencia de Van Eck:
Primeros 10 terminos: 0 0 1 0 2 0 2 2 1 6
Terminos 991 al 1000: 4 7 30 25 67 225 488 0 10 136</pre>
==={{header|GW-BASIC}}===
{{trans|FreeBASIC}}
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">100 CLS
110 L = 1000
120 DIM A(L)
130 FOR N = 0 TO L
140 LET A(N) = 0
150 NEXT N
160 FOR N = 0 TO L-1
170 FOR M = N-1 TO 0 STEP -1
180 IF A(M) = A(N) THEN A(N+1) = N - M : GOTO 200
190 NEXT M
200 NEXT N
210 PRINT "Secuencia de Van Eck:"; CHR$(10)
220 PRINT "Primeros 10 terminos: ";
230 FOR I = 0 TO 9
240 PRINT A(I);
250 NEXT I
260 PRINT CHR$(10); "Terminos 991 al 1000: ";
270 FOR I = 990 TO 999
280 PRINT A(I);
290 NEXT I
300 END</syntaxhighlight>
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="purebasic">If OpenConsole()
Define.i n, m, i, limite
limite = 1001
Dim a.i(limite)
For n = 0 To limite-1
For m = n-1 To 0 Step -1
If a(m) = a(n)
a(n+1) = n-m
Break
EndIf
Next m
Next n
PrintN("Secuencia de Van Eck:" + #CRLF$)
Print("Primeros 10 terminos: ")
For i = 0 To 9
Print(Str(a(i)) + " ")
Next i
Print(#CRLF$ + "Terminos 991 al 1000: ")
For i = 990 To 999
Print(Str(a(i)) + " ")
Next i
PrintN(#CRLF$ + "Press ENTER to exit" + #CRLF$ ): Input()
CloseConsole()
EndIf</syntaxhighlight>
==={{header|QBasic}}===
{{trans|FreeBASIC}}
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">CONST limite = 1000
DIM a(limite)
FOR n = 0 TO limite - 1
FOR m = n - 1 TO 0 STEP -1
IF a(m) = a(n) THEN
a(n + 1) = n - m
EXIT FOR
END IF
NEXT m
NEXT n
PRINT "Secuencia de Van Eck:"; CHR$(10)
PRINT "Primeros 10 terminos: ";
FOR i = 0 TO 9
PRINT a(i);
NEXT i
PRINT CHR$(10); "Terminos 991 al 1000: ";
FOR i = 990 TO 999
PRINT a(i);
NEXT i
END</syntaxhighlight>
==={{header|XBasic}}===
{{trans|FreeBASIC}}
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "Van Eck sequence"
VERSION "0.0000"
DECLARE FUNCTION Entry ()
FUNCTION Entry ()
l = 1000
DIM a[l]
FOR n = 0 TO l-1
FOR m = n-1 TO 0 STEP -1
IF a[m] = a[n] THEN
a[n+1] = n-m
EXIT FOR
END IF
NEXT m
NEXT n
PRINT "Secuencia de Van Eck:"; CHR$(10)
PRINT "Primeros 10 terminos: ";
FOR i = 0 TO 9
PRINT a[i]; " ";
NEXT i
PRINT CHR$(10); "Terminos 991 al 1000: ";
FOR i = 990 TO 999
PRINT a[i]; " ";
NEXT i
END FUNCTION
END PROGRAM</syntaxhighlight>
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">limite = 1001
dim a(limite)
for n = 0 to limite-1
for m = n-1 to 0 step -1
if a(m) = a(n) then
a(n+1) = n-m
break
fi
next m
next n
print "Secuencia de Van Eck: \n"
print "Primeros 10 terminos: ";
for i = 0 to 9
print a(i), " ";
next i
print "\nTerminos 991 al 1000: ";
for i = 990 to 999
print a(i), " ";
next i
print</syntaxhighlight>
=={{header|BCPL}}==
Line 1,424 ⟶ 1,735:
<pre>The first ten terms of the Van Eck sequence are: [0, 0, 1, 0, 2, 0, 2, 2, 1, 6]
Terms 991 to 1000 of the sequence are: [7, 30, 25, 67, 225, 488, 0, 10, 136]</pre>
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
len arr[] 1000
for n to 1000 - 1
for m = n - 1 downto 1
if arr[m] = arr[n]
arr[n + 1] = n - m
break 1
.
.
.
for i to 10
write arr[i] & " "
.
print ""
for i = 991 to 1000
write arr[i] & " "
.
</syntaxhighlight>
=={{header|F_Sharp|F#}}==
Line 1,528 ⟶ 1,859:
4 7 30 25 67 225 488 0 10 136</pre>
=={{header|
{{FormulaeEntry|page=https://formulae.org/?script=examples/Van_Eck_sequence}}
'''Solution'''
[[File:Fōrmulæ - Van Eck sequence 01.png]]
'''Test case'''
[[File:Fōrmulæ - Van Eck sequence 02.png]]
[[File:Fōrmulæ - Van Eck sequence 03.png]]
=={{header|Go}}==
Line 2,101 ⟶ 2,406:
4 7 30 25 67 225 488 0 10 136</pre>
=={{header|MACRO-11}}==
<syntaxhighlight lang="macro11"> .TITLE VANECK
.MCALL .TTYOUT,.EXIT
; CALCULATE VAN ECK SEQUENCE
VANECK::MOV #ECKBUF,R5
MOV R5,R0
CLR (R0)
1$: MOV R0,R1
BR 3$
2$: CMP (R0),(R1)
BEQ 4$
3$: SUB #2,R1
CMP R1,R5
BGE 2$
CLR R3
BR 5$
4$: MOV R0,R3
SUB R1,R3
ASR R3
5$: ADD #2,R0
MOV R3,(R0)
CMP R0,#BUFEND
BLE 1$
; PRINT VALUES
MOV #ECKBUF,R3
JSR PC,PR10
MOV #ECKBUF+<2*^D990>,R3
JSR PC,PR10
.EXIT
; PRINT 10 VALUES STARTING AT R3
PR10: MOV #^D10,R4
1$: MOV (R3)+,R0
JSR PC,PR0
SOB R4,1$
MOV #15,R0
.TTYOUT
MOV #12,R0
.TTYOUT
RTS PC
; PRINT NUMBER IN R0 AS DECIMAL
PR0: MOV #4$,R1
1$: MOV #-1,R2
2$: INC R2
SUB #12,R0
BCC 2$
ADD #72,R0
MOVB R0,-(R1)
MOV R2,R0
BNE 1$
3$: MOVB (R1)+,R0
.TTYOUT
BNE 3$
RTS PC
.ASCII /...../
4$: .ASCIZ / /
.EVEN
ECKBUF: .BLKW ^D1000
BUFEND = .
.END VANECK</syntaxhighlight>
{{out}}
<pre>0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136</pre>
=={{header|MAD}}==
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
Line 2,129 ⟶ 2,501:
E( 8)= 1 E(998)= 10
E( 9)= 6 E(999)=136</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Line 2,137 ⟶ 2,508:
<pre>{0,0,1,0,2,0,2,2,1,6}
{4,7,30,25,67,225,488,0,10,136}</pre>
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [ Stdout (show list ++ "\n")
| list <- [take 10 eck, take 10 (drop 990 eck)]
]
eck :: [num]
eck = 0 : map item [1..]
where item n = find last (tl sofar)
where sofar = reverse (take n eck)
last = hd sofar
find :: *->[*]->num
find i = find' 1
where find' n [] = 0
find' n (a:as) = n, if a = i
= find' (n+1) as, otherwise</syntaxhighlight>
{{out}}
<pre>[0,0,1,0,2,0,2,2,1,6]
[4,7,30,25,67,225,488,0,10,136]
</pre>
=={{header|Modula-2}}==
Line 2,172 ⟶ 2,565:
<pre> 0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136</pre>
=={{header|Nim}}==
<syntaxhighlight lang="nim">const max = 1000
Line 2,931 ⟶ 3,325:
First 10 terms: 8 0 0 1 0 2 0 2 2 1
Terms 991 through 1000: 16 183 0 6 21 10 249 0 5 48</pre>
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
, <Eck 1000>: e.Eck
, <First 10 e.Eck>: (e.First10) e.Rest1
, <Last 10 e.Eck>: (e.Rest2) e.Last10
= <Prout e.First10>
<Prout e.Last10>;
};
Eck {
s.N = <Reverse <Repeat s.N EckStep>>;
};
Reverse {
= ;
e.X s.I = s.I <Reverse e.X>;
};
Repeat {
0 s.F e.X = e.X;
s.N s.F e.X = <Repeat <- s.N 1> s.F <Mu s.F e.X>>;
};
EckStep {
= 0;
s.N e.X, e.X: e.F s.N e.R,
<Lenw e.F>: s.M e.F = <+ s.M 1> s.N e.X;
s.N e.X = 0 s.N e.X;
};</syntaxhighlight>
{{out}}
<pre>0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136</pre>
=={{header|REXX}}==
Line 2,977 ⟶ 3,404:
say 'terms ' LO " through " HI ' of the Van Eck sequence are: ' out</syntaxhighlight>
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} <br><br>
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪
DUP DUP SIZE GET → list item
≪ list SIZE 1 - DUP 1 CF
'''WHILE''' DUP 1 FC? AND '''REPEAT '''
'''IF''' list OVER GET item == '''THEN''' 1 SF '''END'''
1 - '''END '''
- 1 FS? *
≫ ≫ ''''LastOcc'''' STO
≪ { 0 } 2 ROT '''START''' DUP '''LastOcc''' + '''NEXT'''
≫ ''''VANECK'''' STO
≪
OVER SIZE { } ROT ROT '''FOR''' j
OVER j GET + '''NEXT'''
SWAP DROP
≫ ''''LASTL'''' STO
|
''( { sequence } -- pos_from_end )''
Store sequence and last item
Initialize loop
Scan sequence backwards
if item found then break
decrement
Return pos or 0 if not found
''( n -- { 0..VanEck(n) } )''
''( { n items } m -- { m..n } )''
Extract items from mth position
|}
{{in}}
<pre>
10 VANECK
1000 VANECK 991 LASTL
</pre>
{{out}}
<pre>
2: { 0 0 1 0 2 0 2 2 1 6 }
1: { 4 7 30 25 67 225 488 0 10 136 }
</pre>
=={{header|Ruby}}==
Line 3,092 ⟶ 3,573:
Terms 991 to 1000 are List(4, 7, 30, 25, 67, 225, 488, 0, 10, 136).
</pre>
=={{header|SETL}}==
<syntaxhighlight lang="setl">program vaneck;
eck := [0];
loop for i in [1..999] do
prev := eck(..#eck-1);
eck(i+1) := i - (max/[j : e=prev(j) | e=eck(i)] ? i);
end loop;
print(eck(1..10));
print(eck(991..1000));
end program;</syntaxhighlight>
{{out}}
<pre>[0 0 1 0 2 0 2 2 1 6]
[4 7 30 25 67 225 488 0 10 136]</pre>
=={{header|SNOBOL4}}==
Line 3,133 ⟶ 3,630:
say van_eck(10)
say van_eck(1000).
{{out}}
<pre>
Line 3,208 ⟶ 3,705:
vanEck(0..9) = 0 0 1 0 2 0 2 2 1 6
vanEck(990..999) = 4 7 30 25 67 225 488 0 10 136
=={{header|Uiua}}==
<syntaxhighlight lang="Uiua">
F ← ⊂⟨0◌|+1⟩>⊙(-1⧻),,⊗⊃⊢(↘1). # Generator function (prepends).
⇌⍥F999 [0] # Generate 1000 terms and flip.
⊃(&p↙¯)(&p↙∘)10 # Print first and last 10 terms.
</syntaxhighlight>
{{out}}
<pre>
[0 0 1 0 2 0 2 2 1 6]
[4 7 30 25 67 225 488 0 10 136]
</pre>
=={{header|Visual Basic .NET}}==
Line 3,228 ⟶ 3,737:
Same as C#.
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">fn main() {
max := 1000
mut a := []int{len: max} // all zero by default
Line 3,255 ⟶ 3,764:
[4 7 30 25 67 225 488 0 10 136]
</pre>
=={{header|VTL-2}}==
<syntaxhighlight lang="vtl2">1000 :1)=0
1010 I=1
1020 J=I-1
1030 #=J=0*1090
1040 #=:J)=:I)*1070
1050 J=J-1
1060 #=1030
1070 :I+1)=I-J
1080 #=1100
1090 :I+1)=0
1100 I=I+1
1110 #=I<1000*1020
1120 I=1
1130 #=1170
1140 I=991
1150 #=1170
1160 #=9999
1170 R=!
1180 N=10
1190 ?=:I)
1200 $=32
1210 I=I+1
1220 N=N-1
1230 #=N=0=0*1190
1240 ?=""
1250 #=R</syntaxhighlight>
{{out}}
<pre>0 0 1 0 2 0 2 2 1 6
4 7 30 25 67 225 488 0 10 136</pre>
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="
var a = List.filled(max, 0)
var seen = {}
|