www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [your code here]

reply Jeff Pratt <me jpratt.dev> writes:
```
// Estimate π using random integers
void main()
{
     import std.stdio, std.random, std.math, std.conv;
     ulong gcd(ulong a, ulong b)
     {
         if (b == 0) return a;
         return gcd(b, a % b);
     }
     Mt19937_64 gen; gen.seed(unpredictableSeed!ulong);
     int pairCount = 1_000_000;
     int coprimeCount = 0;
     ulong a, b;
     for (int i = 0; i < pairCount; i++) {
         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));
}
```
Apr 27
parent reply Paul Backus <snarwin gmail.com> writes:
On Tuesday, 27 April 2021 at 21:04:19 UTC, Jeff Pratt wrote:
 ```
 // Estimate π using random integers
 void main()
 {
     import std.stdio, std.random, std.math, std.conv;
     ulong gcd(ulong a, ulong b)
     {
         if (b == 0) return a;
         return gcd(b, a % b);
     }
     Mt19937_64 gen; gen.seed(unpredictableSeed!ulong);
     int pairCount = 1_000_000;
     int coprimeCount = 0;
     ulong a, b;
     for (int i = 0; i < pairCount; i++) {
         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));
 }
 ```
Nice example! A couple small improvements: * You can use `std.numeric.gcd` instead of writing your own. * You can use a `foreach` loop instead of a `for` loop. ``` // 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)); } ```
Apr 27
parent reply drug <drug2004 bk.ru> writes:
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)); } ```
Apr 28
parent reply Paul Backus <snarwin gmail.com> writes:
On Wednesday, 28 April 2021 at 07:56:50 UTC, drug wrote:
 ```D
         .map!((a)=>(gcd(a[0], a[1]) == 1) ? 1 : 0)
         .sum(0);
 ```
Alternatively: ```d .filter!(pair => gcd(pair[0], pair[1]) == 1) .count; ```
Apr 28
next sibling parent reply drug <drug2004 bk.ru> writes:
28.04.2021 15:34, Paul Backus пишет:
 On Wednesday, 28 April 2021 at 07:56:50 UTC, drug wrote:
 ```D
         .map!((a)=>(gcd(a[0], a[1]) == 1) ? 1 : 0)
         .sum(0);
 ```
Alternatively: ```d .filter!(pair => gcd(pair[0], pair[1]) == 1) .count; ```
it's better alternative!
Apr 28
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 28.04.21 15:21, drug wrote:
 28.04.2021 15:34, Paul Backus пишет:
 On Wednesday, 28 April 2021 at 07:56:50 UTC, drug wrote:
 ```D
         .map!((a)=>(gcd(a[0], a[1]) == 1) ? 1 : 0)
         .sum(0);
 ```
Alternatively: ```d .filter!(pair => gcd(pair[0], pair[1]) == 1) .count; ```
it's better alternative!
```d .count!(pair => gcd(pair[0], pair[1]) == 1); ```
Apr 29
parent drug <drug2004 bk.ru> writes:
29.04.2021 15:26, Timon Gehr пишет:
 On 28.04.21 15:21, drug wrote:
 28.04.2021 15:34, Paul Backus пишет:
 On Wednesday, 28 April 2021 at 07:56:50 UTC, drug wrote:
 ```D
         .map!((a)=>(gcd(a[0], a[1]) == 1) ? 1 : 0)
         .sum(0);
 ```
Alternatively: ```d .filter!(pair => gcd(pair[0], pair[1]) == 1) .count; ```
it's better alternative!
```d .count!(pair => gcd(pair[0], pair[1]) == 1); ```
it's the best! :)
Apr 29
prev sibling parent reply Q. Schroll <qs.il.paperinik gmail.com> writes:
On Wednesday, 28 April 2021 at 12:34:51 UTC, Paul Backus wrote:
 On Wednesday, 28 April 2021 at 07:56:50 UTC, drug wrote:
 ```D
         .map!((a)=>(gcd(a[0], a[1]) == 1) ? 1 : 0)
         .sum(0);
 ```
Alternatively: ```d .filter!(pair => gcd(pair[0], pair[1]) == 1) .count; ```
```d .filter!(pair => pair.expand.gcd == 1) ``` Not sure if it's trolly or more elegant.
Apr 29
parent reply Berni44 <someone somemail.com> writes:
On Thursday, 29 April 2021 at 14:33:06 UTC, Q. Schroll wrote:
 ```d
 .filter!(pair => pair.expand.gcd == 1)
 ```

 Not sure if it's trolly or more elegant.
I fear that new users might already be swamped with functional style and won't understand what's going on here. (But nice anyway.)
Apr 29
parent reply Dennis <dkorpel gmail.com> writes:
On Thursday, 29 April 2021 at 15:53:43 UTC, Berni44 wrote:
 I fear that new users might already be swamped with functional 
 style and won't understand what's going on here. (But nice 
 anyway.)
I think the range-based functional style of D is overrepresented. When I first landed on the homepage, I saw the "Sort lines" example which scared me off. It looked totally unfamiliar, I thought D was more akin to Scala/Haskell than the Java/C-like languages I was most familiar with. Only later did I figure out that D really is a newer C/C++ like the name implies, and the range programming is just Phobos acting on top of it. In all the examples, there is not a single `if`/`do`/`while`/`for` statement, not a single `class` or `interface` declaration, and only 1 `struct` declaration. I think it wouldn't hurt to have examples showing boring/traditional code in D syntax in addition to the fancy, 'code-golfy' programs.
Apr 29
next sibling parent Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= <ola.fosheim.grostad gmail.com> writes:
On Thursday, 29 April 2021 at 17:04:20 UTC, Dennis wrote:
 When I first landed on the homepage, I saw the "Sort lines" 
 example which scared me off. It looked totally unfamiliar, I 
 thought D was more akin to Scala/Haskell than the Java/C-like 
 languages I was most familiar with. Only later did I figure out
Haha, yes, maybe it would be better to just showcase a slightly longer, but well written code example that looks familiar. Terseness does not make for good programming practice or understanding. The examples showcase code I never want to see... ( ;_; ) Like: ```d "Starting program".writeln; ``` Or this innocent looking code, by good fortune whoami isn't malicious: ``` auto result = ["whoami"].execute; ```
Apr 29
prev sibling parent WebFreak001 <d.forum webfreak.org> writes:
On Thursday, 29 April 2021 at 17:04:20 UTC, Dennis wrote:
 On Thursday, 29 April 2021 at 15:53:43 UTC, Berni44 wrote:
 I fear that new users might already be swamped with functional 
 style and won't understand what's going on here. (But nice 
 anyway.)
I think the range-based functional style of D is overrepresented. When I first landed on the homepage, I saw the "Sort lines" example which scared me off. It looked totally unfamiliar, I thought D was more akin to Scala/Haskell than the Java/C-like languages I was most familiar with. Only later did I figure out that D really is a newer C/C++ like the name implies, and the range programming is just Phobos acting on top of it. In all the examples, there is not a single `if`/`do`/`while`/`for` statement, not a single `class` or `interface` declaration, and only 1 `struct` declaration. I think it wouldn't hurt to have examples showing boring/traditional code in D syntax in addition to the fancy, 'code-golfy' programs.
I personally think it can be used very well with foreach: ```d // Estimate π using random integers void main() { import std.stdio, std.range, std.random, std.math, std.conv, std.numeric; Mt19937_64 randomGen; randomGen.seed(unpredictableSeed!ulong); int pairCount = 1_000_000; int coprimeCount = 0; foreach (randomPair; randomGen.chunks(2).take(pairCount)) { if (gcd(randomPair.front, randomPair.dropOne.front) == 1) coprimeCount++; } double prob = coprimeCount.to!double / pairCount.to!double; writefln("π ≈ %.15f", sqrt(6.0 / prob)); } ``` Though I would have liked more if the `take` amount could be a template parameter instead so it could be tuples where I can directly take [0] and [1]
Apr 29