-4

I got a problem here figuring out how to do this:

  1. I got a document no for example 5843
  2. Starting with the right-most digit, multiply every other digit by 2 and 1 (For example, (3x2)+(4x1)+(8x2)+(5x1) and so on)
  3. For every multiplication that results a number more than 9, add them digits. (For example, from the calculation on (no.2) we will get this : (6)+(4)+(16)+(5). So we got a number (16) which is more that 9. We need to add them (1+6=7). Now the output will be like this : (6)+(4)+(7)+(5).
  4. Next, we add them (6)+(4)+(7)+(5)=22
  5. Now, we divide 22 by 10 and get the remainder which is 2 in this case.
  6. Lastly we minus the remainder with 10.
  7. So the last output will be 8

Can you guys guide me on how to do this? Thank you very much !

Redzwan Latif
  • 886
  • 3
  • 14
  • 38

2 Answers2

2

In case someone gets to the question wondering how to use modulus or 'getting the rest of a division' (%):

echo 5 % 3; // outputs 2


(You can take a look at a step-by-step output after this first part)

This is the commented function:

function func_name($docnum){

    // make sure there is just numbers in $docnum
    $docnum = preg_replace("/[^0-9]/","",$docnum);      

    // change order of values to use in foreach
    $vals = array_reverse(str_split($docnum));

    // multiply every other value by 2
    $mult = true;
    foreach($vals as $k => $v){
        $vals[$k] = $mult ? $v*2: $v;
        $vals[$k] = (string)($vals[$k]);
        $mult = !$mult;
    }

    // checks for two digits (>9)
    $mp = array_map(function($v){
        return ($v > 9) ? $v[0] + $v[1] : $v;
    }, $vals);

    // adds the values
    $sum = array_sum($mp);

    //gets the mod
    $md = $sum % 10;

    // checks how much for 10
    $result = 10 - $md;

    // returns the value
    return $result;
}

Some test runs:

$docnum = 5843;
echo func_name($docnum);

Output:

8


$docnum = 1111;
echo func_name($docnum);

Output:

4


$docnum = '-5a84fadsafds.fdar3';
echo func_name($docnum);

Output:

8


$docnum = 4321;
echo func_name($docnum);

Output:

6


This codes outputs the steps...

$docnum = '-5a84fadsafds.fdar3';

echo 'Original Value: ';
print_r($docnum);
echo '<hr>';

$docnum = preg_replace("/[^0-9]/","",$docnum); 

echo 'Numbers only: ';
print_r($docnum);
echo '<hr>';

$vals = array_reverse(str_split($docnum));

echo 'Reversed Value: ';
print_r($vals);
echo '<hr>';

$mult = true;
foreach($vals as $k => $v){
    $vals[$k] = $mult ? $v*2: $v;
    $vals[$k] = (string)($vals[$k]);
    $mult = !$mult;
}

echo 'After mult.: ';
print_r($vals);
echo '<hr>';

$mp = array_map(function($v){
    return ($v > 9) ? $v[0] + $v[1] : $v;
}, $vals);

echo 'Checked for >9: ';
print_r($mp);
echo '<hr>';

$sum = array_sum($mp);

echo 'All values together: ';
print_r($sum);
echo '<hr>';

$md = $sum % 10;

echo 'Mod: ';
print_r($md);
echo '<hr>';

$result = 10 - $md;

echo 'Final result: ';
print_r($result);
echo '<hr>';

Output:

Original Value: -5a84fadsafds.fdar3
Numbers only: 5843
Reversed Value: Array ( [0] => 3 [1] => 4 [2] => 8 [3] => 5 )
After mult.: Array ( [0] => 6 [1] => 4 [2] => 16 [3] => 5 )
Checked for >9: Array ( [0] => 6 [1] => 4 [2] => 7 [3] => 5 )
All values together: 22
Mod: 2
Final result: 8


Read more about things used in the code:


As suggested by @Floris, you can replace the double-digit sum part with modulus 9, so instead of

$mp = array_map(function($v){
        return ($v > 9) ? $v[0] + $v[1] : $v;
    }, $vals);

you would have

$mp = array_map(function($v){
        return $v % 9;
    }, $vals);

and still keep the same output...

Community
  • 1
  • 1
FirstOne
  • 6,033
  • 7
  • 26
  • 45
  • Wow I don't know how I messed up that comment so badly. I meant to say "if the sum is greater than nine you can either add the digits or take the number modulo 9. You can do the latter for all products regardless of whether they have one or two digits. For example 16%9=7 and 6+1=7 – Floris Mar 12 '16 at 19:03
  • @Floris Yeah, that's a possibility too. So instead of `return ($v > 9) ? $v[0] + $v[1] : $v;`, we'd have `return $v % 9;` – FirstOne Mar 12 '16 at 19:05
  • That's exactly what I meant yes – Floris Mar 12 '16 at 19:05
  • @FirstOne Thanks for the work-through! – Redzwan Latif Mar 14 '16 at 09:07
  • If $md = $sum % 10; results zero should't be this the result of the function? as $result = 10 - $md; wil result into 10 not a single digit – Carlos Huggins Mar 11 '18 at 20:22
  • @CarlosHuggins I think that's a possibility (It's been a while since this code). In such case, a simple `if` in the end should solve it ;) – FirstOne Mar 11 '18 at 23:24
0

So u just want a function to calculate your stuff? i would just convert your integer to a string and then do your calculation number by number. So it would look like this:

<?php

$NUMBER = 5843;

echo CalculateThing($NUMBER); // output: 8

function CalculateThing($inputNumber)
{
    $output = 0;
    $number = (string)$inputNumber;
    $mulTwo = true;
    for($index=strlen($number)-1; $index>=0; $index--) {
        $val = $mulTwo ? intval($number[$index]) * 2 : intval($number[$index]);
        if($val>9){
            $val = (string)$val;
            $val = $val[0]+$val[1];
        }
        $mulTwo = !$mulTwo;
        $output += intval($val);
    }
    $output = (($output % 10)-10)*-1;
    return $output;
}