Is there a simple algorithm that can determine if X is prime?

后端 未结 16 1492
离开以前
离开以前 2020-12-01 00:32

I have been trying to work my way through Project Euler, and have noticed a handful of problems ask for you to determine a prime number as part of it.

  1. I kno

16条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 00:57

    The AKS prime testing algorithm:

    Input: Integer n > 1  
    
    
    if (n is has the form ab with b > 1) then output COMPOSITE  
    
    r := 2  
    while (r < n) {  
        if (gcd(n,r) is not 1) then output COMPOSITE  
        if (r is prime greater than 2) then {  
            let q be the largest factor of r-1  
            if (q > 4sqrt(r)log n) and (n(r-1)/q is not 1 (mod r)) then break  
        }  
        r := r+1  
    }  
    
    for a = 1 to 2sqrt(r)log n {  
        if ( (x-a)n is not (xn-a) (mod xr-1,n) ) then output COMPOSITE  
    }  
    
    output PRIME;   
    

提交回复
热议问题