找素数

1、穷举算法(<number) 时间复杂度O(n)

/**
 * 穷举算法 时间复杂度O(n)
 */
public class PrimeNumber {

    /**
     * @param args
     */
    public static void main(String[] args) {

        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("Find all prime numbers <= n, enter n: ");
        int n = input.nextInt();
        final int NUMBER_OF_PRIMES_LINE = 10; // Display 10 per line
        int count = 0; // Count the number of prime numbers
        int number = 2; // A number to be tested for primeness

        System.out.println("The prime numbers are \n");// Repeatedly find prime numbers
        while (number < n) {
            // Assume the number is prime
            boolean isPrime = true; // Is the current number prime?

            // Test whether number is prime
            for (int divisor = 2; divisor < number; divisor++) {
                if (number % divisor == 0) { // If true, number is not prime
                    isPrime = false; // Set isPrime to false
                    break; // Exit the for loop
                }
            }

            // Print the prime number and increase the count
            if (isPrime) {
                count++; // Increase the count
                if (count % NUMBER_OF_PRIMES_LINE == 0) {
                    // Print the number and advance to the new line
                    System.out.println(number);
                }
                else {
                    System.out.print(number + " ");
                }
            }

            // Check if the next number is prime
            number++;
        }
        System.out.println("\n" + count + " prime(s) less than and equal to "
                + n);
    }
}

2、穷举算法(<= number / 2) 时间复杂度O(n)

/**
 * 穷举算法 时间复杂度O(n)
 */
public class PrimeNumber {

    /**
     * @param args
     */
    public static void main(String[] args) {

        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("Find all prime numbers <= n, enter n: ");
        int n = input.nextInt();
        final int NUMBER_OF_PRIMES_LINE = 10; // Display 10 per line
        int count = 0; // Count the number of prime numbers
        int number = 2; // A number to be tested for primeness

        System.out.println("The prime numbers are \n");// Repeatedly find prime numbers
        while (number < n) {
            // Assume the number is prime
            boolean isPrime = true; // Is the current number prime?

            // Test whether number is prime
            for (int divisor = 2; divisor < number / 2; divisor++) {
                if (number % divisor == 0) { // If true, number is not prime
                    isPrime = false; // Set isPrime to false
                    break; // Exit the for loop
                }
            }

            // Print the prime number and increase the count
            if (isPrime) {
                count++; // Increase the count
                if (count % NUMBER_OF_PRIMES_LINE == 0) {
                    // Print the number and advance to the new line
                    System.out.println(number);
                }
                else {
                    System.out.print(number + " ");
                }
            }

            // Check if the next number is prime
            number++;
        }
        System.out.println("\n" + count + " prime(s) less than and equal to "
                + n);
    }
}

3、使用平方根 时间复杂度O(n√n)

/**
 * 使用平方根 时间复杂度O(n√n)
 */
public class PrimeNumbers {

    /**
     * @param args
     */
    public static void main(String[] args) {

        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("Find all prime numbers <= n, enter n: ");
        int n = input.nextInt();

        final int NUMBER_OF_PRIMES_LINE = 10; // Display 10 per line
        int count = 0; // Count the number of prime numbers
        int number = 2; // A number to be tested for primeness
        int squareRoot = 1;

        System.out.println("The prime numbers are \n");

        // Repeatedly find prime numbers
        while (number <= n) {
            // Assume the number is prime
            boolean isPrime = true; // Is the current number prime?

            squareRoot = (int) Math.sqrt(number);
// Test whether number is prime
            for (int divisor = 2; divisor <= squareRoot; divisor++) {
                if (number % divisor == 0) { // If true, number is not prime
                    isPrime = false; // Set isPrime to false
                    break; // Exit the for loop
                }
            }
            // Print the prime number and increase the count
            if (isPrime) {
                count++; // Increase the count
                if (count % NUMBER_OF_PRIMES_LINE == 0) {
                    // Print the number and advance to the new line
                    System.out.println(number);
                }
                else {
                    System.out.print(number + " ");
                }
            }

            // Check if the next number is prime
            number++;
        }
        System.out.println("\n" + count + " prime(s) less than and equal to "
                + n);
    }
}

4、使用完全平方数 时间复杂度O(n√n)

/**
 * 不使用确切的平方根,只需要找出完全平方数。例如:4、9、16、25、36、49等等。
 * 注意,对于36和48之间并包含36和48的数,它们的(int) Math.sqrt(number) 为6.
* 时间复杂度O(n√n)
*/ public class PrimeNumbers { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("Find all prime numbers <= n, enter n: "); int n = input.nextInt(); final int NUMBER_OF_PRIMES_LINE = 10; // Display 10 per line int count = 0; // Count the number of prime numbers int number = 2; // A number to be tested for primeness int squareRoot = 1; System.out.println("The prime numbers are \n"); // Repeatedly find prime numbers while (number <= n) { // Assume the number is prime boolean isPrime = true; // Is the current number prime? if (squareRoot * squareRoot <= number) { squareRoot++; } // Test whether number is prime for (int divisor = 2; divisor < squareRoot; divisor++) { if (number % divisor == 0) { // If true, number is not prime isPrime = false; // Set isPrime to false break; // Exit the for loop } } // Print the prime number and increase the count if (isPrime) { count++; // Increase the count if (count % NUMBER_OF_PRIMES_LINE == 0) { // Print the number and advance to the new line System.out.println(number); } else { System.out.print(number + " "); } } // Check if the next number is prime number++; } System.out.println("\n" + count + " prime(s) less than and equal to " + n); } }

5、检测2到√i之间的素数能否整除i  时间复杂度O(n√n / logn)

/**
 * 检测2到√i之间的素数能否整除i
 * 原因:如果i不是素数,那就必须存在一个素数p,满足i=pq和p<=q。
 * 证明:假设i不是素数,且p是i的最小因子。那么p肯定是素数,否则,p就有一个因子k,且2<=k<p。
 * k也是i的一个因子,这和p是i的最小因子是冲突的。因此,如果i不是素数,那么可以找到从2到√i之间的整除i的素数。
 * 时间复杂度O(n√n / logn)
 */
public class EfficientPrimeNumbers {

    /**
     * @param args
     */
    public static void main(String[] args) {

        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("Find all prime numbers <= n, enter n: ");
        int n = input.nextInt();

        final int NUMBER_OF_PRIMES_LINE = 10; // Display 10 per line
        List<Integer> primeList = new ArrayList<Integer>();
        int count = 0; // Count the number of prime numbers
        int number = 2; // A number to be tested for primeness
        int squareRoot = 1;

        System.out.println("The prime numbers are \n");

        // Repeatedly find prime numbers
        while (number <= n) {
            // Assume the number is prime
            boolean isPrime = true; // Is the current number prime?

            if (squareRoot * squareRoot <= number) {
                squareRoot++;
            }
            // Test whether number is prime
            for (int i = 0; i < primeList.size()
                    && primeList.get(i) <= squareRoot; i++) {
                if (number % primeList.get(i) == 0) { // If true, number
                                                        // is not prime
                    isPrime = false; // Set isPrime to false
                    break; // Exit the for loop
                }
            }
            // Print the prime number and increase the count
            if (isPrime) {
                count++; // Increase the count
                primeList.add(number);
                if (count % NUMBER_OF_PRIMES_LINE == 0) {
                    // Print the number and advance to the new line
                    System.out.println(number);
                }
                else {
                    System.out.print(number + " ");
                }
            }

            // Check if the next number is prime
            number++;
        }
        System.out.println("\n" + count + " prime(s) less than and equal to "
                + n);
    }
}

6、Eratosthenes算法 时间复杂度O(n√n / logn)

/**
 * Eratosthenes算法  时间复杂度O(n√n / logn)
 * @author LYF
 * @创建时间 2015-12-26下午4:38:10
 * @修改时间
 */
public class SieveOfEratosthenes {

    /**
     * @param args
     */
    public static void main(String[] args) {

        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("Find all prime numbers <= n, enter n: ");
        int n = input.nextInt();

        boolean[] primes = new boolean[n + 1];

        final int NUMBER_OF_PRIMES_LINE = 10; // Display 10 per line

        int count = 0;

        for (int i = 2; i < primes.length; i++) {
            primes[i] = true;
        }

        for (int i = 2; i <= n / i; i++) {
            if (primes[i]) {
                for (int j = i; j <= n / i; j++) {
                    primes[j * i] = false;
                }
            }
        }

        for (int i = 0; i < primes.length; i++) {
            if (primes[i]) {
                count++;
                if (count % NUMBER_OF_PRIMES_LINE == 0) {
                    System.out.println(i + " ");
                }
                else {
                    System.out.print(i + " ");
                }
            }
        }

        System.out.println("\n" + count + " prime(s) less than and equal to "
                + n);
    }
}

 

posted @ 2015-12-26 16:59  第壹时间  阅读(295)  评论(0编辑  收藏  举报