Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c224922

Browse files
committed
修改代码格式和README.md, 新增listSquared
1 parent a224311 commit c224922

File tree

12 files changed

+82
-2
lines changed

12 files changed

+82
-2
lines changed

‎README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1-
# PHPCode
1+
<h1 align="center">PHP编程题目</h1>
2+
3+
<p align="center">:rainbow: 每周两道编程题目, 加强自己对PHP理解和编程能力. 欢迎提出好的思路, 相互交流.</p>
4+
5+
[![Build Status](https://www.codewars.com)](https://www.codewars.com)
6+
7+
8+
### 编程题目
9+
10+
- [alphabet_positon:用字母表数字替换字符串的字母](https://github.com/uuk020/PHPCode/blob/master/alphabet_position/answer.php)
11+
- [cube_add:循环数组中奇数幂运算](https://github.com/uuk020/PHPCode/blob/master/cube_odd/answer.php)
12+
- [find:在奇数数组中找出偶数或者在偶数数组中找出奇数](https://github.com/uuk020/PHPCode/blob/master/find/answer.php)
13+
- [format_duration:转换时间格式](https://github.com/uuk020/PHPCode/blob/master/format_duration/answer.php)
14+
- [longestConsec:返回数组中由k个连续字符串组成的第一个最长字符串。](https://github.com/uuk020/PHPCode/blob/master/longestConsec/answer.php)
15+
- [nbDig:循环某个范围的数,算出每个数的平方,根据第二个参数中数字,看该数字在得出结果出现几](https://github.com/uuk020/PHPCode/blob/master/nbDig/answer.php)
16+
- [pascals_triangle:以数组形式输出杨辉三角](https://github.com/uuk020/PHPCode/blob/master/pascals_triangle/answer.php)
17+
- [series_sum:规律计算题](https://github.com/uuk020/PHPCode/blob/master/series_sum/answer.php)
18+
- [thirt:13的可分性规则](https://github.com/uuk020/PHPCode/blob/master/thirt/answer.php)
19+
- [listSquared:找出一个数是它所有整除的数的平方之和的结果,此结果能够开平方.](https://github.com/uuk020/PHPCode/blob/master/listSquared/answer.php)
220

3-
## 每天一道CodeWar编程题目,分析题目和思路, 选取觉得好的方法,分析. 提高自己对PHP理解
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

‎listSquared/answer.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
*
4+
* Divisors of 42 are : 1, 2, 3, 6, 7, 14, 21, 42. These divisors squared are: 1, 4, 9, 36, 49, 196, 441, 1764. The sum of the squared divisors is 2500 which is 50 * 50, a square!
5+
6+
* Given two integers m, n (1 <= m <= n) we want to find all integers between m and n whose sum of squared divisors is itself a square. 42 is such a number.
7+
8+
* The result will be an array of arrays or of tuples (in C an array of Pair) or a string, each subarray having two elements, first the number whose squared divisors is a square and then the sum of the squared divisors.
9+
10+
* #Examples:
11+
* list_squared(1, 250) --> [[1, 1], [42, 2500], [246, 84100]]
12+
* list_squared(42, 250) --> [[42, 2500], [246, 84100]]
13+
*The form of the examples may change according to the language, see Example Tests: for more details.
14+
*/
15+
16+
function listSquared($m, $n) {
17+
$result = [];
18+
for (;$m <= $n; $m++) {
19+
$sum = 0;
20+
// 低效算法, 循环次数过大
21+
for ($i = 1; $i <= $m; $i++) {
22+
// 找出能够整除$m的数
23+
if ($m % $i == 0) {
24+
$sum += pow($i, 2);
25+
}
26+
}
27+
// 求平方根
28+
$sqrt = sqrt($sum);
29+
if (pow($sqrt, 2) == $sum && $sum % $sqrt == 0) {
30+
$result[] = [$m, $sum];
31+
}
32+
}
33+
return $result;
34+
}
35+
36+
37+
function listSquared_clever($m, $n) {
38+
$results = [];
39+
for ($candidate = $m; $candidate <= $n; $candidate++) {
40+
$sum = 0;
41+
// 算出平方根的数, 进行循环(循环次数少)
42+
for ($divisor = 1; $divisor <= (int)sqrt($candidate); $divisor++) {
43+
// 找出能够整除sqrt($candidate)的数 也就是除数的.
44+
if ($candidate % $divisor === 0) {
45+
$sum += $divisor ** 2;
46+
/**
47+
* 排除两个除数相等, 找出$candidate / $divisor的求商, 补齐商数的平方.
48+
* 如: $candidate = 4时候, 4能够被1,2,4整除. 此时$sum=1*1+2*2; 缺少了4*4.
49+
*/
50+
if ($candidate / $divisor !== $divisor) {
51+
$sum += ($candidate / $divisor) ** 2;
52+
}
53+
}
54+
}
55+
$square = sqrt($sum);
56+
57+
if (filter_var($square, FILTER_VALIDATE_INT)) {
58+
$results[] = [$candidate, $sum];
59+
}
60+
}
61+
62+
return $results;
63+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /