🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 9 (from laksa181)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

📄
INDEXABLE
CRAWLED
13 days ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.5 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://tungmphung.com/introduction-to-prime-numbers/
Last Crawled2026-04-06 07:19:26 (13 days ago)
First Indexed2020-05-23 23:49:56 (5 years ago)
HTTP Status Code200
Meta TitleIntroduction to Prime Numbers
Meta DescriptionI. What is a Prime Number? A number is called prime if it satisfies all the following conditions: is a natural number,greater than 1,has only 2 divisors: 1 and itself. For example, are the first prime numbers. If so, what is NOT a prime number? not a natural number (e.g.…
Meta Canonicalnull
Boilerpipe Text
I. What is a Prime Number? A number is called prime if it satisfies all the following conditions: is a natural number, greater than 1, has only 2 divisors: 1 and itself. For example, are the first prime numbers. If so, what is NOT a prime number? II. Why is Prime Number important? In the modern world, prime numbers are crucial for: Hashing Cryptography 1. Hashing In competitive programming, Hashing is a great technique, which is often used when dealing with complex string operations. Usually, each sub-string is hashed to a number (we represent each sub-string by a number). 2 equal sub-strings will always be transformed to the same number. 2 different sub-strings will most likely be transformed into 2 different numbers. The case when 2 different sub-strings are transformed to the same number is called Collision. When doing hash, we want to make collisions as less likely to happen as possible. The concept of prime numbers is the key to reduce the chance of collision. To be more precise, when the distribution of your “keys” (your sub-strings as in the above example) is not uniform, many of your keys are in the form: y = ka + b with constant b , and somehow a is a divisor of MOD , then there will be many collisions. Hence, to be careful, we can set a prime number for MOD , and because prime numbers do not have any divisors other than 1 and itself, the bad case described above will not happen. If you are a bit confused about Hashing, MOD or any terms from this Hashing section, please take a look at Introduction to Hashing . 2. Cryptography Modern cryptography uses prime numbers a lot. Private-public key algorithms exploit the fact that: it is easy to multiply 2 prime numbers, but very very hard to factorize a big number back to primes. You can imagine that a big number is a number that has 100 (or more) digits in its decimal representation. RSA and Digital Signature are the most famous examples. III. How to test for primality? Given a number x , we need to determine if x is a prime number or not. 1. Exhaustive search Straightforward from the definition of a prime number, we can determine whether x is prime by looping through every number in the range , x is prime if and only if none of these numbers divides x . bool isPrime(int x) { if (x < 2) return false; for (int i = 2; i < x; ++i) if (x % i == 0) return false; return true; } Complexity: 2. Exhaustive search (enhanced) We can reduce the complexity of the above algorithm by an observation: if and x is NOT a prime number, then we can write with , and . Why? Because if both and are true, then . Contradict with the fact that . bool isPrime(int x) { if (x < 2) return false; int sqrtx = sqrt(x); for (int i = 2; i <= sqrtx; ++i) if (x % i == 0) return false; return true; } Complexity: 3. Miller-Rabin test Miller-Robin primality test is a probabilistic test, that is, the test USUALLY outputs the right result (NOT ALWAYS). To be a little bit more specific, when the test outputs False (the test says that x is not a prime number), it is absolutely true that x is not prime number, but when the test outputs True, we know that x is likely to be a prime number (but we are not 100% sure). Good news is that we have a way to make this test deterministic for 32-bit and 64-bit integer numbers. Because the explanation is a bit complex and lengthy, I will move it to a separated post. Follow this link to see it! Complexity: or better (with k ~ 12 for 64-bit integer) 4. Test for primality in with pre-processing (using Sieve of Eratosthenes) Sieve of Eratosthenes is a technique to pre-compute all prime numbers in the range (1, n]. It also supports testing for the primality of any number in the above range in time complexity. Complexity: Details about this technique can be found Here IV. Practice Problems SPOJ – PON V. Further Reading Sieve of Eratosthenes Prime factorization Totient function RSA Digital Signature
Markdown
[Skip to content](https://tungmphung.com/introduction-to-prime-numbers/#content) ## [Tung M Phung's Blog](https://tungmphung.com/) ### Primary Menu - [Home](https://tungmphung.com/) - [About me](https://me.tungmphung.com/) Search [Competitive Programming](https://tungmphung.com/competitive-programming/), [Number Theory](https://tungmphung.com/competitive-programming/number-theory/) # Introduction to Prime Numbers ![](https://tungmphung.com/wp-content/uploads/2019/08/arts-arts-and-crafts-concept-1314543.jpg) I. What is a Prime Number? A number is called prime if it satisfies all the following conditions: - is a natural number, - greater than 1, - has only 2 divisors: 1 and itself. For example, ![\\{ 2, 3, 5, 7 ,11, 13, 17, 19 \\}](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-aa20022af1b7afac9df3381e1fe1470e_l3.png) are the first prime numbers. *If so, what is NOT a prime number?* - not a natural number (e.g. a fraction, like ![\\frac{1}{2}](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-9ef98fc721db891124a0bf73d1da2647_l3.png) or ![\\frac{14}{5}](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-4cc9e8e713543b318ae6f398bbfae351_l3.png)), - less than or equal to 1 (e.g. -3, 0, 1 are NOT prime), - can be formed by multiplying 2 natural numbers less than itself (e.g. ![12 = 3 \* 4](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-f4e76d235f7c95e9ecbb892d0e06a0a9_l3.png) is NOT a prime number). II. Why is Prime Number important? In the modern world, prime numbers are crucial for: - Hashing - Cryptography 1\. Hashing In competitive programming, Hashing is a great technique, which is often used when dealing with complex string operations. Usually, each sub-string is hashed to a number (we represent each sub-string by a number). 2 equal sub-strings will always be transformed to the same number. 2 different sub-strings will most likely be transformed into 2 different numbers. The case when 2 different sub-strings are transformed to the same number is called Collision. When doing hash, we want to make collisions as less likely to happen as possible. The concept of prime numbers is the key to reduce the chance of collision. To be more precise, when the distribution of your “keys” (your sub-strings as in the above example) is not uniform, many of your keys are in the form: *y = ka + b* with constant *b*, and somehow *a* is a divisor of *MOD*, then there will be many collisions. Hence, to be careful, we can set a prime number for *MOD*, and because prime numbers do not have any divisors other than 1 and itself, the bad case described above will not happen. If you are a bit confused about Hashing, MOD or any terms from this Hashing section, please take a look at **[Introduction to Hashing](https://tungmphung.com/introduction-to-hashing/)**. 2\. Cryptography Modern cryptography uses prime numbers a lot. Private-public key algorithms exploit the fact that: it is easy to multiply 2 prime numbers, but very very hard to factorize a big number back to primes. You can imagine that a big number is a number that has 100 (or more) digits in its decimal representation. RSA and Digital Signature are the most famous examples. III. How to test for primality? Given a number *x*, we need to determine if *x* is a prime number or not. 1\. Exhaustive search Straightforward from the definition of a prime number, we can determine whether *x* is prime by looping through every number in the range ![\[2, x-1\]](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-0283d48f8866f23832d24194f06a9b2d_l3.png), *x* is prime if and only if none of these numbers divides *x*. ``` bool isPrime(int x) { if (x < 2) return false; for (int i = 2; i < x; ++i) if (x % i == 0) return false; return true; } ``` Complexity: ![O(n)](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-a16f8e9730a73dbb6d30856fea7a6edb_l3.png) 2\. Exhaustive search (enhanced) We can reduce the complexity of the above algorithm by an observation: if ![x \\geq 2](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-e9c7b3d645fec10cf9a5b9d324831fd4_l3.png) and x is NOT a prime number, then we can write ![x = a\*b](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-48805d68641218c4f597f363a6200ee8_l3.png) with ![a \\geq 2](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-a7b095a97f4665e27cb8341f7fb1a413_l3.png), ![b \\geq 2](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-4d91254035bce11996c193ea3465fe3d_l3.png) and ![min(a, b) \\leq \\sqrt{x}](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-57e8c80923a1a7863d1e68003e4919c5_l3.png). Why? Because if both ![a \> \\sqrt{x}](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-3e9fa12708071df079eed8b6218f0fb7_l3.png) and ![b \> \\sqrt{x}](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-148e128ff707b009b1c5866eca38854f_l3.png) are true, then ![a\*b \> \\sqrt{x} \* \\sqrt{x} = x](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-c4c14b27fc2393d259da54906af48351_l3.png). Contradict with the fact that ![a\*b = x](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-c7c5b961cd9bbb35bc1a53e6a0ae63df_l3.png). ``` bool isPrime(int x) { if (x < 2) return false; int sqrtx = sqrt(x); for (int i = 2; i <= sqrtx; ++i) if (x % i == 0) return false; return true; } ``` Complexity: ![O(\\sqrt{n})](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-e81d36ce0c0129e50b6e2918315ab50d_l3.png) 3\. Miller-Rabin test Miller-Robin primality test is a probabilistic test, that is, the test USUALLY outputs the right result (NOT ALWAYS). To be a little bit more specific, when the test outputs False (the test says that *x* is not a prime number), it is absolutely true that *x* is not prime number, but when the test outputs True, we know that *x* is likely to be a prime number (but we are not 100% sure). Good news is that we have a way to make this test deterministic for 32-bit and 64-bit integer numbers. Because the explanation is a bit complex and lengthy, I will move it to a separated post. Follow **this link** to see it\! Complexity: ![O(k\*(\\log{n})^{3})](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-7ac643b6cd055fb29096fec6b193774b_l3.png) or better (with k ~ 12 for 64-bit integer) 4\. Test for primality in ![O(1)](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-8514351fd951ba2e124fae4028c17723_l3.png) with pre-processing (using Sieve of Eratosthenes) Sieve of Eratosthenes is a technique to pre-compute all prime numbers in the range (1, n\]. It also supports testing for the primality of any number in the above range in ![O(1)](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-8514351fd951ba2e124fae4028c17723_l3.png) time complexity. Complexity: - pre-processing: ![O(n\*\\log{\\log{n}})](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-0fdd6bd3f5d8b76435ace6f1094430d7_l3.png) and can be enhanced to ![O(n)](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-a16f8e9730a73dbb6d30856fea7a6edb_l3.png) - test for primality: ![O(1)](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-8514351fd951ba2e124fae4028c17723_l3.png) Details about this technique can be found **[Here](https://tungmphung.com/sieve-of-eratosthenes/)** IV. Practice Problems [SPOJ – PON](https://href.li/?https://www.spoj.com/problems/PON/) V. Further Reading [Sieve of Eratosthenes](https://tungmphung.com/sieve-of-eratosthenes/) Prime factorization Totient function RSA Digital Signature [August 27, 2019July 31, 2020](https://tungmphung.com/introduction-to-prime-numbers/) [Tung.M.Phung](https://tungmphung.com/author/tungmphung/)[math](https://tungmphung.com/tag/math/), [Miller-Rabin](https://tungmphung.com/tag/miller-rabin/), [prime numbers](https://tungmphung.com/tag/prime-numbers/), [Sieve of Eratosthenes](https://tungmphung.com/tag/sieve-of-eratosthenes/) ### Leave a Reply[Cancel reply](https://tungmphung.com/introduction-to-prime-numbers/#respond) ## Post navigation [Next Next post: What is Multicollinearity (or Collinearlity) ?](https://tungmphung.com/what-is-multicollinearity-or-collinearlity/) Sidebar ## Featured posts [Is ChatGPT as good as humans? A study in the field of programming education](https://tungmphung.com/is-chatgpt-as-good-as-humans-a-study-in-the-field-of-programming-education/) [A review of pre-trained language models: from BERT, RoBERTa, to ELECTRA, DeBERTa, BigBird, and more](https://tungmphung.com/a-review-of-pre-trained-language-models-from-bert-roberta-to-electra-deberta-bigbird-and-more/) [The Transformer neural network architecture](https://tungmphung.com/the-transformer-neural-network-architecture/) [Information Gain, Gain Ratio and Gini Index](https://tungmphung.com/information-gain-gain-ratio-and-gini-index/) [Ensemble: Bagging, Random Forest, Boosting and Stacking](https://tungmphung.com/wp-admin/post.php?post=4088&action=edit) ## Recently posted - [Plan More, Debug Less: Applying Metacognitive Theory to AI-Assisted Programming Education](https://tungmphung.com/plan-more-debug-less-applying-metacognitive-theory-to-ai-assisted-programming-education/) - [Is ChatGPT as good as humans? A study in the field of programming education](https://tungmphung.com/is-chatgpt-as-good-as-humans-a-study-in-the-field-of-programming-education/) - [Advanced (yet simple) Prompt Engineering techniques for Large Language Models](https://tungmphung.com/advanced-yet-simple-prompt-engineering-techniques-for-large-language-models/) - [Conversational AI: ChatGPT alternatives and their advantages](https://tungmphung.com/conversational-ai-chatgpt-alternatives-and-their-advantages/) - [Book Review: Problem Solving 101 by Ken Watanabe](https://tungmphung.com/book-review-problem-solving-101-by-ken-watanabe/) ## Categories - [Book Review](https://tungmphung.com/book-review/) (10) - [Parenting Book](https://tungmphung.com/book-review/parenting-book/) (2) - [Self-Help Book](https://tungmphung.com/book-review/self-help-book/) (8) - [Competitive Programming](https://tungmphung.com/competitive-programming/) (49) - [Dynamic Programming](https://tungmphung.com/competitive-programming/dynamic-programming/) (1) - [Flow](https://tungmphung.com/competitive-programming/flow/) (4) - [Geometry](https://tungmphung.com/competitive-programming/geometry/) (11) - [Graph](https://tungmphung.com/competitive-programming/graph/) (8) - [Hash](https://tungmphung.com/competitive-programming/hash/) (1) - [Matrix](https://tungmphung.com/competitive-programming/matrix/) (1) - [Number Theory](https://tungmphung.com/competitive-programming/number-theory/) (7) - [Range Query](https://tungmphung.com/competitive-programming/range-query/) (6) - [Scheduling](https://tungmphung.com/competitive-programming/scheduling/) (1) - [Search](https://tungmphung.com/competitive-programming/search/) (2) - [String](https://tungmphung.com/competitive-programming/string/) (5) - [Machine Learning – Data Mining](https://tungmphung.com/data-mining/) (96) - [Classification Models](https://tungmphung.com/data-mining/classification-models/) (12) - [Data Cleaning](https://tungmphung.com/data-mining/data-cleaning/) (1) - [Data Crawling and Scraping](https://tungmphung.com/data-mining/data-crawling-and-scraping/) (4) - [Database](https://tungmphung.com/data-mining/database/) (7) - [DataVisualization](https://tungmphung.com/data-mining/datavisualization/) (7) - [Deep Learning](https://tungmphung.com/data-mining/deep-learning/) (16) - [Exploratory Data Analysis](https://tungmphung.com/data-mining/exploratory-data-analysis/) (6) - [Feature Engineering](https://tungmphung.com/data-mining/feature-engineering/) (9) - [Intro to ML](https://tungmphung.com/data-mining/intro-to-ml/) (5) - [Miscellaneous](https://tungmphung.com/data-mining/miscellaneous/) (6) - [Preparatory Phase](https://tungmphung.com/data-mining/preparatory-phase/) (3) - [Regression Models](https://tungmphung.com/data-mining/regression-models/) (11) - [Reinforcement Learning](https://tungmphung.com/data-mining/reinforcement-learning/) (1) - [Statistics](https://tungmphung.com/data-mining/statistics/) (6) - [Subgroup Discovery](https://tungmphung.com/data-mining/subgroup-discovery/) (1) - [Text Mining](https://tungmphung.com/data-mining/text-mining/) (5) - [Software Development](https://tungmphung.com/software-development/) (6) ## Topics [Machine Learning – Data Mining](https://tungmphung.com/data-mining-machine-learning/) [Competitive Programming](https://tungmphung.com/competitive-programming-map/) [Book Review](https://tungmphung.com/book-review-map/)
Readable Markdown
![](https://tungmphung.com/wp-content/uploads/2019/08/arts-arts-and-crafts-concept-1314543.jpg) I. What is a Prime Number? A number is called prime if it satisfies all the following conditions: - is a natural number, - greater than 1, - has only 2 divisors: 1 and itself. For example, ![\\{ 2, 3, 5, 7 ,11, 13, 17, 19 \\}](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-aa20022af1b7afac9df3381e1fe1470e_l3.png) are the first prime numbers. *If so, what is NOT a prime number?* II. Why is Prime Number important? In the modern world, prime numbers are crucial for: - Hashing - Cryptography 1\. Hashing In competitive programming, Hashing is a great technique, which is often used when dealing with complex string operations. Usually, each sub-string is hashed to a number (we represent each sub-string by a number). 2 equal sub-strings will always be transformed to the same number. 2 different sub-strings will most likely be transformed into 2 different numbers. The case when 2 different sub-strings are transformed to the same number is called Collision. When doing hash, we want to make collisions as less likely to happen as possible. The concept of prime numbers is the key to reduce the chance of collision. To be more precise, when the distribution of your “keys” (your sub-strings as in the above example) is not uniform, many of your keys are in the form: *y = ka + b* with constant *b*, and somehow *a* is a divisor of *MOD*, then there will be many collisions. Hence, to be careful, we can set a prime number for *MOD*, and because prime numbers do not have any divisors other than 1 and itself, the bad case described above will not happen. If you are a bit confused about Hashing, MOD or any terms from this Hashing section, please take a look at **[Introduction to Hashing](https://tungmphung.com/introduction-to-hashing/)**. 2\. Cryptography Modern cryptography uses prime numbers a lot. Private-public key algorithms exploit the fact that: it is easy to multiply 2 prime numbers, but very very hard to factorize a big number back to primes. You can imagine that a big number is a number that has 100 (or more) digits in its decimal representation. RSA and Digital Signature are the most famous examples. III. How to test for primality? Given a number *x*, we need to determine if *x* is a prime number or not. 1\. Exhaustive search Straightforward from the definition of a prime number, we can determine whether *x* is prime by looping through every number in the range ![\[2, x-1\]](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-0283d48f8866f23832d24194f06a9b2d_l3.png), *x* is prime if and only if none of these numbers divides *x*. ``` bool isPrime(int x) { if (x < 2) return false; for (int i = 2; i < x; ++i) if (x % i == 0) return false; return true; } ``` Complexity: ![O(n)](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-a16f8e9730a73dbb6d30856fea7a6edb_l3.png) 2\. Exhaustive search (enhanced) We can reduce the complexity of the above algorithm by an observation: if ![x \\geq 2](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-e9c7b3d645fec10cf9a5b9d324831fd4_l3.png) and x is NOT a prime number, then we can write ![x = a\*b](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-48805d68641218c4f597f363a6200ee8_l3.png) with ![a \\geq 2](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-a7b095a97f4665e27cb8341f7fb1a413_l3.png), ![b \\geq 2](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-4d91254035bce11996c193ea3465fe3d_l3.png) and ![min(a, b) \\leq \\sqrt{x}](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-57e8c80923a1a7863d1e68003e4919c5_l3.png). Why? Because if both ![a \> \\sqrt{x}](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-3e9fa12708071df079eed8b6218f0fb7_l3.png) and ![b \> \\sqrt{x}](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-148e128ff707b009b1c5866eca38854f_l3.png) are true, then ![a\*b \> \\sqrt{x} \* \\sqrt{x} = x](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-c4c14b27fc2393d259da54906af48351_l3.png). Contradict with the fact that ![a\*b = x](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-c7c5b961cd9bbb35bc1a53e6a0ae63df_l3.png). ``` bool isPrime(int x) { if (x < 2) return false; int sqrtx = sqrt(x); for (int i = 2; i <= sqrtx; ++i) if (x % i == 0) return false; return true; } ``` Complexity: ![O(\\sqrt{n})](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-e81d36ce0c0129e50b6e2918315ab50d_l3.png) 3\. Miller-Rabin test Miller-Robin primality test is a probabilistic test, that is, the test USUALLY outputs the right result (NOT ALWAYS). To be a little bit more specific, when the test outputs False (the test says that *x* is not a prime number), it is absolutely true that *x* is not prime number, but when the test outputs True, we know that *x* is likely to be a prime number (but we are not 100% sure). Good news is that we have a way to make this test deterministic for 32-bit and 64-bit integer numbers. Because the explanation is a bit complex and lengthy, I will move it to a separated post. Follow **this link** to see it\! Complexity: ![O(k\*(\\log{n})^{3})](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-7ac643b6cd055fb29096fec6b193774b_l3.png) or better (with k ~ 12 for 64-bit integer) 4\. Test for primality in ![O(1)](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-8514351fd951ba2e124fae4028c17723_l3.png) with pre-processing (using Sieve of Eratosthenes) Sieve of Eratosthenes is a technique to pre-compute all prime numbers in the range (1, n\]. It also supports testing for the primality of any number in the above range in ![O(1)](https://tungmphung.com/wp-content/ql-cache/quicklatex.com-8514351fd951ba2e124fae4028c17723_l3.png) time complexity. Complexity: Details about this technique can be found **[Here](https://tungmphung.com/sieve-of-eratosthenes/)** IV. Practice Problems [SPOJ – PON](https://href.li/?https://www.spoj.com/problems/PON/) V. Further Reading [Sieve of Eratosthenes](https://tungmphung.com/sieve-of-eratosthenes/) Prime factorization Totient function RSA Digital Signature
Shard9 (laksa)
Root Hash8433080203494937609
Unparsed URLcom,tungmphung!/introduction-to-prime-numbers/ s443