Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,24 @@
PROGRAM RANDOM_PI
!
! for rosettacode.org
!
!$DOUBLE
PROCEDURE MONTECARLO(T->RES)
LOCAL I,N
FOR I=1 TO T DO
IF RND(1)^2+RND(1)^2<1 THEN N+=1 END IF
END FOR
RES=4*N/T
END PROCEDURE
BEGIN
RANDOMIZE(TIMER) ! init rnd number generator
MONTECARLO(1000->RES) PRINT(RES)
MONTECARLO(10000->RES) PRINT(RES)
MONTECARLO(100000->RES) PRINT(RES)
MONTECARLO(1000000->RES) PRINT(RES)
MONTECARLO(10000000->RES) PRINT(RES)
END PROGRAM

View file

@ -0,0 +1,33 @@
' version 23-10-2016
' compile with: fbc -s console
Randomize Timer 'seed the random function
Dim As Double x, y, pi, error_
Dim As UInteger m = 10, n, n_start, n_stop = m, p
Print
Print " Mumber of throws Ratio (Pi) Error"
Print
Do
For n = n_start To n_stop -1
x = Rnd
y = Rnd
If (x * x + y * y) <= 1 Then p = p +1
Next
Print Using " ############, "; m ;
pi = p * 4 / m
error_ = 3.141592653589793238462643383280 - pi
Print RTrim(Str(pi),"0");Tab(35); Using "##.#############"; error_
m = m * 10
n_start = n_stop
n_stop = m
Loop Until m > 1000000000 ' 1,000,000,000
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

View file

@ -0,0 +1,46 @@
default(f32)
fun dirvcts(): [2][30]int =
[
[
536870912, 268435456, 134217728, 67108864, 33554432, 16777216, 8388608, 4194304, 2097152, 1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1
],
[
536870912, 805306368, 671088640, 1006632960, 570425344, 855638016, 713031680, 1069547520, 538968064, 808452096, 673710080, 1010565120, 572653568, 858980352, 715816960, 1073725440, 536879104, 805318656, 671098880, 1006648320, 570434048, 855651072, 713042560, 1069563840, 538976288, 808464432, 673720360, 1010580540, 572662306, 858993459
]
]
fun grayCode(x: int): int = (x >> 1) ^ x
----------------------------------------
--- Sobol Generator
----------------------------------------
fun testBit(n: int, ind: int): bool =
let t = (1 << ind) in (n & t) == t
fun xorInds(n: int) (dir_vs: [num_bits]int): int =
let reldv_vals = zipWith (fn dv i =>
if testBit(grayCode n,i)
then dv else 0)
dir_vs (iota num_bits)
in reduce (^) 0 reldv_vals
fun sobolIndI (dir_vs: [m][num_bits]int, n: int): [m]int =
map (xorInds n) dir_vs
fun sobolIndR(dir_vs: [m][num_bits]int) (n: int ): [m]f32 =
let divisor = 2.0 ** f32(num_bits)
let arri = sobolIndI( dir_vs, n )
in map (fn (x: int): f32 => f32(x) / divisor) arri
fun main(n: int): f32 =
let rand_nums = map (sobolIndR (dirvcts())) (iota n)
let dists = map (fn xy =>
let (x,y) = (xy[0],xy[1]) in sqrt32(x*x + y*y))
rand_nums
let bs = map (fn d => if d <= 1.0f32 then 1 else 0) dists
let inside = reduce (+) 0 bs
in 4.0f32*f32(inside)/f32(n)

View file

@ -0,0 +1,12 @@
import math
randomize()
proc pi(nthrows): float =
var inside = 0
for i in 1..int64(nthrows):
if hypot(random(1.0), random(1.0)) < 1:
inc inside
return float(4 * inside) / nthrows
for n in [10e4, 10e6, 10e7, 10e8]:
echo pi(n)

View file

@ -0,0 +1,12 @@
decimals(8)
see "monteCarlo(1000) = " + monteCarlo(1000) + nl
see "monteCarlo(10000) = " + monteCarlo(10000) + nl
see "monteCarlo(100000) = " + monteCarlo(100000) + nl
func monteCarlo t
n=0
for i = 1 to t
if sqrt(pow(random(1),2) + pow(random(1),2)) <= 1 n += 1 ok
next
t = (4 * n) / t
return t

View file

@ -0,0 +1,26 @@
import Foundation
func mcpi(sampleSize size:Int) -> Double {
var x = 0 as Double
var y = 0 as Double
var m = 0 as Double
for i in 0..<size {
x = Double(arc4random()) / Double(UINT32_MAX)
y = Double(arc4random()) / Double(UINT32_MAX)
if ((x * x) + (y * y) < 1) {
m += 1
}
}
return (4.0 * m) / Double(size)
}
println(mcpi(sampleSize: 100))
println(mcpi(sampleSize: 1000))
println(mcpi(sampleSize: 10000))
println(mcpi(sampleSize: 100000))
println(mcpi(sampleSize: 1000000))
println(mcpi(sampleSize: 10000000))
println(mcpi(sampleSize: 100000000))