php找出指定範圍內迴文數且平方根也是迴文數的方法_php技巧

來源:互聯網
上載者:User

本文執行個體講述了php找出指定範圍內迴文數且平方根也是迴文數的方法。分享給大家供大家參考。具體如下:

一、要求:

給出兩個數值X和Y,統計在這個區間裡的迴文數,並且要求它們的平方根也是迴文數。其中 1<= x <= y < 10 14

二、解決方案:

<?phperror_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<$end;$i++){    if(isPlalindrome($i) &&isPlalindrome($n=$i*$i) ){     echo $n." <br/>";    }  }}showPlalindrome(1,100000000000000);$t2=microtime();$starttime = explode(" ",$t1);$endtime = explode(" ",$t2);$totaltime = $endtime[0]-$starttime[0]+$endtime[1]-$starttime[1];$timecost = sprintf("%s",$totaltime);echo "頁面已耗用時間: $timecost 秒";?>

希望本文所述對大家的php程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.