Task: Padovan sequence

C: #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>


int pRec(int n) {
    static int *memo = NULL;
    static size_t curSize = 0;
    
    
    if (curSize <= (size_t) n) {
        size_t lastSize = curSize;
        while (curSize <= (size_t) n) curSize += 1024 * sizeof(int);
        memo = realloc(memo, curSize * sizeof(int));
        memset(memo + lastSize, 0, (curSize - lastSize) * sizeof(int));
    }
    
    
    if (memo[n] == 0) {
        if (n<=2) memo[n] = 1;
        else memo[n] = pRec(n-2) + pRec(n-3);
    }
    
    return memo[n];
}


int pFloor(int n) {
    long double p = 1.324717957244746025960908854;
    long double s = 1.0453567932525329623;
    return powl(p, n-1)/s + 0.5;
}


void nextLSystem(const char *prev, char *buf) {
    while (*prev) {
        switch (*prev++) {
            case 'A': *buf++ = 'B'; break;
            case 'B': *buf++ = 'C'; break;
            case 'C': *buf++ = 'A'; *buf++ = 'B'; break;
        }
    }
    *buf = '\0';
}

int main() {
    
    #define BUFSZ 8192
    char buf1[BUFSZ], buf2[BUFSZ];
    int i;
    
    
    printf("P_0 .. P_19: ");
    for (i=0; i<20; i++) printf("%d ", pRec(i));
    printf("\n");
    
    
    printf("The floor- and recurrence-based functions ");
    for (i=0; i<64; i++) {
        if (pRec(i) != pFloor(i)) {
            printf("do not match at %d: %d != %d.\n",
                i, pRec(i), pFloor(i));
            break;
        }
    }
    if (i == 64) {
        printf("match from P_0 to P_63.\n");
    }
    
    
    printf("\nThe first 10 L-system strings are:\n"); 
    for (strcpy(buf1, "A"), i=0; i<10; i++) {
        printf("%s\n", buf1);
        strcpy(buf2, buf1);
        nextLSystem(buf2, buf1);
    }
    
    
    printf("\nThe floor- and L-system-based functions ");
    for (strcpy(buf1, "A"), i=0; i<32; i++) {
        if ((int)strlen(buf1) != pFloor(i)) {
            printf("do not match at %d: %d != %d\n",
                i, (int)strlen(buf1), pFloor(i));
            break;
        }
        strcpy(buf2, buf1);
        nextLSystem(buf2, buf1);
    }
    if (i == 32) {
        printf("match from P_0 to P_31.\n");
    }
    
    return 0;
}







Python: from math import floor
from collections import deque
from typing import Dict, Generator


def padovan_r() -> Generator[int, None, None]:
    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')
             ) -> Generator[str, None, None]:
    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!")