Discuz教程网

php 求素数(质素) 的实现代码

[复制链接]
authicon dly 发表于 2011-4-14 14:16:33 | 显示全部楼层 |阅读模式

  1. <?php
  2. class timer
  3. {
  4. var $time_start;
  5. var $time_end;

  6. function __construct()
  7. {
  8. $this->time_start = 0;
  9. $this->time_end = 0;
  10. }

  11. function timer()
  12. {
  13. $this->__construct();
  14. }

  15. function start()
  16. {
  17. list($usec,$sec) = explode(" ",microtime());
  18. $this->time_start = (float)$usec + (float)$sec;
  19. }

  20. function stop()
  21. {
  22. list($usec,$sec) = explode(" ",microtime());
  23. $this->time_end = (float)$usec + (float)$sec;
  24. }

  25. function show($output = false)
  26. {
  27. $total = $this->time_end - $this->time_start;
  28. if ($output) {
  29. echo $total," sec";
  30. return true;
  31. }
  32. return $total." sec";
  33. }

  34. }
  35. ?>
  36. <?php
  37. echo 'check prime<br/>';
  38. function IsPrime($i)
  39. {
  40. if($i<2)
  41. {
  42. return false;
  43. }
  44. //var $iterator;
  45. for($iterator = 2 ; $iterator <= sqrt($i) ; $iterator++)
  46. {
  47. if($i % $iterator==0)
  48. {
  49. return false;
  50. }
  51. }
  52. return true;
  53. }

  54. $sw=new timer();
  55. $sw->start();
  56. for($j=1;$j<100;$j++)
  57. {
  58. if(IsPrime($j))
  59. {
  60. echo 'true<br/>';
  61. }
  62. else
  63. {
  64. echo 'false<br/>';
  65. }
  66. }
  67. $sw->stop();
  68. $sw->show(true);

  69. ?>
复制代码






上一篇:php一个简单的路由类
下一篇:php表单提交问题的解决方法
authicon 21585151 发表于 2011-5-11 03:59:53 | 显示全部楼层
前来看看那
authicon 82xiaochong911 发表于 2011-5-20 00:59:57 | 显示全部楼层
继续来索要
authicon 21585151 发表于 2011-5-21 10:59:45 | 显示全部楼层
继续来索要
authicon yuki佳 发表于 2011-5-24 00:59:37 | 显示全部楼层
很好的,我喜欢
authicon soul2511 发表于 2011-5-25 06:59:59 | 显示全部楼层
看帖必回
authicon shakesxia 发表于 2011-5-27 01:59:56 | 显示全部楼层
看看  好像不错
authicon 婷婷爱牛牛 发表于 2011-6-17 00:59:44 | 显示全部楼层
看一下啊,嘻嘻
authicon forever21 发表于 2011-6-17 21:59:54 | 显示全部楼层
顶啦,不错吧
authicon lanyuqing 发表于 2011-6-18 21:00:01 | 显示全部楼层
万分感谢楼主
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 18:54

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表