要检查给定数字是否存在于无限序列中,PHP代码如下-
<?php function contains_val($m, $n, $o){ if ($m == $n) return true; if (($n - $m) * $o > 0 && ($n - $m) % $o == 0) return true; return false; } $m = 3; $n = 5; $o = 9; if (contains_val($m, $n, $o)) echo "The number is present in the infinite series"; else echo "The number is not present in the infinite series"; ?>
输出结果
The number is not present in the infinite series
上面定义了三个变量,并通过传递这三个值来调用函数-
$m = 3; $n = 5; $o = 9;
定义了一个名为“ contains_val”的函数,该函数带有这三个变量。比较前两个变量,如果相等,则返回“ true”。否则,如果前两个变量与第三个数字的乘积之差大于0,并且前两个变量模数与第三个数字的乘积之差为0,则返回“ true”。否则,该函数返回false-
function contains_val($m, $n, $o){ if ($m == $n) return true; if (($n - $m) * $o > 0 && ($n - $m) % $o == 0) return true; return false; }