Padovan sequence

From Rosetta Code
Revision as of 23:14, 26 February 2021 by rosettacode>Paddy3118 (New draft task with Python solution)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Padovan sequence is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.


The Padovan sequence is similar to the Fibonacci sequence in several ways. Some are given in the table below, and the referenced video shows some of the geometric similarities.

Padovan                         Fibonacci                       Comment
==============================  ==============================  ==================================

Richard Padovan                 Leonardo of Pisa: Fibonacci     Named after.

P(0)=P(1)=P(2)=1                F(0)=F(1)=1                     Recurrence initial values.
P(n)=P(n-2)+P(n-3)              F(n)=F(n-1)+F(n-2)              Recurrence relation.

1,1,1,2,2,3,4,5,7,9             0,1,1,2,3,5,8,13,21,34          First 10 terms.

Plastic ratio, p                Golden ratio, g                 Ratio of successive terms...
1.324717957244746025960908854…  1.6180339887498948482...
((9+69**.5)/18)**(1/3) + ((9-69**.5)/18)**(1/3)                 Exact formula of ratio p.
                                (1+5**0.5)/2                    Exact formula of ratio g.
p: x**3-x-1                     g: x**2-x-1                     Ratio is real root of polynomial.

Equilateral triangles           Squares                         Spirally tiling the plane using.

s= 1.0453567932525329623        a=5**0.5                        Constants for ...
P(n)=floor(p**(n-1) / s + .5)   F(n)=floor(g**n / a + .5)       ... Computing by truncation.

A,B,C                           A,B                             L-System Variables.
A                               A                               L-System Start/Axiom.
A->B,B->C,C->AB                 A->B,B->A                       L-System Rules.

Task
  • Write a function/method/subroutine to compute successive members of the Padovan series using the recurrence relation.
  • Write a function/method/subroutine to compute successive members of the Padovan series using the floor function.
  • Show the first twenty terms of the sequence.
  • Confirm that the recurrence and floor based functions give the same results for 64 terms,
  • Write a function/method/... using the L-system to generate successive strings.
  • Show the first 10 strings produced from the L-system
  • Confirm that the length of the first 32 strings produced is the Padovan sequence.

Show output here, on this page.

Ref

Python

<lang python>from math import floor from collections import deque from typing import Dict


def padovan_r():

   last = deque([1, 1, 1], 4)
   while True:
       last.append(last[-2] + last[-3])
       yield last.popleft()

_p, _s = 1.324717957244746025960908854, 1.0453567932525329623

def padovan_f(n: int) -> int:

   return floor(_p**(n-1) / _s + .5)

def padovan_l(start: str='A',

            rules: Dict[str, str]=dict(A='B', B='C', C='AB')) -> str:
   axiom = start
   while True:
       yield axiom
       axiom = .join(rules[ch] for ch in axiom)


if __name__ == "__main__":

   from itertools import islice
   print("The first twenty terms of the sequence.")
   print(str([padovan_f(n) for n in range(20)])[1:-1])
   r_generator = padovan_r()
   if all(next(r_generator) == padovan_f(n) for n in range(64)):
       print("\nThe recurrence and floor based algorithms match to n=63 .")
   else:
       print("\nThe recurrence and floor based algorithms DIFFER!")
   print("\nThe first 10 L-system string-lengths and strings")
   l_generator = padovan_l(start='A', rules=dict(A='B', B='C', C='AB'))
   print('\n'.join(f"  {len(string):3} {repr(string)}"
                   for string in islice(l_generator, 10)))
   r_generator = padovan_r()
   l_generator = padovan_l(start='A', rules=dict(A='B', B='C', C='AB'))
   if all(len(next(l_generator)) == padovan_f(n) == next(r_generator)
          for n in range(32)):
       print("\nThe L-system, recurrence and floor based algorithms match to n=31 .")
   else:
       print("\nThe L-system, recurrence and floor based algorithms DIFFER!")</lang>
Output:
The first twenty terms of the 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 algorithms match to n=63 .

The first 10 L-system string-lengths and strings
    1 'A'
    1 'B'
    1 'C'
    2 'AB'
    2 'BC'
    3 'CAB'
    4 'ABBC'
    5 'BCCAB'
    7 'CABABBC'
    9 'ABBCBCCAB'

The L-system, recurrence and floor based algorithms match to n=31 .