Padovan sequence: Difference between revisions

→‎{{header|jq}}: ; also fix typos in Julia entry
(Added 11l)
(→‎{{header|jq}}: ; also fix typos in Julia entry)
Line 1,474:
 
true</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
This entry differs from others in that the L-system is used to define
two functions - `padovanStrings` and `padovanNumbers` - that emit unbounded
streams of the Padovan strings and numbers respectively.
 
<lang jq># Output: first $n Padovans
def padovanRecur($n):
[range(0;$n) | 1] as $p
| if $n < 3 then $p
else reduce range(3;$n) as $i ($p; .[$i] = .[$i-2] + .[$i-3])
end;
 
# Output: first $n Padovans
def padovanFloor($n):
{ p: 1.324717957244746025960908854,
s: 1.0453567932525329623,
pow: 1 }
| reduce range (1;$n) as $i ( .f = [ ((.pow/.p/.s) + 0.5)|floor];
.f[$i] = (((.pow/.s) + 0.5)|floor)
| .pow *= .p)
| .f ;
# Output: a stream of the L-System Padovan strings
def padovanStrings:
{A: "B", B: "C", C: "AB", "": "A"} as $rules
| $rules[""]
| while(true;
ascii_downcase
| gsub("a"; $rules["A"]) | gsub("b"; $rules["B"]) | gsub("c"; $rules["C"]) ) ;
 
# Output: a stream of the Padovan numbers using the L-System strings
def padovanNumbers:
padovanStrings | length;
 
def task:
def s1($n):
if padovanFloor($n) == padovanRecur($n) then "give" else "do not give" end;
 
def s2($n):
if [limit($n; padovanNumbers)] == padovanRecur($n) then "give" else "do not give" end;
 
"The first 20 members of the Padovan sequence:", padovanRecur(20),
"",
"The recurrence and floor-based functions \(s1(64)) the same results for 64 terms.",
"",
([limit(10; padovanStrings)]
| "First 10 members of the Padovan L-System:", .,
"and their lengths:",
map(length)),
"",
"The recurrence and L-system based functions \(s2(32)) the same results for 32 terms."
;
 
task</lang>
{{out}}
<pre>
The first 20 members of the Padovan sequence:
[1,1,1,2,2,3,4,5,7,9,12,16,21,28,37,49,65,86,114,151]
 
The recurrence and floor-based functions give the same results for 64 terms.
 
First 10 members of the Padovan L-System:
["A","B","C","AB","BC","CAB","ABBC","BCCAB","CABABBC","ABBCBCCAB"]
and their lengths:
[1,1,1,2,2,3,4,5,7,9]
 
The recurrence and L-system based functions give the same results for 32 terms.
</pre>
 
=={{header|Julia}}==
<lang julia>""" Recursive PadowanPadovan """
rPadovan(n) = (n < 4) ? one(n) : rPadovan(n - 3) + rPadovan(n - 2)
 
""" Floor function calculation PadowanPadovan """
function fPadovan(n)::Int
p, s = big"1.324717957244746025960908854", big"1.0453567932525329623"
Line 1,485 ⟶ 1,557:
end
 
""" LSystem PadowanPadovan """
function list_LsysPadowanlist_LsysPadovan(N)
rules = Dict("A" => "B", "B" => "C", "C" => "AB")
seq, lens = ["A"], [1]
Line 1,498 ⟶ 1,570:
 
const lr, lf = [rPadovan(i) for i in 1:64], [fPadovan(i) for i in 1:64]
const sL, lL = list_LsysPadowanlist_LsysPadovan(32)
println("N Recursive Floor LSystem String\n=============================================")
foreach(i -> println(rpad(i, 4), rpad(lr[i], 12), rpad(lf[i], 12),
2,458

edits