Testing the equality of the sum of a digits within a number on python?
Question: for example `def f(n):`

and I wanna check whether the sum of the numbers within `n` equal to 100
whether it is in 1s, 2s, 3,s 4s, 5s and so on, depending on the length of `n`.

    
    
    f(5050)
    >>> True
    

This tests whether `5 + 0 + 5 + 0 == 100` and whether `50 + 50 ==100` and if
any are true, it returns `True`.

Whether it tests in 1s, 2s 3s 4s and so on, depends on the length of the
number. For example a number with a length of 5 can only be tested in 1s.

    
    
    f(12345)
    >>> False
    

This tests whether `1 + 2 + 3 + 4 + 5 == 100` and only that.

If the length of `n` was 15, it would test the digits in 1s, 3s and 5s.

and finally one more example:

    
    
    f(25252525)
    >>> True
    

This would test whether `2+5+2+5+2+5+2+5 == 100` and `25+25+25+25==100` and
whether `2525+2525==100` So `n`, which has a length of 8 would be tested in 1s
, 2s , and 4s. It cannot be tested with 3s and 5s because the length of all
the digits within the number being summed up must be the same.

I hope I was able to explain what I'm after. Usually I would post what I've
tried but I have no idea how to iterate over the digits of a number in such a
way


Answer: The below approach uses generator to split the integer, and no `integer <->
string` conversion.

This will likely be the most efficient approach of the ones currently listed.

    
    
    import math
    
    # Define a generator that will split the integer v into chunks of length cl
    # Note: This will return the chunks in "reversed" order.
    #   split(1234, 2) => [34, 12]
    def split(v, cl):
        while v:
            (v,c) = divmod(v, 10**cl)
            yield c
    
    
    def f(v):
        # Determine the number of digits in v
        n = int(math.log10(v)) + 1
        m = int(math.sqrt(v))
        # Check all chunk lengths in [1, sqrt(v)]
        for cl in range(m):
            # Skip the chunk lengths that would result in unequal chunk sizes 
            if n % (cl+1): continue
            # Create a generator, to split the value v into chunks of length cl
            chunks = split(v, cl+1)
            # If the sum of the chunks is 100, print a message and return True
            if sum(chunks) == 100:
                print("sum = 100 with chunklength: %d" % cl)
                return True
        # If we get here, none of the chunk lengths result in a sum of 100, return False
        return False
    
    print(f(5050))      # True (cl=2)
    print("---")
    print(f(12345))     # False
    print("---")
    print(f(25252525))  # True (cl=2)
    print("---")
    

Output:

    
    
    sum = 100 with chunklength: 2
    True
    ---
    False
    ---
    sum = 100 with chunklength: 2
    True
    ---
    

Without comments and debugging `print`:

    
    
    import math
    
    def split(v, cl):
        while v:
            (v,c) = divmod(v, 10**cl)
            yield c
    
    def f(v):
        n = int(math.log10(v)) + 1
        m = int(math.sqrt(v))
        for cl in range(m):
            if n % (cl+1): continue
            if sum(split(v, cl+1)) == 100: return True
        return False
    
    print(f(5050))      # True
    print(f(12345))     # False
    print(f(25252525))  # True
    

