一种快速判断是否为质数的方法

来源:互联网 发布:淘宝天使心写真集原版 编辑:程序博客网 时间:2024/06/06 15:49
public static boolean isprime(int x) {
    
if (x <= 7{
      
if (x == 2 || x == 3 || x==5 || x == 7)
        
return true;
    }

    
int c = 7;
    
if (x % 2 == 0)
      
return false;
    
if (x % 3 == 0)
      
return false;
    
if (x % 5 == 0)
      
return false;
    
int end = (int) Math.sqrt(x);
    
while (c <= end) {
      
if (x % c == 0{
        
return false;
      }

      c 
+= 4;
      
if (x % c == 0{
        
return false;
      }

      c 
+= 2;
      
if (x % c == 0{
        
return false;
      }

      c 
+= 4;
      
if (x % c == 0{
        
return false;
      }

      c 
+= 2;
      
if (x % c == 0{
        
return false;
      }

      c 
+= 4;
      
if (x % c == 0{
        
return false;
      }

      c 
+= 6;
      
if (x % c == 0{
        
return false;
      }

      c 
+= 2;
      
if (x % c == 0{
        
return false;
      }

      c 
+= 6;
    }

    
return true;
  }
原创粉丝点击