Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
59
Task/Recamans-sequence/COBOL/recamans-sequence.cob
Normal file
59
Task/Recamans-sequence/COBOL/recamans-sequence.cob
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. RECAMAN.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 RECAMAN-SEQUENCE COMP.
|
||||
02 A PIC 999 OCCURS 99 TIMES INDEXED BY I.
|
||||
02 N PIC 999 VALUE 0.
|
||||
|
||||
01 VARIABLES COMP.
|
||||
02 ADDC PIC S999.
|
||||
02 SUBC PIC S999.
|
||||
02 SPTR PIC 99 VALUE 1.
|
||||
|
||||
01 OUTPUT-FORMAT.
|
||||
02 OUTI PIC Z9.
|
||||
02 OUTN PIC BZ9.
|
||||
02 OUTS PIC X(79).
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
BEGIN.
|
||||
PERFORM GENERATE-NEXT-ITEM 15 TIMES.
|
||||
PERFORM COLLATE-ITEM VARYING I FROM 1 BY 1
|
||||
UNTIL I IS GREATER THAN 15.
|
||||
DISPLAY 'First 15 items:' OUTS.
|
||||
|
||||
FIND-REPEATING.
|
||||
PERFORM GENERATE-NEXT-ITEM.
|
||||
SET I TO 1.
|
||||
SEARCH A VARYING I
|
||||
WHEN I IS NOT LESS THAN N
|
||||
NEXT SENTENCE
|
||||
WHEN A(I) IS EQUAL TO A(N)
|
||||
SUBTRACT 1 FROM N GIVING OUTI
|
||||
MOVE A(N) TO OUTN
|
||||
DISPLAY 'First repeated item: A(' OUTI ') =' OUTN
|
||||
STOP RUN.
|
||||
GO TO FIND-REPEATING.
|
||||
|
||||
GENERATE-NEXT-ITEM.
|
||||
IF N IS EQUAL TO ZERO
|
||||
MOVE ZERO TO A(1)
|
||||
ELSE
|
||||
ADD N, A(N) GIVING ADDC
|
||||
SUBTRACT N FROM A(N) GIVING SUBC
|
||||
IF SUBC IS NOT GREATER THAN ZERO
|
||||
MOVE ADDC TO A(N + 1)
|
||||
ELSE
|
||||
SET I TO 1
|
||||
SEARCH A VARYING I
|
||||
WHEN I IS NOT LESS THAN N
|
||||
MOVE SUBC TO A(N + 1)
|
||||
WHEN A(I) IS EQUAL TO SUBC
|
||||
MOVE ADDC TO A(N + 1).
|
||||
ADD 1 TO N.
|
||||
|
||||
COLLATE-ITEM.
|
||||
MOVE A(I) TO OUTN.
|
||||
STRING OUTN DELIMITED BY SIZE INTO OUTS WITH POINTER SPTR.
|
||||
|
|
@ -1,17 +1,15 @@
|
|||
arrbase a[] 0
|
||||
arrbase seen[] 0
|
||||
len seen[] 100
|
||||
#
|
||||
a[] &= 0
|
||||
seen[0] = 1
|
||||
seen[1] = 1
|
||||
i = 1
|
||||
repeat
|
||||
h = a[i - 1] - i
|
||||
if h <= 0 or seen[h] = 1
|
||||
h = a[i - 1] + i
|
||||
h = a[i - 1 + 1] - i
|
||||
if h <= 0 or seen[h + 1] = 1
|
||||
h = a[i - 1 + 1] + i
|
||||
.
|
||||
until seen[h] = 1
|
||||
seen[h] = 1
|
||||
until seen[h + 1] = 1
|
||||
seen[h + 1] = 1
|
||||
a[] &= h
|
||||
if i = 14
|
||||
print a[]
|
||||
|
|
|
|||
|
|
@ -27,17 +27,17 @@ def recaman_required($capture):
|
|||
| if .n < $capture then .a += [.current] else . end
|
||||
| if ($alreadyUsed|not)
|
||||
then .used[$s] = true
|
||||
| if (.current >= 0 and .current <= $required)
|
||||
then .found[$s] = true | .nfound+=1
|
||||
else . end
|
||||
| if (.current >= 0 and .current <= $required)
|
||||
then .found[$s] = true | .nfound+=1
|
||||
else . end
|
||||
else .
|
||||
end
|
||||
end
|
||||
| if (.foundDup|not) and $alreadyUsed
|
||||
then .foundDup = .current
|
||||
| .foundDupAt = .n
|
||||
| .foundDupAt = .n
|
||||
else .
|
||||
end );
|
||||
|
||||
end );
|
||||
|
||||
1000 as $required
|
||||
| 15 as $capture
|
||||
| $required | recaman_required($capture)
|
||||
|
|
|
|||
|
|
@ -24,11 +24,11 @@ def covers(s):
|
|||
. as $required
|
||||
| first(foreach s as $x ( { i: -1, found: {}, nfound: 0};
|
||||
.i += 1
|
||||
| ($x|tostring) as $xs
|
||||
| ($x|tostring) as $xs
|
||||
| if .found[$xs] then .
|
||||
elif $x <= $required
|
||||
then .found[$xs] = true | .nfound += 1
|
||||
| if .nfound > $required then .emit=.i else . end
|
||||
else .
|
||||
end;
|
||||
elif $x <= $required
|
||||
then .found[$xs] = true | .nfound += 1
|
||||
| if .nfound > $required then .emit=.i else . end
|
||||
else .
|
||||
end;
|
||||
select(.emit).emit) );
|
||||
|
|
|
|||
|
|
@ -12,34 +12,34 @@ $foundDup = false;
|
|||
$n = 1;
|
||||
|
||||
while($n <= 15 || !$foundDup || count($used1000) < 1001) {
|
||||
$next = $a[$n - 1] - $n;
|
||||
if ($next < 1 || in_array($next, $used)) {
|
||||
$next += 2 * $n;
|
||||
}
|
||||
$alreadyUsed = in_array($next, $used);
|
||||
array_push($a, $next);
|
||||
if (!$alreadyUsed) {
|
||||
array_push($used, $next);
|
||||
if (0 <= $next && $next <= 1000) {
|
||||
array_push($used1000, $next);
|
||||
}
|
||||
}
|
||||
if ($n == 14) {
|
||||
echo "The first 15 terms of the Recaman sequence are : [";
|
||||
foreach($a as $i => $v) {
|
||||
if ( $i == count($a) - 1)
|
||||
echo "$v";
|
||||
else
|
||||
echo "$v, ";
|
||||
}
|
||||
echo "]\n";
|
||||
}
|
||||
if (!$foundDup && $alreadyUsed) {
|
||||
printf("The first duplicate term is a[%d] = %d\n", $n, $next);
|
||||
$foundDup = true;
|
||||
}
|
||||
if (count($used1000) == 1001) {
|
||||
printf("Terms up to a[%d] are needed to generate 0 to 1000\n", $n);
|
||||
}
|
||||
$n++;
|
||||
$next = $a[$n - 1] - $n;
|
||||
if ($next < 1 || in_array($next, $used)) {
|
||||
$next += 2 * $n;
|
||||
}
|
||||
$alreadyUsed = in_array($next, $used);
|
||||
array_push($a, $next);
|
||||
if (!$alreadyUsed) {
|
||||
array_push($used, $next);
|
||||
if (0 <= $next && $next <= 1000) {
|
||||
array_push($used1000, $next);
|
||||
}
|
||||
}
|
||||
if ($n == 14) {
|
||||
echo "The first 15 terms of the Recaman sequence are : [";
|
||||
foreach($a as $i => $v) {
|
||||
if ( $i == count($a) - 1)
|
||||
echo "$v";
|
||||
else
|
||||
echo "$v, ";
|
||||
}
|
||||
echo "]\n";
|
||||
}
|
||||
if (!$foundDup && $alreadyUsed) {
|
||||
printf("The first duplicate term is a[%d] = %d\n", $n, $next);
|
||||
$foundDup = true;
|
||||
}
|
||||
if (count($used1000) == 1001) {
|
||||
printf("Terms up to a[%d] are needed to generate 0 to 1000\n", $n);
|
||||
}
|
||||
$n++;
|
||||
}
|
||||
|
|
|
|||
28
Task/Recamans-sequence/Pluto/recamans-sequence.pluto
Normal file
28
Task/Recamans-sequence/Pluto/recamans-sequence.pluto
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
require "map"
|
||||
|
||||
local a = {0}
|
||||
local used = set.of(0)
|
||||
local used1000 = set.of(0)
|
||||
local found_dup = false
|
||||
local n = 1
|
||||
while n <= 15 or !found_dup or used1000:size() < 1001 do
|
||||
local nxt = a[n] - n
|
||||
if nxt < 1 or used:contains(nxt) then nxt += 2 * n end
|
||||
local already_used = used:contains(nxt)
|
||||
a:insert(nxt)
|
||||
if !already_used then
|
||||
used:add(nxt)
|
||||
if 0 <= nxt <= 1000 then used1000:add(nxt) end
|
||||
end
|
||||
if n == 14 then
|
||||
print($"The first 15 terms of the Recaman's sequence are:\n{a:concat(", ")}")
|
||||
end
|
||||
if !found_dup and already_used then
|
||||
print($"The first duplicated term is a[{n}] = {nxt}")
|
||||
found_dup = true
|
||||
end
|
||||
if used1000:size() == 1001 then
|
||||
print($"Terms up to a[{n}] are needed to generate 0 to 1000")
|
||||
end
|
||||
n += 1
|
||||
end
|
||||
|
|
@ -3,12 +3,12 @@ visited <- vector('logical', 1e8)
|
|||
terms <- vector('numeric')
|
||||
|
||||
in_a_interval <- function(v) {
|
||||
visited[[v+1]]
|
||||
visited[[v+1]]
|
||||
}
|
||||
|
||||
add_value <- function(v) {
|
||||
visited[[v+1]] <<- TRUE
|
||||
terms <<- append(terms, v)
|
||||
visited[[v+1]] <<- TRUE
|
||||
terms <<- append(terms, v)
|
||||
}
|
||||
|
||||
add_value(0)
|
||||
|
|
@ -18,24 +18,24 @@ value <- 0
|
|||
founddup <- FALSE
|
||||
|
||||
repeat {
|
||||
if ((value-step>0) && (!in_a_interval(value-step))) {
|
||||
value <- value - step
|
||||
} else {
|
||||
value <- value + step
|
||||
}
|
||||
if (in_a_interval(value) && !founddup) {
|
||||
cat("The first duplicated term is a[",step,"] = ",value,"\n", sep = "")
|
||||
founddup <- TRUE
|
||||
}
|
||||
add_value(value)
|
||||
if (all(visited[1:1000])) {
|
||||
cat("Terms up to a[",step,"] are needed to generate 0 to 1000\n",sep = "")
|
||||
break
|
||||
}
|
||||
step <- step + 1
|
||||
if (step == 15) {
|
||||
cat("The first 15 terms are :")
|
||||
for (aterm in terms) { cat(aterm," ", sep = "") }
|
||||
cat("\n")
|
||||
}
|
||||
if ((value-step>0) && (!in_a_interval(value-step))) {
|
||||
value <- value - step
|
||||
} else {
|
||||
value <- value + step
|
||||
}
|
||||
if (in_a_interval(value) && !founddup) {
|
||||
cat("The first duplicated term is a[",step,"] = ",value,"\n", sep = "")
|
||||
founddup <- TRUE
|
||||
}
|
||||
add_value(value)
|
||||
if (all(visited[1:1000])) {
|
||||
cat("Terms up to a[",step,"] are needed to generate 0 to 1000\n",sep = "")
|
||||
break
|
||||
}
|
||||
step <- step + 1
|
||||
if (step == 15) {
|
||||
cat("The first 15 terms are :")
|
||||
for (aterm in terms) { cat(aterm," ", sep = "") }
|
||||
cat("\n")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
31
Task/Recamans-sequence/Rebol/recamans-sequence.rebol
Normal file
31
Task/Recamans-sequence/Rebol/recamans-sequence.rebol
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
Rebol [
|
||||
title: "Rosetta code: Recaman's sequence"
|
||||
file: %Recaman's_sequence.r3
|
||||
url: https://rosettacode.org/wiki/Recaman%27s_sequence
|
||||
]
|
||||
|
||||
recaman-until: function/with [code [block!]][
|
||||
r: 0 n: 1
|
||||
result: reduce [r] ;; result sequence
|
||||
seen: reduce [r] ;; set of visited values for O(1) duplicate check
|
||||
bind code 'n
|
||||
until [
|
||||
append result r: recaman-succ seen n r
|
||||
if new?: not find seen r [ append seen r ]
|
||||
++ n
|
||||
do code ;; evaluate stop condition with current n in scope
|
||||
]
|
||||
result
|
||||
][
|
||||
recaman-succ: function [seen [block!] n [integer!] r [integer!]][
|
||||
back: r - n
|
||||
;; go back if positive and not already seen, otherwise go forward
|
||||
either any [back < 0 find seen back] [r + n] [back]
|
||||
]
|
||||
]
|
||||
|
||||
print "First 15 Recaman numbers:"
|
||||
print recaman-until [n = 15]
|
||||
print ""
|
||||
print "First duplicate Recaman number:"
|
||||
print last recaman-until [not new?]
|
||||
9
Task/Recamans-sequence/Uiua/recamans-sequence.uiua
Normal file
9
Task/Recamans-sequence/Uiua/recamans-sequence.uiua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# Sofar A(n-1) n Seen
|
||||
Start ← [0] 0 1 1_0_0_0
|
||||
N ← ⨬(˜-|+)⤚₂(⬚1⊡˜-) # A(n-1) - n if valid, else A(n-1) + n.
|
||||
Seen′ ← ⍜⊡⋅1⟜(⬚0↙↥⊸⊓+₁⧻)⊙⋅⋅∘ # Update Seen, expanding as needed.
|
||||
Step ← ⊃(⊃(∘|⋅⋅+₁)|Seen′)◡N # Run one step.
|
||||
&p$"First fifteen terms: \n_"⍥⤚Step15 ◌ Start
|
||||
&p$"First repeat: _"⋅⊙⋅◌⍢(⊸⊂⊙Step|=∩⧻⊸◴) Start
|
||||
&p$"...Thinking for _ seconds..."⍜now(⍢(⊸⊂⊙Step|≠1000/+⬚0↙1000⋅⋅⋅∘) Start)
|
||||
&p$"0-1000 generated at: _"⋅⋅⊙◌
|
||||
58
Task/Recamans-sequence/VBScript/recamans-sequence.vbs
Normal file
58
Task/Recamans-sequence/VBScript/recamans-sequence.vbs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
' Recaman's sequence - vbscript - 04/08/2015
|
||||
nx=15
|
||||
h=1000
|
||||
Wscript.StdOut.WriteLine "Recaman's sequence for the first " & nx & " numbers:"
|
||||
Wscript.StdOut.WriteLine recaman("seq",nx)
|
||||
Wscript.StdOut.WriteLine "The first duplicate number is: " & recaman("firstdup",0)
|
||||
Wscript.StdOut.WriteLine "The number of terms to complete the range 0--->"& h &" is: "& recaman("numterm",h)
|
||||
Wscript.StdOut.Write vbCrlf&".../...": zz=Wscript.StdIn.ReadLine()
|
||||
|
||||
function recaman(op,nn)
|
||||
Dim b,d,h
|
||||
Set b = CreateObject("Scripting.Dictionary")
|
||||
Set d = CreateObject("Scripting.Dictionary")
|
||||
list="0" : firstdup=0
|
||||
if op="firstdup" then
|
||||
nn=1000 : firstdup=1
|
||||
end if
|
||||
if op="numterm" then
|
||||
h=nn : nn=10000000 : numterm=1
|
||||
end if
|
||||
ax=0 'a(0)=0
|
||||
b.Add 0,1 'b(0)=1
|
||||
s=0
|
||||
for n=1 to nn-1
|
||||
an=ax-n
|
||||
if an<=0 then
|
||||
an=ax+n
|
||||
elseif b.Exists(an) then
|
||||
an=ax+n
|
||||
end if
|
||||
ax=an 'a(n)=an
|
||||
if not b.Exists(an) then b.Add an,1 'b(an)=1
|
||||
if op="seq" then
|
||||
list=list&" "&an
|
||||
end if
|
||||
if firstdup then
|
||||
if d.Exists(an) then
|
||||
recaman="a("&n&")="&an
|
||||
exit function
|
||||
else
|
||||
d.Add an,1 'd(an)=1
|
||||
end if
|
||||
end if
|
||||
if numterm then
|
||||
if an<=h then
|
||||
if not d.Exists(an) then
|
||||
s=s+1
|
||||
d.Add an,1 'd(an)=1
|
||||
end if
|
||||
if s>=h then
|
||||
recaman=n
|
||||
exit function
|
||||
end if
|
||||
end if
|
||||
end if
|
||||
next 'n
|
||||
recaman=list
|
||||
end function 'recaman
|
||||
Loading…
Add table
Add a link
Reference in a new issue