Split a number and then add individual digits

  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 327
  • Loc: Canada

Post 3+ Months Ago

Hi All

I am trying to write a PHP script but can't seem to get there.
Hopefully someone can help.
Here is what I am trying to do.

I have a number
I want to first get the length of the number
If the length of the number is <=1 then I use the number as is.
If the length is >=2 then I want to split the number into individual digits
I then want to add these digits together.
If the sum length >=2, I then want to split this sum again into digits.
Now I add these digits together.
I keep doing this until the sum length <=1 and then use this number

For example

$i=2345
The length is 4
Now I want to spit and add the digits which would be 2+3+4+5=14
The length here is 2
Again I want to split and add the digits which would be 1+4=5
The length is 1 so I use the number

Thanks
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1244
  • Loc: Sydney

Post 3+ Months Ago

i have been looking at this problem for an hour now and i am still puzzled what are you trying to make? I have a half worked up solution.
  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Posts: 214
  • Loc: Sweden

Post 3+ Months Ago

Edit, I think there was a small bug in my code. I might post a new code later lol. :P

EDIT, maybe there was nothing wrong with the code I was thinking like "how can 91 become 1?" then I realized that 9 + 1 = 10 and then it rerun and 1 + 0 = 1. I repost the code here anyway maybe it can serve as an idea at least.

Code: [ Select ]
<?php
function NumbersAsSum($myNumber)
{
    if(!is_numeric($myNumber)) //optional, can filter some bad code
    {
        die("The input seems to contain some nasty (or unsupported) stuff, Script halted");
    }
    while (strlen($myNumber) > 1)
    {
        $myNumber = array_sum(str_split($myNumber, 1));
    }
    return $myNumber; //or echo?
}

$test = NumbersAsSum(101188);
echo $test . "<br />";
?>
  1. <?php
  2. function NumbersAsSum($myNumber)
  3. {
  4.     if(!is_numeric($myNumber)) //optional, can filter some bad code
  5.     {
  6.         die("The input seems to contain some nasty (or unsupported) stuff, Script halted");
  7.     }
  8.     while (strlen($myNumber) > 1)
  9.     {
  10.         $myNumber = array_sum(str_split($myNumber, 1));
  11.     }
  12.     return $myNumber; //or echo?
  13. }
  14. $test = NumbersAsSum(101188);
  15. echo $test . "<br />";
  16. ?>


The thing with the code tho is that negative numbers gets threatened and thrown back as positive ones. So -1002 and 1002 both return 3.

But I'm sure you can check for that if that is important. :D
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6254
  • Loc: Seattle, WA

Post 3+ Months Ago

This problem screams recursion. The solution is very elegant if you go that route.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 327
  • Loc: Canada

Post 3+ Months Ago

Hey WritingBadCode

Your code works create.

I do have a few exceptions where if the length is greater than 1 but the sum is 11, 22 or 33, then it should stop and not reduce it further but echo only 11, 22 or 33

Could you add this exception to your code

thanks
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 327
  • Loc: Canada

Post 3+ Months Ago

Hi again

I modified your code as follows and now it takes care of the exceptions:

Code: [ Select ]
<?php
function NumbersAsSum($myNumber)
{
  if(!is_numeric($myNumber)) //optional, can filter some bad code
  {
    die("The input seems to contain some nasty (or unsupported) stuff, Script halted");
  }
  while (strlen($myNumber) > 1)
  {
    $myNumber = array_sum(str_split($myNumber, 1));
    if($myNumber == 11 || $myNumber ==22 || $myNumber ==33)
      {
      break;
      }
    else
      {
      continue;
      }
  }
  return $myNumber; //or echo?
}

$test = NumbersAsSum(9997);
echo $test . "<br />";
?>
  1. <?php
  2. function NumbersAsSum($myNumber)
  3. {
  4.   if(!is_numeric($myNumber)) //optional, can filter some bad code
  5.   {
  6.     die("The input seems to contain some nasty (or unsupported) stuff, Script halted");
  7.   }
  8.   while (strlen($myNumber) > 1)
  9.   {
  10.     $myNumber = array_sum(str_split($myNumber, 1));
  11.     if($myNumber == 11 || $myNumber ==22 || $myNumber ==33)
  12.       {
  13.       break;
  14.       }
  15.     else
  16.       {
  17.       continue;
  18.       }
  19.   }
  20.   return $myNumber; //or echo?
  21. }
  22. $test = NumbersAsSum(9997);
  23. echo $test . "<br />";
  24. ?>


Thanks for your help

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 101 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.