Java small example: Find prime numbers <br />Price numbers (prime numbers) refer to numbers that cannot be decomposed. No other numbers can be divided except 1 and itself. Here is a small example of how to find all prime numbers within 100,000.
There is no rule in the distribution of prime numbers, so to check whether a number is a prime number, it must be divided with all numbers smaller than it. But there is an easy way, which is to not test all numbers smaller than it, but just test all prime numbers smaller than it. If all prime numbers smaller than it cannot be divisible, then it is a prime number.
public class Primes { public static void main(String[] args) { // Find the prime number List<Integer> primes = getPrimes(100000); // Output result 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(); } } } /** * Find all prime numbers within n* * @param n Range * * @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; } / ** * 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 divisible by any of primes, it returns true. */ private static boolean divisible(int n, List<Integer> primes) { for (Integer prime : primes) { if (n % prime == 0) { return true; } } return false; } }
Java small example: Fraction class that simulates fractions
Here is an example of simulated fractional operations: the Fraction class. After the fraction is calculated, the greatest common divisor should be used to divide the numerator and denominator. So here is an example of using the transitional phase division to find the greatest common divisor. In addition, if the denominator is zero when constructing a Fraction object, an exception will be 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.p rintln(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() + ")"); } } // Fraction class Fraction { private int numerator; // numerator private int denominator; // denominator Fraction(int numerator, int denomin ator) { 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; } pu blic void setDenominator(int denominator) { this.denominator = denominator; } // The numerator and denominator are divided by the greatest common divisor private Fraction shrink() { int maxCommonDivisor = getMaxCommonDivisor(this.denominator, this.numerator); this.numerator /= maxCommonDivisor; this.denominator /= maxCommonDivisor; return this; } // 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); } // fractional subtraction public Fraction minus(Fraction that) { return new Fraction(this.numerator * that.denominator - this.denominator * that.numerator, this.denominator * that.denominator); } // fraction multiplication public Fraction multiply( Fraction that) { return new Fraction(this.numerator * that.numerator, this.denominator * that.denominator); } // fractional division public Fraction divide(Fraction that) { return new Fraction(this.numerator * that.denominator, th s.denominator * that.numerator); } public double doubleValue() { return (double) numerator / denominator; } @Override public String toString() { return String.format("{%d/%d}", this.numerator, this .denominator); } }
Run 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)