Skip to content

Instantly share code, notes, and snippets.

@lexnjugz
Last active March 29, 2017 06:41
Show Gist options
  • Save lexnjugz/7a88282c2b54275089e114ea8260bcd3 to your computer and use it in GitHub Desktop.
Save lexnjugz/7a88282c2b54275089e114ea8260bcd3 to your computer and use it in GitHub Desktop.
function super_sum($A){
$sum = 0;
foreach ($A as $key => $value){
//$sum += $value;
do {
$sum += $value % 10;
} while ($value = (int) $value / 10);
}
return $sum;
}
$A = [120, 13, 45];
print_r(super_sum($A));
@profnandaa
Copy link

Will this code sum up the digits really? i.e. 1 + 2 + 0 + 1 + 3 + 4 + 5 for $A = [120, 13, 34]?

@lexnjugz
Copy link
Author

Hi Nandaa,
I think understood it all wrong. I thought it was adding individual values in array. Let me update it right now, now that I understand it better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment