工具 指南
math 免费 无需注册

质数检查器

检查任意10¹²以内的数是否为质数,查看其因式分解,并探索相关质数信息。

正在加载工具…

关于此工具

输入任意不超过一万亿的整数,即时判断其是否为质数。对大数使用Miller-Rabin质数测试。显示合数的质因数分解,找出下一个和上一个质数,识别特殊质数(梅森质数、孪生质数、安全质数),并可生成前N个质数。

使用方法

  1. 1 在数字字段中输入一个正整数。
  2. 2 点击检查以测试质数性质并查看因式分解。
  3. 3 使用"下一个质数"和"上一个质数"在质数序列中导航。
  4. 4 设置数量并点击"生成质数"以列出前N个质数。

What makes a number prime

A prime number is a whole number greater than 1 whose only divisors are 1 and itself. 7 is prime because nothing between 2 and 6 divides it evenly; 9 is not, because 3 × 3 = 9. Primes are the atoms of arithmetic: every integer above 1 is either prime or a unique product of primes, a result so fundamental it is called the Fundamental Theorem of Arithmetic. That uniqueness is why this tool, when a number is not prime, shows its full prime factorization — the one and only way to break it into prime building blocks.

How the tool tests primality so fast

The obvious way to check a number is trial division — try dividing by every integer up to its square root. That is fine for small numbers but crawls for large ones. This tool instead uses the Miller–Rabin primality test with a fixed set of "witnesses." Miller–Rabin reframes the question: it picks a base a and checks an algebraic identity that every prime must satisfy. If the number fails the identity for any witness, it is definitely composite. If it passes for the right set of witnesses, it is guaranteed prime below a known limit.

Crucially, the implementation uses a deterministic set of witnesses — the small primes 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41 — which are mathematically proven to give a correct answer for every number this tool accepts (up to 10¹²). The arithmetic is done with JavaScript BigInt so that the intermediate squarings never overflow. The result is an exact yes/no, not a probabilistic guess, computed almost instantly even for twelve-digit inputs.

A worked example

Enter 561. It looks like it might be prime — it is odd and not obviously divisible by small numbers. Trial division finds 561 = 3 × 11 × 17, so the tool reports it composite and shows that factorization. (561 is famous as the smallest Carmichael number, a composite that fools the simplest primality tests — but not Miller–Rabin with these witnesses.) Now enter 8191. It passes every witness, so the tool reports PRIME, and because 8191 = 2¹³ − 1 it also flags it as a Mersenne prime.

The special-prime badges

When a number is prime, the tool surfaces some of the structure mathematicians find interesting:

BadgeMeaningExample
Mersenne primeOne less than a power of two, 2ᵏ − 1, where k is itself prime31 = 2⁵ − 1
Twin primeDiffers by 2 from another prime(11, 13)
Safe prime(p − 1)/2 is also prime23, since 11 is prime

These are not trivia for their own sake. Mersenne primes drive the search for the largest known primes; twin primes sit at the heart of a famous unsolved conjecture; and safe primes are deliberately chosen in cryptography because their structure resists certain attacks on Diffie–Hellman key exchange.

Reading the factorization

For composite numbers the tool computes the prime factorization by trial division up to the square root, collecting repeated factors into exponents. So 360 is shown as 360 = 2³ × 3² × 5 rather than the longer 2 × 2 × 2 × 3 × 3 × 5. The exponent form is the compact, canonical way to write a factorization and the form you need for finding divisor counts, greatest common divisors, or least common multiples.

Why primes matter in the real world

  • Public-key cryptography. RSA encryption relies on the fact that multiplying two large primes is easy but factoring the product back is, with current methods, infeasible. The security of much of the web rests on that asymmetry.
  • Hashing and data structures. Hash tables often use a prime number of buckets to spread keys evenly and avoid clustering.
  • Pseudo-random generators. Many random-number algorithms use prime moduli to maximise their cycle length.
  • Error-correcting codes. Several coding schemes are built over fields whose size is prime.

Tips and common misconceptions

  • 1 is not prime. By definition a prime has exactly two distinct divisors; 1 has only one, so it is excluded. The tool requires inputs of at least 1 and treats 1 as non-prime.
  • 2 is the only even prime. Every other even number is divisible by 2, so primality "thins out" to odd candidates above 2.
  • Primes never stop. Euclid proved over two thousand years ago that there are infinitely many — the "Generate first N primes" panel just lists them in order, starting from 2.
  • Ending in 5 or an even digit means composite. Except for 2 and 5 themselves, any number ending in 0, 2, 4, 5, 6, or 8 is divisible by 2 or 5 and cannot be prime — a quick sanity check before you even type.

Everything runs in your browser; numbers you test are never sent anywhere.

常见问题

{# Alpine.js — self-hosted. (The previous jsdelivr CDN tag had a stale SRI integrity hash, so the browser refused to run it and window.Alpine was never defined — silently breaking every FAQ accordion and Alpine tool.) #}