Harshad or Niven series: Difference between revisions

Updated to compile with Nim 1.4. Simplified the code.
No edit summary
(Updated to compile with Nim 1.4. Simplified the code.)
Line 2,228:
 
=={{header|Nim}}==
<lang nim>proc slice[T](iter: iterator(): T {.closure.},; sl: Slice[T]): seq[T] =
<lang nim>import strutils
 
proc slice[T](iter: iterator(): T {.closure.}, sl): seq[T] =
var result {.gensym.}: seq[int64] = @[]
var i = 0
for n in iter():
if i > sl.b: break
if i >= sl.a: result.add(n)
break
if i >= sl.a:
result.add(n)
inc i
result
iterator harshad(): int64int {.closure.} =
 
for n in 1 .. < int64 int.high:
iterator harshad(): int64 {.closure.} =
for n in 1 .. < int64.high:
var sum = 0
for ch in string($n):
sum += parseIntord(""ch) &- chord('0')
if n mod sum == 0:
yield n
 
echo harshad.slice 0 .. < 20
 
for n in harshad():
if n > 1000:
Anonymous user