-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from ieedan/new-blocks
- Loading branch information
Showing
8 changed files
with
119 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import * as math from '.'; | ||
|
||
describe('simplify', () => { | ||
it('simplifies fractions correctly', () => { | ||
expect(math.fractions.simplify(1920, 1080)).toStrictEqual([16, 9]); | ||
// like in usage | ||
expect(math.fractions.simplify(1920, 1080).join(':')).toBe('16:9'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { gcf } from './gcf'; | ||
|
||
/** Simplifies the fraction | ||
* | ||
* @param numerator | ||
* @param denominator | ||
* @returns | ||
* | ||
* ## Usage | ||
* ```ts | ||
* simplify(1920, 1080).join(":"); // 16:9 | ||
* ``` | ||
*/ | ||
const simplify = (numerator: number, denominator: number): [number, number] => { | ||
const factor = gcf(numerator, denominator); | ||
|
||
return [numerator / factor, denominator / factor]; | ||
}; | ||
|
||
export { simplify }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import * as math from '.'; | ||
|
||
describe('gcf', () => { | ||
it('solves GCF correctly', () => { | ||
expect(math.gcf(1920, 1080)).toBe(120); | ||
expect(math.gcf(-1920, 1080)).toBe(120); | ||
expect(math.gcf(1080, 1920)).toBe(120); | ||
expect(math.gcf(2, 2)).toBe(2); | ||
}); | ||
}); | ||
|
||
describe('gcd', () => { | ||
it('solves GCD correctly', () => { | ||
expect(math.gcd(1920, 1080)).toBe(120); | ||
expect(math.gcd(-1920, 1080)).toBe(120); | ||
expect(math.gcd(1080, 1920)).toBe(120); | ||
expect(math.gcd(2, 2)).toBe(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** Solves the GCF (Greatest Common Factor) using the **Euclidean Algorithm** | ||
* | ||
* @param a | ||
* @param b | ||
* @returns | ||
* | ||
* ## Usage | ||
* ```ts | ||
* gcf(1920, 1080); // 120 | ||
* gcf(2, 2); // 2 | ||
* ``` | ||
*/ | ||
const gcf = (a: number, b: number): number => { | ||
// if they are negative we really just want the same thing | ||
let num1: number = Math.abs(a); | ||
let num2: number = Math.abs(b); | ||
|
||
while (num1 !== num2) { | ||
if (num1 > num2) { | ||
num1 -= num2; | ||
} else { | ||
num2 -= num1; | ||
} | ||
} | ||
|
||
return num1; | ||
}; | ||
|
||
/** Solves the GCD (Greatest Common Divisor) using the **Euclidean Algorithm** (Alternate alias of `gcf`) | ||
* | ||
* @param a | ||
* @param b | ||
* @returns | ||
* | ||
* ## Usage | ||
* ```ts | ||
* gcd(1920, 1080); // 120 | ||
* gcd(2, 2); // 2 | ||
* ``` | ||
*/ | ||
const gcd = gcf; | ||
|
||
export { gcf, gcd }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import * as fractions from './fractions'; | ||
import { gcd, gcf } from './gcf'; | ||
|
||
export { gcf, gcd, fractions }; |