forked from tangweikun/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
24 lines (19 loc) · 747 Bytes
/
index.ts
File metadata and controls
24 lines (19 loc) · 747 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { greatestCommonDivisor } from '../greatestCommonDivisor'
export function fractionalSimplify(numerator: number, denominator: number) {
if (numerator === 0 || denominator === 0) return [0]
const absNumerator = Math.abs(numerator)
const absDenominator = Math.abs(denominator)
const SIGN = Math.sign(numerator) * Math.sign(denominator)
const REMINDER = absNumerator % absDenominator
const QUOTIENT = absNumerator / absDenominator
if (REMINDER === 0) return [SIGN * QUOTIENT]
const GREATEST_COMMON_DIVISOR = greatestCommonDivisor(
absNumerator,
absDenominator,
)
return [
SIGN * Math.floor(QUOTIENT) || 0,
SIGN * (REMINDER / GREATEST_COMMON_DIVISOR),
absDenominator / GREATEST_COMMON_DIVISOR,
]
}