Harshad or Niven series: Difference between revisions

Content deleted Content added
Hout (talk | contribs)
m →‎Python: Functional: Updated primitives
Hout (talk | contribs)
m →‎Python: Functional: Pruned out one import.
Line 3,217: Line 3,217:
>>> </lang>
>>> </lang>


And we can sum digits more directly (without string coercion) while still preserving functional composition:
And we could also sum digits more directly (without string coercion) while still preserving functional composition:


{{Works with|Python|3.7}}
{{Works with|Python|3.7}}
<lang python>'''Harshad or Niven series'''
<lang python>'''Harshad or Niven series'''


from itertools import chain, count, dropwhile, islice
from itertools import count, dropwhile, islice




Line 3,228: Line 3,228:
def harshads():
def harshads():
'''Harshad series'''
'''Harshad series'''
def go(x):
return (
return [x] if 0 == (x % digitSum(x)) else []
x for x in count(1)
if 0 == x % digitSum(x)
return chain.from_iterable(
map(go, count(1))
)
)


Line 3,333: Line 3,332:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()
main()</lang>
</lang>
{{Out}}
{{Out}}
<pre>Harshad or Niven series:
<pre>Harshad or Niven series: