Java simple code example for finding primes and the greatest common divisor

  • 2020-04-01 04:11:43
  • OfStack

Java example: find a prime number
A prime number is a number that cannot be divided, and there is no number that is divisible except 1 and itself. Here is a small example of how to find all primes up to 100,000.
 
There is no law in the distribution of prime Numbers, so to test whether a number is prime or not, you must divide it by all the Numbers smaller than it. But the easy way to do it is, instead of checking all the Numbers that are less than it, you just check all the primes that are less than it. It is a prime if all primes less than it cannot be divisible.


public class Primes { 
  
  public static void main(String[] args) { 
    //For prime
    List<Integer> primes = getPrimes(100000); 
  
    //The output
    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(); 
      } 
    } 
  } 
  
   
  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; 
  } 
  
   
  private static boolean divisible(int n, List<Integer> primes) { 
    for (Integer prime : primes) { 
      if (n % prime == 0) { 
        return true; 
      } 
    } 
    return false; 
  } 
} 


Java example: the class Fraction for analog fractions

Here is an example of analog Fraction: the Fraction class. When you're done, you divide the numerator and the denominator by the greatest common divisor. So here's an example of using division to find the greatest common divisor. In addition, when constructing the Fraction object, an exception will be thrown if the denominator is zero, which is also a necessary check.


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() + ")"); 
  } 
} 
  
//score
class Fraction { 
  private int numerator;   //molecular
  
  private int denominator;  //The denominator
  
  Fraction(int numerator, int denominator) { 
    if (denominator == 0) { 
      throw new IllegalArgumentException(" The denominator cannot be  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; 
  } 
  
  //Divide the numerator and the denominator by the greatest common divisor
  private Fraction shrink() { 
    int maxCommonDivisor = getMaxCommonDivisor(this.denominator, this.numerator); 
    this.numerator /= maxCommonDivisor; 
    this.denominator /= maxCommonDivisor; 
    return this; 
  } 
  
  //We can find the greatest common divisor by division
  private int getMaxCommonDivisor(int a, int b) { 
    int mod = a % b; 
  
    if (mod == 0) { 
      return b; 
    } else { 
      return getMaxCommonDivisor(b, mod); 
    } 
  } 
  
  //Score addition
  public Fraction add(Fraction that) { 
    return new Fraction(this.numerator * that.denominator + this.denominator * that.numerator, 
        this.denominator * that.denominator); 
  } 
  
  //To subtract one fraction from another
  public Fraction minus(Fraction that) { 
    return new Fraction(this.numerator * that.denominator - this.denominator * that.numerator, 
        this.denominator * that.denominator); 
  } 
  
  //Score the multiplication
  public Fraction multiply(Fraction that) { 
    return new Fraction(this.numerator * that.numerator, 
        this.denominator * that.denominator); 
  } 
  
  //Grade division
  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); 
  } 
} 

 
Operation output:


{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)


Related articles: