Testing the Usefulness of Prime Number Test

ramsey2879
Messages
841
Reaction score
3
Does anyone have any comment on the usefulness of the following test?
Input P
Prime = true
Triangular = 0
n = 0
Do until Triangular > P
n = n + 1
Triangular = Triangular + n
loop
X = Triangular Mod P
Do
Y = Int(Sqr(2*X))
' Comment if X is triangular then (Y*Y+Y)/2 = X
If (Y*Y+Y)/2 = X then
Prime = False
Exit Do
End If
n = n + 1
X = (X+ n) mod P
loop until n = (P+1)/2
Print "P is prime is " & Prime

Edit: I forgot to mention that this test does not work for primes less than 11 since then the sum of all numbers from 1 to (P-1)/2 is not greater than P but I checked it as an Excel macro for all odd numbers between 8 and 90,000 and the test worked perfectly.
 
Last edited:
Physics news on Phys.org
I should post the following modification to more quickly complete the test in the case that P is an odd triangular number. Change the statement "Do until Triangular > P" to --Do until Triangular > P-1 --.
 
Here's my test code (Pari/GP):
Code:
test(p)={
  my(tri=0,n=0,x,y);
  while(tri<=p,tri+=n++);
  x=tri%p;
  while(n!=(p+1)/2,
    y=sqrtint(x+x);
    if(x==y*(y+1)/2,return(0));
    x=(x+n++)%p
  );
  1
};

It seems to be an extremely slow test compared to trial division. I have attempted to repeat your verification to 90,000 but it has not completed in ten minutes. (I did not uncover any mistakes in that time, though.)
 
CRGreathouse said:
Here's my test code (Pari/GP):
Code:
test(p)={
  my(tri=0,n=0,x,y);
  while(tri<=p,tri+=n++);
  x=tri%p;
  while(n!=(p+1)/2,
    y=sqrtint(x+x);
    if(x==y*(y+1)/2,return(0));
    x=(x+n++)%p
  );
  1
};

It seems to be an extremely slow test compared to trial division. I have attempted to repeat your verification to 90,000 but it has not completed in ten minutes. (I did not uncover any mistakes in that time, though.)
once the test is verified/proven then improvements/unnecessary steps such as recalculating the triangular number can be considered.
 
ramsey2879 said:
once the test is verified/proven then improvements/unnecessary steps such as recalculating the triangular number can be considered.

I moved to a faster computer and was able to verify that your test works on all odd numbers n with 3 < n < 90,000. Here's a slightly (18%) faster version of the test:

Code:
test(p)={
  my(n=sqrtint(2*p),x=n*(n+1)/2,y,target=(p+1)/2);
  while(x<=p,x+=n++);
  x=x%p;
  while(n!=target,
    y=sqrtint(x+x);
    if(x==y*(y+1)/2,return(0));
    x=(x+n++)%p
  );
  1
};

It's clearly far slower than trial division if p is prime -- it takes about p/2 steps instead of about 2sqrt(p)/log(p) steps. It seems to take longer for composites as well.
 
This is closely related to Fermat's method
if n isn't divisible by 2.

(all calculations mod n).
Since n isn't divisible by 2, multiplying the equation by 2 is OK

(1/2)x(x+1) = (1/2)y(y+1) <=>
x(x+1) = y(y+1) <=>
(x+1/2)^2 + (1/4) = (y+1/2)^2 + (1/4) <=>
(2x+1)^2 = (2y+1)^2

so the difference of 2 squares is a multiple of n, and gcd(x-y, n) or gcd(x+y, n)
will give a factor.


\pi
 
I asked online questions about Proposition 2.1.1: The answer I got is the following: I have some questions about the answer I got. When the person answering says: ##1.## Is the map ##\mathfrak{q}\mapsto \mathfrak{q} A _\mathfrak{p}## from ##A\setminus \mathfrak{p}\to A_\mathfrak{p}##? But I don't understand what the author meant for the rest of the sentence in mathematical notation: ##2.## In the next statement where the author says: How is ##A\to...
The following are taken from the two sources, 1) from this online page and the book An Introduction to Module Theory by: Ibrahim Assem, Flavio U. Coelho. In the Abelian Categories chapter in the module theory text on page 157, right after presenting IV.2.21 Definition, the authors states "Image and coimage may or may not exist, but if they do, then they are unique up to isomorphism (because so are kernels and cokernels). Also in the reference url page above, the authors present two...
When decomposing a representation ##\rho## of a finite group ##G## into irreducible representations, we can find the number of times the representation contains a particular irrep ##\rho_0## through the character inner product $$ \langle \chi, \chi_0\rangle = \frac{1}{|G|} \sum_{g\in G} \chi(g) \chi_0(g)^*$$ where ##\chi## and ##\chi_0## are the characters of ##\rho## and ##\rho_0##, respectively. Since all group elements in the same conjugacy class have the same characters, this may be...

Similar threads

Replies
8
Views
2K
Replies
36
Views
246
Replies
2
Views
3K
Replies
2
Views
1K
Replies
2
Views
1K
Replies
1
Views
3K
Back
Top