• 找素数


    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 
    ");// 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("
    " + 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 
    ");// 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("
    " + 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 
    ");
    
            // 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("
    " + 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 "); // 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(" " + 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 
    ");
    
            // 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("
    " + 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("
    " + count + " prime(s) less than and equal to "
                    + n);
        }
    }
  • 相关阅读:
    RTTI机制
    QT 实现 QTabWidget 的多个Tab宽度之和 等于 TabWidget 的宽度
    安装QT
    给QT 程序的EXE文件添加图标
    OceanBase使用libeasy原理源码分析:服务器端
    libev 源码浅析
    [转载]CAP理论十二年回顾:"规则"变了
    replication 技术摘要
    函数调用和函数返回 栈变化情况
    The Secret To 10 Million Concurrent Connections The Kernel Is The Problem, Not The Solution
  • 原文地址:https://www.cnblogs.com/diyishijian/p/5078400.html
Copyright © 2020-2023  润新知