Riordan numbers: Difference between revisions

Created Nim solution.
(Added AppleScript.)
(Created Nim solution.)
Line 777:
</pre>
 
=={{header|Nim}}==
===Task===
<syntaxhighlight lang="Nim">import std/strformat
 
iterator riordan(): int =
var prev = 1
var curr = 0
yield prev
var n = 1
while true:
yield curr
inc n
let next = (n - 1) * (2 * curr + 3 * prev) div (n + 1)
prev = curr
curr = next
 
echo &"First 32 Riordan numbers:"
var count = 0
for n in riordan():
inc count
stdout.write &"{n:<13}"
stdout.write if count mod 4 == 0: '\n' else: ' '
if count == 32: break
</syntaxhighlight>
 
{{out}}
<pre>First 32 Riordan numbers:
1 0 1 1
3 6 15 36
91 232 603 1585
4213 11298 30537 83097
227475 625992 1730787 4805595
13393689 37458330 105089229 295673994
834086421 2358641376 6684761125 18985057351
54022715451 154000562758 439742222071 1257643249140
</pre>
 
===Stretch task===
{{libheader|Nim-Integers}}
<syntaxhighlight lang="Nim">import std/strformat
import integers
 
iterator riordan(): Integer =
var prev = newInteger(1)
var curr = newInteger(0)
yield prev
var n = 1
while true:
yield curr
inc n
let next = (n - 1) * (2 * curr + 3 * prev) div (n + 1)
prev = curr
curr = next
 
var count = 0
for n in riordan():
inc count
if count in [1000, 10000]:
echo &"The {count}th Riordan number has {len($n)} digits."
if count == 10000: break
</syntaxhighlight>
 
{{out}}
<pre>The 1000th Riordan number has 472 digits.
The 10000th Riordan number has 4765 digits.
</pre>
 
=={{header|Perl}}==
256

edits