Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,13 @@
def digital_root (n):
ap = 0
n = abs(int(n))
while n >= 10:
n = sum(int(digit) for digit in str(n))
ap += 1
return ap, n
if __name__ == '__main__':
for n in [627615, 39390, 588225, 393900588225, 55]:
persistance, root = digital_root(n)
print("%12i has additive persistance %2i and digital root %i."
% (n, persistance, root))

View file

@ -0,0 +1,64 @@
from functools import (reduce)
# main :: IO ()
def main():
print (
tabulated(digitalRoot)(
'Integer -> (additive persistence, digital root):'
)([627615, 39390, 588225, 393900588225, 55])
)
# digitalRoot :: Int -> (Int, Int)
def digitalRoot(n):
'''Integer -> (additive persistence, digital root)'''
# f :: (Int, Int) -> (Int, Int)
def f(pn):
p, n = pn
return (
1 + p,
reduce(lambda a, x: a + int(x), str(n), 0)
)
# p :: (Int , Int) -> Bool
def p(pn):
return 10 > pn[1]
return until(p)(f)(
(0, abs(int(n)))
)
# GENERIC -------------------------------------------------
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
def compose(g):
return lambda f: lambda x: g(f(x))
# tabulated :: (a -> b) -> String -> String
def tabulated(f):
'''function -> heading -> input List -> tabulated output string'''
def go(s, xs):
fw = compose(len)(str)
w = fw(max(xs, key=fw))
return s + '\n' + '\n'.join(list(map(
lambda x: str(x).rjust(w, ' ') + ' -> ' + str(f(x)), xs
)))
return lambda s: lambda xs: go(s, xs)
# until :: (a -> Bool) -> (a -> a) -> a -> a
def until(p):
def go(f, x):
v = x
while not p(v):
v = f(v)
return v
return lambda f: lambda x: go(f, x)
if __name__ == '__main__':
main()