Testing the Usefulness of Prime Number Test

  • Context: Undergrad 
  • Thread starter Thread starter ramsey2879
  • Start date Start date
  • Tags Tags
    Prime Test Testing
Click For Summary

Discussion Overview

The discussion focuses on the usefulness and efficiency of a proposed prime number test, particularly in relation to triangular numbers. Participants explore the implementation of the test, its performance compared to traditional methods, and potential modifications to improve its speed and accuracy.

Discussion Character

  • Exploratory, Technical explanation, Debate/contested, Mathematical reasoning

Main Points Raised

  • One participant presents a prime number test that involves calculating triangular numbers and checking conditions based on modular arithmetic.
  • Another participant suggests a modification to the test to improve efficiency when P is an odd triangular number.
  • A participant shares their implementation of the test in Pari/GP, noting that it is significantly slower than trial division and takes a long time to complete for larger numbers.
  • Another participant reiterates the slow performance of the test and mentions the need for verification before considering optimizations.
  • A later reply proposes a faster version of the test, claiming an 18% improvement in speed while still acknowledging that it is slower than trial division.
  • One participant draws a connection between the proposed test and Fermat's method, discussing the implications of certain mathematical properties related to the test.

Areas of Agreement / Disagreement

Participants express differing views on the efficiency and practicality of the proposed prime number test, with some acknowledging its limitations compared to established methods like trial division. There is no consensus on the overall effectiveness of the test or its modifications.

Contextual Notes

The discussion highlights limitations in the current implementation, such as the slow performance for larger inputs and the need for further verification of the test's validity. Participants also note that the test does not work for primes less than 11.

Who May Find This Useful

Readers interested in prime number testing, mathematical algorithms, and computational efficiency may find the discussion relevant.

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.


[tex]\pi[/tex]
 

Similar threads

  • · Replies 21 ·
Replies
21
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
Replies
48
Views
6K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
2K
  • · Replies 12 ·
Replies
12
Views
1K
  • · Replies 26 ·
Replies
26
Views
1K