primality test speed
up vote
0
down vote
favorite
Noticed using a lattice with line x+y=n for n prime, every grid point on the line is co-prime. For example:
Lattice with x+y=n
To test, I wrote a C program to specify a number (e.g., 104729) then travels along the line looking for a non co-prime point (x,y) and outputs prime or not prime:
#include "stdafx.h"
unsigned int gcd(unsigned int a, unsigned int b) {
unsigned int t;
while (b != 0) {
t = a;
a = b;
b = t % b;
}
return a;
}
int main(int argc, char *argv)
{
// x+y=n
unsigned int x, y, n;
// This number is prime:
n = 104729;
for (x = 1; x < n; x++)
{
y = n - x;
if (gcd(x, y) != 1)
{
printf("Not Prime: %un", n);
break;
}
}
if (x == n)
{
printf("Prime: %un", n);
}
return 0;
}
My maths background is very limited, so I'm wondering if it's possible to speed up the prime check using some fancy maths tricks.
Thanks.
primality-test
add a comment |
up vote
0
down vote
favorite
Noticed using a lattice with line x+y=n for n prime, every grid point on the line is co-prime. For example:
Lattice with x+y=n
To test, I wrote a C program to specify a number (e.g., 104729) then travels along the line looking for a non co-prime point (x,y) and outputs prime or not prime:
#include "stdafx.h"
unsigned int gcd(unsigned int a, unsigned int b) {
unsigned int t;
while (b != 0) {
t = a;
a = b;
b = t % b;
}
return a;
}
int main(int argc, char *argv)
{
// x+y=n
unsigned int x, y, n;
// This number is prime:
n = 104729;
for (x = 1; x < n; x++)
{
y = n - x;
if (gcd(x, y) != 1)
{
printf("Not Prime: %un", n);
break;
}
}
if (x == n)
{
printf("Prime: %un", n);
}
return 0;
}
My maths background is very limited, so I'm wondering if it's possible to speed up the prime check using some fancy maths tricks.
Thanks.
primality-test
2
I suspect this test is much slower than trial division.
– Lord Shark the Unknown
Nov 18 at 4:52
Your current algorithm will run in $O(n log n)$ time if $n$ is a prime (the $log $ comes from $mathsf{gcd}$), while you can decide primality in $O(n^{1/2})$ time in the worst case. Indeed, if $n$ is not prime, then its smallest divisor larger than $1$, must not exceed $n^{1/2}$, since if $d$ divides $n$ then $n/d$ also divides $n$, thus both cannot become larger than $n^{1/2}$. Thus, all you need is to check the numbers from $2...[n^{1/2}] + 1 $ to see if any of them divides $n$. If none of them does, then $n$ is prime, otherwise you have non prime.
– Hayk
Nov 18 at 5:32
Nice explanation...Thanks!
– vengy
Nov 20 at 0:22
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Noticed using a lattice with line x+y=n for n prime, every grid point on the line is co-prime. For example:
Lattice with x+y=n
To test, I wrote a C program to specify a number (e.g., 104729) then travels along the line looking for a non co-prime point (x,y) and outputs prime or not prime:
#include "stdafx.h"
unsigned int gcd(unsigned int a, unsigned int b) {
unsigned int t;
while (b != 0) {
t = a;
a = b;
b = t % b;
}
return a;
}
int main(int argc, char *argv)
{
// x+y=n
unsigned int x, y, n;
// This number is prime:
n = 104729;
for (x = 1; x < n; x++)
{
y = n - x;
if (gcd(x, y) != 1)
{
printf("Not Prime: %un", n);
break;
}
}
if (x == n)
{
printf("Prime: %un", n);
}
return 0;
}
My maths background is very limited, so I'm wondering if it's possible to speed up the prime check using some fancy maths tricks.
Thanks.
primality-test
Noticed using a lattice with line x+y=n for n prime, every grid point on the line is co-prime. For example:
Lattice with x+y=n
To test, I wrote a C program to specify a number (e.g., 104729) then travels along the line looking for a non co-prime point (x,y) and outputs prime or not prime:
#include "stdafx.h"
unsigned int gcd(unsigned int a, unsigned int b) {
unsigned int t;
while (b != 0) {
t = a;
a = b;
b = t % b;
}
return a;
}
int main(int argc, char *argv)
{
// x+y=n
unsigned int x, y, n;
// This number is prime:
n = 104729;
for (x = 1; x < n; x++)
{
y = n - x;
if (gcd(x, y) != 1)
{
printf("Not Prime: %un", n);
break;
}
}
if (x == n)
{
printf("Prime: %un", n);
}
return 0;
}
My maths background is very limited, so I'm wondering if it's possible to speed up the prime check using some fancy maths tricks.
Thanks.
primality-test
primality-test
asked Nov 18 at 4:42
vengy
6
6
2
I suspect this test is much slower than trial division.
– Lord Shark the Unknown
Nov 18 at 4:52
Your current algorithm will run in $O(n log n)$ time if $n$ is a prime (the $log $ comes from $mathsf{gcd}$), while you can decide primality in $O(n^{1/2})$ time in the worst case. Indeed, if $n$ is not prime, then its smallest divisor larger than $1$, must not exceed $n^{1/2}$, since if $d$ divides $n$ then $n/d$ also divides $n$, thus both cannot become larger than $n^{1/2}$. Thus, all you need is to check the numbers from $2...[n^{1/2}] + 1 $ to see if any of them divides $n$. If none of them does, then $n$ is prime, otherwise you have non prime.
– Hayk
Nov 18 at 5:32
Nice explanation...Thanks!
– vengy
Nov 20 at 0:22
add a comment |
2
I suspect this test is much slower than trial division.
– Lord Shark the Unknown
Nov 18 at 4:52
Your current algorithm will run in $O(n log n)$ time if $n$ is a prime (the $log $ comes from $mathsf{gcd}$), while you can decide primality in $O(n^{1/2})$ time in the worst case. Indeed, if $n$ is not prime, then its smallest divisor larger than $1$, must not exceed $n^{1/2}$, since if $d$ divides $n$ then $n/d$ also divides $n$, thus both cannot become larger than $n^{1/2}$. Thus, all you need is to check the numbers from $2...[n^{1/2}] + 1 $ to see if any of them divides $n$. If none of them does, then $n$ is prime, otherwise you have non prime.
– Hayk
Nov 18 at 5:32
Nice explanation...Thanks!
– vengy
Nov 20 at 0:22
2
2
I suspect this test is much slower than trial division.
– Lord Shark the Unknown
Nov 18 at 4:52
I suspect this test is much slower than trial division.
– Lord Shark the Unknown
Nov 18 at 4:52
Your current algorithm will run in $O(n log n)$ time if $n$ is a prime (the $log $ comes from $mathsf{gcd}$), while you can decide primality in $O(n^{1/2})$ time in the worst case. Indeed, if $n$ is not prime, then its smallest divisor larger than $1$, must not exceed $n^{1/2}$, since if $d$ divides $n$ then $n/d$ also divides $n$, thus both cannot become larger than $n^{1/2}$. Thus, all you need is to check the numbers from $2...[n^{1/2}] + 1 $ to see if any of them divides $n$. If none of them does, then $n$ is prime, otherwise you have non prime.
– Hayk
Nov 18 at 5:32
Your current algorithm will run in $O(n log n)$ time if $n$ is a prime (the $log $ comes from $mathsf{gcd}$), while you can decide primality in $O(n^{1/2})$ time in the worst case. Indeed, if $n$ is not prime, then its smallest divisor larger than $1$, must not exceed $n^{1/2}$, since if $d$ divides $n$ then $n/d$ also divides $n$, thus both cannot become larger than $n^{1/2}$. Thus, all you need is to check the numbers from $2...[n^{1/2}] + 1 $ to see if any of them divides $n$. If none of them does, then $n$ is prime, otherwise you have non prime.
– Hayk
Nov 18 at 5:32
Nice explanation...Thanks!
– vengy
Nov 20 at 0:22
Nice explanation...Thanks!
– vengy
Nov 20 at 0:22
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Mathematics Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3003140%2fprimality-test-speed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
I suspect this test is much slower than trial division.
– Lord Shark the Unknown
Nov 18 at 4:52
Your current algorithm will run in $O(n log n)$ time if $n$ is a prime (the $log $ comes from $mathsf{gcd}$), while you can decide primality in $O(n^{1/2})$ time in the worst case. Indeed, if $n$ is not prime, then its smallest divisor larger than $1$, must not exceed $n^{1/2}$, since if $d$ divides $n$ then $n/d$ also divides $n$, thus both cannot become larger than $n^{1/2}$. Thus, all you need is to check the numbers from $2...[n^{1/2}] + 1 $ to see if any of them divides $n$. If none of them does, then $n$ is prime, otherwise you have non prime.
– Hayk
Nov 18 at 5:32
Nice explanation...Thanks!
– vengy
Nov 20 at 0:22