forked from trekhleb/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add chinesre translation. (trekhleb#615)
* Add chinesre translation of math/bits README.md * Add chinese translation of math/fibonacii README.md * Add chinesre translation of math/prime-factors README.md * fix * fix Co-authored-by: zackdk <[email protected]> Co-authored-by: Oleksii Trekhleb <[email protected]>
- Loading branch information
1 parent
89a8614
commit 848dc35
Showing
6 changed files
with
302 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
# 位运算 | ||
|
||
_Read this in other languages:_ | ||
[français](README.fr-FR.md), | ||
[english](README.md) | ||
|
||
#### Get Bit | ||
|
||
该方法向右移动目标位到最右边,即位数组的第0个位置上。然后在该数上与形如 `0001`的二进制形式的数进行`ADD`操作。这会清理掉除了目标位的所有其它位的数据。如果目标位是1,那么结果就是`1`,反之,结果是`0`; | ||
|
||
> 查看[getBit.js](getBit.js)了解更多细节。 | ||
#### Set Bit | ||
|
||
该方法把`1`向左移动了`bitPosition`位,生成了一个二进制形如`00100`的值。然后我们拿该值与目标数字进行`OR`操作,就能把目标位设置位`1`而不影响其它位。 | ||
|
||
> 查看[setBit.js](setBit.js)了解更多细节。 | ||
#### Clear Bit | ||
|
||
该方法把`1`向左移动了`bitPosition`位,生成了一个二进制形如`00100`的值。然后反转每一位的数字,得到一个二进制形如`11011`的值。接着与目标值进行`ADD`操作,就能清除掉目标位的值。 | ||
|
||
> 查看[clearBit.js](clearBit.js)了解更多细节。 | ||
#### Update Bit | ||
|
||
该方法组合了“Clear Bit”和“Set Bit” | ||
|
||
> 查看[updateBit.js](updateBit.js)了解更多细节。 | ||
#### isEven | ||
|
||
该方法检测传入的number是否是偶数。它的实现基于奇数的最右边的位永远是`1`这个事实。 | ||
|
||
```text | ||
Number: 5 = 0b0101 | ||
isEven: false | ||
Number: 4 = 0b0100 | ||
isEven: true | ||
``` | ||
|
||
> 查看[isEven.js](isEven.js)了解更多细节。 | ||
#### isPositive | ||
|
||
该方法检测传入的number是否是正数。它的实现基于正数最左边的位永远是`0`这个事实。然而如果传入的number是0或者-0,它也应该返回false。 | ||
|
||
```text | ||
Number: 1 = 0b0001 | ||
isPositive: true | ||
Number: -1 = -0b0001 | ||
isPositive: false | ||
``` | ||
|
||
> 查看[isPositive.js](isPositive.js)了解更多细节。 | ||
#### Multiply By Two | ||
|
||
该方法将原始数字向左移动一位。因此所有位都将乘以2,因此数字本身也将乘以2。 | ||
|
||
``` | ||
Before the shift | ||
Number: 0b0101 = 5 | ||
Powers of two: 0 + 2^2 + 0 + 2^0 | ||
After the shift | ||
Number: 0b1010 = 10 | ||
Powers of two: 2^3 + 0 + 2^1 + 0 | ||
``` | ||
|
||
> 查看[multiplyByTwo.js](multiplyByTwo.js)了解更多细节。 | ||
#### Divide By Two | ||
|
||
该方法将原始数字向右移动一位。因此所有位都将除以2,因此数字本身也将除以2,且不会产生余数。 | ||
|
||
``` | ||
Before the shift | ||
Number: 0b0101 = 5 | ||
Powers of two: 0 + 2^2 + 0 + 2^0 | ||
After the shift | ||
Number: 0b0010 = 2 | ||
Powers of two: 0 + 0 + 2^1 + 0 | ||
``` | ||
|
||
> 查看[divideByTwo.js](divideByTwo.js)了解更多细节。 | ||
#### Switch Sign | ||
|
||
该方法将正数变成负数,反之亦然。为了做到这一点,它使用了“二进制补码”的方法,即取反所有位然后加1. | ||
|
||
``` | ||
1101 -3 | ||
1110 -2 | ||
1111 -1 | ||
0000 0 | ||
0001 1 | ||
0010 2 | ||
0011 3 | ||
``` | ||
|
||
> 查看[switchSign.js](switchSign.js)了解更多细节。 | ||
#### Multiply Two Signed Numbers | ||
|
||
该方法使用位运算符计算两个有符号数的乘积。实现基于以下事实: | ||
|
||
```text | ||
a * b 可以被改写成如下形式: | ||
0 a为0,b为0,或者a,b都为0 | ||
2a * (b/2) b是偶数 | ||
2a * (b - 1)/2 + a b是奇数,正数 | ||
2a * (b + 1)/2 - a b是奇数,负数 | ||
``` | ||
|
||
这样转换的优势在于,递归的每一步,递归的操作数的值都减少了一半。因此,运行时的时间复杂度为`O(log(b))`,其中b是在每个递归步骤上减少为一半的操作数。 | ||
|
||
|
||
> 查看[multiply.js](multiply.js)了解更多细节。 | ||
#### Multiply Two Unsigned Numbers | ||
|
||
该方法使用位运算符计算两个无符号数的乘积。实现基于“每个数字都可以表示为一系列2的幂的和”。 | ||
|
||
逐位乘法的主要思想是,每个数字都可以拆分为两个乘方的和: | ||
|
||
比如: | ||
|
||
```text | ||
19 = 2^4 + 2^1 + 2^0 | ||
``` | ||
|
||
然后`19`乘`x`就等价于: | ||
|
||
```text | ||
x * 19 = x * 2^4 + x * 2^1 + x * 2^0 | ||
``` | ||
|
||
接着我们应该意识到`x*2^4`是等价于`x`向左移动`4`位(`x << 4`)的; | ||
|
||
> 查看[multiplyUnsigned.js](multiplyUnsigned.js)了解更多细节。 | ||
#### Count Set Bits | ||
|
||
该方法使用位运算符对一个数字里设置为`1`的位进行记数。主要方法是,把数字每次向右移动1位,然后使用`&`操作符取出最右边一位的值,`1`则记数加1,`0`则不计。 | ||
|
||
```text | ||
Number: 5 = 0b0101 | ||
Count of set bits = 2 | ||
``` | ||
|
||
> 查看[countSetBits.js](countSetBits.js)了解更多细节。 | ||
#### Count Bits to Flip One Number to Another | ||
|
||
|
||
该方法输出把一个数字转换为另一个数字所需要转换的位数。这利用了以下特性:当数字进行`XOR`异或运算时,结果将是不同位数的数量(即异或的结果中所有被设置为1的位的数量)。 | ||
|
||
``` | ||
5 = 0b0101 | ||
1 = 0b0001 | ||
Count of Bits to be Flipped: 1 | ||
``` | ||
|
||
> 查看[bitsDiff.js](bitsDiff.js)了解更多细节。 | ||
#### Count Bits of a Number | ||
|
||
为了计算数字的有效位数,我们需要把`1`每次向左移动一位,然后检查产生的值是否大于输入的数字。 | ||
|
||
``` | ||
5 = 0b0101 | ||
有效位数: 3 | ||
当我们把1向左移动4位的时候,会大于5. | ||
``` | ||
|
||
> 查看[bitLength.js](bitLength.js)了解更多细节。 | ||
#### Is Power of Two | ||
|
||
该方法检测数字是否可以表示为2的幂。它使用了以下特性,我们定义`powerNumber`是可以写成2的幂的形式的数(2,4,8,16 etc.)。然后我们会把`powerNumber`和`powerNumber - 1`进行`&`操作,它会返回`0`(如果该数字可以表示为2的幂)。 | ||
|
||
``` | ||
Number: 4 = 0b0100 | ||
Number: 3 = (4 - 1) = 0b0011 | ||
4 & 3 = 0b0100 & 0b0011 = 0b0000 <-- Equal to zero, is power of two. | ||
Number: 10 = 0b01010 | ||
Number: 9 = (10 - 1) = 0b01001 | ||
10 & 9 = 0b01010 & 0b01001 = 0b01000 <-- Not equal to zero, not a power of two. | ||
``` | ||
|
||
> 查看[isPowerOfTwo.js](isPowerOfTwo.js)了解更多细节。 | ||
#### Full Adder | ||
|
||
该方法使用位运算符计算两个数的和。 | ||
|
||
它实现了[完整的加法器](<https://en.wikipedia.org/wiki/Adder_(electronics)>)电子电路逻辑,以补码的形式计算两个32位数字的和。它使用布尔逻辑来覆盖了两个位相加的所有情况:从前一位相加的时候,产没产生进位“carry bit”。 | ||
|
||
Legend: | ||
|
||
- `A`: 数字 `A` | ||
- `B`: 数字 `B` | ||
- `ai`: 数字`A`以二进制表示时的位下标 | ||
- `bi`: 数字`B`以二进制表示时的位下标 | ||
- `carryIn`: 本次计算产生的进位 | ||
- `carryOut`: 带入此次计算的进位 | ||
- `bitSum`: `ai`, `bi`, 和 `carryIn` 的和 | ||
- `resultBin`: 当前计算的结果(二进制形式) | ||
- `resultDec`: 当前计算的结果(十进制形式) | ||
|
||
``` | ||
A = 3: 011 | ||
B = 6: 110 | ||
┌──────┬────┬────┬─────────┬──────────┬─────────┬───────────┬───────────┐ | ||
│ bit │ ai │ bi │ carryIn │ carryOut │ bitSum │ resultBin │ resultDec │ | ||
├──────┼────┼────┼─────────┼──────────┼─────────┼───────────┼───────────┤ | ||
│ 0 │ 1 │ 0 │ 0 │ 0 │ 1 │ 1 │ 1 │ | ||
│ 1 │ 1 │ 1 │ 0 │ 1 │ 0 │ 01 │ 1 │ | ||
│ 2 │ 0 │ 1 │ 1 │ 1 │ 0 │ 001 │ 1 │ | ||
│ 3 │ 0 │ 0 │ 1 │ 0 │ 1 │ 1001 │ 9 │ | ||
└──────┴────┴────┴─────────┴──────────┴─────────┴───────────┴───────────┘ | ||
``` | ||
|
||
> 查看[fullAdder.js](fullAdder.js)了解更多细节。 | ||
> 查看[Full Adder on YouTube](https://www.youtube.com/watch?v=wvJc9CZcvBc&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8). | ||
## References | ||
|
||
- [Bit Manipulation on YouTube](https://www.youtube.com/watch?v=NLKQEOgBAnw&t=0s&index=28&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8) | ||
- [Negative Numbers in binary on YouTube](https://www.youtube.com/watch?v=4qH4unVtJkE&t=0s&index=30&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8) | ||
- [Bit Hacks on stanford.edu](https://graphics.stanford.edu/~seander/bithacks.html) |
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,22 @@ | ||
# 斐波那契数 | ||
|
||
_Read this in other languages:_ | ||
[français](README.fr-FR.md), | ||
[english](README.md). | ||
|
||
在数学中,斐波那契数是以下整数序列(称为斐波那契数列)中的数字,其特征在于前两个数字之后的每个数字都是前两个数字的和: | ||
|
||
`0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...` | ||
|
||
边长为连续斐波纳契数的正方形平铺 | ||
|
||
 | ||
|
||
|
||
斐波那契螺旋:通过绘制连接斐波那契平铺中正方形的相对角的圆弧而创建的金色螺旋的近似值; [4]该三角形使用大小为1、1、2、3、5、8、13和21的正方形。 | ||
|
||
 | ||
|
||
## References | ||
|
||
- [Wikipedia](https://en.wikipedia.org/wiki/Fibonacci_number) |
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,38 @@ | ||
# 质数因子 | ||
|
||
_Read this in other languages:_ | ||
[english](README.md). | ||
|
||
**质数** 是一个比 `1` 大的整数,且 **不能**由其它整数相乘得出。前几个质数是: `2`, `3`, `5`, `7`, `11`, `13`, `17`, `19`,依此类推。 | ||
|
||
如果我们**能**通过其它整数相乘得出,我们则称它为**合数** | ||
|
||
 | ||
|
||
_Image source: [Math is Fun](https://www.mathsisfun.com/prime-factorization.html)_ | ||
|
||
|
||
**质数因子**是那些相乘得到原始数的[质数](https://en.wikipedia.org/wiki/Prime_number)。例如`39`的质数因子是`3`和`13`,`15`的质数因子是`3`和`5`。 | ||
|
||
 | ||
|
||
_Image source: [Math is Fun](https://www.mathsisfun.com/prime-factorization.html)_ | ||
|
||
## 正确计算所有的质数因子及其数量 | ||
|
||
这个方法将自然数`n`从`i = 2`除到`i = n`(仅按质数索引)。且每次循环后`n`的值被`(n / i)`的值替换。 | ||
|
||
在最坏的情况下,即循环从`i = 2`执行到 `i = n`,上述方法的时间复杂度为`O(n)`。时间复杂度其实可以从`O(n)`减少到`O(sqrt(n))`,通过减少循环的执行次数,从`i = 2`执行到 `i = sqrt(n)`。因为可以确认,当`i`大于`sqrt(n)`时,除了`n`本身,再没有数可以被整除了。 | ||
|
||
## Hardy-Ramanujan公式用于计算质数因子的个数 | ||
|
||
1917年,G.H Hardy和Srinivasa Ramanujan提出了一个定理,该定理指出,自然数 `n` 的不同素数的数 `ω(n)` 的正态次序是`log(log(n))`。 | ||
|
||
粗略地讲,这意味着大多数数字具有这个数量的质数因子。 | ||
|
||
## References | ||
|
||
- [Prime numbers on Math is Fun](https://www.mathsisfun.com/prime-factorization.html) | ||
- [Prime numbers on Wikipedia](https://en.wikipedia.org/wiki/Prime_number) | ||
- [Hardy–Ramanujan theorem on Wikipedia](https://en.wikipedia.org/wiki/Hardy%E2%80%93Ramanujan_theorem) | ||
- [Prime factorization of a number on Youtube](https://www.youtube.com/watch?v=6PDtgHhpCHo&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8&index=82) |