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.










share|cite|improve this question


















  • 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















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.










share|cite|improve this question


















  • 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













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.










share|cite|improve this question













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






share|cite|improve this question













share|cite|improve this question











share|cite|improve this question




share|cite|improve this question










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














  • 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















active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Quarter-circle Tiles

build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

Mont Emei