2

I recently stumbled upon a fact that all prime numbers past $3$ are of the form either $6n-1$ or $6n+1$.

Is it true? at least for numbers less than $10^9$.

And does it cover all primes?

  • 1
    Hint $\ $ Primes $>3,$ are coprime to $,2,3,$ so coprime to $,6.,$ The integers $,n,$ coprime to $6$ are those of form $,6q!\color{#c00}{+!1},\ 6q!+!5 = 6(q!+!1)\color{#c00}{-!1},,$ since $,2\mid 6q!+!r,\ r\in{0,2,4},,$ and $, 3\mid 6q!+!3,,$ exhausting all possible cases, since, by the Division Algorithm, $\ n = 6q+r,$ for unique remainder $, 0\le r \le 5.\ \ $ – Bill Dubuque Aug 13 '14 at 13:23

3 Answers3

9

All prime numbers past $3$ are of one of those two forms.

Think of it this way:

All integers are of one of this forms:

$$\begin{cases}6n-2 & \Rightarrow& 2·(3n-1) \\ 6n -1 \\6n & \Rightarrow & 2·3·n \\ 6n+1 \\ 6n+2 & \Rightarrow& 2·(3n+1) \\ 6n+3 & \Rightarrow & 3·(2n+1)\end{cases}$$

Note that all other than $6n-1$ and $6n+1$ can be expressed as a product of two integers bigger than $1$. So a prime number cannot be of any form other than $6n\pm 1$.

(That doesn't mean that all numbers of the form $6n\pm 1$ are prime)

Darth Geek
  • 12,296
  • 1
    yes. thanks... now i have a reduced search space..!! – Raj Shah Aug 13 '14 at 09:30
  • Are you trying to find all the primes below $10^9$? You can find them all over the internet. Try here http://www.naturalnumbers.org/primes.html – Darth Geek Aug 13 '14 at 09:34
  • i need to submit a program that generates all the primes below 10^9 on an online judge. segmented sieve isn't fast enough – Raj Shah Aug 13 '14 at 09:37
  • 4
    All the primes are in one of the form $$30k±1, 30k±7, 30k±11, 30k±13$$ – Bumblebee Aug 13 '14 at 09:43
  • 1
    @RajShah Are you sure you are not getting timing problems already from I/O? There are about 48 million primes below $10^9$ – Hagen von Eitzen Aug 13 '14 at 10:15
  • first i tried cin,cout it took 41 secs. then i tried printf, scanf which took 35 secs. den finally i used handwritten functions and i could manage in 17-18 secs. but time limit is 10 secs. i wanna ask why do we calculate in segments of length equal to our prime array? if i hav a given range why not run it all at once? – Raj Shah Aug 13 '14 at 16:23
  • Do you output the result at the end or as you go? I would time the algorithm itself and then output the results at the end. –  Sep 05 '14 at 04:35
  • @Nilan, I'm not sure how to get 2, 3, or 5 from your list. – JB King Sep 05 '14 at 05:10
3

Hint : By Division Algorithm, any integer can be represented as one of : $6n, 6n+1, 6n+2, 6n+3, 6n+4, 6n+5$ and notice below

$2 | 6n+2$

$3 | 6n+3$

$2 | 6n+4$

AgentS
  • 12,195
3

Indeed all primes greater than 3, are in the form of $6n-1$ and $6n+1$. I've studied these a few years ago. Here's a basic visual proof of that using a sieve and isolation method.

First , list down all the numbers in 6 columns :

1-------- 2---------3--------4 ---------5--------6

7-------- 8-------- 9--------10--------11--------12

13-------14--------15------- 16--------17------- 18 .....[ the list will be infinite ]

Then, we cross out the columns of $2, 3, 4$ and $6$ as they are all composite. We are just left out with two columns of 1 and 5.

Using algebraic progression with a difference of 6:

  • Column 1 generates the prime path of $6n+1$ : $[ 7, 13, 19, ...]$

  • Column 5 generates the prime path of $6n-1$ : $[ 5, 11, 17, 23, ...]$

Thus , by isolation and sieve method, we can see that all primes > 3 must be in the form of $6n+1$ and $6n-1$.

creme332
  • 169