# Split a number and then add individual digits

• Proficient
• Posts: 379

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
• Guru
• Posts: 1295
• Loc: Sydney

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.
• Posts: 214
• Loc: Sweden

3+ Months Ago

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

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.
• Brewmaster
• Posts: 6296
• Loc: Seattle, WA

3+ Months Ago

This problem screams recursion. The solution is very elegant if you go that route.
• Proficient
• Posts: 379

3+ Months Ago

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

thanks
• Proficient
• Posts: 379

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. ?>

## Post Information

• Total Posts in this topic: 6 posts
• Users browsing this forum: No registered users and 75 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