Finding integer with the most natural dividers Problem description: You are given a segment [568023; 569230]. Find integer within given range that has the most natural dividers. If 2 or more integers are found, return the lowest integer among them.
Solution:
def allnums(k):
    cnt = 0
    for i in range(2, k // 2):
        if k % i == 0:
            cnt += 1
    return cnt

MAX_NUM = 0
MAX = 0

for i in range(568023, 569231):
    cnt = allnums(i)
    if MAX &lt; cnt:
        MAX = cnt
        MAX_NUM = i

print(MAX, ' ', MAX_NUM)


Output: 141   568260
Runtime: 17s

Question: Is there any way to make this code more concise, maybe also improving its runtime? Thank you in advance.
It's not clear what you mean with &quot;natural dividers&quot;. Better specify that, show examples, and document allnums to say what it does. A better name would also help. For example allnums(6) returns only 1, despite having divisors 1, 2, 3 and 6. At the very least I'd expect answer 2 (for the divisors 2 and 3). So probably it's incorrect, but we can't even tell because you didn't say what exactly it's supposed to do.
Anyway, as usual it suffices to try divisors until the square root, not until half. If you find a divisor i of k, then don't just count that divisor but also count its partner divisor k/i. Unless that's the same.
And max can take an iterable and key.
So:
from math import isqrt

def divisors(k):
    &quot;&quot;&quot;Compute the number of k's divisors 1 &lt; divisor &lt; k.&quot;&quot;&quot;
    cnt = 0
    for i in range(2, isqrt(k) + 1):
        if k % i == 0:
            cnt += 2 - (k // i == i)
    return cnt

print(max(range(568023, 569231), key=divisors))

Takes about 0.1 seconds.