Padovan sequence: Difference between revisions

m (→‎{{header|Perl}}: removed state dependence for 'floor' routine)
(→‎{{header|Ruby}}: Added Ruby)
Line 1,678:
</pre>
 
=={{header|Ruby}}==
<lang ruby>padovan = Enumerator.new do |y|
ar = [1, 1, 1]
loop do
ar << ar.first(2).sum
y << ar.shift
end
end
 
P, S = 1.324717957244746025960908854, 1.0453567932525329623
def padovan_f(n) = (P**(n-1) / S + 0.5).floor
puts "Recurrence Padovan: #{padovan.take(20)}"
puts "Floor function: #{(0...20).map{|n| padovan_f(n)}}"
 
n = 63
bool = (0...n).map{|n| padovan_f(n)} == padovan.take(n)
puts "Recurrence and floor function are equal upto #{n}: #{bool}."
puts
def l_system(axiom = "A", rules = {"A" => "B", "B" => "C", "C" => "AB"} )
return enum_for(__method__, axiom, rules) unless block_given?
loop do
yield axiom
axiom = axiom.chars.map{|c| rules[c] }.join
end
end
puts "First 10 elements of L-system: #{l_system.take(10).join(", ")} "
n = 32
bool = l_system.take(n).map(&:size) == padovan.take(n)
puts "Sizes of first #{n} l_system strings equal to recurrence padovan? #{bool}."
</lang>
{{out}}
<pre>Recurrence Padovan: [1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37, 49, 65, 86, 114, 151]
Floor function: [1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37, 49, 65, 86, 114, 151]
Recurrence and floor function are equal upto 63: true.
 
First 10 elements of L-system: A, B, C, AB, BC, CAB, ABBC, BCCAB, CABABBC, ABBCBCCAB
Sizes of first 32 l_system strings equal to recurrence padovan? true.
</pre>
=={{header|Rust}}==
<lang rust>fn padovan_recur() -> impl std::iter::Iterator<Item = usize> {
1,149

edits