D and Euler

I’ve been meaning to learn D (just for shits and grins) for a while now and figured I’d go through Project Euler as an excuse to play with D.

So – my first D program to solve this (mind bending) problem:

import std.stdio;

int main(char[][] args)
{
	int res = 0;
	for(int i = 0; i < 1000; i++)
	{
		res += (i % 3 == 0 || i % 5 == 0) ? i : 0;
	}
	writefln("Result: %s", res);
  	return 0;
}

D on OSX:

  • Update Ports
  • sudo port install gdc
  • Save the file above as euler1.d
  • gdc euler1.d
  • ./a.out