Home > php教程 > PHP源码 > 《Ruby太慢了》php版回文数计算代码

《Ruby太慢了》php版回文数计算代码

PHP中文网
Release: 2016-05-25 17:06:00
Original
1836 people have browsed it

跳至

<?php
//给出两个数值X和Y,统计在这个区间里的回文数,并且要求它们的平方根也是回文数。其中 1<= x <= y < 10 14 
error_reporting(E_ALL);
ini_set("display_errors", 1);
//避免超时
set_time_limit(0);
$t1=microtime();
function  isPlalindrome($num){
    $str="$num";
	$len=strlen($num);
	$k = intval($len/2) + 1;//获取中间位数
	for($j=0;$j<$k;$j++){
	   if($str{$j}!=$str{$len-1-$j}){   
			return false;
	   }
	}	
	return true;
}

function showPlalindrome($min,$max){
	//因为要计算在$min,$max间的回文数且其自身平方根也是回文数,所以相当于求一sqrt($min)~sqrt($max)间数,其平方在$min~$max间也是回文数
	//$min~$max是连续正整数,所以可以这样缩小很多计算量,否则……
	$start=sqrt($min);
	$end=sqrt($max);
	for($i=$start;$i
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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template