[PYTHON] Library organization of competition professionals ~ enumeration of divisors ~

This article is a diversion of what was written as Python code and a rewrite of it in C ++. Also, when using C ++ code, please use it together with Template (it will not work unless you use it together).

enumerate_divisors.py


def make_divisors(n):
    divisors=[]
    for i in range(1,int(n**0.5)+1):
        if n%i==0:
            divisors.append(i)
            if i!=n//i:
                divisors.append(n//i)
    #If you want to sort in ascending order of divisors
    #divisors.sort()
    #If you want to sort in descending order of divisors
    #divisors.sort(reverse=True)
    return divisors

enumerate_divisors.cc


vector<ll> divisors;//Array to store divisors

void make_divisors(ll n){
    FOR(i,1,sqrt(n)){
        if(n%i==0){
            divisors.PB(i);
            if(i!=n/i){
                divisors.PB(n/i);
            }
        }
    }
    //If you want to sort in ascending order of divisors
    //sort(ALL(divisors));
    //If you want to sort in descending order of divisors
    //sort(ALL(divisors),greater<ll>());
}

Recommended Posts

Library organization of competition professionals ~ enumeration of divisors ~
Library organization of competition professionals ~ Two-dimensional linear indefinite equation ~
[For competition professionals] Summary of doubling
Organize the library of competitive professionals ~ Dice ~
Confirmation of basics of competition professionals ~ gcd and lcm ~