Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
36
Task/Padovan-sequence/Phix/padovan-sequence-1.phix
Normal file
36
Task/Padovan-sequence/Phix/padovan-sequence-1.phix
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">padovan</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">padovanr</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">padovan</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">padovan</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">padovan</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">padovan</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">padovan</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1.324717957244746025960908854</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1.0453567932525329623</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">padovana</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">s</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">0.5</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"B"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"C"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"AB"</span><span style="color: #0000FF;">}</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">padovanl</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">prev</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">prev</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">[</span><span style="color: #000000;">prev</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">64</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">pl</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"A"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l10</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">64</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">pn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">padovanr</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">padovana</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">pn</span> <span style="color: #008080;">or</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pl</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">pn</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">crash</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"oops"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">10</span> <span style="color: #008080;">then</span> <span style="color: #000000;">l10</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pl</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">pl</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">padovanl</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pl</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The first 20 terms of the Padovan sequence: %v\n\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">padovan</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">20</span><span style="color: #0000FF;">]})</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The first 10 L-system strings: %v\n\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">l10</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"recursive, algorithmic, and l-system agree to n=64\n"</span><span style="color: #0000FF;">)</span>
|
||||
<!--
|
||||
49
Task/Padovan-sequence/Phix/padovan-sequence-2.phix
Normal file
49
Task/Padovan-sequence/Phix/padovan-sequence-2.phix
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
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!")
|
||||
136
Task/Padovan-sequence/Phix/padovan-sequence-3.phix
Normal file
136
Task/Padovan-sequence/Phix/padovan-sequence-3.phix
Normal 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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue