Padovan sequence: Difference between revisions
m
simpler output
(Added Swift solution) |
m (simpler output) |
||
Line 1,188:
=={{header|Julia}}==
<lang julia>"""
rPadovan(n) = (n < 4) ? one(n) : rPadovan(n - 3) + rPadovan(n - 2)
"""
function fPadovan(n)::Int
p, s = big"1.324717957244746025960908854", big"1.0453567932525329623"
return Int(floor(p^(n-2) / s + .5))
LSystemPadowan() = new(Dict("A" => "B", "B" => "C", "C" => "AB"), "A", "")▼
end
""" LSystem Padowan """
step(rules, current) = (s = ""; for c in current s *= rules[string(c)] end; s)
function list_LsysPadowan(N)
seq, lens =
for i in 1:N
str = step(
push!(seq,
push!(lens, length(str))
end
return seq, lens
end
list_rPadowan(N) = [rPadovan(i) for i in 1:N]
list_fPadowan(N) = [fPadovan(i) for i in 1:N]
const lr, lf = list_rPadowan(64), list_fPadowan(64)
const sL, lL = list_LsysPadowan(32)
println("N Recursive Floor LSystem String\n=============================================")▼
foreach(i -> println(rpad(i, 4), rpad(lr[i], 12), rpad(lf[i], 12),
▲println("N Recursive Floor LSystem\n=====================================")
</lang>{{out}}
<pre>
N Recursive Floor LSystem String
=============================================
1 1 1 1 A
2 1 1 1 B
3 1 1 1 C
4 2 2 2 AB
5 2 2 2 BC
6 3 3 3 CAB
7 4 4 4 ABBC
8 5 5 5 BCCAB
9 7 7 7 CABABBC
10 9 9 9 ABBCBCCAB
11 12 12 12
12 16 16 16
Line 1,288 ⟶ 1,254:
31 3329 3329 3329
32 4410 4410 4410
</pre>
|