Last active
December 12, 2016 08:16
-
-
Save mildfuzz/c5e64b634e6aa61006751a1a8b746650 to your computer and use it in GitHub Desktop.
Factorise integers. Use the list of factors to find highest and lowest common denominators
This file contains hidden or 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
| function factors(x: number): number[] { | |
| const f = []; | |
| for(let i = 2; (i*i) <= x; i++) { | |
| if(x % i === 0) { | |
| f.push(i); | |
| } | |
| } | |
| return f; | |
| } | |
| function lowComDom(x: number, y: number): number { | |
| const xF: number[] = factors(x); | |
| const yF: number[] = factors(y); | |
| return xF.find( i => yF.indexOf(i) >= 0) | |
| } | |
| function highComDom(x: number, y: number): number { | |
| const xF: number[] = factors(x); | |
| const yF: number[] = factors(y); | |
| return xF.reverse()find( i => yF.indexOf(i) >= 0); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment