guest@cp-base:~/home/number-theory$
templates/number-theory-basics.cpp
compilable
$cat templates/number-theory-basics

Number Theory Basics

Primality test, factorization, Euler's totient, divisors, integer sqrt, and perfect power check.

#prime#factorization#totient#divisors#sqrt
Log in to track progress, save custom versions, and organize into collections.Log In
$cat source_code/
cpp
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

bool isPrime(ll n) {
    if (n < 2) return false;
    if (n < 4) return true;
    if (n % 2 == 0 || n % 3 == 0) return false;
    for (ll i = 5; i * i <= n; i += 6)
        if (n % i == 0 || n % (i + 2) == 0) return false;
    return true;
}

vector<ll> primeFactorization(ll n) {
    vector<ll> factors;
    while (n % 2 == 0) { factors.push_back(2); n /= 2; }
    for (ll i = 3; i * i <= n; i += 2)
        while (n % i == 0) { factors.push_back(i); n /= i; }
    if (n > 1) factors.push_back(n);
    return factors;
}

ll totient(ll n) {
    ll result = n;
    for (ll i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            while (n % i == 0) n /= i;
            result -= result / i;
        }
    }
    if (n > 1) result -= result / n;
    return result;
}

vector<ll> divisors(ll n) {
    vector<ll> divs;
    for (ll i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            divs.push_back(i);
            if (i != n / i) divs.push_back(n / i);
        }
    }
    return divs;
}

int countDivisors(ll n) {
    int cnt = 0;
    for (ll i = 1; i * i <= n; i++)
        if (n % i == 0) { cnt += 2; if (i == n / i) cnt--; }
    return cnt;
}

ll sumDivisors(ll n) {
    ll sum = 0;
    for (ll i = 1; i * i <= n; i++)
        if (n % i == 0) { sum += i; if (i != n / i) sum += n / i; }
    return sum;
}

ll isqrt(ll n) {
    ll s = (ll)sqrt((double)n);
    while (s > 0 && s * s > n) s--;
    while ((s + 1) * (s + 1) <= n) s++;
    return s;
}

bool isPerfectSquare(ll n) {
    if (n < 0) return false;
    ll s = isqrt(n);
    return s * s == n;
}

bool isPerfectPower(ll n, int base) {
    if (n <= 0) return false;
    if (n == 1) return true;
    ll p = 1;
    while (p < n) {
        p *= base;
        if (p == n) return true;
        if (p > n / base) break;
    }
    return false;
}
83 linesutf-8
$cat explanation_notes.md
notes_viewer --renderedmarkdown (math enabled)

Number Theory Basics

Functions

  • isPrime(n) — trial division with 6k±16k \pm 1 optimization, O(n)O(\sqrt{n})

  • primeFactorization(n) — returns all prime factors with repetition

  • totient(n) — Euler's φ(n)=npn(11/p)\varphi(n) = n \prod_{p \mid n} (1 - 1/p)

  • divisors(n) — all divisors (unsorted)

  • countDivisors(n) / sumDivisors(n) — count or sum of divisors

  • isqrt(n) — integer square root, adjusted from floating-point to avoid precision bugs for large nn

  • isPerfectSquare(n) — uses isqrt, safe for full ll range

  • isPerfectPower(n, base) — integer-only loop, no floating-point
  • Bug Fixes vs Original

  • isqrt adjusts ±1\pm 1 from (ll)sqrt(n) — the original silently fails for large perfect squares like 101810^{18}

  • isPerfectPower uses multiplication loop instead of log to avoid precision errors

  • primeFactorization uses ll loop variable (original used int, overflows for large nn)
  • Complexity

  • All functions — O(n)O(\sqrt{n}) except isPerfectPower which is O(logbasen)O(\log_{\text{base}} n)