Prime Factorization and Divisors

Recall: Fundamental Theorem Of Arithmetic

Important Points

  1. The number of divisors is odd only for perfect square numbers. For the rest, the count is even. Use floor(sqrt(b)) - ceil(sqrt(a)) + 1 to calculate all perfect square numbers between a and b.
  2. Only a prime’s square has exactly 3 distinct factors i.e. 1, p, and p2.
  3. Power of a prime p in n! is given by Legendre’s Factorization of n! => Power of p in n! = floor(n/p) + floor(n/p^2) + floor(n/p^3)..... till p^k > n. We can also calculate prime signature by finding out the exponents for each prime starting from 2 till p <= sqrt(n).
  4. Maximum occurring divisor in an interval is 2.
  5. Common divisors of two numbers are divisors of their GCD.
  6. Numbers which gets divided by both n and m upto q => q / LCM(n, m), Numbers that get divided by n or m upto q => (q / m + q / n - q / LCM(n, m)).

Finding Divisors

Time = O(n)

void printDivisors(int n)
{
	for (int i = 1; i <= n; ++i)		//till n
	{
		if(n % i == 0) 
			cout << i << " ";
	}
}

Efficient Approach: Suitable for finding sum of divisors (Aliquot Sum) as it does not print divisors in ascending order.

Time = O(โˆšn)

void printDivisors(int n)
{
    for (int i = 2; i*i <= n; ++i)      //till sqrt(n)
    {
        if(n % i == 0) 
        {   
            if(i*i == n) cout << i << " ";      //for cases like, n=16, i and (n/i) both will be 4, we print only one
            else cout << i << " " << (n/i) << " ";
        }    
    }

    cout << 1; //don't forget the 1
}

Aliquot Sum:

int sumDiv(int n)
{
    int sum = 0;
    for (int i = 2; i * i <= n; ++i)
    {
        if (n % i == 0)
        {
            if (i * i == n) sum += i;
            else sum += i + (n / i);
        }
    }

    return sum + 1;     //as we started loop from 2 to avoid adding n itself
}

Sieve Based Method:

Time = O(len) where len is the number of divisors, and

Space = O(MAX)

const int MAX = 1e5;

vector<int> divisor[MAX + 1];   //array of vectors

void divisorSieve()
{
    for (int i = 1; i <= MAX; i++)
    {
        for (int j = i; j <= MAX; j += i)
        {
            divisor[j].push_back(i);
        }
    }
}

void printDiv(int n)
{

    for (int j = 0; j < divisor[n].size(); j++)
    {
        cout << divisor[n][j] << " ";
    }
}

int main() {

    int n = 10;
    //cin >> n;

    divisorSieve();

    printDiv(n);
}

Prime Factorization

Time = O(โˆšn)

void printPrimeFactors(int n)
{
    for(int i = 2; i*i <= n; i++)
    {
        while(n % i == 0)       //removing every i from n
        {
            cout << i << " ";
            n /= i;
        }
    }

    if(n > 1) cout << n;    //the largest factor is prime itself
}

Using Sieve in O(log n) time

Link: https://www.geeksforgeeks.org/prime-factorization-using-sieve-olog-n-multiple-queries/

The idea is to store the Smallest Prime Factor (SPF) for every number in a separate array. Then to calculate the prime factorization of the given number by dividing the given number repeatedly with its smallest prime factor till it becomes 1 and storing the quotient in every step. Those quotients are prime factors of the number.

void factorSieve(int n, bool* s, int* spf)
{
    s[0] = s[1] = false;
    spf[1] = 1;
    for (int i = 2; i <= n; i++)    //notice i<=n
    {
        if (s[i])
        {
            spf[i] = i;     //change
            for (int j = i * i; j <= n; j += i)
            {
                s[j] = false;
                if (spf[j] == -1) spf[j] = i;   //change
            }
        }
    }
}

int main()
{

    int n;
    cin >> n;

    bool s[n + 1];
    int spf[n + 1];
    memset(s, true, sizeof(s));
    memset(spf, -1, sizeof(spf));
        
    factorSieve(n, s, spf);

    //main logic
    while (n != 1)
    {
        cout << spf[n] << " ";
        n /= spf[n];
    }

    return 0;
}
Pollard’s Rho Algorithm

Link: https://www.geeksforgeeks.org/pollards-rho-algorithm-prime-factorization/

Applications

Prime Signature - https://mathworld.wolfram.com/PrimeSignature.html

Link: https://www.handakafunda.com/number-system-concepts-for-cat-even-factors-odd-factors-sum-of-factors/

  1. All factors
  2. Even/Odd factors
  3. Sum of all factors
  4. Sum of Even/Odd factors
  5. Sum of all factors divisible by a number

Types of Numbers based on Divisors

  1. Smith Numbers - Composite numbers with sum of prime factors equal to sum of digits in the number.
  2. Sphenic Numbers - A positive integer having exactly three distinct prime factors. Alternatively, it is a product of exactly three distinct primes. Every sphenic number will have exactly 8 divisors.
  3. Hoax Numbers - Similar to Smith numbers but here factors must be distinct. Some Hoax Numbers are Smith Numbers.
  4. Frugal Number - A number whose number of digits is strictly greater than the number of digits in its prime factorization (including exponents). As apparent, a prime number cannot be frugal.
  5. Blum Integer - It is a semiprime (product of two distinct primes) and those two prime factors are of the form 4t+3 where t is some integer.
  6. Superperfect Number - A superperfect number is a positive integer which satisfies ฯƒ2(n) = ฯƒ(ฯƒ(n)) = 2*n, where ฯƒ is divisor summatory function.
  7. Powerful Number - A number is said to be Powerful Number if for every prime factor p of it, p2 also divides it. For example, 36 is a powerful number as it is divisible by both 3 and square of 3 i.e, 9.
  8. Deficient Number - Sum of divisors of the number is less than twice the value of the number n. The difference between these two values is called the deficiency.
  9. Perfect Number - A number is a perfect number if is equal to sum of its proper divisors, that is, sum of its positive divisors excluding the number itself. Ex - 6 = 1 + 2 + 3. It is unknown whether there are any odd perfect numbers.
  10. Betrothed numbers - Betrothed numbers are two positive numbers such that the sum of the proper divisors of either number is one more than (or one plus) the value of the other number. n1 = sum2 - 1 and n2 = sum1 - 1.
  11. k-Rough Number or k-Jagged Number - A k-rough or k-jagged number is a number whose smallest prime factor is greater than or equal to the number โ€˜kโ€™.
  12. Amicable Pair - Two different numbers so related that the sum of the proper divisors of each is equal to the other number.
  13. Friendly Pair - Two numbers whose ratio of sum of divisors and number itself is equal.
  14. P-Smooth Number or P-friable Number - An integer is Pโ€“smooth number if the largest Prime factor of that number <= p. 1 is considered (by OEIS) as Pโ€“smooth number for any possible value of P because it does not have any prime factor.
  15. k-Hyperperfect Number - A number n is called k-hyperperfect if: n = 1 + k โˆ‘idi where all di are the proper divisors of n. Taking k = 1 will give us perfect numbers.

LC Problems

  • Bulb Switcher link