From 84bf83a36beb498e0e60934a21f2a452a655d5da Mon Sep 17 00:00:00 2001 From: Jeremy Baxter Date: Fri, 16 Feb 2024 09:29:09 +1300 Subject: [PATCH] dmath: init directory --- .gitignore | 2 ++ dmath/README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ dmath/factors.d | 33 +++++++++++++++++++++++++++++++++ dmath/hcf.d | 36 ++++++++++++++++++++++++++++++++++++ dmath/isfactor.d | 29 +++++++++++++++++++++++++++++ dmath/lcm.d | 15 +++++++++++++++ dmath/primes.d | 37 +++++++++++++++++++++++++++++++++++++ 7 files changed, 200 insertions(+) create mode 100644 dmath/README.md create mode 100644 dmath/factors.d create mode 100644 dmath/hcf.d create mode 100644 dmath/isfactor.d create mode 100644 dmath/lcm.d create mode 100644 dmath/primes.d diff --git a/.gitignore b/.gitignore index b2be92b..c4162a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ result +.*.~undo-tree~ +*~ diff --git a/dmath/README.md b/dmath/README.md new file mode 100644 index 0000000..37334eb --- /dev/null +++ b/dmath/README.md @@ -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) diff --git a/dmath/factors.d b/dmath/factors.d new file mode 100644 index 0000000..7b87e72 --- /dev/null +++ b/dmath/factors.d @@ -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; +} diff --git a/dmath/hcf.d b/dmath/hcf.d new file mode 100644 index 0000000..f9946f8 --- /dev/null +++ b/dmath/hcf.d @@ -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; +} diff --git a/dmath/isfactor.d b/dmath/isfactor.d new file mode 100644 index 0000000..f95eba8 --- /dev/null +++ b/dmath/isfactor.d @@ -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; +} diff --git a/dmath/lcm.d b/dmath/lcm.d new file mode 100644 index 0000000..7d48ec0 --- /dev/null +++ b/dmath/lcm.d @@ -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); + } +} diff --git a/dmath/primes.d b/dmath/primes.d new file mode 100644 index 0000000..5858a22 --- /dev/null +++ b/dmath/primes.d @@ -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; +}