@arcjumper22033

Generally if you do not understand a math concept it would be wise to just google it or head to the local library.

Digit sum is easy because you are just adding it up.

Factorials

Ok, so the easiest way for a beginner to think about digit sum it to think about it as you would on a piece of paper.

It reads left to right and you add it up the same way.

In programming it is easy enough to do this.

**STEPS**

- Convert number into a string for the computer
- Create a loop to go over the string
- Convert the digit it is on back to an integer and add it to the total.
- Return the total

Once you get the steps you need to do you can program anything. Now that we have the steps let's follow through.

**DIGIT SUM CODE:** Using the int to string method.

```
def digit_sum(number):
total = 0
for digit in str(number):
total += int(digit)
return total
```

Ok, that was easy enough. Now there are other methods to get the total also but they involve math, if you are good with math then it will be easy to use.

- Get the value of the ones place
- Add the ones place to the total
- Remove the ones place and repeat till no number is left

**DIGIT SUM CODE:** Using math

```
# We will use floor so it works for all versions of python
from math import floor
def digit_sum(number):
total = 0
while number > 0:
total += number % 10
number = floor(number / 10)
return total
```

Ok, now that was easy as pie again, but that is because we laid out the steps needed to do our problem.

Now let's see a more advanced version of both sets of code.

**DIGIT SUM CODE:** Using int to string method ADVANCED

```
def digit_sum(number):
return sum((int(digit) for digit in str(number))
```

**DIGIT SUM CODE:** Using Math method ADVANCED

```
# This method has a max of 2000ish digits unless you remove the recursion limit in python
from math import floor
def digit_sum(number, total=0):
return digit_sum((floor(number / 10)) ,(total + number % 10)) if number > 0 else (total + number % 10)
```

All of the above methods work wonders but converting into a string method is the best in my opinion.

Now on to Factorials.

Factorials are the product of all numbers starting at 1 to the number in questions, and can be expresses as [!10].

So

`!10 = 3628800`

OR

`1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 = 3628800`

So the steps we need to compete this time are as follows,

- Create a list of all numbers needed to multiply
- Loop through the list of numbers multiplying the total by the current number
- Return the total

Ok, so the steps are straight forward so let's work on them.

**FACTORIAL CODE:** Using simple for loop - Can be extremely CPU bound

```
def factorial(number):
total = 1
for i in range(2, number +1):
total *= i
return total
```

We can optimize out factorial code for larger numbers but that is out of the scope of this discussion right now.

I think that should be all the tools you need to complete both tasks. If you need any more help fell free to ask.