loophp/collection

There is wrong example of Not multiple of seven in documentation

Bhavesh164 opened this issue · 2 comments

## Steps required to reproduce the problem

/**
 * Check if a number is a multiple of 7.
 *
 * @param $value
 *   The number.
 *
 * @return bool
 *   Whether or not the number is a multiple of 7.
 */
$notMultipleOf7 = static function ($value): bool {
    $number = $value;

    while (14 <= $number) {
        $lastDigit = mb_substr((string) $number, -1);

        if ('0' === $lastDigit) {
            return true;
        }

        $number = (int) abs((int) mb_substr((string) $number, 0, -1) - 2 * (int) $lastDigit);
    }

    return !(0 === $number || 7 === $number);
};
echo notMultipleOf7 (70);

Expected Result

False

Actual Result

True

Hello,

You're right and wrong at the same time, let me explain.

The example you're looking at is the Sieve of Erathostenes: https://loophp-collection.readthedocs.io/en/latest/pages/examples.html#find-prime-numbers

Basically what it does, it removes all the non prime number, starting from 2 to... infinity, in this example, we will bound the limit from 0 to 100.

So, starting with number 2, it will remove: 2, 4, 6, ..., 66, 68, 70, ..., 96, 98, 100.
Then number 3, it will remove 6, 12, 18,..., 96.
Then number 4 is completely useless to do because all numbers divisible by 4 are divisible by 2, which has already been processed.
Then number 5 will remove every numbers ending with 5 or 0
Then number 6 is useless as well because 6 = 2*3 and 2 and 3 are already processed.
Then number 7... it should remove 21, but it has been removed by number 3, it should remove 70 indeed, but it has already been remove when doing number 2... so the processing of number 7 will remove 49, 77, 91.
And it goes on...

So, you're right when you say that the callback should consider 70 as a multiple of 7, but that number will never pass in the callback because it has already been eliminated by previous number 2, so, we are safe :-)

If you feel that the example could be improved, feel free to submit a pull request, I'll merge it gladly.

If you think this issue could be closed, please do so as well.

Thanks for the clarification. But the indent is it should not be multiple of 7. For e.g. if i input 8 which is not a prime no. and also it is not divisible by 7 and it returns true.