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

Analyze prime numbers by selecting and applying the appropriate algorithm for the task at hand: primality testing, integer factorization, or prime distribution analysis. Verify results computationally and relate findings to the Prime Number Theorem.

When to Use

Inputs

Procedure

Step 1: Determine the Task Type

Classify the request into one of three categories and select the appropriate algorithmic path.

  1. Primality test: Given a single integer n, determine whether n is prime.
  2. Factorization: Given a composite integer n, find its complete prime factorization.
  3. Distribution analysis: Given a bound N, analyze the primes up to N (count, list, gaps, density).

Record the task type and the input value(s).

Got: A clear classification with the input values recorded.

If fail: If the input is ambiguous (e.g., "analyze 60"), ask the user to clarify whether they want a primality test, factorization, or distribution analysis. Default to factorization for composite numbers and primality confirmation for suspected primes.

Step 2: Apply Primality Testing (if task = primality)

Test whether n is prime using an algorithm matched to the size of n.

  1. Handle trivial cases: n < 2 is not prime. n = 2 or n = 3 is prime. If n is even and n > 2, it is composite.
  1. Small n (n < 10^6): Use trial division.
  1. Large n (n >= 10^6): Use Miller-Rabin probabilistic test.
  1. Record the verdict: prime or composite, with the witness or certificate.

Small primes reference (first 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 | | |

Got: A definitive answer (prime or composite) with the algorithm used and any witnesses or divisors found.

If fail: If Miller-Rabin reports "probably prime" but certainty is required, escalate to a deterministic test (e.g., AKS or ECPP). For trial division, if computation is too slow, switch to Miller-Rabin.

Step 3: Apply Factorization (if task = factorization)

Factor n completely into its prime power decomposition.

  1. Extract small factors by trial division:
  1. If cofactor > 1 and cofactor < 10^12: Continue trial division up to sqrt(cofactor).
  1. If cofactor > 1 and cofactor >= 10^12: Apply Pollard's rho algorithm.
  1. Verify: Multiply all found prime factors (with exponents) and confirm the product equals the original n. Test each factor for primality.
  1. Present the result in standard form: n = p1^a1 * p2^a2 * ... * pk^ak with p1 < p2 < ... < pk.

Algorithm complexity notes:

| Algorithm | 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 |

Got: A complete prime factorization in canonical form, verified by multiplication.

If fail: If Pollard's rho fails to find a factor after many iterations (cycle detected without a non-trivial gcd), try different values of c (at least 5 attempts). If all fail, the cofactor may be prime -- confirm with a primality test.

Step 4: Apply Distribution Analysis (if task = distribution)

Analyze the distribution of primes up to a given bound N.

  1. Generate primes using the Sieve of Eratosthenes:
  1. Count primes: Compute pi(N) = number of primes up to N.
  1. Compare with the Prime Number Theorem:
  1. Analyze prime gaps (optional):
  1. Present findings in a summary table:
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

Got: A count of primes with PNT comparison and optional gap analysis.

If fail: If N is too large for in-memory sieving (N > 10^9), use a segmented sieve that processes the range in blocks. If only a count is needed (not a list), use the Meissel-Lehmer algorithm for pi(N) directly.

Step 5: Verify Results Computationally

Cross-check all results using an independent computation method.

  1. For primality: If trial division was used, verify with a quick Miller-Rabin pass (or vice versa). For known primes, check against published prime tables or OEIS sequences.
  1. For factorization: Multiply all factors and confirm equality with the original input. Independently test each claimed prime factor for primality.
  1. For distribution: Spot-check by testing 3-5 individual numbers from the sieve output for primality. Compare pi(N) against published values for standard benchmarks (pi(10^k) for k = 1, ..., 9).

Published values of 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. Document the verification with the method used and the outcome.

Got: All results independently verified with no discrepancies.

If fail: If verification reveals a discrepancy, re-run the original computation with extra checks enabled (e.g., verbose trial division logging). The most common errors are off-by-one in sieve bounds, integer overflow in modular arithmetic, and mistaking a pseudoprime for a prime.

Validation

Pitfalls

Related Skills

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.