[your code here]

drug drug2004 at bk.ru
Wed Apr 28 07:56:50 UTC 2021


28.04.2021 00:43, Paul Backus пишет:
> // Estimate π using random integers
> void main()
> {
>      import std.stdio, std.random, std.math, std.conv, std.numeric;
>      Mt19937_64 gen; gen.seed(unpredictableSeed!ulong);
>      int pairCount = 1_000_000;
>      int coprimeCount = 0;
>      ulong a, b;
>      foreach (int i; 0 .. pairCount) {
>          a = gen.front; gen.popFront();
>          b = gen.front; gen.popFront();
>          if (gcd(a, b) == 1) coprimeCount++;
>      }
>      double prob = coprimeCount.to!double / pairCount.to!double;
>      writefln("π ≈ %.15f", sqrt(6.0 / prob));
> }
Functional style
```D
// Estimate π using random integers
void main()
{
     import std.algorithm, std.stdio, std.random, std.math, std.conv, 
std.numeric, std.range;
     Mt19937_64 gen; gen.seed(unpredictableSeed!ulong);
     int pairCount = 1_000_000;
     double coprimeCount = zip(gen, gen.dropOne)
         .take(pairCount)
         .map!((a)=>(gcd(a[0], a[1]) == 1) ? 1 : 0)
         .sum(0);

     auto prob = coprimeCount / pairCount;
     writefln("π ≈ %.15f", sqrt(6.0 / prob));
}
```


More information about the Digitalmars-d mailing list