Sieving Cullen Numbers

Share your advanced PureBasic knowledge/code with the community.
michaeled314
Enthusiast
Enthusiast
Posts: 340
Joined: Tue Apr 24, 2007 11:14 pm

Sieving Cullen Numbers

Post by michaeled314 »

This code goes along with Yves Gallot's Proth.exe

http://primes.utm.edu/programs/gallot/

If you do not have it already

This program sieves cullen numbers: put the file in the same directory and then run Proth.exe over primes.txt.

Code: Select all

Procedure.l gcd(a,b)
 While b <> 0
  t = b
  b = a % b
  a = t
 Wend
 ProcedureReturn a
EndProcedure

Procedure.l RhoAlgorithm(a.l)
 Protected x=2
 Protected y=2
 Protected d=1
 Protected count=0
 Protected t=0
 While count < 5
  count + 1
  While d=1
   x = (x*x+count)%a
   y = (y*y+count)%a
   y = (y*y+count)%a
   d = gcd(Abs(x-y),a)
  Wend
  If d = n
   t=0
  EndIf
  If d > 1 And d<n
   ProcedureReturn d
  EndIf
 Wend
 ProcedureReturn 0
EndProcedure

If CreateFile(0,"primes.txt")
 n.l = 0
 b.l = 7
 
 Loop:
  n+1
  
  If n = 10000000
   End
  EndIf
  
  If n%2 And b%2
   Goto Loop
  EndIf
  
  If (n-2)%6=0
   If (b-1)%3=0 Or (b+1)%3=0
    Goto Loop
   EndIf
  EndIf
  
  If (n-4)%10=0
   If (b-1)%10=0 Or (b+1)%10=0
    Goto Loop
   EndIf
  EndIf
  
  If (n-4)%20=0 Or (n-6)%20=0
   If (b-3)%10=0 Or (b+3)%10=0
    Goto Loop
   EndIf
  EndIf
  
  
  For i = 3 To Int(b/2) Step 2
   If Not RhoAlgorithm(i)
    If (b-1)%i=0 Or (b+1)%i=0
     If (n-(i-1))%(2*i)=0
      Goto Loop
     EndIf
    EndIf
   EndIf
  Next
  
  
  WriteStringN(0,Str(n)+" "+Str(n))
  Goto Loop
 EndIf