Average loop length: Difference between revisions

added Easylang
m (→‎{{header|Wren}}: Changed to Wren S/H)
(added Easylang)
 
(2 intermediate revisions by one other user not shown)
Line 746:
20 5,2960 5,2936 0,046%
</pre>
=={{header|EasyLang}}==
{{trans|Lua}}
<syntaxhighlight>
func average n reps .
for r to reps
f[] = [ ]
for i to n
f[] &= randint n
.
seen[] = [ ]
len seen[] n
x = 1
while seen[x] = 0
seen[x] = 1
x = f[x]
count += 1
.
.
return count / reps
.
func analytical n .
s = 1
t = 1
for i = n - 1 downto 1
t = t * i / n
s += t
.
return s
.
print " N average analytical (error)"
print "=== ======= ========== ======="
for n to 20
avg = average n 1e6
ana = analytical n
err = (avg - ana) / ana * 100
numfmt 0 2
write n
numfmt 4 9
print avg & ana & err & "%"
.
</syntaxhighlight>
 
=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
Line 2,577 ⟶ 2,619:
40 7.6151 7.6091 0.0789
═══ ═════════ ═════════ ═════════
</pre>
 
=={{header|RPL}}==
This task is an opportunity to showcase several useful instructions - <code>CON, SEQ</code> and <code>∑</code> - which avoid to use a <code>FOR..NEXT</code> loop, to create a constant array, to generate a list of random numbers and to calculate a finite sum.
{{works with|HP|48G}}
« 0 → n times count
« 1 times '''FOR''' j
n { } + 0 CON
« n RAND * CEIL » 'z' 1 n 1 SEQ
1
'''WHILE''' 3 PICK OVER GET NOT '''REPEAT'''
ROT OVER 1 PUT ROT ROT
OVER SWAP GET
'count' 1 STO+
'''END''' 3 DROPN
'''NEXT'''
count times /
» » '<span style="color:blue">XPRMT</span>' STO
« { } DUP
1 20 '''FOR''' n
SWAP n 1000 <span style="color:blue">XPRMT</span> +
SWAP 'j' 1 n 'n!/n^j/(n-j)!' ∑ +
'''NEXT'''
DUP2 SWAP %CH ABS 2 FIX "%" ADD STD
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
3: { 1 1.497 1.882 2.2 2.468 2.823 3 3.258 3.475 3.647 3.854 4.003 4.234 4.46 4.589 4.767 4.852 4.929 5.154 5.251 }
2: { 1 1.5 1.88888888889 2.21875 2.5104 2.77469135802 3.0181387007 3.24501800538 3.45831574488 3.66021568 3.85237205073 4.03607367511 4.21234791298 4.38202942438 4.54580728514 4.70425824709 4.85787082081 5.00706309901 5.15219620097 5.29358458601 }
1:{ "0.00%" "0.20%" "0.36%" "0.85%" "1.69%" "1.74%" "0.60%" "0.40%" "0.48%" "0.36%" "0.04%" "0.82%" "0.51%" "1.78%" "0.95%" "1.33%" "0.12%" "1.56%" "0.04%" "0.80%" }
</pre>
 
2,046

edits