Data update

This commit is contained in:
Ingy döt Net 2025-08-11 18:05:26 -07:00
parent 4d5544505c
commit 4924dd0264
3073 changed files with 55820 additions and 4408 deletions

View file

@ -1,5 +1,5 @@
""" Recursive Padovan """
rPadovan(n) = (n < 4) ? one(n) : rPadovan(n - 3) + rPadovan(n - 2)
rPadovan(n) = (n < 4) ? oneunit(n) : rPadovan(n - 3) + rPadovan(n - 2)
""" Floor function calculation Padovan """
function fPadovan(n)::Int

View file

@ -0,0 +1 @@
padovan(n) = last(BigInt[0 1 1; 1 0 0; 0 1 0] ^ n * BigInt[1, 1, 1])

View file

@ -1,49 +1,61 @@
from math import floor
from collections import deque
from typing import Dict, Generator
%! padovan_recurrent(-P) is multi.
padovan_recurrent(1).
padovan_recurrent(1).
padovan_recurrent(P) :- padovan_recurrent(1, 1, 1, P).
padovan_recurrent( _, _, P , P).
padovan_recurrent(P0, P1, P2, P) :- P3 is P0 + P1, padovan_recurrent(P1, P2, P3, P).
def padovan_r() -> Generator[int, None, None]:
last = deque([1, 1, 1], 4)
while True:
last.append(last[-2] + last[-3])
yield last.popleft()
%! padovan_truncated(+N, -P) is det.
padovan_truncated(N, P) :-
P is floor(1.324717957244746 ^ (N - 1) / 1.0453567932525329623 + 0.5).
_p, _s = 1.324717957244746025960908854, 1.0453567932525329623
padovan_l_system(['B' | LS]) --> ['A'], padovan_l_system(LS).
padovan_l_system(['C' | LS]) --> ['B'], padovan_l_system(LS).
padovan_l_system(['A', 'B' | LS]) --> ['C'], padovan_l_system(LS).
padovan_l_system([]) --> [].
def padovan_f(n: int) -> int:
return floor(_p**(n-1) / _s + .5)
fibonacci_l_system(['B' | LS]) --> ['A'], fibonacci_l_system(LS).
fibonacci_l_system(['A', 'B' | LS]) --> ['B'], fibonacci_l_system(LS).
fibonacci_l_system([]) --> [].
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)
l_system(_, String, String).
l_system(DCG, String0, String) :-
once(phrase(call(DCG, String1), String0)),
l_system(DCG, String1, String).
:- meta_predicate l_system(3, -).
%! l_system(:DCG, +String) is multi.
l_system(DCG, String) :- l_system(DCG, ['A'], String).
if __name__ == "__main__":
from itertools import islice
task :-
format("The first 20 Padovan numbers are:~n"),
foreach(
limit(20, padovan_recurrent(Padovan)),
format("~d ", [Padovan])
),
format("~nThe first 10 strings produced by the L-system are:~n"),
foreach(
limit(10, l_system(padovan_l_system, LString)),
format("~s ", [LString])
),
nl,
run_tests(padovan_sequence).
print("The first twenty terms of the sequence.")
print(str([padovan_f(n) for n in range(20)])[1:-1])
:- begin_tests(padovan_sequence).
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!")
test(recurrence_and_floor_are_same_sequence) :-
once(findnsols(64, PR, padovan_recurrent(PR), PRs)),
numlist(0, 63, Ns),
maplist(padovan_truncated, Ns, PTs),
assertion(PRs == PTs).
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)))
test(lengths_of_first_32_strings_is_Padovan_sequence) :-
once(findnsols(32, PR, padovan_recurrent(PR), PRs)),
once(findnsols(32, PL, (
l_system(padovan_l_system, String),
length(String, PL)
), PLs)),
assertion(PRs == PLs).
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!")
:- end_tests(padovan_sequence).

View file

@ -1,136 +1,49 @@
'''Padovan series'''
from itertools import chain, islice
from math import floor
from operator import eq
from collections import deque
from typing import Dict, Generator
# padovans :: [Int]
def padovans():
'''Non-finite series of Padovan numbers,
defined in terms of recurrence relations.
'''
def recurrence(abc):
a, b, c = abc
return a, (b, c, a + b)
def padovan_r() -> Generator[int, None, None]:
last = deque([1, 1, 1], 4)
while True:
last.append(last[-2] + last[-3])
yield last.popleft()
return unfoldr(recurrence)(
(1, 1, 1)
)
_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)
# padovanFloor :: [Int]
def padovanFloor():
'''The Padovan series, defined in terms
of a floor function.
'''
p = 1.324717957244746025960908854
s = 1.0453567932525329623
if __name__ == "__main__":
from itertools import islice
def f(n):
return floor(p ** (n - 1) / s + 0.5), 1 + n
print("The first twenty terms of the sequence.")
print(str([padovan_f(n) for n in range(20)])[1:-1])
return unfoldr(f)(0)
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)))
# padovanLSystem : [Int]
def padovanLSystem():
'''An L-system generating terms whose lengths
are the values of the Padovan integer series.
'''
def rule(c):
return 'B' if 'A' == c else (
'C' if 'B' == c else 'AB'
)
def f(s):
return s, ''.join(list(concatMap(rule)(s)))
return unfoldr(f)('A')
# ------------------------- TEST -------------------------
# prefixesMatch :: [a] -> [a] -> Bool
def prefixesMatch(xs, ys, n):
'''True if the first n items of each
series are the same.
'''
return all(map(eq, take(n)(xs), ys))
# main :: IO ()
def main():
'''Test three Padovan functions for
equivalence and expected results.
'''
print('\n'.join([
"First 20 padovans:\n",
repr(take(20)(padovans())),
"\nThe recurrence and floor-based functions" + (
" match over 64 terms:\n"
),
repr(prefixesMatch(
padovans(),
padovanFloor(),
64
)),
"\nFirst 10 L-System strings:\n",
repr(take(10)(padovanLSystem())),
"\nThe lengths of the first 32 L-System strings",
"match the Padovan sequence:\n",
repr(prefixesMatch(
padovans(),
(len(x) for x in padovanLSystem()),
32
))
]))
# ----------------------- GENERIC ------------------------
# concatMap :: (a -> [b]) -> [a] -> [b]
def concatMap(f):
'''A concatenated map'''
def go(xs):
return chain.from_iterable(map(f, xs))
return go
# unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
def unfoldr(f):
'''A lazy (generator) list unfolded from a seed value
by repeated application of f until no residue remains.
Dual to fold/reduce.
f returns either None, or just (value, residue).
For a strict output list, wrap the result with list()
'''
def go(x):
valueResidue = f(x)
while None is not valueResidue:
yield valueResidue[0]
valueResidue = f(valueResidue[1])
return go
# take :: Int -> [a] -> [a]
# take :: Int -> String -> String
def take(n):
'''The prefix of xs of length n,
or xs itself if n > length xs.
'''
def go(xs):
return (
xs[0:n]
if isinstance(xs, (list, tuple))
else list(islice(xs, n))
)
return go
# MAIN ---
if __name__ == '__main__':
main()
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!")

View file

@ -0,0 +1,136 @@
'''Padovan series'''
from itertools import chain, islice
from math import floor
from operator import eq
# padovans :: [Int]
def padovans():
'''Non-finite series of Padovan numbers,
defined in terms of recurrence relations.
'''
def recurrence(abc):
a, b, c = abc
return a, (b, c, a + b)
return unfoldr(recurrence)(
(1, 1, 1)
)
# padovanFloor :: [Int]
def padovanFloor():
'''The Padovan series, defined in terms
of a floor function.
'''
p = 1.324717957244746025960908854
s = 1.0453567932525329623
def f(n):
return floor(p ** (n - 1) / s + 0.5), 1 + n
return unfoldr(f)(0)
# padovanLSystem : [Int]
def padovanLSystem():
'''An L-system generating terms whose lengths
are the values of the Padovan integer series.
'''
def rule(c):
return 'B' if 'A' == c else (
'C' if 'B' == c else 'AB'
)
def f(s):
return s, ''.join(list(concatMap(rule)(s)))
return unfoldr(f)('A')
# ------------------------- TEST -------------------------
# prefixesMatch :: [a] -> [a] -> Bool
def prefixesMatch(xs, ys, n):
'''True if the first n items of each
series are the same.
'''
return all(map(eq, take(n)(xs), ys))
# main :: IO ()
def main():
'''Test three Padovan functions for
equivalence and expected results.
'''
print('\n'.join([
"First 20 padovans:\n",
repr(take(20)(padovans())),
"\nThe recurrence and floor-based functions" + (
" match over 64 terms:\n"
),
repr(prefixesMatch(
padovans(),
padovanFloor(),
64
)),
"\nFirst 10 L-System strings:\n",
repr(take(10)(padovanLSystem())),
"\nThe lengths of the first 32 L-System strings",
"match the Padovan sequence:\n",
repr(prefixesMatch(
padovans(),
(len(x) for x in padovanLSystem()),
32
))
]))
# ----------------------- GENERIC ------------------------
# concatMap :: (a -> [b]) -> [a] -> [b]
def concatMap(f):
'''A concatenated map'''
def go(xs):
return chain.from_iterable(map(f, xs))
return go
# unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
def unfoldr(f):
'''A lazy (generator) list unfolded from a seed value
by repeated application of f until no residue remains.
Dual to fold/reduce.
f returns either None, or just (value, residue).
For a strict output list, wrap the result with list()
'''
def go(x):
valueResidue = f(x)
while None is not valueResidue:
yield valueResidue[0]
valueResidue = f(valueResidue[1])
return go
# take :: Int -> [a] -> [a]
# take :: Int -> String -> String
def take(n):
'''The prefix of xs of length n,
or xs itself if n > length xs.
'''
def go(xs):
return (
xs[0:n]
if isinstance(xs, (list, tuple))
else list(islice(xs, n))
)
return go
# MAIN ---
if __name__ == '__main__':
main()