Analyze prime numbers

From pjt222/agent-almanac · View interactive version

Analyze prime numbers is an AI agent skill from the GitHub repository pjt222/agent-almanac. Analyze prime numbers using primality tests, factorization algorithms, prime distribution analysis, and sieve methods. Covers trial division, Miller-Rabin, Sieve of Eratosthenes, and the Prime Number Theorem. Use when determining whether an integer is prime or composite, finding prime factorizations

How to use this skill

Skill file

Analyze Prime Numbers

Select + apply right algo: primality, factorization, distribution. Verify computationally + relate to Prime Number Theorem.

Use When

In

Do

Step 1: Determine Task

Classify → 1 of 3 + select algo path:

  1. Primality: Int n, prime?
  2. Factorization: Composite n, complete prime factorization
  3. Distribution: Bound N, analyze primes ≤ N (count, list, gaps, density)

Record task + in values.

→ Clear classification + in values recorded.

If err: Ambiguous ("analyze 60") → ask clarify primality vs factorization vs distribution. Default factorization for composites + primality confirm suspected primes.

Step 2: Primality Testing (if task = primality)

Test n prime, algo matched to size:

  1. Trivial: n < 2 not prime. n = 2 or 3 prime. n even + n > 2 → composite.
  1. Small n (<10^6): Trial division.
  1. Large n (>=10^6): Miller-Rabin probabilistic.
  1. Record verdict: prime or composite + witness/cert.

Small primes (1st 25):

| Index | Prime | Index | Prime | Index | Prime |

|---|---|---|---|---|---|

| 1 | 2 | 10 | 29 | 19 | 67 |

| 2 | 3 | 11 | 31 | 20 | 71 |

| 3 | 5 | 12 | 37 | 21 | 73 |

| 4 | 7 | 13 | 41 | 22 | 79 |

| 5 | 11 | 14 | 43 | 23 | 83 |

| 6 | 13 | 15 | 47 | 24 | 89 |

| 7 | 17 | 16 | 53 | 25 | 97 |

| 8 | 19 | 17 | 59 | | |

| 9 | 23 | 18 | 61 | | |

→ Definitive (prime/composite) + algo used + witnesses/divisors.

If err: Miller-Rabin "probably prime" + certainty needed → escalate deterministic (AKS or ECPP). Trial div too slow → Miller-Rabin.

Step 3: Factorization (if task = factorization)

Factor n completely → prime power decomposition:

  1. Extract small factors by trial div:
  1. Cofactor > 1 + <10^12: Continue trial div ≤ sqrt(cofactor).
  1. Cofactor >= 10^12: Pollard's rho.
  1. Verify: Multiply all prime factors + exponents = original n. Test each factor primality.
  1. Present: n = p1^a1 * p2^a2 * ... * pk^ak, p1 < p2 < ... < pk.

Algo complexity:

| Algo | Complexity | Best for |

|---|---|---|

| Trial division | O(sqrt(n)) | n < 10^12 |

| Pollard's rho | O(n^{1/4}) expected | n up to ~10^18 |

| Quadratic sieve | L(n)^{1+o(1)} | n up to ~10^50 |

| GNFS | L(n)^{(64/9)^{1/3}+o(1)} | n > 10^50 |

→ Complete prime factorization canonical form + multiplication verified.

If err: Pollard's rho fails after many iters (cycle w/o non-trivial gcd) → try diff c (≥5 attempts). All fail → cofactor may be prime → confirm primality.

Step 4: Distribution Analysis (if task = distribution)

Distribution of primes up to N:

  1. Generate via Sieve Eratosthenes:
  1. Count: pi(N) = primes up to N.
  1. Compare w/ PNT:
  1. Analyze gaps (optional):
  1. Present summary:
Bound N:       1,000,000
pi(N):         78,498
N/ln(N):       72,382
Li(N):         78,628
Relative error (N/ln(N)):  7.79%
Relative error (Li(N)):    0.17%
Max prime gap:  148 (between 492113 and 492227)
Twin primes:    8,169 pairs

→ Count + PNT compare + optional gap analysis.

If err: N too large in-mem sieve (N > 10^9) → segmented sieve processes range in blocks. Count only (no list) → Meissel-Lehmer for pi(N) direct.

Step 5: Verify Computationally

Cross-check via independent method:

  1. Primality: Trial div used → verify quick Miller-Rabin (or vice versa). Known primes → check published tables or OEIS.
  1. Factorization: Multiply factors + confirm = original. Independently test each claimed prime.
  1. Distribution: Spot-check 3-5 numbers from sieve out for primality. Compare pi(N) published values (pi(10^k) k = 1, ..., 9).

Published pi(N):

| N | pi(N) |

|---|---|

| 10 | 4 |

| 100 | 25 |

| 1,000 | 168 |

| 10,000 | 1,229 |

| 100,000 | 9,592 |

| 10^6 | 78,498 |

| 10^7 | 664,579 |

| 10^8 | 5,761,455 |

| 10^9 | 50,847,534 |

  1. Doc verification + method + outcome.

→ All results independently verified no discrepancies.

If err: Verification → discrepancy → re-run w/ extra checks (verbose trial div logging). Common: off-by-one sieve bounds, int overflow modular arithmetic, pseudoprime mistaken prime.

Check

Traps

→

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.