Skip to content

Commit

Permalink
feat: math gcm; update vuepress doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Rain120 committed Jan 30, 2020
1 parent 013b5ca commit 066bf3c
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 25 deletions.
21 changes: 0 additions & 21 deletions deploy.sh

This file was deleted.

5 changes: 5 additions & 0 deletions docs/.vuepress/utils/sidebarHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const README_REG = /README/;
const mapper = code => {
return {
'guide': '介绍',
'math': '数学',
'factorial': '阶乘',
'fibonacci': '斐波那契数列',
'gcd': '最大公约数',
'lcm': '最小公倍数',
}[code];
}

Expand Down
9 changes: 7 additions & 2 deletions docs/zh/guide/README.md → docs/guide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

<div align="center">

[![WATCH](https://img.shields.io/github/watchers/rain120/awesome-javascript-code-implementation?style=social)](https://github.com/Rain120/awesome-javascript-code-implementation/watchers) [![STAR](https://img.shields.io/github/stars/rain120/awesome-javascript-code-implementation?style=social)](https://github.com/Rain120/awesome-javascript-code-implementation/stargazers) [![FORK](https://img.shields.io/github/forks/rain120/awesome-javascript-code-implementation?style=social)](https://github.com/Rain120/awesome-javascript-code-implementation/network/members)
[![Netlify Status](https://api.netlify.com/api/v1/badges/aa1cb15a-9a66-42df-ab3b-6cf8a607c9c4/deploy-status)](https://app.netlify.com/sites/awesome-javascript-code-implementation/deploys) [![STAR](https://img.shields.io/github/stars/rain120/awesome-javascript-code-implementation?style=social)](https://github.com/Rain120/awesome-javascript-code-implementation/stargazers) [![FORK](https://img.shields.io/github/forks/rain120/awesome-javascript-code-implementation?style=social)](https://github.com/Rain120/awesome-javascript-code-implementation/network/members)

[![TEST](https://github.com/rain120/awesome-javascript-code-implementation/workflows/.github/workflows/test.yml/badge.svg)](https://github.com/Rain120/awesome-javascript-code-implementation/actions) ![LANGUAGES](https://img.shields.io/github/languages/top/rain120/awesome-javascript-code-implementation?style=flat-square)
<!-- [![TEST](https://github.com/rain120/awesome-javascript-code-implementation/workflows/.github/workflows/test.yml/badge.svg)](https://github.com/Rain120/awesome-javascript-code-implementation/actions) -->
![LANGUAGES](https://img.shields.io/github/languages/top/rain120/awesome-javascript-code-implementation?style=flat-square)
[![VERSION](https://img.shields.io/github/package-json/v/rain120/awesome-javascript-code-implementation?style=flat-square)](https://github.com/Rain120/awesome-javascript-code-implementation/blob/master/package.json) [![LICENSE](https://img.shields.io/github/license/rain120/awesome-javascript-code-implementation?style=flat-square)](https://github.com/Rain120/awesome-javascript-code-implementation/blob/master/LICENSE) [![ISSUES](https://img.shields.io/github/issues/rain120/awesome-javascript-code-implementation?style=flat-square)](https://github.com/Rain120/awesome-javascript-code-implementation/issues) [![COMMIT](https://img.shields.io/github/last-commit/rain120/awesome-javascript-code-implementation?style=flat-square)](https://github.com/Rain120/awesome-javascript-code-implementation/commits/master)

</div>
Expand Down Expand Up @@ -118,6 +119,10 @@ We welcome all contributions. You can submit any ideas as [pull requests](https:

- [ ] Lodash

- [ ] Internal

- [x] BaseFindIndex

- [ ] 函数式编程库

- [ ] Ramda
Expand Down
20 changes: 20 additions & 0 deletions docs/math/factorial/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
### 阶乘

#### 定义

一个正整数 `n` 的阶乘 (写作 `n!`), 就是所有小于等于 `n`的正整数的乘积

**数学公式**

$$ f(0) = 1 $$

$$ f(n) = n * (n - 1) * ... * 1 $$

Eg:
```text
n! = 1 * 2 * ... * (n - 1)
```

### 参考资料

[阶乘 - wiki](https://zh.wikipedia.org/zh-cn/%E9%9A%8E%E4%B9%98)
15 changes: 15 additions & 0 deletions docs/math/fibonacci/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
### 斐波那契数列

#### 定义

**数学公式**

$$F_{0} = 0$$

$$F_{1} = 1$$

$$ F_n = F_{n - 1} + F_{n - 2} (n ≧ 2)$$

### 参考资料

[斐波那契数列 - wiki](https://zh.wikipedia.org/wiki/%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97)
23 changes: 23 additions & 0 deletions docs/math/gcd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
### 最大公约数

#### 定义

能够整除多个整数的最大正整数。而多个整数不能都为零。

求两个整数最大公约数主要的方法:

- [穷举法](https://zh.wikipedia.org/wiki/%E7%AA%AE%E8%88%89%E6%B3%95): 分别列出两整数的所有约数, 并找出最大的公约数。

- [素因数分解](https://zh.wikipedia.org/wiki/%E8%B3%AA%E5%9B%A0%E6%95%B8%E5%88%86%E8%A7%A3): 分别列出两数的素因数分解式, 并计算共同项的乘积。

- [短除法](https://zh.wikipedia.org/wiki/%E7%9F%AD%E9%99%A4%E6%B3%95): 两数除以其共同素因数, 直到两数互素时, 所有除数的乘积即为最大公约数。

- [辗转相除法](https://zh.wikipedia.org/wiki/%E8%BC%BE%E8%BD%89%E7%9B%B8%E9%99%A4%E6%B3%95): 又称欧几里得算法 `(Euclidean Algorithm)`, 两数相除, 取余数重复进行相除, 直到余数为 `0` 时, 前一个除数即为最大公约数。

**Note**

$$ gcm(a, b) * lcm(a, b) = |ab| $$

### 参考资料

[最大公约数 - wiki](https://zh.wikipedia.org/zh-cn/%E6%9C%80%E5%A4%A7%E5%85%AC%E5%9B%A0%E6%95%B8)
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"lint-staged:ts": "tslint -c ./tslint.json src/**/**.ts",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
"docs:dev": "vuepress dev docs",
"docs:build": "vuepress build docs",
"deploy": "sh deploy.sh"
"docs:build": "vuepress build docs"
},
"keywords": [
"awesome",
Expand Down
23 changes: 23 additions & 0 deletions src/Math/gcd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
### 最大公约数

#### 定义

能够整除多个整数的最大正整数。而多个整数不能都为零。

求两个整数最大公约数主要的方法:

- [穷举法](https://zh.wikipedia.org/wiki/%E7%AA%AE%E8%88%89%E6%B3%95): 分别列出两整数的所有约数, 并找出最大的公约数。

- [素因数分解](https://zh.wikipedia.org/wiki/%E8%B3%AA%E5%9B%A0%E6%95%B8%E5%88%86%E8%A7%A3): 分别列出两数的素因数分解式, 并计算共同项的乘积。

- [短除法](https://zh.wikipedia.org/wiki/%E7%9F%AD%E9%99%A4%E6%B3%95): 两数除以其共同素因数, 直到两数互素时, 所有除数的乘积即为最大公约数。

- [辗转相除法](https://zh.wikipedia.org/wiki/%E8%BC%BE%E8%BD%89%E7%9B%B8%E9%99%A4%E6%B3%95): 又称欧几里得算法 `(Euclidean Algorithm)`, 两数相除, 取余数重复进行相除, 直到余数为 `0` 时, 前一个除数即为最大公约数。

**Note**

$$ gcm(a, b) * lcm(a, b) = |ab| $$

### 参考资料

[最大公约数 - wiki](https://zh.wikipedia.org/zh-cn/%E6%9C%80%E5%A4%A7%E5%85%AC%E5%9B%A0%E6%95%B8)
24 changes: 24 additions & 0 deletions src/Math/gcd/__tests__.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* @Author: Rainy
* @Date: 2020-01-30 11:42:57
* @LastEditors : Rainy
* @LastEditTime : 2020-01-30 13:49:59
*/

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

test('gcd_enumeration(5, 10) should be 5', () => {
expect(gcd_enumeration(5, 10)).toEqual(5);
});

test('gcd_division_recursive(5, 10) should be 5', () => {
expect(gcd_division_recursive(5, 10)).toEqual(5);
});

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);
});
54 changes: 54 additions & 0 deletions src/Math/gcd/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* @Author: Rainy
* @Date: 2020-01-30 11:42:57
* @LastEditors : Rainy
* @LastEditTime : 2020-01-30 14:18:01
*/

export function gcd_enumeration(a: number, b: number): number {
let gcd = 1;

let smaller = Math.min(a, b);

for (let i = 2; i <= smaller; i++) {
if ((a % i === 0) && (b % i === 0)) {
gcd = i;
}
}

return gcd;
}

export function gcd_division_recursive(a: number, b: number): number {
if (b === 0) {
return a;
}

return gcd_division_recursive(b , a % b);
}

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

return a > b ? gcd_sub_recursive(a - b, b) : gcd_sub_recursive(a, b - a);
}

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

if ((a & 1) === 0 && (b & 1) === 0) {
return gcd_best(a >> 1, b >> 1);
} else if ((a & 1) === 0 && (b & 1) !== 0) {
return gcd_best(a >> 1, b);
} else if ((a & 1) !== 0 && (b & 1) === 0) {
return gcd_best(a, b >> 1);
} else {
const max = Math.max(a, b);
const min = Math.min(a, b);
return gcd_best(max - min, min);
}
}

0 comments on commit 066bf3c

Please sign in to comment.