Padovan sequence: Difference between revisions

m
simpler output
(Added Swift solution)
m (simpler output)
Line 1,188:
 
=={{header|Julia}}==
<lang julia>""" recursiveRecursive Padowan """
rPadovan(n) = (n < 4) ? one(n) : rPadovan(n - 3) + rPadovan(n - 2)
 
""" floorFloor based roundingfunction calculation Padowan """
function fPadovan(n)::Int
p, s = big"1.324717957244746025960908854", big"1.0453567932525329623"
return Int(floor(p^(n-2) / s + .5))
end
 
mutable struct LSystemPadowan
rules::Dict{String, String}
init::String
current::String
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 step(lsys::LSystemPadowan)
s = ""
if isempty(lsys.current)
lsys.current = lsys.init
else
for c in lsys.current
s *= lsys.rules[string(c)]
end
lsys.current = s
end
return lsys.current
end
 
function list_LsysPadowan(N)
LSystemPadowan()rules = new(Dict("A" => "B", "B" => "C", "C" => "AB"), "A", "")
lsys = LSystemPadowan()
seq, lens = Int["A"], [1]
for i in 1:N
str = step(lsysrules, seq[end])
push!(seq, length(lsys.current)str)
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=====================================")
foreach(i -> println( rpad(i, 4),< rpad(lr33 ? lL[i] : "", 12), rpad(lf[i], 12),< lL11 ? sL[i] : "")), 1:32)
 
if all(i -> lr[i] == lf[i], 1:64)
println("\nThe recursive and floor methods match to an N of 64")
end
 
if all(i -> lr[i] == lf[i] == lL[i], 1:32)
println("\nThe recursive, floor, and LSys methods match to an N of 32\n")
end
 
println("N LSystem string\n===========================")
const lsys = LSystemPadowan()
for i in 1:10
step(lsys)
println(rpad(i, 10), lsys.current)
end
</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
 
The recursive and floor methods match to an N of 64
 
The recursive, floor, and LSys methods match to an N of 32
 
N LSystem string
===========================
1 A
2 B
3 C
4 AB
5 BC
6 CAB
7 ABBC
8 BCCAB
9 CABABBC
10 ABBCBCCAB
</pre>
 
4,105

edits