Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
44
Task/Average-loop-length/Go/average-loop-length.go
Normal file
44
Task/Average-loop-length/Go/average-loop-length.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
const nmax = 20
|
||||
|
||||
func main() {
|
||||
fmt.Println(" N average analytical (error)")
|
||||
fmt.Println("=== ========= ============ =========")
|
||||
for n := 1; n <= nmax; n++ {
|
||||
a := avg(n)
|
||||
b := ana(n)
|
||||
fmt.Printf("%3d %9.4f %12.4f (%6.2f%%)\n",
|
||||
n, a, b, math.Abs(a-b)/b*100)
|
||||
}
|
||||
}
|
||||
|
||||
func avg(n int) float64 {
|
||||
const tests = 1e4
|
||||
sum := 0
|
||||
for t := 0; t < tests; t++ {
|
||||
var v [nmax]bool
|
||||
for x := 0; !v[x]; x = rand.Intn(n) {
|
||||
v[x] = true
|
||||
sum++
|
||||
}
|
||||
}
|
||||
return float64(sum) / tests
|
||||
}
|
||||
|
||||
func ana(n int) float64 {
|
||||
nn := float64(n)
|
||||
term := 1.
|
||||
sum := 1.
|
||||
for i := nn - 1; i >= 1; i-- {
|
||||
term *= i / nn
|
||||
sum += term
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
|
@ -1,36 +1,38 @@
|
|||
/*REXX program to read a config file and assign VARs as found within. */
|
||||
numeric digits 10000 /*be able to calculate !(runs). */
|
||||
/*REXX pgmm computes avg loop length mapping a random field 1..N to 1..N*/
|
||||
parse arg runs tests seed .
|
||||
if runs ==',' | runs =='' then runs = 40 /*num of runs. */
|
||||
if tests ==',' | tests =='' then tests = 1000000 /*num of trials.*/
|
||||
if seed\==',' & seed\=='' then call random ,,seed /*repeatability?*/
|
||||
numeric digits 100000; !.=0; !.0=1 /*be able to calculate !(25000).*/
|
||||
numeric digits max(9,length(!(runs))) /*set NUMERIC digits for !(runs).*/
|
||||
say right( runs, 24) 'runs' /*display # of runs we're using*/
|
||||
say right( tests, 24) 'tests' /* " " " tests " " */
|
||||
say right( digits(), 24) 'digits' /* " " " digits " " */
|
||||
say
|
||||
say ' N average exact % error' /*headers & pad.*/
|
||||
h= ' ─── ───────── ───────── ─────────'; say h; pad=left('',3)
|
||||
h= ' ─── ───────── ───────── ─────────'; say h; pad=left('',3)
|
||||
|
||||
do #=1 for runs; ##=right(#,9) /*## is used for indenting output*/
|
||||
a= format(exact(#) ,,4) /*use 4 digits past decimal point*/
|
||||
e= format(exper(#) ,,4) /* " " " " " " */
|
||||
err= format(abs(e-a)*100/a ,,4) /* " " " " " " */
|
||||
if err=0 then err=err/1 /*present a clean & concise zero.*/
|
||||
say ## pad e pad a pad err /*display a line of statistics. */
|
||||
avg = fmtD(exact(#)) /*use 4 digits past decimal point*/
|
||||
exa = fmtD(exper(#)) /* " " " " " " */
|
||||
err = fmtD(abs(exa-avg)*100/avg) /* " " " " " " */
|
||||
say ## pad exa pad avg pad err /*display a line of statistics. */
|
||||
end /*#*/
|
||||
say h /*display the final header bar. */
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────! subroutine────────────────────────*/
|
||||
!: procedure; !=1; do j=2 to arg(1); !=!*j; end; return !
|
||||
!: procedure expose !.; parse arg z; if !.z\==0 then return !.z
|
||||
!=1; do j=1 for z; !=!*j; !.j=!; end; return !
|
||||
/*──────────────────────────────────EXACT subroutine────────────────────*/
|
||||
exact: parse arg x; s=0; do j=1 for x; s=s+!(x)/!(x-j)/x**j; end; return s
|
||||
/*──────────────────────────────────EXPER subroutine────────────────────*/
|
||||
exper: parse arg n
|
||||
k=0; do tests; !.=0 /*repeat TESTS times, reset found*/
|
||||
!.=0 /*stemmed array: expected results*/
|
||||
do n; r=random(1,n); if !.r then leave
|
||||
!.r=1; k=k+1 /*bump the ctr. */
|
||||
k=0; do tests; $.=0 /*repeat TESTS times, reset FOUND*/
|
||||
do n; r=random(1,n); if $.r then leave
|
||||
$.r=1; k=k+1 /*bump the ctr. */
|
||||
end /*n*/
|
||||
end /*tests*/
|
||||
return k/tests
|
||||
/*──────────────────────────────────FMTD subroutine─────────────────────*/
|
||||
fmtD: parse arg y,d; d=word(d 4,1); y=format(y,,d); parse var y w '.' f
|
||||
if f=0 then return w || left('',d+1); return y
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue