PHP检查值是否是具有共同差异的数字序列的一部分

问题描述 投票:2回答:7

我正在编写一个脚本,需要识别序列的第一个数字为0的每个第7个数字(因此序列为6,13,20,27,34,41,48等)

这是我到目前为止所做的(以有限的方式工作),但我更喜欢编写一个循环,以便序列中的值不必硬编码:

<?php

$x = 7;

if ($x === 6 || $x === 13 || $x === 20 || $x === 27 || $x === 34 || $x === 41 || $x === 48) {
    $result = 1;
  } else {
    $result = 0;
}

echo $result;

?>

提前感谢任何建议!


更新:这是我使用@robbieaverill的解决方案更新的脚本,并检查数组的值。这里有很多很棒的解决方案,谢谢大家!

<?php

$divisor = 7;
$limit = 500; 

$sequence = array();

for ($i = 0; $i <= $limit; $i++) {
  if (is_int(($i + 1) / $divisor)) {
    $sequence[] = $i;
  }
}

$x = 41;

if (in_array($x, $sequence, true)) {
  $result = 1;
} else {
  $result = 0;
}

echo $result;
?>

此处的输出为1,表示如果$ x的值为41,则在$ sequence数组中找到它。

php math
7个回答
2
投票

可能有很多不同的方法可以做到这一点。如果您需要识别可被7整除的数字(加一,因为您基于零),您可以执行类似this的操作:

$divisor = 7;
$limit = 100; // when do you want to stop searching?

for ($i = 0; $i <= $limit; $i++) {
    if (is_int(($i + 1) / $divisor)) {
        var_dump($i);
    }
}

请注意,is_int()正在检查分割结果是否为整数,对于十进制/浮点数,它将返回false。


1
投票

以下是使用modulus operator执行此操作的另一种方法:

<?php
$start = 0;
$sequence = 7;
$limit = 100;

for ($i=$start; $i<$limit; $i++) {
    // if the divition of the current number / the sequence is a full number (there is no remainder) and we're not at 0
    if(($i+1) % $sequence == 0 && $i!=0) {
       echo $i . "<br>";
    }
}

// output:
6
13
20
27
34
41
48
55
62
69
76
83
90
97

既然你说“需要识别每个第7个数字”,你可能只想检查一个给定的数字是否是第7个,这意味着你想要一个函数来测试。那么这可以帮助:

function isMultipleOf($input, $sequence) {
    if(($input+1) % $sequence == 0 && $input!=0) {
        return true;
    } else {
        return false;
    }

}


$check = 601;
echo isMultipleOf($check, $sequence); // true/1;
$check = 2;
echo isMultipleOf($check, $sequence); // false;

1
投票

考虑使用range生成系列:

$begin = 6;
$end = 48;
$step = 7;
$seq = range($begin, $end, $step);
print_r($seq);

然后你可以使用in_array检查一个数字是否在该系列中:

if (in_array(41, $seq)) // ...

See it live on 3v4l.org.

这绝不是有效的,但它是一个明显的实现,适用于小型系列。我认为小到数千条或更少。


1
投票

将1添加到序列号$i并使用模数n并返回$i % 7 == 0

EG

function isNth(int $i, int $nth, int $offset = -1): bool
{
    return !(($i - $offset) % $nth);
}

用法示例:

foreach ([0,1,2,3,4,5,6,7,8,9,10,11,12,13] as $number)
{
    if (isNth($number, 7)) print "$number\n";
}

// will print 6\n13\n

foreach ([1,2,3,4,5,6,7,8,9,10,11,12,13,14] as $number)
{
    if (isNth($number, 7, 0)) print "$number\n";
}

// will print 7\n14\n

0
投票

像这样的解决方案也可以。

$index = 0;
for($i = 0; $i <= 700; $i++){
    if($index == 6){
        echo $i . "<br/>";
        $index = 0;
    } else {
       $index++; 
    }
}

将输出6 13 20 27 34 41 48 55


0
投票

一般来说,我们可以写一个像这样的算术序列:

{a, a+d, a+2d, a+3d, ... }

其中a = 6(第一项)d = 7(术语之间的“共同差异”)

所以我们可以{a,a + d,a + 2d,a + 3d,...}

{6, 6+7, 6+2×7, 6+3×7, ... }

{6,13,20,27 ......}所以我们可以有一个规则

x[n] = a + d(n−1)

可以用php编写如下

<?php
$a=6;
$n=10; //number of terms
$d=7;
for($i=1;$i<=10;$i++){
    echo $term=$a+($i-1)*$d;
    echo "<br>";
}

?>

-2
投票

所以,如果你有一个最好的数字列表将是

<?php
   $list = array(0,1,8,89,785,0,789785,56,8,9,1,2,3,2);
   for($i = 0; $i < count($list);$i++){
      if($list[$i] === 0){
          echo "Found and occurence. Value is ".($list[$i+7])."<br>";
       }
    }
?>

将输出Found和occurrence。值为56 Found和occurrence。价值是3

© www.soinside.com 2019 - 2024. All rights reserved.