How to resolve the algorithm Padovan sequence step by step in the Nim programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Padovan sequence step by step in the Nim programming language
Table of Contents
Problem Statement
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. Show output here, on this page.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Padovan sequence step by step in the Nim programming language
Source code in the nim programming language
import sequtils, strutils, tables
const
P = 1.324717957244746025960908854
S = 1.0453567932525329623
Rules = {'A': "B", 'B': "C", 'C': "AB"}.toTable
iterator padovan1(n: Natural): int {.closure.} =
## Yield the first "n" Padovan values using recurrence relation.
for _ in 1..min(n, 3): yield 1
var a, b, c = 1
var count = 3
while count < n:
(a, b, c) = (b, c, a + b)
yield c
inc count
iterator padovan2(n: Natural): int {.closure.} =
## Yield the first "n" Padovan values using formula.
if n > 1: yield 1
var p = 1.0
var count = 1
while count < n:
yield (p / S).toInt
p *= P
inc count
iterator padovan3(n: Natural): string {.closure.} =
## Yield the strings produced by the L-system.
var s = "A"
var count = 0
while count < n:
yield s
var next: string
for ch in s:
next.add Rules[ch]
s = move(next)
inc count
echo "First 20 terms of the Padovan sequence:"
echo toSeq(padovan1(20)).join(" ")
let list1 = toSeq(padovan1(64))
let list2 = toSeq(padovan2(64))
echo "The first 64 iterative and calculated values ",
if list1 == list2: "are the same." else: "differ."
echo ""
echo "First 10 L-system strings:"
echo toSeq(padovan3(10)).join(" ")
echo ""
echo "Lengths of the 32 first L-system strings:"
let list3 = toSeq(padovan3(32)).mapIt(it.len)
echo list3.join(" ")
echo "These lengths are",
if list3 == list1[0..31]: " " else: " not ",
"the 32 first terms of the Padovan sequence."
You may also check:How to resolve the algorithm Active Directory/Search for a user step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Assertions step by step in the Dyalect programming language
You may also check:How to resolve the algorithm Old lady swallowed a fly step by step in the IS-BASIC programming language
You may also check:How to resolve the algorithm Hamming numbers step by step in the Racket programming language
You may also check:How to resolve the algorithm Inheritance/Single step by step in the F# programming language