Skip to content

Commit 97cf975

Browse files
committed
2 parents ecb8fcf + d505f9b commit 97cf975

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
lines changed

README-EN.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<h1 align="center">:whale:A collection of algorithms that are implemented in PHP:whale: </h1>
1+
<h1 align="center">:whale:A collection of algorithms that are implemented in PHP:whale: </h1>
22

33
<p align="center">
44
<a href="https://github.com/PuShaoWei/arithmetic-php#简易结构">
@@ -66,7 +66,9 @@
6666
│ └── Square.php
6767
│ └── Prim.php
6868
│ └── CartesianProduct.php
69-
│ └── RotationSort.php
69+
│ └── Square.php
70+
│ └── Judge.php
71+
│ └── Factorial.php
7072
7173
├──LICENSE
7274
└──README.md
@@ -112,7 +114,7 @@ In this case, you know how fast these algorithms are
112114
| 1 000 000 000 | 11day | 30ms |
113115

114116
- ` O ` said hair is pointed out that how fast algorithms, such as list contains ` n ` element, a simple search need to check each element, so you need to perform ` n ` time operations
115-
Using large ` O ` said ` O (n) to make this operation `, binary search need to perform log<sub>n</sub> using large ` O ` said to`O(log n)`
117+
Using large ` O ` said ` O (n) to make this operation `, binary search need to perform log<sub>n</sub> using large ` O ` said to`O(log n)`
116118
- Some common big O runtime
117119
- O(log n) ,It's also called log time, and this algorithm includes binary algorithms
118120
- O(n),Also known as linear time, this algorithm includes simple lookups.
@@ -130,7 +132,7 @@ Using large ` O ` said ` O (n) to make this operation `, binary search need to p
130132
1. From a procedural point of view, the recursion manifests itself as calling itself, and the loop does not have this form.
131133
2. Recursive proceed from the ultimate goal of the problem, and gradually to a complex problem into a simple problem, and simple question solution and complicated problem, at the same time the presence of the benchmark, can eventually get a problem, is the reverse. And the circulation is from the simple question, step by step forward development, finally get the question, is positive.
132134
3. Any cycle can be represented by recursion, but it is necessary to use the loop to achieve recursion (except for one-way recursion and tail recursion), and the stack structure must be introduced to stack the stack.
133-
4.In general, non-recursive efficiency is higher than recursion. And recursive function calls are expensive and recursive times are limited by stack size.
135+
4.In general, non-recursive efficiency is higher than recursion. And recursive function calls are expensive and recursive times are limited by stack size.
134136

135137
## Progressive learning
136138

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<h1 align="center">:whale: 用 PHP 的方式实现的各类算法合集 :whale: </h1>
1+
<h1 align="center">:whale: 用 PHP 的方式实现的各类算法合集 :whale: </h1>
22

33
<p align="center">
44
<a href="https://github.com/PuShaoWei/arithmetic-php#简易结构">
@@ -80,7 +80,9 @@
8080
│ └── Square.php Facebook面试题之判断四个点能否组成正方形算法
8181
│ └── Prim.php Prim算法(最小生成树算法)
8282
│ └── CartesianProduct.php 笛卡尔积算法
83-
│ └── RotationSort.php 面试题之平面任意四点能否组成一个矩形
83+
│ └── Square.php 面试题之平面任意四点能否组成一个矩形
84+
│ └── Judge.php 面试题之扑克牌中任选五张判断是不是顺子
85+
│ └── Factorial.php 面试题之N的阶乘末尾有多少个0
8486
8587
8688
├──LICENSE
@@ -141,7 +143,7 @@ log<sub>10</sub>100 相当于问"将多少个10相乘的结果为100",答案
141143
| 1 000 000 000 个元素 | 11天 | 30ms |
142144

143145
-`O`表示法指出了算法有多快,例如列表包含`n`个元素,简单查找需要检查每个元素,因此需要执行`n`次操作
144-
使用大`O`表示法这个运行时间为`O(n)`,二分查找需要执行log<sub>n</sub>次操作,使用大`O`表示为`O(log n)`
146+
使用大`O`表示法这个运行时间为`O(n)`,二分查找需要执行log<sub>n</sub>次操作,使用大`O`表示为`O(log n)`
145147
- 一些常见的大O运行时间
146148
- O(log n) ,也叫对数时间,这样的算法包括二分算法
147149
- O(n),也叫线性时间,这样的算法包括简单查找。

package/Other/Factorial.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* N的阶乘末尾有多个0
4+
*
5+
* 阶乘
6+
* N! = 1*2*3*...N;
7+
* 比如 5! = 1*2*3*4*5 = 120 末端有1个0
8+
*
9+
* 解题思路:
10+
* N! = K*(10^M)
11+
* N的阶乘为K和10的M次方的乘积,那么N!末尾就有M的0。如果将N的阶
12+
* 乘分解后,那么
13+
* N的阶乘可以分解为: 2的X次方,3的Y次方,4的5次Z方,.....的成绩。由于10 = 2 *
14+
* 5,所以M只能和X和Z有关,每一对2和5相乘就可以得到一个10,于是M = MIN(
15+
* X,Z),不难看出X大于Z,因为被2整除的频率比被5整除的频率高的多。所以可以把公式简化为M=Z
16+
* 所以我们求解N的阶乘最后的0.相当于求解里面有多个5的因子
17+
*
18+
* 当我们遇到一大问题可以拆成一个个小的问题,来求解。
19+
*
20+
*/
21+
22+
function factorial($n)
23+
{
24+
$n = intval($n);
25+
if ($n < 0) {
26+
return 0;
27+
}
28+
$sum = 0;
29+
for ($i = 5; $i <= $n; $i = $i + 5) {
30+
$j = $i;
31+
32+
//求解5的个数 比如15 里面有3个5
33+
while ($j % 5 == 0) {
34+
$sum++;
35+
$j = $j / 5;
36+
}
37+
}
38+
return $sum;
39+
40+
}
41+
42+
var_dump(factorial(100));
43+
44+
var_dump(factorial(1));

package/Other/Judge.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ function judge(array $array)
6262
return false;
6363

6464
}
65-
$res = judge([1,2,3,4,5]);
66-
$res = judge([1,2,0,3,5]);
67-
$res = judge([1,5,0,3,0]);
68-
var_dump($res);
65+
$res1 = judge([1,2,3,4,5]); //没有王
66+
$res2 = judge([1,2,0,3,5]);// 一张王
67+
$res3 = judge([1,5,0,3,0]); // 两张王牌
68+
$res4 = judge([1,5,8,3,7]);
69+
var_dump($res1,$res2,$res3,$res4);

0 commit comments

Comments
 (0)