Home > php教程 > php手册 > 真·水仙花开

真·水仙花开

WBOY
Release: 2016-06-06 19:33:33
Original
1088 people have browsed it

自幂数,又称阿姆斯特朗数,民间通称水仙花数。实则只有3位自幂数才是水仙花数。4位5位6位等等各有别的叫法。 PHP ?php//阿姆斯特朗数:一个k位数,它的每个位上的数字的k次幂之和等于它本身。(例如:1^3 + 5^3 + 3^3 = 153)class Armstrong {static functi

自幂数,又称阿姆斯特朗数,民间通称水仙花数。实则只有3位自幂数才是水仙花数。4位5位6位等等各有别的叫法。 PHP
<?php
//阿姆斯特朗数:一个k位数,它的每个位上的数字的k次幂之和等于它本身。(例如:1^3 + 5^3 + 3^3 = 153)
class Armstrong {

	static function index(){
		for ( $i = 100; $i < 100000; $i++ ) {
			echo self::is_armstrong($i) ? $i . '<br>' : '';
		}
	}

	static function is_armstrong($num){
		$s = 0;
		$k = strlen($num);
		$d = str_split($num);
		foreach ($d as $r) {
			$s += bcpow($r, $k);
		}
		return $num == $s;
	}

}

Armstrong::index();
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template