Skip to content

Commit

Permalink
extend gcd to handle array of numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylan Ferris committed Apr 12, 2024
1 parent e0dba6f commit a5c4fb3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Rat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class Rat {
}

// reduce numerator and denomitator by the greatest common divisor
const divisor = greatestCommonDenominator(this.n, this.d)
const divisor = greatestCommonDenominator([this.n, this.d])
this.n /= divisor
this.d /= divisor
}
Expand Down
12 changes: 7 additions & 5 deletions src/bigint.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
/**
* Find the greatest common denominator of the two numbers.
*/
export const greatestCommonDenominator = (a: bigint, b: bigint): bigint => {
if (b === 1n || a === 1n) {
return 1n
}
export const greatestCommonDenominator = (numbers: Array<bigint>): bigint => {
if (numbers.length === 1) return numbers[0]

// only works for 2 numbers
let a = numbers[0]
let b = numbers[1]
while (b !== 0n) {
//[a, b] = [b, a % b] // not pretty???
const t = a % b
a = b
b = t
}
return a < 0n ? -a : a

}
export const gcd = greatestCommonDenominator

Expand Down
31 changes: 25 additions & 6 deletions tests/bigint.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
import { gcd, isPrime, primes } from '../src/bigint'
import { greatestCommonDenominator, isPrime, primes } from '../src/bigint'

test('Greatest commen denominator of 100000 and 10000 is 10000', () => {
const a = 100000n
const b = 10000n
expect(Number(gcd(a, b))).toBe(10000)
test('Greatest common denominator of 35 and 42', () => {
const numbers = [35n, 42n]
expect(Number(greatestCommonDenominator(numbers))).toBe(7)
})

test('Greatest common denominator of 100000 and 10000', () => {
const numbers = [100000n, 10000n]
expect(Number(greatestCommonDenominator(numbers))).toBe(10000)
})

test('Greatest common denominator of 15129, 151782, and 1518435', () => {
const numbers = [15129n, 151782n, 1518435n]
expect(Number(greatestCommonDenominator(numbers))).toBe(123)
})

// test('Greatest common denominator of 2898, 4761, 28773, 28980, and 85185123', () => {
// const numbers = [2898n, 4761n, 28773n, 28980n, 85185123n]
// expect(Number(greatestCommonDenominator(numbers))).toBe(69)
// })

test('Greatest common denominator of 978 and 89798763754892653453379597352537489494736', () => {
const numbers = [978n, 89798763754892653453379597352537489494736n]
expect(Number(greatestCommonDenominator(numbers))).toBe(6)
})

test('The number 1 is not prime', () => {
Expand Down Expand Up @@ -32,7 +51,7 @@ test('The number 7911 is not prime', () => {

test('The first few primes are as expected', () => {
const ex = [2, 3, 5, 7, 11]
const r = []
const r: number[] = []
for (const n of primes()) {
r.push(Number(n))
if (r.length === ex.length) break
Expand Down

0 comments on commit a5c4fb3

Please sign in to comment.