1. 云栖社区>
  2. PHP教程>
  3. 正文

Project Euler: problem 48 (PHP) – Self powers

作者:用户 来源:互联网 时间:2017-12-01 18:38:31

Project Euler: problem 48 (PHP) – Self powers - 摘要: 本文讲的是Project Euler: problem 48 (PHP) – Self powers, I previously blogged about this Project Euler puzzlenearly 6 years ago, using Python.  This is my approach using PHP as

I previously blogged about this Project Euler puzzlenearly 6 years ago, using Python.  This is my approach using PHP as a simple practical exercise for myself, and I’d appreciate any feedback on my PHP code.

Problem 48:

The series, 11+ 22+ 33+ … + 1010= 10405071317.

Find the last ten digits of the series, 11+ 22+ 33+ … + 10001000.

Code:

<?php$limit = 1000;$sum = 0;foreach (range(1, $limit) as $i) {	$sum = bcadd($sum, bcpow($i, $i));}echo substr($sum, -10);

Short question, short answer.  Using theBC Math functionsdue to the size of the numbers involved… not quite as fast to spit out the answer as it was with Python though.

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索,以便于您获取更多的相关知识。