Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
43
Task/Average-loop-length/FreeBASIC/average-loop-length.basic
Normal file
43
Task/Average-loop-length/FreeBASIC/average-loop-length.basic
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
Const max_N = 20, max_ciclos = 1000000
|
||||
|
||||
Function Factorial(Byval N As Integer) As Double
|
||||
Dim As Double d: d = 1
|
||||
If N = 0 Then Factorial = 1: Exit Function
|
||||
While (N > 1)
|
||||
d *= N
|
||||
N -= 1
|
||||
Wend
|
||||
Factorial = d
|
||||
End Function
|
||||
|
||||
Function Analytical(N As Integer) As Double
|
||||
Dim As Double i, sum = 0
|
||||
For i = 1 To N
|
||||
sum += Factorial(N) / N^i / Factorial(N-i)
|
||||
Next i
|
||||
Return sum
|
||||
End Function
|
||||
|
||||
Function Average(N As Integer, ciclos As Double) As Double
|
||||
Dim As Integer i, x, bits, sum = 0
|
||||
For i = 0 To ciclos - 1
|
||||
x = 1 : bits = 0
|
||||
While (bits And x) = 0
|
||||
sum += 1
|
||||
bits Or= x
|
||||
x = 1 Shl (Rnd * (N - 1))
|
||||
Wend
|
||||
Next i
|
||||
Return sum / ciclos
|
||||
End Function
|
||||
|
||||
Randomize Timer
|
||||
Print " N promedio analitico (error)"
|
||||
Print "--- ---------- ----------- ----------"
|
||||
For N As Integer = 1 To max_N
|
||||
Dim As Double avg = Average(N, max_ciclos)
|
||||
Dim As Double ana = Analytical(N)
|
||||
Dim As Double diff = abs(avg-ana) / ana * 100
|
||||
Print Using " ## #####.###0 #####.###0 ###.#0%"; N; avg; ana; diff
|
||||
Next N
|
||||
Sleep
|
||||
Loading…
Add table
Add a link
Reference in a new issue