dmath: init directory

This commit is contained in:
Jeremy Baxter 2024-02-16 09:29:09 +13:00
parent afa80b0937
commit 84bf83a36b
7 changed files with 200 additions and 0 deletions

37
dmath/primes.d Normal file
View file

@ -0,0 +1,37 @@
import std.conv : to;
import std.stdio : write, writeln;
void
main(string[] args)
{
int min, max;
min = 1;
max = 100;
if (args.length == 2) {
max = args[1].to!int();
}
if (args.length == 3) {
min = args[1].to!int();
max = args[2].to!int();
}
foreach (int i; min .. max + 1) {
if (factorsOf(i).length == 2)
writeln(i);
}
}
int[]
factorsOf(int x)
{
int[] a;
foreach (int i; 1 .. x + 1) {
if (x % i == 0)
a ~= i;
}
return a;
}