Java Simple code example for Prime and GCD _java

Source: Internet
Author: User
Tags gcd

Java Small Example: the number of primes
(prime numbers) refers to numbers that cannot be decomposed, except for 1 and no other number is divisible. Here is a small example of how to find all the primes within 100,000.
&NBSP
The distribution of prime numbers is not regular, so to verify that a number is a prime, you must divide it with all numbers smaller than it. But there is an easy way to do this is not to check all the numbers that are less than it, and just test all the primes that are less than it. If all of its primes are not divisible, then it is prime.

public class Primes {public static void main (string[] args) {//primes list<integer> Primes = Getpri 
  
    Mes (100000); 
      Output for (int i = 0; i < primes.size (); i++) {Integer prime = Primes.get (i); 
      System.out.printf ("%8d", Prime); 
      if (i% = = 9) {System.out.println (); }}/** * Find all primes within n * * @param n Range * * * @return N all primes/private static L 
    ist<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;  /** * Determine whether n can be divisible * @param n the number to be judged * @param primes a list of prime numbers * * @return if n can be primes 
   Returns true if any one of the integers is divisible. * * private static Boolean divisible (int n, list<integer> primes) {for (Integer prime:primes) {iF (n% prime = = 0) {return true; 
  return false; 

 } 
}


Java Small example: class fraction for analog fractions

Here is an example of a simulated fractional operation: the Fraction class. After the fractional operation, use GCD to divide the numerator denominator. So there is also an example of gcd by the Euclidean method. In addition, if the denominator is zero when the fraction object is constructed, the exception is thrown, 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; Denominator fraction (int numerator, int denominator) {if (denominator = = 0) {throw new illegalargumentexcept 
    Ion ("Denominator cannot be 0"); 
    } this.numerator = numerator; 
    This.denominator = denominator; 
  Shrink (); 
  } fraction () {This (0, 1); 
  public int Getnumerator () {return numerator;The public void setnumerator (int numerator) {this.numerator = numerator; 
  public int Getdenominator () {return denominator; 
  The public void setdenominator (int denominator) {this.denominator = denominator; }//numerator denominator divided by gcd private fraction shrink () {int maxcommondivisor = Getmaxcommondivisor (This.denominator, t 
    His.numerator); 
    This.numerator/= Maxcommondivisor; 
    This.denominator/= Maxcommondivisor; 
  return this; 
  
    }//Euclidean algorithm gcd private int getmaxcommondivisor (int a, int b) {int mod = a% B; 
    if (mod = = 0) {return b; 
    else {return getmaxcommondivisor (b, MoD); }//Fractional addition public fraction add (fraction) {return new fraction (This.numerator * that.denominator + 
  This.denominator * that.numerator, This.denominator * that.denominator); }//Fractional subtraction public fraction minus (fraction,) {return to New fraction (This.numerator * that).Denominator-this.denominator * that.numerator, This.denominator * that.denominator); 
        }//Fractional multiplication public fraction multiply (fraction.) {return new fraction (This.numerator * that.numerator, 
  This.denominator * that.denominator); 
        }//Fractional Division public fraction devide (fraction.) {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); 
 } 
}


Run the 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)
   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.