I've absolutely no idea about 4/15 & 5/15


@mbunel @geomaticmashups @zeziba @mtf @factoradic

I just want to say that I've been following this and trying to understand this section well but it's like I've absolutely no idea about 4/15 & 5/15 and I've read answers on the forum and have no clue how people got them. Is there anything I should read up? Any help would be appreciated.

Thanks in advance


4/15 int object is not callable


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.


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.


  1. Convert number into a string for the computer
  2. Create a loop to go over the string
  3. Convert the digit it is on back to an integer and add it to the total.
  4. 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.

  1. Get the value of the ones place
  2. Add the ones place to the total
  3. 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))


# 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].


!10 = 3628800


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

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

  1. Create a list of all numbers needed to multiply
  2. Loop through the list of numbers multiplying the total by the current number
  3. 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.


Thanks for reply.

Since I'm just the beginner in programming languages so I want to understand deeply about math concept and what is recursion?

just like I just follow your steps in my coding and I understood that how actually it works but to be precise I want to understand the other methods also i.e. math, int to string Advanced and math method Advanced.

Please help me with that also.



Recursion in programming is a pretty friendly concept. When function you defined calls itself - it is a recursive function. Let me show you an example.

@zeziba explained how to calculate factorial, here is a recursive definition (for n > 0):

n! = 1,           if n == 1
n! = n * (n-1)!,  if n != 1

For example, for n = 5 it would be:

5! = 5 * 4! = 5 * 4 * 3! = 5 * 4 * 3 * 2 * 1! = 5 * 4 * 3 * 2 * 1

We can code this in a really simple way:

def factorial(n):
    return 1 if n == 1 else n * factorial(n - 1)

What are disadvantages and advantages of recursion?

Recursive functions are usually much simpler than iterative versions. And this is the only advantage of recursion - simplicity.

Disadvantages, hm. Take a look at our example with 5!, please note how many steps it took to compute a result in format 5 * 4 * 3 * 2 * 1, you still have to calculate this now. In the iterative version you get result in half of the steps needed here.

Recursion is limited by space in the stack or by the limit of depth. So it is safer to write iterative functions.

There is a one exception to what I have written about disadvantages of recursion -> tail-recursive functions. I will show you an example:

def greatest_common_divisor(x, y):
    return x if y == 0 else greatest_common_divisor(y, x % y)

Let's say that we want to calculate gcd(54, 24):

gcd(54, 24) = gcd(24, 6) = gcd(6, 0) = 6

Do you see? We have a result, we don't have to calculate anything more now.

As @zeziba mentioned, Python does not optimize tail-recursive functions so you still can reach the limit of depth. I will leave this part of post because this is still valuable concept used in many languages.


Tail-recursion is not officially supported in Python, you can program it in however. I forget what PEP it's in but It is not supported nor is there any plans to ever support it.


@factoradic Thanks for reply.. nice explanation. :smiley:


@zeziba hmm k thanks for that also..


Yes, you are right! Just checked. Kind of strange, in my opinion.

I will edit my post, thanks!


It is simply because Guido van Rossumdoes not want to support any type of obscurification. Though as I said you can easily put it into python but there is rarely a case where you actually need it.