From a8e500e0e80adfd1e0051921b7b065f39c0d1a0f Mon Sep 17 00:00:00 2001 From: khashayar Date: Sun, 14 Jun 2026 02:56:34 -0700 Subject: [PATCH 01/11] refactor(math): prevent overflow in ceil by avoiding integer casting --- src/math/ceil.d | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/math/ceil.d b/src/math/ceil.d index 77c796b..e3e0dd0 100644 --- a/src/math/ceil.d +++ b/src/math/ceil.d @@ -1,20 +1,21 @@ import std; -/** - * Returns the smallest (closest to negative infinity) - * - * @param number the number - * @return the smallest (closest to negative infinity) of given - * {@code number} -*/ +/// Calculates the ceiling of a number. +/// Returns the smallest integer value greater than or equal to the given number. +/// +/// Params: +/// number = The floating-point number to process. +/// Returns: +/// The rounded-up value as a double to prevent integer overflow. -double ceil(double number) { - if (number - cast(int) number == 0) { - return number; - } else if (number - cast(int) number > 0) { - return cast(int)(number + 1); +double ceil(double number){ + double rem = number % 1; + if( number > 0 && rem != 0) { + return number - rem + 1; + } else if (number < 0 && rem != 0) { + return number - rem; } else { - return cast(int) number; + return number; } } @@ -23,6 +24,10 @@ unittest { assert(ceil(10.2) == 11); assert(ceil(9) == 9); assert(ceil(12.6) == 13); + + assert(ceil(-10.2) == -10); + assert(ceil(-12.6) == -12); + assert(ceil(-5.0) == -5); } From 2242c9ac1ee0a82078e354b967e619eaec36315c Mon Sep 17 00:00:00 2001 From: khashayar Date: Sun, 14 Jun 2026 02:57:48 -0700 Subject: [PATCH 02/11] (math): add floor algorithm with negative number support and unittests --- src/math/floor.d | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/math/floor.d diff --git a/src/math/floor.d b/src/math/floor.d new file mode 100644 index 0000000..b51a4b6 --- /dev/null +++ b/src/math/floor.d @@ -0,0 +1,33 @@ +module math.floor; + + +/// Calculates the floor of a number. +/// Returns the largest integer value less than or equal to the given number. +/// +/// Params: +/// x = The floating-point number to process. +/// Returns: +/// The rounded-down value as a double to prevent integer overflow. + +double floor(double x) { + double rem = x % 1; + if( x > 0 && rem != 0) { + return x - rem; + } else if (x < 0 && rem != 0) { + return x - rem - 1; + } else { + return x; + } +} + + + + +unittest { + assert(floor(3.5) == 3); + assert(floor(-3.5) == -4); + assert(floor(3.0) == 3); + assert(floor(-3.0) == -3); +} + +void main() {} \ No newline at end of file From 8f030f97a9bf8b4cc812f740024343ef3307af87 Mon Sep 17 00:00:00 2001 From: khashayar Date: Sun, 14 Jun 2026 02:59:16 -0700 Subject: [PATCH 03/11] (math): add round algorithm --- src/math/round.d | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/math/round.d diff --git a/src/math/round.d b/src/math/round.d new file mode 100644 index 0000000..c60da2f --- /dev/null +++ b/src/math/round.d @@ -0,0 +1,39 @@ +module math.round; + +/// Rounds a floating-point number to the nearest integer value. +/// Halfway cases (0.5 or -0.5) are rounded away from zero. +/// +/// Params: +/// x = The floating-point number to process. +/// Returns: +/// The rounded value as a double to prevent integer overflow. + +double round(double x){ + double rem = x % 1; + if (rem >= 0.5 && x > 0){ + return x - rem + 1; + }else if (rem < 0.5 && x > 0){ + return x - rem; + }else if (rem > -0.5 && x < 0){ + return x - rem; + }else if (rem <= -0.5 && x < 0){ + return x - rem - 1; + } else { + return x; + } +} + + +unittest{ + assert(round(10.2) == 10); + assert(round(9) == 9); + assert(round(12.6) == 13); + + assert(round(-10.2) == -10); + assert(round(-12.6) == -13); + assert(round(-5.0) == -5); +} + +void main(){ + +} \ No newline at end of file From 84a9ae6547aab60f325b634226b4ce3a2fc74f89 Mon Sep 17 00:00:00 2001 From: khashayar Date: Sun, 14 Jun 2026 03:27:15 -0700 Subject: [PATCH 04/11] (math): add gcd algorithm --- src/math/gcd.d | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/math/gcd.d diff --git a/src/math/gcd.d b/src/math/gcd.d new file mode 100644 index 0000000..396c64f --- /dev/null +++ b/src/math/gcd.d @@ -0,0 +1,32 @@ +module math.gcd; + +/// Calculates the Greatest Common Divisor (GCD) of two integers. +/// Uses the efficient iterative Euclidean algorithm. +/// +/// Params: +/// a = The first integer. +/// b = The second integer. +/// Returns: +/// The greatest common divisor of a and b as a long. + + +long gcd(long a, long b) { + if (a == 0 || b == 0) { + return 0; + } + while (b != 0) { + long temp = b; + b = a % b; + a = temp; + } + return a; +} + +unittest { + assert(gcd(48, 18) == 6); + assert(gcd(56, 98) == 14); + assert(gcd(101, 10) == 1); + assert(gcd(54, 24) == 6); +} + +void main(){} \ No newline at end of file From 3866baa4e2b5bf75d952bc9b053d1908373b211c Mon Sep 17 00:00:00 2001 From: khashayar Date: Sun, 14 Jun 2026 03:27:44 -0700 Subject: [PATCH 05/11] (math): add lcm algorithm --- src/math/lcm.d | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/math/lcm.d diff --git a/src/math/lcm.d b/src/math/lcm.d new file mode 100644 index 0000000..848e391 --- /dev/null +++ b/src/math/lcm.d @@ -0,0 +1,35 @@ +module math.lcm; + +/// Calculates the Least Common Multiple (LCM) of two integers. +/// Relies on the relationship between GCD and LCM to find the result. +/// +/// Params: +/// a = The first integer. +/// b = The second integer. +/// Returns: +/// The least common multiple of a and b as a long. + +long lcm(long a , long b){ + if (a == 0 || b == 0) { + return 0; + } + if(a < 0) a = -a; + if(b < 0) b = -b; + + long result = a * b; + while (b != 0) { + long temp = b; + b = a % b; + a = temp; + } + return result / a; +} + +unittest { + assert(lcm(4, 6) == 12); + assert(lcm(21, 6) == 42); + assert(lcm(8, 9) == 72); + assert(lcm(15, 20) == 60); +} + +void main() {} \ No newline at end of file From 4a77389def6dc2852c83a44f9ba3a5dd7f2103eb Mon Sep 17 00:00:00 2001 From: khashayar Date: Sun, 14 Jun 2026 03:28:09 -0700 Subject: [PATCH 06/11] (math): add is_prime algorithm --- src/math/is_prime.d | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/math/is_prime.d diff --git a/src/math/is_prime.d b/src/math/is_prime.d new file mode 100644 index 0000000..13b8159 --- /dev/null +++ b/src/math/is_prime.d @@ -0,0 +1,31 @@ +module math.is_prime; + +/// Checks whether a given integer is a prime number. +/// Utilizes trial division up to the square root of the number for efficiency. +/// +/// Params: +/// n = The integer to check. +/// Returns: +/// true if the number is prime, false otherwise. + +bool is_prime(int n){ + if(n <= 1) return false; + if(n == 2) return true; + if(n % 2 == 0) return false; + for(int i = 3; i * i <= n; i += 2){ + if(n % i == 0) return false; + } + return true; +} +unittest { + assert(is_prime(2) == true); + assert(is_prime(3) == true); + assert(is_prime(4) == false); + assert(is_prime(5) == true); + assert(is_prime(10) == false); + assert(is_prime(13) == true); + assert(is_prime(17) == true); + assert(is_prime(20) == false); +} + +void main() {} \ No newline at end of file From e22736dfc52f6cb80000e00474b9cc23e82fec11 Mon Sep 17 00:00:00 2001 From: khashayar Date: Sun, 14 Jun 2026 03:34:34 -0700 Subject: [PATCH 07/11] (math): add sieve of eratosthenes for mass prime generation --- src/math/prime_sieve.d | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/math/prime_sieve.d diff --git a/src/math/prime_sieve.d b/src/math/prime_sieve.d new file mode 100644 index 0000000..392ea6b --- /dev/null +++ b/src/math/prime_sieve.d @@ -0,0 +1,41 @@ +module math.prime_sieve; + +/// Generates all prime numbers up to a given limit N using the Sieve of Eratosthenes. +/// This algorithm is highly efficient for finding small primes en masse. +/// +/// Params: +/// n = The upper bound limit (inclusive) to find primes. +/// Returns: +/// An array of integers containing all prime numbers up to n. + +int[] prime_sieve(int n){ + if(n < 2) return []; + bool[] is_prime = new bool[n + 1]; + is_prime[] = true; + is_prime[0] = false; + is_prime[1] = false; + for(int i = 2; i * i <= n; i++){ + if(is_prime[i]){ + for(int j = i * i; j <= n; j += i){ + is_prime[j] = false; + } + } + } + int[] primes = []; + for(int i = 2; i <= n; i++){ + if(is_prime[i]){ + primes ~= i; + } + } + return primes; +} + +unittest { + assert(prime_sieve(10) == [2, 3, 5, 7]); + assert(prime_sieve(20) == [2, 3, 5, 7, 11, 13, 17, 19]); + assert(prime_sieve(1) == []); + assert(prime_sieve(2) == [2]); +} + +void main() {} + From d340d6b06a409fc98d35195c4f0b9dc19140abce Mon Sep 17 00:00:00 2001 From: khashayar Date: Sun, 14 Jun 2026 09:00:23 -0700 Subject: [PATCH 08/11] style(math): fix docs format and remove module declarations --- .gitignore | 1 + src/math/ceil.d | 18 +++++++++++------- src/math/floor.d | 22 ++++++++++------------ src/math/gcd.d | 24 +++++++++++++----------- src/math/is_prime.d | 20 +++++++++++--------- src/math/lcm.d | 24 ++++++++++++++---------- src/math/prime_sieve.d | 19 ++++++++++--------- src/math/round.d | 21 +++++++++++---------- 8 files changed, 81 insertions(+), 68 deletions(-) diff --git a/.gitignore b/.gitignore index 74b926f..1ea206a 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ docs/ # Code coverage *.lst +.aider* diff --git a/src/math/ceil.d b/src/math/ceil.d index e3e0dd0..a26287e 100644 --- a/src/math/ceil.d +++ b/src/math/ceil.d @@ -1,13 +1,17 @@ + import std; -/// Calculates the ceiling of a number. -/// Returns the smallest integer value greater than or equal to the given number. -/// -/// Params: -/// number = The floating-point number to process. -/// Returns: -/// The rounded-up value as a double to prevent integer overflow. +/** + * Calculates the ceiling of a number. + * Returns the smallest integer value greater than or equal to the given number. + * + * Params: + * number = The floating-point number to process. + * Returns: + * The rounded-up value as a double to prevent integer overflow. + */ + double ceil(double number){ double rem = number % 1; if( number > 0 && rem != 0) { diff --git a/src/math/floor.d b/src/math/floor.d index b51a4b6..81e8d45 100644 --- a/src/math/floor.d +++ b/src/math/floor.d @@ -1,14 +1,12 @@ -module math.floor; - - -/// Calculates the floor of a number. -/// Returns the largest integer value less than or equal to the given number. -/// -/// Params: -/// x = The floating-point number to process. -/// Returns: -/// The rounded-down value as a double to prevent integer overflow. - +/** + * Calculates the floor of a number. + * Returns the largest integer value less than or equal to the given number. + * + * Params: + * x = The floating-point number to process. + * Returns: + * The rounded-down value as a double to prevent integer overflow. + */ double floor(double x) { double rem = x % 1; if( x > 0 && rem != 0) { @@ -30,4 +28,4 @@ unittest { assert(floor(-3.0) == -3); } -void main() {} \ No newline at end of file +void main() {} diff --git a/src/math/gcd.d b/src/math/gcd.d index 396c64f..e9eac81 100644 --- a/src/math/gcd.d +++ b/src/math/gcd.d @@ -1,15 +1,17 @@ -module math.gcd; - -/// Calculates the Greatest Common Divisor (GCD) of two integers. -/// Uses the efficient iterative Euclidean algorithm. -/// -/// Params: -/// a = The first integer. -/// b = The second integer. -/// Returns: -/// The greatest common divisor of a and b as a long. +import std; +/** + * Calculates the Greatest Common Divisor (GCD) of two integers. + * Uses the efficient iterative Euclidean algorithm. + * + * Params: + * a = The first integer. + * b = The second integer. + * Returns: + * The greatest common divisor of a and b as a long. + */ + long gcd(long a, long b) { if (a == 0 || b == 0) { return 0; @@ -29,4 +31,4 @@ unittest { assert(gcd(54, 24) == 6); } -void main(){} \ No newline at end of file +void main(){} diff --git a/src/math/is_prime.d b/src/math/is_prime.d index 13b8159..211b72e 100644 --- a/src/math/is_prime.d +++ b/src/math/is_prime.d @@ -1,12 +1,14 @@ -module math.is_prime; +import std; -/// Checks whether a given integer is a prime number. -/// Utilizes trial division up to the square root of the number for efficiency. -/// -/// Params: -/// n = The integer to check. -/// Returns: -/// true if the number is prime, false otherwise. +/** + * Checks whether a given integer is a prime number. + * Utilizes trial division up to the square root of the number for efficiency. + * + * Params: + * n = The integer to check. + * Returns: + * true if the number is prime, false otherwise. + */ bool is_prime(int n){ if(n <= 1) return false; @@ -28,4 +30,4 @@ unittest { assert(is_prime(20) == false); } -void main() {} \ No newline at end of file +void main() {} diff --git a/src/math/lcm.d b/src/math/lcm.d index 848e391..ff0e9d1 100644 --- a/src/math/lcm.d +++ b/src/math/lcm.d @@ -1,14 +1,18 @@ -module math.lcm; -/// Calculates the Least Common Multiple (LCM) of two integers. -/// Relies on the relationship between GCD and LCM to find the result. -/// -/// Params: -/// a = The first integer. -/// b = The second integer. -/// Returns: -/// The least common multiple of a and b as a long. +import std; +/** + * Calculates the Least Common Multiple (LCM) of two integers. + * Relies on the relationship between GCD and LCM to find the result. + * + * Params: + * a = The first integer. + * b = The second integer. + * Returns: + * The least common multiple of a and b as a long. + */ + + long lcm(long a , long b){ if (a == 0 || b == 0) { return 0; @@ -32,4 +36,4 @@ unittest { assert(lcm(15, 20) == 60); } -void main() {} \ No newline at end of file +void main() {} diff --git a/src/math/prime_sieve.d b/src/math/prime_sieve.d index 392ea6b..81827e7 100644 --- a/src/math/prime_sieve.d +++ b/src/math/prime_sieve.d @@ -1,13 +1,14 @@ -module math.prime_sieve; - -/// Generates all prime numbers up to a given limit N using the Sieve of Eratosthenes. -/// This algorithm is highly efficient for finding small primes en masse. -/// -/// Params: -/// n = The upper bound limit (inclusive) to find primes. -/// Returns: -/// An array of integers containing all prime numbers up to n. +import std; +/** + * Generates all prime numbers up to a given limit N using the Sieve of Eratosthenes. + * This algorithm is highly efficient for finding small primes en masse. + * + * Params: + * n = The upper bound limit (inclusive) to find primes. + * Returns: + * An array of integers containing all prime numbers up to n. + */ int[] prime_sieve(int n){ if(n < 2) return []; bool[] is_prime = new bool[n + 1]; diff --git a/src/math/round.d b/src/math/round.d index c60da2f..d8be729 100644 --- a/src/math/round.d +++ b/src/math/round.d @@ -1,13 +1,14 @@ -module math.round; - -/// Rounds a floating-point number to the nearest integer value. -/// Halfway cases (0.5 or -0.5) are rounded away from zero. -/// -/// Params: -/// x = The floating-point number to process. -/// Returns: -/// The rounded value as a double to prevent integer overflow. +import std; +/** + * Rounds a floating-point number to the nearest integer value. + * Halfway cases (0.5 or -0.5) are rounded away from zero. + * + * Params: + * x = The floating-point number to process. + * Returns: + * The rounded value as a double to prevent integer overflow. + */ double round(double x){ double rem = x % 1; if (rem >= 0.5 && x > 0){ @@ -36,4 +37,4 @@ unittest{ void main(){ -} \ No newline at end of file +} From bbc14d6f1c2811dfcf3596479c8a55aa27a74bf7 Mon Sep 17 00:00:00 2001 From: khashayar Date: Sun, 14 Jun 2026 10:52:55 -0700 Subject: [PATCH 09/11] comment(math): update comment style --- src/math/ceil.d | 16 ++++++---------- src/math/floor.d | 14 ++++++-------- src/math/gcd.d | 19 ++++++++----------- src/math/is_prime.d | 15 +++++++-------- src/math/lcm.d | 18 +++++++----------- src/math/prime_sieve.d | 17 +++++++++-------- src/math/round.d | 16 ++++++++-------- 7 files changed, 51 insertions(+), 64 deletions(-) diff --git a/src/math/ceil.d b/src/math/ceil.d index a26287e..f22cf66 100644 --- a/src/math/ceil.d +++ b/src/math/ceil.d @@ -1,17 +1,13 @@ import std; -/** - * Calculates the ceiling of a number. - * Returns the smallest integer value greater than or equal to the given number. - * - * Params: - * number = The floating-point number to process. - * Returns: - * The rounded-up value as a double to prevent integer overflow. +/* + * Ceiling Calculation - Computes the smallest integer value that is greater than or equal to a given floating-point number. + * 1) Calculate the remainder when the input number is divided by 1. This isolates the fractional part. + * 2) If the input number is positive and has a non-zero remainder, it means we need to round up. This is achieved by subtracting the remainder and adding 1 to the original number. + * 3) If the input number is negative and has a non-zero remainder, the ceiling is the number itself without its fractional part. This is achieved by subtracting the remainder. + * 4) If the input number is already an integer (remainder is zero), it is returned as is. */ - - double ceil(double number){ double rem = number % 1; if( number > 0 && rem != 0) { diff --git a/src/math/floor.d b/src/math/floor.d index 81e8d45..12e93d0 100644 --- a/src/math/floor.d +++ b/src/math/floor.d @@ -1,11 +1,9 @@ -/** - * Calculates the floor of a number. - * Returns the largest integer value less than or equal to the given number. - * - * Params: - * x = The floating-point number to process. - * Returns: - * The rounded-down value as a double to prevent integer overflow. +/* + * Floor Calculation - Computes the largest integer value that is less than or equal to a given floating-point number. + * 1) Calculate the remainder when the input number is divided by 1. This isolates the fractional part. + * 2) If the input number is positive and has a non-zero remainder, the floor is the number without its fractional part. This is achieved by subtracting the remainder. + * 3) If the input number is negative and has a non-zero remainder, we need to round down to the next lower integer. This is achieved by subtracting the remainder and then subtracting an additional 1. + * 4) If the input number is already an integer (remainder is zero), it is returned as is. */ double floor(double x) { double rem = x % 1; diff --git a/src/math/gcd.d b/src/math/gcd.d index e9eac81..aec948a 100644 --- a/src/math/gcd.d +++ b/src/math/gcd.d @@ -1,17 +1,14 @@ import std; -/** - * Calculates the Greatest Common Divisor (GCD) of two integers. - * Uses the efficient iterative Euclidean algorithm. - * - * Params: - * a = The first integer. - * b = The second integer. - * Returns: - * The greatest common divisor of a and b as a long. +/* + * Greatest Common Divisor (GCD) Calculation - Computes the largest positive integer that divides two integers without leaving a remainder. This implementation uses the iterative Euclidean algorithm. + * 1) Handle the base case: if either input integer is 0, the GCD is defined as 0. + * 2) Initiate a loop that continues as long as the second integer (b) is not zero. + * 3) Inside the loop, store the current value of 'b' in a temporary variable 'temp'. + * 4) Update 'b' to be the remainder of 'a' divided by 'b' (a % b). + * 5) Update 'a' to be the value stored in 'temp'. This effectively replaces 'a' with the previous 'b', and 'b' with the remainder. + * 6) When the loop terminates (meaning 'b' has become 0), the value of 'a' holds the GCD of the original two numbers. */ - - long gcd(long a, long b) { if (a == 0 || b == 0) { return 0; diff --git a/src/math/is_prime.d b/src/math/is_prime.d index 211b72e..105b2c0 100644 --- a/src/math/is_prime.d +++ b/src/math/is_prime.d @@ -1,13 +1,12 @@ import std; -/** - * Checks whether a given integer is a prime number. - * Utilizes trial division up to the square root of the number for efficiency. - * - * Params: - * n = The integer to check. - * Returns: - * true if the number is prime, false otherwise. +/* + * Prime Check - A comparison-based algorithm to determine if an integer is a prime number. + * 1) First, we check the base cases: any number less than or equal to 1 is immediately classified as not prime. + * 2) The number 2 is evaluated next, as it is the only even prime number in mathematics. + * 3) All other even numbers are excluded right away by checking if they are divisible by 2. + * 4) For remaining odd numbers, we iterate through odd factors starting from 3 up to the square root of the number (i * i <= n). + * 5) If any factor divides the number perfectly, it is classified as composite; otherwise, it is confirmed as a prime number. */ bool is_prime(int n){ diff --git a/src/math/lcm.d b/src/math/lcm.d index ff0e9d1..62ffdd4 100644 --- a/src/math/lcm.d +++ b/src/math/lcm.d @@ -1,18 +1,14 @@ import std; -/** - * Calculates the Least Common Multiple (LCM) of two integers. - * Relies on the relationship between GCD and LCM to find the result. - * - * Params: - * a = The first integer. - * b = The second integer. - * Returns: - * The least common multiple of a and b as a long. +/* + * Least Common Multiple (LCM) Calculation - Computes the smallest positive integer that is divisible by both of two given integers. This implementation uses the relationship LCM(a, b) = |a * b| / GCD(a, b). + * 1) Handle the base case: if either input integer is 0, the LCM is defined as 0. + * 2) Ensure both input numbers 'a' and 'b' are positive by taking their absolute values if they are negative. + * 3) Calculate the product of 'a' and 'b' and store it in 'result'. This will be used later. + * 4) Employ the Euclidean algorithm to find the Greatest Common Divisor (GCD) of 'a' and 'b'. This involves repeatedly taking the remainder until 'b' becomes 0, at which point 'a' holds the GCD. + * 5) Divide the pre-calculated product ('result') by the GCD found in the previous step. This division yields the LCM. */ - - long lcm(long a , long b){ if (a == 0 || b == 0) { return 0; diff --git a/src/math/prime_sieve.d b/src/math/prime_sieve.d index 81827e7..acc186d 100644 --- a/src/math/prime_sieve.d +++ b/src/math/prime_sieve.d @@ -1,13 +1,14 @@ import std; -/** - * Generates all prime numbers up to a given limit N using the Sieve of Eratosthenes. - * This algorithm is highly efficient for finding small primes en masse. - * - * Params: - * n = The upper bound limit (inclusive) to find primes. - * Returns: - * An array of integers containing all prime numbers up to n. +/* + * Prime Sieve - Generates all prime numbers up to a specified limit N using the Sieve of Eratosthenes algorithm. + * 1) Handle the edge case: if N is less than 2, return an empty array as there are no primes below 2. + * 2) Initialize a boolean array 'is_prime' of size N+1, marking all entries as true, assuming all numbers are potentially prime initially. + * 3) Explicitly mark 0 and 1 as not prime by setting their corresponding entries in 'is_prime' to false. + * 4) Iterate from i = 2 up to the square root of N. For each 'i': + * a) If 'is_prime[i]' is true (meaning 'i' is considered prime), then mark all multiples of 'i' (starting from i*i) up to N as not prime by setting their 'is_prime' entries to false. + * 5) After the sieving process, iterate through the 'is_prime' array from 2 to N. + * 6) Collect all numbers 'i' for which 'is_prime[i]' is true into a result array. These are the prime numbers up to N. */ int[] prime_sieve(int n){ if(n < 2) return []; diff --git a/src/math/round.d b/src/math/round.d index d8be729..7f01135 100644 --- a/src/math/round.d +++ b/src/math/round.d @@ -1,13 +1,13 @@ import std; -/** - * Rounds a floating-point number to the nearest integer value. - * Halfway cases (0.5 or -0.5) are rounded away from zero. - * - * Params: - * x = The floating-point number to process. - * Returns: - * The rounded value as a double to prevent integer overflow. +/* + * Rounding Function - Rounds a floating-point number to the nearest integer. Numbers exactly halfway between two integers are rounded away from zero. + * 1) Calculate the remainder when the input number is divided by 1. This isolates the fractional part. + * 2) If the fractional part is 0.5 or greater and the number is positive, round up by subtracting the remainder and adding 1. + * 3) If the fractional part is less than 0.5 and the number is positive, round down by simply subtracting the remainder. + * 4) If the fractional part is greater than -0.5 (i.e., between -0.499... and 0) and the number is negative, round towards zero by subtracting the remainder. + * 5) If the fractional part is -0.5 or less and the number is negative, round down (away from zero) by subtracting the remainder and then subtracting an additional 1. + * 6) If the number is already an integer (remainder is zero), it is returned as is. */ double round(double x){ double rem = x % 1; From 42cd5bca796a6249b50f6afb4aec205a5e28c0b2 Mon Sep 17 00:00:00 2001 From: khashayar Date: Sun, 14 Jun 2026 12:03:37 -0700 Subject: [PATCH 10/11] update comments. --- src/math/ceil.d | 8 ++++---- src/math/floor.d | 8 ++++---- src/math/gcd.d | 12 ++++++------ src/math/is_prime.d | 10 +++++----- src/math/lcm.d | 10 +++++----- src/math/prime_sieve.d | 14 +++++++------- src/math/round.d | 16 +++++++--------- 7 files changed, 38 insertions(+), 40 deletions(-) diff --git a/src/math/ceil.d b/src/math/ceil.d index f22cf66..9f1c7c2 100644 --- a/src/math/ceil.d +++ b/src/math/ceil.d @@ -3,10 +3,10 @@ import std; /* * Ceiling Calculation - Computes the smallest integer value that is greater than or equal to a given floating-point number. - * 1) Calculate the remainder when the input number is divided by 1. This isolates the fractional part. - * 2) If the input number is positive and has a non-zero remainder, it means we need to round up. This is achieved by subtracting the remainder and adding 1 to the original number. - * 3) If the input number is negative and has a non-zero remainder, the ceiling is the number itself without its fractional part. This is achieved by subtracting the remainder. - * 4) If the input number is already an integer (remainder is zero), it is returned as is. + * 1) @param rem = for get remainder of number when divided by 1 + * 2) in first if = if number is Positive and rem not zero(it has remainder) then number - rem + 1 (2.2 - 0.2 + 1) + * 3) in second if = if number is Negative and rem not zero(it has remainder) then number - rem (2.2 - 0.2) + * 4) if number is already an integer then return the number as is */ double ceil(double number){ double rem = number % 1; diff --git a/src/math/floor.d b/src/math/floor.d index 12e93d0..ab55a97 100644 --- a/src/math/floor.d +++ b/src/math/floor.d @@ -1,9 +1,9 @@ /* * Floor Calculation - Computes the largest integer value that is less than or equal to a given floating-point number. - * 1) Calculate the remainder when the input number is divided by 1. This isolates the fractional part. - * 2) If the input number is positive and has a non-zero remainder, the floor is the number without its fractional part. This is achieved by subtracting the remainder. - * 3) If the input number is negative and has a non-zero remainder, we need to round down to the next lower integer. This is achieved by subtracting the remainder and then subtracting an additional 1. - * 4) If the input number is already an integer (remainder is zero), it is returned as is. + * 1) @param rem = for get remainder of number when divided by 1 + * 2) in first if = if number is Positive and rem not zero(it has remainder) then number - rem + * 3) in second if = if number is Negative and rem not zero(it has remainder) then number - rem - 1 + * 4) if number is already an integer then return the number as is */ double floor(double x) { double rem = x % 1; diff --git a/src/math/gcd.d b/src/math/gcd.d index aec948a..5486617 100644 --- a/src/math/gcd.d +++ b/src/math/gcd.d @@ -2,12 +2,12 @@ import std; /* * Greatest Common Divisor (GCD) Calculation - Computes the largest positive integer that divides two integers without leaving a remainder. This implementation uses the iterative Euclidean algorithm. - * 1) Handle the base case: if either input integer is 0, the GCD is defined as 0. - * 2) Initiate a loop that continues as long as the second integer (b) is not zero. - * 3) Inside the loop, store the current value of 'b' in a temporary variable 'temp'. - * 4) Update 'b' to be the remainder of 'a' divided by 'b' (a % b). - * 5) Update 'a' to be the value stored in 'temp'. This effectively replaces 'a' with the previous 'b', and 'b' with the remainder. - * 6) When the loop terminates (meaning 'b' has become 0), the value of 'a' holds the GCD of the original two numbers. + * 1) if either input int is 0, the GCD is defined as 0. + * 2) This loop continues until b is zero. + * 3) @param temp = store the value of b temporarily before updating b to a % b. + * 4) b = a % b = update b to the remainder of a divided by b, which is the key step in the Euclidean algorithm. + * 5) a = temp = update a to the previous value of b. + * 6) in the end return a which is the GCD of the original pair of integers. */ long gcd(long a, long b) { if (a == 0 || b == 0) { diff --git a/src/math/is_prime.d b/src/math/is_prime.d index 105b2c0..e9409a4 100644 --- a/src/math/is_prime.d +++ b/src/math/is_prime.d @@ -2,11 +2,11 @@ import std; /* * Prime Check - A comparison-based algorithm to determine if an integer is a prime number. - * 1) First, we check the base cases: any number less than or equal to 1 is immediately classified as not prime. - * 2) The number 2 is evaluated next, as it is the only even prime number in mathematics. - * 3) All other even numbers are excluded right away by checking if they are divisible by 2. - * 4) For remaining odd numbers, we iterate through odd factors starting from 3 up to the square root of the number (i * i <= n). - * 5) If any factor divides the number perfectly, it is classified as composite; otherwise, it is confirmed as a prime number. + * 1) first if input n is one return false because 1 is not a prime number. + * 2) second if input n is two return true because 2 is a prime number. + * 3) third if input n is even and greater than 2 return false because all even numbers greater than 2 are not prime. + * 4) start a loop from 3 to the square root of n, incrementing by 2 (to check only odd numbers). If n is divisible by any of these numbers, return false because it means n has a divisor other than 1 and itself. + * 5) in the end return true because if n is not divisible by any number from 2 to the square root of n, it is a prime number. */ bool is_prime(int n){ diff --git a/src/math/lcm.d b/src/math/lcm.d index 62ffdd4..8f2ff66 100644 --- a/src/math/lcm.d +++ b/src/math/lcm.d @@ -3,11 +3,11 @@ import std; /* * Least Common Multiple (LCM) Calculation - Computes the smallest positive integer that is divisible by both of two given integers. This implementation uses the relationship LCM(a, b) = |a * b| / GCD(a, b). - * 1) Handle the base case: if either input integer is 0, the LCM is defined as 0. - * 2) Ensure both input numbers 'a' and 'b' are positive by taking their absolute values if they are negative. - * 3) Calculate the product of 'a' and 'b' and store it in 'result'. This will be used later. - * 4) Employ the Euclidean algorithm to find the Greatest Common Divisor (GCD) of 'a' and 'b'. This involves repeatedly taking the remainder until 'b' becomes 0, at which point 'a' holds the GCD. - * 5) Divide the pre-calculated product ('result') by the GCD found in the previous step. This division yields the LCM. + * 1) In the first condition, if any of the inputs (a, b) are zero, return 0. + * 2) if any of inputs are negative number, we take the absolute value of that. + * 3) @param result = to store the output and multiply two inputs + * 4) in the While loop, we take GCD of the two inputs + * 5) in the end, we multiply the two inputs, divide by their GCD, and return this. */ long lcm(long a , long b){ if (a == 0 || b == 0) { diff --git a/src/math/prime_sieve.d b/src/math/prime_sieve.d index acc186d..995017f 100644 --- a/src/math/prime_sieve.d +++ b/src/math/prime_sieve.d @@ -2,13 +2,13 @@ import std; /* * Prime Sieve - Generates all prime numbers up to a specified limit N using the Sieve of Eratosthenes algorithm. - * 1) Handle the edge case: if N is less than 2, return an empty array as there are no primes below 2. - * 2) Initialize a boolean array 'is_prime' of size N+1, marking all entries as true, assuming all numbers are potentially prime initially. - * 3) Explicitly mark 0 and 1 as not prime by setting their corresponding entries in 'is_prime' to false. - * 4) Iterate from i = 2 up to the square root of N. For each 'i': - * a) If 'is_prime[i]' is true (meaning 'i' is considered prime), then mark all multiples of 'i' (starting from i*i) up to N as not prime by setting their 'is_prime' entries to false. - * 5) After the sieving process, iterate through the 'is_prime' array from 2 to N. - * 6) Collect all numbers 'i' for which 'is_prime[i]' is true into a result array. These are the prime numbers up to N. + * 1) in the first, we check that the input cannot be less than 2 because it is not a prime number + * 2) @param is_prime = we create a boolean array(One of larger ones from input(n)) To save valid outputs. + * 3) first we set the entire array to true. + * 4) we set members 0 and 1 to false Because 0 , 1 are not prime + * 5) in the loop, we check whether the current number is prime or not, and if not, saved in is_prime array + * 6) @param primes = to store prime numbers for return + * 7) in the end, checking values inside out validator array and added in primes array, return primes. */ int[] prime_sieve(int n){ if(n < 2) return []; diff --git a/src/math/round.d b/src/math/round.d index 7f01135..f74e061 100644 --- a/src/math/round.d +++ b/src/math/round.d @@ -2,12 +2,12 @@ import std; /* * Rounding Function - Rounds a floating-point number to the nearest integer. Numbers exactly halfway between two integers are rounded away from zero. - * 1) Calculate the remainder when the input number is divided by 1. This isolates the fractional part. - * 2) If the fractional part is 0.5 or greater and the number is positive, round up by subtracting the remainder and adding 1. - * 3) If the fractional part is less than 0.5 and the number is positive, round down by simply subtracting the remainder. - * 4) If the fractional part is greater than -0.5 (i.e., between -0.499... and 0) and the number is negative, round towards zero by subtracting the remainder. - * 5) If the fractional part is -0.5 or less and the number is negative, round down (away from zero) by subtracting the remainder and then subtracting an additional 1. - * 6) If the number is already an integer (remainder is zero), it is returned as is. + * 1) @param rem = for get remainder of number when divided by 1 + * 2) in first if = if number is positive and fractional part >= 0.5, round up by subtracting rem and adding 1 + * 3) in second if = if number is positive and fractional part < 0.5, round down by subtracting rem + * 4) in third if = if number is negative and fractional part > -0.5, round towards zero by subtracting rem + * 5) in fourth if = if number is negative and fractional part <= -0.5, round away from zero by subtracting rem and 1 + * 6) else = if number is already an integer, return the number as is */ double round(double x){ double rem = x % 1; @@ -35,6 +35,4 @@ unittest{ assert(round(-5.0) == -5); } -void main(){ - -} +void main(){} From d106eb6f67a9d9aacb3919fb48e00760edc63b2e Mon Sep 17 00:00:00 2001 From: khashayar Date: Mon, 15 Jun 2026 11:41:13 -0700 Subject: [PATCH 11/11] Fix Comments, Use Ddoc Style --- src/math/ceil.d | 29 +++++++++++++++++++---------- src/math/floor.d | 22 ++++++++++++++++------ src/math/gcd.d | 25 +++++++++++++++++-------- src/math/is_prime.d | 21 ++++++++++++++------- src/math/lcm.d | 24 +++++++++++++++++------- src/math/prime_sieve.d | 24 ++++++++++++++++-------- src/math/round.d | 24 +++++++++++++++++------- 7 files changed, 116 insertions(+), 53 deletions(-) diff --git a/src/math/ceil.d b/src/math/ceil.d index 9f1c7c2..fbe1c93 100644 --- a/src/math/ceil.d +++ b/src/math/ceil.d @@ -1,21 +1,30 @@ import std; -/* - * Ceiling Calculation - Computes the smallest integer value that is greater than or equal to a given floating-point number. - * 1) @param rem = for get remainder of number when divided by 1 - * 2) in first if = if number is Positive and rem not zero(it has remainder) then number - rem + 1 (2.2 - 0.2 + 1) - * 3) in second if = if number is Negative and rem not zero(it has remainder) then number - rem (2.2 - 0.2) - * 4) if number is already an integer then return the number as is +/** + * Computes the smallest integer value that is greater than or equal to a given floating-point number. + * + * ### Step-by-Step: + * 1. `rem` = for get remainder of number when divided by 1. + * 2. In first if = if number is Positive and `rem != 0` (it has remainder) then `number - rem + 1`. + * 3. In second if = if number is Negative and `rem != 0` (it has remainder) then `number - rem`. + * 4. If number is already an integer then return the number as is. + * + * Params: + * number = The floating-point value to evaluate. + * + * Returns: + * If input has no remainder, it returns `number`. Otherwise it removes remaining part, adds one to `number` and returns it. */ + double ceil(double number){ double rem = number % 1; - if( number > 0 && rem != 0) { + if( number > 0 && rem != 0) { return number - rem + 1; - } else if (number < 0 && rem != 0) { + } else if (number < 0 && rem != 0) { return number - rem; - } else { - return number; + } else { + return number; } } diff --git a/src/math/floor.d b/src/math/floor.d index ab55a97..532366b 100644 --- a/src/math/floor.d +++ b/src/math/floor.d @@ -1,10 +1,20 @@ -/* - * Floor Calculation - Computes the largest integer value that is less than or equal to a given floating-point number. - * 1) @param rem = for get remainder of number when divided by 1 - * 2) in first if = if number is Positive and rem not zero(it has remainder) then number - rem - * 3) in second if = if number is Negative and rem not zero(it has remainder) then number - rem - 1 - * 4) if number is already an integer then return the number as is + +/** + * Computes the largest integer value that is less than or equal to a given floating-point number. + * + * ### Step-by-Step: + * 1. `rem` = for get remainder of number when divided by 1. + * 2. In first if = if number is Positive and `rem != 0` (it has remainder) then `x - rem`. + * 3. In second if = if number is Negative and `rem != 0` (it has remainder) then `x - rem - 1`. + * 4. If number is already an integer then return the number as is. + * + * Params: + * x = The floating-point value to evaluate. + * + * Returns: + * If input has no remainder, return `x`. Otherwise if number positive remove remaining and returns it, if negative, subtract one and return it. */ + double floor(double x) { double rem = x % 1; if( x > 0 && rem != 0) { diff --git a/src/math/gcd.d b/src/math/gcd.d index 5486617..d5b1811 100644 --- a/src/math/gcd.d +++ b/src/math/gcd.d @@ -1,14 +1,23 @@ import std; -/* - * Greatest Common Divisor (GCD) Calculation - Computes the largest positive integer that divides two integers without leaving a remainder. This implementation uses the iterative Euclidean algorithm. - * 1) if either input int is 0, the GCD is defined as 0. - * 2) This loop continues until b is zero. - * 3) @param temp = store the value of b temporarily before updating b to a % b. - * 4) b = a % b = update b to the remainder of a divided by b, which is the key step in the Euclidean algorithm. - * 5) a = temp = update a to the previous value of b. - * 6) in the end return a which is the GCD of the original pair of integers. +/** + * Greatest Common Divisor (GCD) Calculation - Computes the largest positive integer that divides two integers without leaving a remainder. + * + * ### Step-by-Step: + * 1. If either input `a` or `b` is 0, return 0. + * 2. Loop continues until `b` is zero. + * 3. `temp` = store the value of `b` temporarily before update `b` to `a % b`. + * 4. `b = a % b` = update `b` to remainder of `a` divided by `b`. + * 5. `a = temp` = update `a` to previous value of `b`. + * + * Params: + * a = The first integer. + * b = The second integer. + * + * Returns: + * In the end return `a` which is the GCD of the original pair of integers. */ + long gcd(long a, long b) { if (a == 0 || b == 0) { return 0; diff --git a/src/math/is_prime.d b/src/math/is_prime.d index e9409a4..972985b 100644 --- a/src/math/is_prime.d +++ b/src/math/is_prime.d @@ -1,12 +1,19 @@ import std; -/* - * Prime Check - A comparison-based algorithm to determine if an integer is a prime number. - * 1) first if input n is one return false because 1 is not a prime number. - * 2) second if input n is two return true because 2 is a prime number. - * 3) third if input n is even and greater than 2 return false because all even numbers greater than 2 are not prime. - * 4) start a loop from 3 to the square root of n, incrementing by 2 (to check only odd numbers). If n is divisible by any of these numbers, return false because it means n has a divisor other than 1 and itself. - * 5) in the end return true because if n is not divisible by any number from 2 to the square root of n, it is a prime number. +/** + * Prime Check - An algorithm to determine if an integer is a prime number. + * + * ### Step-by-Step: + * 1. First if input `n <= 1` return false because it is not prime number. + * 2. Second if input `n == 2` return true because 2 is a prime number. + * 3. Third if input `n` is even return false because even numbers greater than 2 are not prime. + * 4. Start a loop from 3 to square root of `n`, increment by 2. If `n` is divisible by any of these, return false. + * + * Params: + * n = The integer to check. + * + * Returns: + * In the end return true if `n` is not divisible by any number from 2 to square root of `n`. */ bool is_prime(int n){ diff --git a/src/math/lcm.d b/src/math/lcm.d index 8f2ff66..dc9b260 100644 --- a/src/math/lcm.d +++ b/src/math/lcm.d @@ -1,14 +1,24 @@ import std; -/* - * Least Common Multiple (LCM) Calculation - Computes the smallest positive integer that is divisible by both of two given integers. This implementation uses the relationship LCM(a, b) = |a * b| / GCD(a, b). - * 1) In the first condition, if any of the inputs (a, b) are zero, return 0. - * 2) if any of inputs are negative number, we take the absolute value of that. - * 3) @param result = to store the output and multiply two inputs - * 4) in the While loop, we take GCD of the two inputs - * 5) in the end, we multiply the two inputs, divide by their GCD, and return this. +/** + * Least Common Multiple (LCM) Calculation - Computes the smallest positive integer that is divisible by both of two given integers. + * + * ### Step-by-Step: + * 1. If any of inputs `a` or `b` are zero, return 0. + * 2. If any of inputs are negative number, we take absolute value of that. + * 3. `result` = store output and multiply two inputs. + * 4. In while loop, we take GCD of the two inputs. + * + * Params: + * a = The first integer. + * b = The second integer. + * + * Returns: + * In the end, multiply two inputs, divide by their GCD, and return this result. */ + + long lcm(long a , long b){ if (a == 0 || b == 0) { return 0; diff --git a/src/math/prime_sieve.d b/src/math/prime_sieve.d index 995017f..0267e22 100644 --- a/src/math/prime_sieve.d +++ b/src/math/prime_sieve.d @@ -1,15 +1,23 @@ import std; -/* +/** * Prime Sieve - Generates all prime numbers up to a specified limit N using the Sieve of Eratosthenes algorithm. - * 1) in the first, we check that the input cannot be less than 2 because it is not a prime number - * 2) @param is_prime = we create a boolean array(One of larger ones from input(n)) To save valid outputs. - * 3) first we set the entire array to true. - * 4) we set members 0 and 1 to false Because 0 , 1 are not prime - * 5) in the loop, we check whether the current number is prime or not, and if not, saved in is_prime array - * 6) @param primes = to store prime numbers for return - * 7) in the end, checking values inside out validator array and added in primes array, return primes. + * + * ### Step-by-Step: + * 1. In the first, we check that the input cannot be less than 2 because it is not a prime number. + * 2. We create a boolean array `is_prime` (size `n + 1`) to save valid outputs. + * 3. First we set the entire array to true. + * 4. We set members 0 and 1 to false because 0 and 1 are not prime. + * 5. In the loop, we check whether the current number is prime or not, and if not, saved in `is_prime` array. + * + * Params: + * n = The upper bound limit. + * + * Returns: + * In the end, checking values inside our validator array and added in `primes` array, return `primes`. */ + + int[] prime_sieve(int n){ if(n < 2) return []; bool[] is_prime = new bool[n + 1]; diff --git a/src/math/round.d b/src/math/round.d index f74e061..aa4cfab 100644 --- a/src/math/round.d +++ b/src/math/round.d @@ -1,14 +1,24 @@ import std; -/* +/** * Rounding Function - Rounds a floating-point number to the nearest integer. Numbers exactly halfway between two integers are rounded away from zero. - * 1) @param rem = for get remainder of number when divided by 1 - * 2) in first if = if number is positive and fractional part >= 0.5, round up by subtracting rem and adding 1 - * 3) in second if = if number is positive and fractional part < 0.5, round down by subtracting rem - * 4) in third if = if number is negative and fractional part > -0.5, round towards zero by subtracting rem - * 5) in fourth if = if number is negative and fractional part <= -0.5, round away from zero by subtracting rem and 1 - * 6) else = if number is already an integer, return the number as is + * + * ### Step-by-Step: + * 1. `rem` = for get remainder of number when divided by 1. + * 2. In first if = if number is positive and fractional part `>= 0.5`, round up by subtracting `rem` and adding 1. + * 3. In second if = if number is positive and fractional part `< 0.5`, round down by subtracting `rem`. + * 4. In third if = if number is negative and fractional part `> -0.5`, round towards zero by subtracting `rem`. + * 5. In fourth if = if number is negative and fractional part `<= -0.5`, round away from zero by subtracting `rem` and 1. + * 6. Else = if number is already an integer, return the number as is. + * + * Params: + * x = The floating-point value to round. + * + * Returns: + * The rounded integer value according to the step conditions. */ + + double round(double x){ double rem = x % 1; if (rem >= 0.5 && x > 0){