逆风的香1314 发表于 2013-2-4 19:55:46

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

<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif public static boolean isprime(int x) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    if (x <= 7) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      if (x == 2 || x == 3 || x==5 || x == 7)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return true;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    int c = 7;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    if (x % 2 == 0)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      return false;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    if (x % 3 == 0)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      return false;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    if (x % 5 == 0)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      return false;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    int end = (int) Math.sqrt(x);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    while (c <= end) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif      if (x % c == 0) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return false;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif      }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      c += 4;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif      if (x % c == 0) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return false;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif      }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      c += 2;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif      if (x % c == 0) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return false;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif      }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      c += 4;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif      if (x % c == 0) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return false;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif      }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      c += 2;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif      if (x % c == 0) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return false;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif      }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      c += 4;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif      if (x % c == 0) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return false;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif      }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      c += 6;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif      if (x % c == 0) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return false;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif      }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      c += 2;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif      if (x % c == 0) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return false;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif      }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      c += 6;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    return true;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif  }
页: [1]
查看完整版本: 一种快速判断是否为质数的方法