Skip to content

Commit

Permalink
feat: lcm
Browse files Browse the repository at this point in the history
  • Loading branch information
Rain120 committed Jan 31, 2020
1 parent ab7c056 commit 2a32d29
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 14 deletions.
8 changes: 4 additions & 4 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ module.exports = {
anchor: {
permalink: true,
},
// toc: {
// includeLevel: [1, 2],
// },
config: md => {
toc: {
includeLevel: [1, 2],
},
extendMarkdown: md => {
md.use(require('markdown-it-task-lists'));
md.use(require('markdown-it-imsize'), { autofill: true });
md.set({ html: true });
Expand Down
8 changes: 4 additions & 4 deletions src/Math/gcd/__tests__.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* @Author: Rainy
* @Date: 2020-01-30 11:42:57
* @LastEditors : Rainy
* @LastEditTime : 2020-01-30 13:49:59
* @LastEditTime : 2020-01-31 13:28:09
*/

import { gcd_enumeration, gcd_division_recursive, gcd_sub_recursive, gcd_best, } from '.';
import { gcd_enumeration, gcd_division_recursive, gcd_sub_recursive, gcd_optimal, } from '.';

test('gcd_enumeration(5, 10) should be 5', () => {
expect(gcd_enumeration(5, 10)).toEqual(5);
Expand All @@ -19,6 +19,6 @@ test('gcd_sub_recursive(5, 10) should be 5', () => {
expect(gcd_sub_recursive(5, 10)).toEqual(5);
});

test('gcd_best(5, 10) should be 5', () => {
expect(gcd_best(5, 10)).toEqual(5);
test('gcd_optimal(5, 10) should be 5', () => {
expect(gcd_optimal(5, 10)).toEqual(5);
});
13 changes: 7 additions & 6 deletions src/Math/gcd/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
* @Author: Rainy
* @Date: 2020-01-30 11:42:57
* @LastEditors : Rainy
* @LastEditTime : 2020-01-30 14:18:01
* @LastEditTime : 2020-01-31 13:28:43
*/

// the greatest common divisor
export function gcd_enumeration(a: number, b: number): number {
let gcd = 1;

Expand Down Expand Up @@ -35,20 +36,20 @@ export function gcd_sub_recursive(a: number, b: number): number {
return a > b ? gcd_sub_recursive(a - b, b) : gcd_sub_recursive(a, b - a);
}

export function gcd_best(a: number, b: number): number {
export function gcd_optimal(a: number, b: number): number {
if (a === b) {
return a;
}

if ((a & 1) === 0 && (b & 1) === 0) {
return gcd_best(a >> 1, b >> 1);
return gcd_optimal(a >> 1, b >> 1);
} else if ((a & 1) === 0 && (b & 1) !== 0) {
return gcd_best(a >> 1, b);
return gcd_optimal(a >> 1, b);
} else if ((a & 1) !== 0 && (b & 1) === 0) {
return gcd_best(a, b >> 1);
return gcd_optimal(a, b >> 1);
} else {
const max = Math.max(a, b);
const min = Math.min(a, b);
return gcd_best(max - min, min);
return gcd_optimal(max - min, min);
}
}
13 changes: 13 additions & 0 deletions src/Math/lcm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### 最小公倍数

#### 定义

若有一个数 `X`, 可以被另外两个数 `A``B` 整除, 且 `X` 大于(或等于) `A``B`, 则 `X``A``B` 的公倍数。 `A``B` 的公倍数有无限个, 而所有的公倍数中, 最小的公倍数就叫做最小公倍数。

**Note**

$$ lcm(a, b) = \frac{gcm(a, b)}{|a * b|} $$

### 参考资料

[最小公倍数 - wiki](https://zh.wikipedia.org/zh-cn/%E6%9C%80%E5%B0%8F%E5%85%AC%E5%80%8D%E6%95%B8)
12 changes: 12 additions & 0 deletions src/Math/lcm/__tests__.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* @Author: Rainy
* @Date: 2020-01-30 11:42:57
* @LastEditors : Rainy
* @LastEditTime : 2020-01-31 13:23:08
*/

import { lcm } from '.';

test('lcm(5, 10) should be 10', () => {
expect(lcm(5, 10)).toEqual(10);
});
13 changes: 13 additions & 0 deletions src/Math/lcm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* @Author: Rainy
* @Date: 2020-01-31 12:33:43
* @LastEditors : Rainy
* @LastEditTime : 2020-01-31 13:25:56
*/

import { gcd_optimal } from '../gcd';

// the least common multiple
export function lcm(a: number, b: number): number {
return ((a === 0) || (b === 0)) ? 0 : Math.abs(a * b) / gcd_optimal(a, b);
}

0 comments on commit 2a32d29

Please sign in to comment.