我发现我之前的代码(来自我之前的问题)太慢了,无法做我想要的,所以现在我有一个快速工作的代码。我添加了一个简单的代码来计算。至于无法重新声明课程,我已经阅读了其他涉及引用.php文件的问题,例如require_once(something.php)我除了这个之外没有任何其他文件。我尝试设置两个文件,所以我有一个.php文件,但后来我遇到了多个错误。这是我的代码。
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
ini_set("log_errors", 0);
$numbers = 0;
$amout = 0;
while ($numbers < 50) {
$numbers + 1;
// This till end of class is to get the divisor numbers
class Divisors {
public $factor = array();
public function __construct($num) {
$this->num = $num;
}
// count number of divisors of a number
public function countDivisors() {
if ($this->num == 1) return 1;
$this->_primefactors();
$array_primes = array_count_values($this->factor);
$divisors = 1;
foreach($array_primes as $power) {
$divisors *= ++$power;
}
return $divisors;
}
// prime factors decomposer
private function _primefactors() {
$this->factor = array();
$run = true;
while($run && @$this->factor[0] != $this->num) {
$run = $this->_getFactors();
}
}
// get all factors of the number
private function _getFactors() {
if($this->num == 1) {
return ;
}
$root = ceil(sqrt($this->num)) + 1;
$i = 2;
while($i <= $root) {
if($this->num % $i == 0) {
$this->factor[] = $i;
$this->num = $this->num / $i;
return true;
}
$i++;
}
$this->factor[] = $this->num;
return false;
}
} // our class ends here
$example = new Divisors($numbers);
// Here it will check if the divisor has 5 divisors
if ($example->countDivisors() == 5) {
// if true it will add 1 to the amount of numbers with 5 divisors
$amount + 1;
}
}
// when the loop has checked 50 numbers it will print the amount
if ($numbers == 50){
print "There are $amount numbers with 5 divisors";
}
?>
我该如何解决这个问题? (第二个是不是类有效的代码吗?)
答案 0 :(得分:1)
您的类在while
循环中声明,并且每次迭代都会重新声明。
你可能意味着,每次迭代都要创建一个新的实例。
这是修改后的代码
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
ini_set("log_errors", 0);
// This till end of class is to get the divisor numbers
class Divisors
{
public $factor = array();
public function __construct($num) {
$this->num = $num;
}
// count number of divisors of a number
public function countDivisors() {
if ($this->num == 1) return 1;
$this->_primefactors();
$array_primes = array_count_values($this->factor);
$divisors = 1;
foreach($array_primes as $power) {
$divisors *= ++$power;
}
return $divisors;
}
// prime factors decomposer
private function _primefactors() {
$this->factor = array();
$run = true;
while($run && @$this->factor[0] != $this->num) {
$run = $this->_getFactors();
}
}
// get all factors of the number
private function _getFactors() {
if($this->num == 1) {
return ;
}
$root = ceil(sqrt($this->num)) + 1;
$i = 2;
while($i <= $root) {
if($this->num % $i == 0) {
$this->factor[] = $i;
$this->num = $this->num / $i;
return true;
}
$i++;
}
$this->factor[] = $this->num;
return false;
}
} // our class ends here
$numbers = 0;
$amout = 0;
while ($numbers < 50)
{
$numbers + 1;
$example = new Divisors($numbers);
// Here it will check if the divisor has 5 divisors
if ($example->countDivisors() == 5) {
// if true it will add 1 to the amount of numbers with 5 divisors
$amount + 1;
}
}
// when the loop has checked 50 numbers it will print the amount
if ($numbers == 50){
print "There are $amount numbers with 5 divisors";
}
?>