Are there any exercises/challenges for D?
Meta via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Aug 25 18:58:25 PDT 2014
On Tuesday, 26 August 2014 at 01:57:06 UTC, Meta wrote:
> Have you heard of Project Euler? https://projecteuler.net/
>
> The problems are mostly mathematical, and once you answer you
> can compare your solution to the other solutions people have
> written in other languages. The early questions also have some
> very unique and beautiful range-based D solutions.
>
> import std.algorithm;
> import std.range;
> import std.stdio;
>
> alias fold = std.functional.binaryReverseArgs!(reduce!((n1, n2)
> => n1 + n2));
> enum limit = 4_000_000;
>
> void main()
> {
> recurrence!q{a[n-1] + a[n-2]}(1, 1)
> .take(1000)
> .filter!(n => n >= 0 && n < limit && n % 2 == 0)
> .sum.writeln;
> }
Whoops, wrong code. Also, this is the answer to PE problem 2.
import std.algorithm;
import std.range;
import std.stdio;
enum limit = 4_000_000;
void main()
{
recurrence!q{a[n-1] + a[n-2]}(1, 1)
.take(1000)
.filter!(n => n >= 0 && n < limit && n % 2 == 0)
.sum
.writeln;
}
More information about the Digitalmars-d-learn
mailing list