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

2
.gitignore vendored
View file

@ -1 +1,3 @@
result
.*.~undo-tree~
*~

48
dmath/README.md Normal file
View file

@ -0,0 +1,48 @@
D programs that calculate various mathematical values
## factors.d - calculate the factors of a number
To work out the factors of 31:
ldc2 -run factors.d 31
## isfactor.d - determine whether a number is a factor of another
To determine whether 8 is a factor of 32:
ldc2 -run isfactor.d 8 32
## hcf.d - calculate the highest common factor of two numbers
To work out the HCF of 12 and 28:
ldc2 -run hcf.d 12 28
To keep it simple this program will write out a list of factors
for each number side-by-side.
## lcm.d - calculate the lowest common multiple of two numbers
To work out the LCM of 28 and 42:
ldc2 -run lcm.d 28 42
To keep it simple this program will write out a list of multiples
for each number side-by-side.
## primes.d - generate a list of prime numbers up to a maximum value
To generate a list of primes up to 100:
ldc2 -run primes.d
If an argument is supplied, the program will calculate primes up to that number:
ldc2 -run primes.d 10
(outputs 2, 3, 5, and 7)
Two arguments can be supplied to specify a minimum as well:
ldc2 -run primes.d 1 20
(outputs primes from 1 to 20)

33
dmath/factors.d Normal file
View 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;
}

36
dmath/hcf.d Normal file
View file

@ -0,0 +1,36 @@
import std.conv : to;
import std.stdio : write, writeln;
void
main(string[] args)
{
int i, x, y;
int[] xf, yf;
x = args[1].to!int();
y = args[2].to!int();
xf = factorsOf(x);
yf = factorsOf(y);
for (i = 0; xf.length > i || yf.length > i; i++) {
if (i < xf.length)
write(xf[i]);
write(" ");
if (i < yf.length)
write(yf[i]);
writeln();
}
}
int[]
factorsOf(int x)
{
int[] a;
foreach (int i; 1 .. x + 1) {
if (x % i == 0)
a ~= i;
}
return a;
}

29
dmath/isfactor.d Normal file
View file

@ -0,0 +1,29 @@
import std.conv : to;
import std.stdio : stderr, writeln;
int
main(string[] args)
{
int f, i, n;
if (args.length < 3) {
stderr.writeln("usage: isfactor.d fac num");
return 1;
}
f = args[1].to!int();
n = args[2].to!int();
i = f;
while (i < n) {
i += f;
}
if (i == n) {
writeln(f, " is a factor of ", n);
return 0;
}
writeln(f, " is NOT a factor of ", n);
return 1;
}

15
dmath/lcm.d Normal file
View file

@ -0,0 +1,15 @@
import std.conv : to;
import std.stdio : writeln;
void
main(string[] args)
{
int x, y;
x = args[1].to!int();
y = args[2].to!int();
foreach (int i; 1 .. 12) {
writeln(x * i, " ", y * i);
}
}

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;
}