Java求素數和最大公約數的簡單程式碼範例_java

來源:互聯網
上載者:User

Java小例子:求素數
素數(質數)指的是不能被分解的數,除了 1 和它本身之外就沒有其它數能夠整除。這裡是一個小例子,說明如何求取十萬以內的所有素數。
 
素數的分布沒有規律可言,所以要檢驗一個數是不是素數,就必須將它同所有小於它的數作除法。不過有一個簡便的方法,就是不需要檢驗所有小於它的數,而只要檢驗所有小於它的素數。如果所有小於它的素數都不能將其整除,那麼它就是素數。

public class Primes {     public static void main(String[] args) {     // 求素數     List<Integer> primes = getPrimes(100000);       // 輸出結果     for (int i = 0; i < primes.size(); i++) {       Integer prime = primes.get(i);       System.out.printf("%8d", prime);       if (i % 10 == 9) {         System.out.println();       }     }   }     /**    * 求 n 以內的所有素數    *    * @param n 範圍    *    * @return n 以內的所有素數    */   private static List<Integer> getPrimes(int n) {     List<Integer> result = new ArrayList<Integer>();     result.add(2);      for (int i = 3; i <= n; i += 2) {       if (!divisible(i, result)) {         result.add(i);       }     }       return result;   }     /**    * 判斷 n 是否能被整除    *    * @param n   要判斷的數字    * @param primes 包含素數的列表    *    * @return 如果 n 能被 primes 中任何一個整除,則返回 true。    */   private static boolean divisible(int n, List<Integer> primes) {     for (Integer prime : primes) {       if (n % prime == 0) {         return true;       }     }     return false;   } } 


Java小例子:類比分數的類 Fraction

這裡是一個類比分數運算的例子:Fraction 類。分數運算完後要用最大公約數除分子分母。所以這裡也有個用輾轉相除法求最大公約數的例子。另外在構造 Fraction 對象時如果分母為零將會拋出異常,這也是必要的檢查。

public class FractionTest {     public static void main(String[] args) {     Fraction a = new Fraction(7, 32);     Fraction b = new Fraction(13, 32);     System.out.println(a + " + " + b + " = " + a.add(b) + "(" + a.add(b).doubleValue() + ")");     System.out.println(a + " - " + b + " = " + a.minus(b) + "(" + a.minus(b).doubleValue() + ")");     System.out.println(a + " * " + b + " = " + a.multiply(b) + "(" + a.multiply(b).doubleValue() + ")");     System.out.println(a + " / " + b + " = " + a.devide(b) + "(" + a.devide(b).doubleValue() + ")");   } }   // 分數 class Fraction {   private int numerator;   // 分子     private int denominator;  // 分母     Fraction(int numerator, int denominator) {     if (denominator == 0) {       throw new IllegalArgumentException("分母不能為 0");     }       this.numerator = numerator;     this.denominator = denominator;     shrink();   }     Fraction() {     this(0, 1);   }     public int getNumerator() {     return numerator;   }     public void setNumerator(int numerator) {     this.numerator = numerator;   }     public int getDenominator() {     return denominator;   }     public void setDenominator(int denominator) {     this.denominator = denominator;   }     // 分子分母同除以最大公約數   private Fraction shrink() {     int maxCommonDivisor = getMaxCommonDivisor(this.denominator, this.numerator);     this.numerator /= maxCommonDivisor;     this.denominator /= maxCommonDivisor;     return this;   }     // 輾轉相除法求最大公約數   private int getMaxCommonDivisor(int a, int b) {     int mod = a % b;       if (mod == 0) {       return b;     } else {       return getMaxCommonDivisor(b, mod);     }   }     // 分數加法   public Fraction add(Fraction that) {     return new Fraction(this.numerator * that.denominator + this.denominator * that.numerator,         this.denominator * that.denominator);   }     // 分數減法   public Fraction minus(Fraction that) {     return new Fraction(this.numerator * that.denominator - this.denominator * that.numerator,         this.denominator * that.denominator);   }     // 分數乘法   public Fraction multiply(Fraction that) {     return new Fraction(this.numerator * that.numerator,         this.denominator * that.denominator);   }     // 分數除法   public Fraction devide(Fraction that) {     return new Fraction(this.numerator * that.denominator,         this.denominator * that.numerator);   }     public double doubleValue() {     return (double) numerator / denominator;   }     @Override   public String toString() {     return String.format("{%d/%d}", this.numerator, this.denominator);   } } 

 
運行輸出:

{7/32} + {13/32} = {5/8}(0.625){7/32} - {13/32} = {-3/16}(-0.1875){7/32} * {13/32} = {91/1024}(0.0888671875){7/32} / {13/32} = {7/13}(0.5384615384615384)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.