The nth prime calculator
How to prove the correctness of the following algorithm :
Input: $n$ : the ordinal number
Output: $x$ : the nth prime number
$b_1=6$ , $b_2=6$ , $b_3=6$
If $n==1$ , then $x=2$ , else $x=3$
$k=4$ , $m=3$
While $m leq n$ do:
$phantom{5}$ $b_4=b_1+ operatorname{lcm}(k-2,b_1)$
$phantom{5}$ $a=b_4/b_1-1$
$phantom{5}$ $k=k+1$
$phantom{5}$ $b_1=b_2$ , $b_2=b_3$ , $b_3=b_4$
$phantom{5}$ If $x<a$ then $x=a$ , $m=m+1$
Return $x$
You can run this code here .
GUI application that implements this algorithm can be found here .
Fast command line program that implements this algorithm can be found here .
I can confirm that algorithm produces correct results for all $n$ up to $10000$ .
elementary-number-theory prime-numbers algorithms
add a comment |
How to prove the correctness of the following algorithm :
Input: $n$ : the ordinal number
Output: $x$ : the nth prime number
$b_1=6$ , $b_2=6$ , $b_3=6$
If $n==1$ , then $x=2$ , else $x=3$
$k=4$ , $m=3$
While $m leq n$ do:
$phantom{5}$ $b_4=b_1+ operatorname{lcm}(k-2,b_1)$
$phantom{5}$ $a=b_4/b_1-1$
$phantom{5}$ $k=k+1$
$phantom{5}$ $b_1=b_2$ , $b_2=b_3$ , $b_3=b_4$
$phantom{5}$ If $x<a$ then $x=a$ , $m=m+1$
Return $x$
You can run this code here .
GUI application that implements this algorithm can be found here .
Fast command line program that implements this algorithm can be found here .
I can confirm that algorithm produces correct results for all $n$ up to $10000$ .
elementary-number-theory prime-numbers algorithms
1
$10000$ is a small number in this context. You should explain what your routine is doing, not just post the code. It will be easier to find what is wrong with a description of the algorithm
– Ross Millikan
May 31 '18 at 15:33
Seems to be a very ineffective method. If I compute with 32-bit integers, it overflows for $n ge 10$ (using $mathrm{lcm}(a,b)=ab/gcd(a,b)$). What is the purpose of the algorithm?
– gammatester
May 31 '18 at 15:41
3
Not easy to determine what the algorithm does. It might be trial division.
– Peter
May 31 '18 at 15:46
add a comment |
How to prove the correctness of the following algorithm :
Input: $n$ : the ordinal number
Output: $x$ : the nth prime number
$b_1=6$ , $b_2=6$ , $b_3=6$
If $n==1$ , then $x=2$ , else $x=3$
$k=4$ , $m=3$
While $m leq n$ do:
$phantom{5}$ $b_4=b_1+ operatorname{lcm}(k-2,b_1)$
$phantom{5}$ $a=b_4/b_1-1$
$phantom{5}$ $k=k+1$
$phantom{5}$ $b_1=b_2$ , $b_2=b_3$ , $b_3=b_4$
$phantom{5}$ If $x<a$ then $x=a$ , $m=m+1$
Return $x$
You can run this code here .
GUI application that implements this algorithm can be found here .
Fast command line program that implements this algorithm can be found here .
I can confirm that algorithm produces correct results for all $n$ up to $10000$ .
elementary-number-theory prime-numbers algorithms
How to prove the correctness of the following algorithm :
Input: $n$ : the ordinal number
Output: $x$ : the nth prime number
$b_1=6$ , $b_2=6$ , $b_3=6$
If $n==1$ , then $x=2$ , else $x=3$
$k=4$ , $m=3$
While $m leq n$ do:
$phantom{5}$ $b_4=b_1+ operatorname{lcm}(k-2,b_1)$
$phantom{5}$ $a=b_4/b_1-1$
$phantom{5}$ $k=k+1$
$phantom{5}$ $b_1=b_2$ , $b_2=b_3$ , $b_3=b_4$
$phantom{5}$ If $x<a$ then $x=a$ , $m=m+1$
Return $x$
You can run this code here .
GUI application that implements this algorithm can be found here .
Fast command line program that implements this algorithm can be found here .
I can confirm that algorithm produces correct results for all $n$ up to $10000$ .
elementary-number-theory prime-numbers algorithms
elementary-number-theory prime-numbers algorithms
edited Dec 12 '18 at 15:19
Peđa Terzić
asked May 31 '18 at 15:05
Peđa TerzićPeđa Terzić
7,90022572
7,90022572
1
$10000$ is a small number in this context. You should explain what your routine is doing, not just post the code. It will be easier to find what is wrong with a description of the algorithm
– Ross Millikan
May 31 '18 at 15:33
Seems to be a very ineffective method. If I compute with 32-bit integers, it overflows for $n ge 10$ (using $mathrm{lcm}(a,b)=ab/gcd(a,b)$). What is the purpose of the algorithm?
– gammatester
May 31 '18 at 15:41
3
Not easy to determine what the algorithm does. It might be trial division.
– Peter
May 31 '18 at 15:46
add a comment |
1
$10000$ is a small number in this context. You should explain what your routine is doing, not just post the code. It will be easier to find what is wrong with a description of the algorithm
– Ross Millikan
May 31 '18 at 15:33
Seems to be a very ineffective method. If I compute with 32-bit integers, it overflows for $n ge 10$ (using $mathrm{lcm}(a,b)=ab/gcd(a,b)$). What is the purpose of the algorithm?
– gammatester
May 31 '18 at 15:41
3
Not easy to determine what the algorithm does. It might be trial division.
– Peter
May 31 '18 at 15:46
1
1
$10000$ is a small number in this context. You should explain what your routine is doing, not just post the code. It will be easier to find what is wrong with a description of the algorithm
– Ross Millikan
May 31 '18 at 15:33
$10000$ is a small number in this context. You should explain what your routine is doing, not just post the code. It will be easier to find what is wrong with a description of the algorithm
– Ross Millikan
May 31 '18 at 15:33
Seems to be a very ineffective method. If I compute with 32-bit integers, it overflows for $n ge 10$ (using $mathrm{lcm}(a,b)=ab/gcd(a,b)$). What is the purpose of the algorithm?
– gammatester
May 31 '18 at 15:41
Seems to be a very ineffective method. If I compute with 32-bit integers, it overflows for $n ge 10$ (using $mathrm{lcm}(a,b)=ab/gcd(a,b)$). What is the purpose of the algorithm?
– gammatester
May 31 '18 at 15:41
3
3
Not easy to determine what the algorithm does. It might be trial division.
– Peter
May 31 '18 at 15:46
Not easy to determine what the algorithm does. It might be trial division.
– Peter
May 31 '18 at 15:46
add a comment |
2 Answers
2
active
oldest
votes
Consider the variable $k$. Apart from the initialisation and increment, it is only used once, as $k-2$. It is therefore clearer if we simply lower its value by $2$ throughout:
b1 = 6 , b2 = 6 , b3 = 6
If n == 1, then x = 2 , else x = 3
k = 2, m = 3
While m ≤ n
do:
b4 = b1 + lcm(k, b1)
a = b4/b1 − 1
k = k + 1
b1 = b2 , b2 = b3 , b3 = b4
If x < a then x = a , m = m + 1
Return x
Next, let's substitute the expression for $b_4$ into the expression for $a$.
$$a=frac{b_4}{b_1}−1 = frac{b_1+textrm{lcm}(k,b_1)}{b_1}-1 = frac{textrm{lcm}(k,b_1)}{b_1} = frac{k}{gcd(k,b_1)}$$
b1 = 6 , b2 = 6 , b3 = 6
If n == 1, then x = 2 , else x = 3
k = 2, m = 3
While m ≤ n
do:
b4 = b1 + lcm(k, b1)
a = k/gcd(k, b_1)
k = k + 1
b1 = b2 , b2 = b3 , b3 = b4
If x < a then x = a , m = m + 1
Return x
Basically what is happening is that $k$ is continually incremented. If it is a prime, then $gcd(k,b_1)$ will be $1$, and $a$ will have the same value. The if statement will increment its prime counter, and store the prime in $x$. If the counter is at the requested value, the prime is returned.
If $k$ is not prime, then the $gcd$ is highly likely not $1$ and $a$ is a smaller value than the previous prime found. The if statement will then do nothing, and the loop continues.
The only thing that remains to be shown is that $gcd(k,b_1)$ is always larger than $1$ if $k$ is not prime. To be more precise:
Let $b_i$ be a sequence defined by
$$ b_1=b_2=b_3=6\ b_{i+3}=b_i+textrm{lcm}(i+1,b_i)$$
It remains to be shown that $gcd(k,b_{k-1})>1$ for all composite $k$.
The $b_i$ are highly composite ($b_{i-3}|b_i$) and the sequence grows large very quickly. Furthermore it starts with sixes so that it has factors $2$ and $3$ to begin with. That could well be enough to make it work for the relatively small $n$ that it has been tested with. I wouldn't expect it to fail until you get to large semiprimes, i.e. large composite numbers without small factors.
The cycle can be simplified: $a= k/gcd(k, b1); b4 = b1(1+a)cdots$
– Yuri Negometyanov
Jun 7 '18 at 22:32
add a comment |
Updated 11.06.18
The proposed calculator enumerates the prime numbers on the pass.
Let us construct the similar algorithm based on the ideas of Eratosthenes sieve.
m = 1;
x = 2;
b = 2;
while (m < n)
{
for(k = 3; ; k = k+2)
{
if(gcd(b,k)==1)
{
x = k;
m = m + 1;
b = b * x;
}
}
}
return x;
In the both of the algorithms, a sequence of positive integers is tested for the presence of a common factor with a high composite number $b.$ If it equals to 1, then the integer is prime.
The algorithm above shows that it's sufficiently to use $b$ which equals the primorial $x#$.
The OP calculator algorithm forms the greater value of $b,$ which contains any prime $p$ in the degree $d,$ wherein
$$p^dle x < p^{d+1},$$
but uses its delayed value.
This means that the OP calculator can be improved.
At this time, analysis of delaying influence shows that OP calculator is correct.
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f2803156%2fthe-nth-prime-calculator%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Consider the variable $k$. Apart from the initialisation and increment, it is only used once, as $k-2$. It is therefore clearer if we simply lower its value by $2$ throughout:
b1 = 6 , b2 = 6 , b3 = 6
If n == 1, then x = 2 , else x = 3
k = 2, m = 3
While m ≤ n
do:
b4 = b1 + lcm(k, b1)
a = b4/b1 − 1
k = k + 1
b1 = b2 , b2 = b3 , b3 = b4
If x < a then x = a , m = m + 1
Return x
Next, let's substitute the expression for $b_4$ into the expression for $a$.
$$a=frac{b_4}{b_1}−1 = frac{b_1+textrm{lcm}(k,b_1)}{b_1}-1 = frac{textrm{lcm}(k,b_1)}{b_1} = frac{k}{gcd(k,b_1)}$$
b1 = 6 , b2 = 6 , b3 = 6
If n == 1, then x = 2 , else x = 3
k = 2, m = 3
While m ≤ n
do:
b4 = b1 + lcm(k, b1)
a = k/gcd(k, b_1)
k = k + 1
b1 = b2 , b2 = b3 , b3 = b4
If x < a then x = a , m = m + 1
Return x
Basically what is happening is that $k$ is continually incremented. If it is a prime, then $gcd(k,b_1)$ will be $1$, and $a$ will have the same value. The if statement will increment its prime counter, and store the prime in $x$. If the counter is at the requested value, the prime is returned.
If $k$ is not prime, then the $gcd$ is highly likely not $1$ and $a$ is a smaller value than the previous prime found. The if statement will then do nothing, and the loop continues.
The only thing that remains to be shown is that $gcd(k,b_1)$ is always larger than $1$ if $k$ is not prime. To be more precise:
Let $b_i$ be a sequence defined by
$$ b_1=b_2=b_3=6\ b_{i+3}=b_i+textrm{lcm}(i+1,b_i)$$
It remains to be shown that $gcd(k,b_{k-1})>1$ for all composite $k$.
The $b_i$ are highly composite ($b_{i-3}|b_i$) and the sequence grows large very quickly. Furthermore it starts with sixes so that it has factors $2$ and $3$ to begin with. That could well be enough to make it work for the relatively small $n$ that it has been tested with. I wouldn't expect it to fail until you get to large semiprimes, i.e. large composite numbers without small factors.
The cycle can be simplified: $a= k/gcd(k, b1); b4 = b1(1+a)cdots$
– Yuri Negometyanov
Jun 7 '18 at 22:32
add a comment |
Consider the variable $k$. Apart from the initialisation and increment, it is only used once, as $k-2$. It is therefore clearer if we simply lower its value by $2$ throughout:
b1 = 6 , b2 = 6 , b3 = 6
If n == 1, then x = 2 , else x = 3
k = 2, m = 3
While m ≤ n
do:
b4 = b1 + lcm(k, b1)
a = b4/b1 − 1
k = k + 1
b1 = b2 , b2 = b3 , b3 = b4
If x < a then x = a , m = m + 1
Return x
Next, let's substitute the expression for $b_4$ into the expression for $a$.
$$a=frac{b_4}{b_1}−1 = frac{b_1+textrm{lcm}(k,b_1)}{b_1}-1 = frac{textrm{lcm}(k,b_1)}{b_1} = frac{k}{gcd(k,b_1)}$$
b1 = 6 , b2 = 6 , b3 = 6
If n == 1, then x = 2 , else x = 3
k = 2, m = 3
While m ≤ n
do:
b4 = b1 + lcm(k, b1)
a = k/gcd(k, b_1)
k = k + 1
b1 = b2 , b2 = b3 , b3 = b4
If x < a then x = a , m = m + 1
Return x
Basically what is happening is that $k$ is continually incremented. If it is a prime, then $gcd(k,b_1)$ will be $1$, and $a$ will have the same value. The if statement will increment its prime counter, and store the prime in $x$. If the counter is at the requested value, the prime is returned.
If $k$ is not prime, then the $gcd$ is highly likely not $1$ and $a$ is a smaller value than the previous prime found. The if statement will then do nothing, and the loop continues.
The only thing that remains to be shown is that $gcd(k,b_1)$ is always larger than $1$ if $k$ is not prime. To be more precise:
Let $b_i$ be a sequence defined by
$$ b_1=b_2=b_3=6\ b_{i+3}=b_i+textrm{lcm}(i+1,b_i)$$
It remains to be shown that $gcd(k,b_{k-1})>1$ for all composite $k$.
The $b_i$ are highly composite ($b_{i-3}|b_i$) and the sequence grows large very quickly. Furthermore it starts with sixes so that it has factors $2$ and $3$ to begin with. That could well be enough to make it work for the relatively small $n$ that it has been tested with. I wouldn't expect it to fail until you get to large semiprimes, i.e. large composite numbers without small factors.
The cycle can be simplified: $a= k/gcd(k, b1); b4 = b1(1+a)cdots$
– Yuri Negometyanov
Jun 7 '18 at 22:32
add a comment |
Consider the variable $k$. Apart from the initialisation and increment, it is only used once, as $k-2$. It is therefore clearer if we simply lower its value by $2$ throughout:
b1 = 6 , b2 = 6 , b3 = 6
If n == 1, then x = 2 , else x = 3
k = 2, m = 3
While m ≤ n
do:
b4 = b1 + lcm(k, b1)
a = b4/b1 − 1
k = k + 1
b1 = b2 , b2 = b3 , b3 = b4
If x < a then x = a , m = m + 1
Return x
Next, let's substitute the expression for $b_4$ into the expression for $a$.
$$a=frac{b_4}{b_1}−1 = frac{b_1+textrm{lcm}(k,b_1)}{b_1}-1 = frac{textrm{lcm}(k,b_1)}{b_1} = frac{k}{gcd(k,b_1)}$$
b1 = 6 , b2 = 6 , b3 = 6
If n == 1, then x = 2 , else x = 3
k = 2, m = 3
While m ≤ n
do:
b4 = b1 + lcm(k, b1)
a = k/gcd(k, b_1)
k = k + 1
b1 = b2 , b2 = b3 , b3 = b4
If x < a then x = a , m = m + 1
Return x
Basically what is happening is that $k$ is continually incremented. If it is a prime, then $gcd(k,b_1)$ will be $1$, and $a$ will have the same value. The if statement will increment its prime counter, and store the prime in $x$. If the counter is at the requested value, the prime is returned.
If $k$ is not prime, then the $gcd$ is highly likely not $1$ and $a$ is a smaller value than the previous prime found. The if statement will then do nothing, and the loop continues.
The only thing that remains to be shown is that $gcd(k,b_1)$ is always larger than $1$ if $k$ is not prime. To be more precise:
Let $b_i$ be a sequence defined by
$$ b_1=b_2=b_3=6\ b_{i+3}=b_i+textrm{lcm}(i+1,b_i)$$
It remains to be shown that $gcd(k,b_{k-1})>1$ for all composite $k$.
The $b_i$ are highly composite ($b_{i-3}|b_i$) and the sequence grows large very quickly. Furthermore it starts with sixes so that it has factors $2$ and $3$ to begin with. That could well be enough to make it work for the relatively small $n$ that it has been tested with. I wouldn't expect it to fail until you get to large semiprimes, i.e. large composite numbers without small factors.
Consider the variable $k$. Apart from the initialisation and increment, it is only used once, as $k-2$. It is therefore clearer if we simply lower its value by $2$ throughout:
b1 = 6 , b2 = 6 , b3 = 6
If n == 1, then x = 2 , else x = 3
k = 2, m = 3
While m ≤ n
do:
b4 = b1 + lcm(k, b1)
a = b4/b1 − 1
k = k + 1
b1 = b2 , b2 = b3 , b3 = b4
If x < a then x = a , m = m + 1
Return x
Next, let's substitute the expression for $b_4$ into the expression for $a$.
$$a=frac{b_4}{b_1}−1 = frac{b_1+textrm{lcm}(k,b_1)}{b_1}-1 = frac{textrm{lcm}(k,b_1)}{b_1} = frac{k}{gcd(k,b_1)}$$
b1 = 6 , b2 = 6 , b3 = 6
If n == 1, then x = 2 , else x = 3
k = 2, m = 3
While m ≤ n
do:
b4 = b1 + lcm(k, b1)
a = k/gcd(k, b_1)
k = k + 1
b1 = b2 , b2 = b3 , b3 = b4
If x < a then x = a , m = m + 1
Return x
Basically what is happening is that $k$ is continually incremented. If it is a prime, then $gcd(k,b_1)$ will be $1$, and $a$ will have the same value. The if statement will increment its prime counter, and store the prime in $x$. If the counter is at the requested value, the prime is returned.
If $k$ is not prime, then the $gcd$ is highly likely not $1$ and $a$ is a smaller value than the previous prime found. The if statement will then do nothing, and the loop continues.
The only thing that remains to be shown is that $gcd(k,b_1)$ is always larger than $1$ if $k$ is not prime. To be more precise:
Let $b_i$ be a sequence defined by
$$ b_1=b_2=b_3=6\ b_{i+3}=b_i+textrm{lcm}(i+1,b_i)$$
It remains to be shown that $gcd(k,b_{k-1})>1$ for all composite $k$.
The $b_i$ are highly composite ($b_{i-3}|b_i$) and the sequence grows large very quickly. Furthermore it starts with sixes so that it has factors $2$ and $3$ to begin with. That could well be enough to make it work for the relatively small $n$ that it has been tested with. I wouldn't expect it to fail until you get to large semiprimes, i.e. large composite numbers without small factors.
edited Jun 4 '18 at 21:27
Mr. Brooks
16111237
16111237
answered Jun 4 '18 at 16:18
Jaap ScherphuisJaap Scherphuis
3,976616
3,976616
The cycle can be simplified: $a= k/gcd(k, b1); b4 = b1(1+a)cdots$
– Yuri Negometyanov
Jun 7 '18 at 22:32
add a comment |
The cycle can be simplified: $a= k/gcd(k, b1); b4 = b1(1+a)cdots$
– Yuri Negometyanov
Jun 7 '18 at 22:32
The cycle can be simplified: $a= k/gcd(k, b1); b4 = b1(1+a)cdots$
– Yuri Negometyanov
Jun 7 '18 at 22:32
The cycle can be simplified: $a= k/gcd(k, b1); b4 = b1(1+a)cdots$
– Yuri Negometyanov
Jun 7 '18 at 22:32
add a comment |
Updated 11.06.18
The proposed calculator enumerates the prime numbers on the pass.
Let us construct the similar algorithm based on the ideas of Eratosthenes sieve.
m = 1;
x = 2;
b = 2;
while (m < n)
{
for(k = 3; ; k = k+2)
{
if(gcd(b,k)==1)
{
x = k;
m = m + 1;
b = b * x;
}
}
}
return x;
In the both of the algorithms, a sequence of positive integers is tested for the presence of a common factor with a high composite number $b.$ If it equals to 1, then the integer is prime.
The algorithm above shows that it's sufficiently to use $b$ which equals the primorial $x#$.
The OP calculator algorithm forms the greater value of $b,$ which contains any prime $p$ in the degree $d,$ wherein
$$p^dle x < p^{d+1},$$
but uses its delayed value.
This means that the OP calculator can be improved.
At this time, analysis of delaying influence shows that OP calculator is correct.
add a comment |
Updated 11.06.18
The proposed calculator enumerates the prime numbers on the pass.
Let us construct the similar algorithm based on the ideas of Eratosthenes sieve.
m = 1;
x = 2;
b = 2;
while (m < n)
{
for(k = 3; ; k = k+2)
{
if(gcd(b,k)==1)
{
x = k;
m = m + 1;
b = b * x;
}
}
}
return x;
In the both of the algorithms, a sequence of positive integers is tested for the presence of a common factor with a high composite number $b.$ If it equals to 1, then the integer is prime.
The algorithm above shows that it's sufficiently to use $b$ which equals the primorial $x#$.
The OP calculator algorithm forms the greater value of $b,$ which contains any prime $p$ in the degree $d,$ wherein
$$p^dle x < p^{d+1},$$
but uses its delayed value.
This means that the OP calculator can be improved.
At this time, analysis of delaying influence shows that OP calculator is correct.
add a comment |
Updated 11.06.18
The proposed calculator enumerates the prime numbers on the pass.
Let us construct the similar algorithm based on the ideas of Eratosthenes sieve.
m = 1;
x = 2;
b = 2;
while (m < n)
{
for(k = 3; ; k = k+2)
{
if(gcd(b,k)==1)
{
x = k;
m = m + 1;
b = b * x;
}
}
}
return x;
In the both of the algorithms, a sequence of positive integers is tested for the presence of a common factor with a high composite number $b.$ If it equals to 1, then the integer is prime.
The algorithm above shows that it's sufficiently to use $b$ which equals the primorial $x#$.
The OP calculator algorithm forms the greater value of $b,$ which contains any prime $p$ in the degree $d,$ wherein
$$p^dle x < p^{d+1},$$
but uses its delayed value.
This means that the OP calculator can be improved.
At this time, analysis of delaying influence shows that OP calculator is correct.
Updated 11.06.18
The proposed calculator enumerates the prime numbers on the pass.
Let us construct the similar algorithm based on the ideas of Eratosthenes sieve.
m = 1;
x = 2;
b = 2;
while (m < n)
{
for(k = 3; ; k = k+2)
{
if(gcd(b,k)==1)
{
x = k;
m = m + 1;
b = b * x;
}
}
}
return x;
In the both of the algorithms, a sequence of positive integers is tested for the presence of a common factor with a high composite number $b.$ If it equals to 1, then the integer is prime.
The algorithm above shows that it's sufficiently to use $b$ which equals the primorial $x#$.
The OP calculator algorithm forms the greater value of $b,$ which contains any prime $p$ in the degree $d,$ wherein
$$p^dle x < p^{d+1},$$
but uses its delayed value.
This means that the OP calculator can be improved.
At this time, analysis of delaying influence shows that OP calculator is correct.
edited Jun 11 '18 at 15:50
answered Jun 10 '18 at 4:09
Yuri NegometyanovYuri Negometyanov
10.9k1727
10.9k1727
add a comment |
add a comment |
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%2f2803156%2fthe-nth-prime-calculator%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
1
$10000$ is a small number in this context. You should explain what your routine is doing, not just post the code. It will be easier to find what is wrong with a description of the algorithm
– Ross Millikan
May 31 '18 at 15:33
Seems to be a very ineffective method. If I compute with 32-bit integers, it overflows for $n ge 10$ (using $mathrm{lcm}(a,b)=ab/gcd(a,b)$). What is the purpose of the algorithm?
– gammatester
May 31 '18 at 15:41
3
Not easy to determine what the algorithm does. It might be trial division.
– Peter
May 31 '18 at 15:46