-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
172 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
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) |
This file contains 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
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) |
This file contains 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
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) |
This file contains 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
This file contains 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
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) |
This file contains 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
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); | ||
}); |
This file contains 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
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); | ||
} | ||
} |