Humble numbers: Difference between revisions

→‎{{header|Python}}: Defined humbles in terms of yield and set, rather than filter
(→‎{{header|Python}}: Defined humbles in terms of yield and set, rather than filter)
Line 1,755:
<lang python>'''Humble numbers'''
 
from itertools import count, groupby, islice
from functools import reduce
from math import floor, sqrt
 
 
Line 1,765 ⟶ 1,764:
OEIS A002473
'''
defhs isHumble= set(x[1]):
while not p(v)True:
return all(8 > x for x in primeFactors(x))
vnxt = fmin(vhs)
return filter(isHumble, count(1))
def phs.remove(qrnxt):
returnhs all= hs.union(8nxt >* x for x in primeFactors(x)[2, 3, 5, 7])
vyield = xnxt
 
 
Line 1,785 ⟶ 1,787:
 
print('\nCounts of Humble numbers with n digits:\n')
for tpl in take(510)(
(k, len(list(g))) for k, g in
groupby(map(compose(len, (str(x)), for x in humbles()))
):
print(tpl)
Line 1,804 ⟶ 1,806:
range(0, len(xs), n), []
) if 0 < n else []
 
 
# compose :: ((a -> a), ...) -> (a -> a)
def compose(*fs):
'''Composition, from right to left,
of a series of functions.
'''
return lambda x: reduce(
lambda a, f: f(a),
fs[::-1], x
)
 
 
# primeFactors :: Int -> [Int]
def primeFactors(n):
'''A list of the prime factors of n.
'''
def f(qr):
r = qr[1]
return step(r), 1 + r
 
def step(x):
return 1 + (x << 2) - ((x >> 1) << 1)
 
def go(x):
root = floor(sqrt(x))
 
def p(qr):
q = qr[0]
return root < q or 0 == (x % q)
 
q = until(p)(f)(
(2 if 0 == x % 2 else 3, 1)
)[0]
return [x] if q > root else [q] + go(x // q)
 
return go(n)
 
 
Line 1,849 ⟶ 1,814:
or xs itself if n > length xs.
'''
return lambda xs: list(islice(xs, n))
list(islice(xs, n))
 
''')
 
# until :: (a -> Bool) -> (a -> a) -> a -> a
def until(p):
'''The result of repeatedly applying f until p holds.
The initial seed value is x.
'''
def go(f, x):
v = x
while not p(v):
v = f(v)
return v
return lambda f: lambda x: go(f, x)
 
 
Line 1,883 ⟶ 1,837:
(3, 95)
(4, 197)
(5, 356)</pre>
(6, 579)
(7, 882)
(8, 1272)
(9, 1767)
(10, 2381)</pre>
 
=={{header|Racket}}==
9,659

edits