dmath: init directory
This commit is contained in:
parent
afa80b0937
commit
84bf83a36b
7 changed files with 200 additions and 0 deletions
33
dmath/factors.d
Normal file
33
dmath/factors.d
Normal file
|
@ -0,0 +1,33 @@
|
|||
import std.conv : to;
|
||||
import std.stdio : stderr, write, writeln;
|
||||
|
||||
int
|
||||
main(string[] args)
|
||||
{
|
||||
int n;
|
||||
|
||||
if (args.length == 1) {
|
||||
stderr.writeln("usage: factors.d number");
|
||||
return 1;
|
||||
}
|
||||
|
||||
n = args[1].to!int();
|
||||
foreach (int factor; factorsOf(n)) {
|
||||
writeln(factor);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int[]
|
||||
factorsOf(int x)
|
||||
{
|
||||
int[] a;
|
||||
|
||||
foreach (int i; 1 .. x + 1) {
|
||||
if (x % i == 0)
|
||||
a ~= i;
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue