algorithm@ Sieve of Eratosthenes (素数筛选算法) & Related Problem (Return two prime numbers )

Sieve of Eratosthenes (素数筛选算法)

Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.
For example, if n is 10, the output should be “2, 3, 5, 7″. If n is 20, the output should be “2, 3, 5, 7, 11, 13, 17, 19″.

The sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than n when n is smaller than 10 million or so (Ref Wiki).

Following is the algorithm to find all the prime numbers less than or equal to a given integer n by Eratosthenes’ method:

  1. Create a list of consecutive integers from 2 to n: (2, 3, 4, …, n).
  2. Initially, let p equal 2, the first prime number.
  3. Starting from p, count up in increments of p and mark each of these numbers greater than p itself in the list. These numbers will be 2p, 3p, 4p, etc.; note that some of them may have already been marked.
  4. Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this number (which is the next prime), and repeat from step 3.

When the algorithm terminates, all the numbers in the list that are not marked are prime.

Explanation with Example:
Let us take an example when n = 50. So we need to print all print numbers smaller than or equal to 50.

We create a list of all numbers from 2 to 50.
Sieve1

According to the algorithm we will mark all the numbers which are divisible by 2.
sieve2

Now we move to our next unmarked number 3 and mark all the numbers which are multiples of 3.
sieve3

We move to our next unmarked number 5 and mark all multiples of 5.
Sieve4

We continue this process and our final table will look like below:
Sieve5

So the prime numbers are the unmarked ones: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47.

Related Practice Problem

http://www.practice.geeksforgeeks.org/problem-page.php?pid=425

Return two prime numbers

Given an even number ( greater than 2 ), return two prime numbers whose sum will be equal to given number. There are several combinations possible. Print only first such pair. 

NOTE: A solution will always exist, read Goldbach’s conjecture.

Also, solve the problem in linear time complexity, i.e., O(n).

Input:

The first line contains T, the number of test cases. The following T lines consist of a number each, for which we'll find two prime numbers.

Note: The number would always be an even number.

 
Output:

For every test case print two prime numbers space separated, such that the smaller number appears first. Answer for each test case must be in a new line.


Constraints:

1 ≤ T ≤ 70
1 ≤ N ≤ 10000


Example:

Input:

5
74
1024
66 
8
9990

Output:

3 71
3 1021
5 61
3 5
17 9973

import java.util.*;
import java.lang.*;
import java.io.*;

class GFG {
    
    public static void func(int n) {
        
        boolean[] prime = new boolean[n+1];
        for(int i=2; i<=n; ++i) {
            prime[i] = true;
        }
        
        for(int p=2; p*p<=n; ++p) {
            if(prime[p]) {
                for(int k=2*p; k<=n; k+=p) {
                    prime[k] = false;
                }
            }
        }
        
        ArrayList<Integer> rs = new ArrayList<Integer> ();
        for(int i=2; i<=n; ++i) {
            if(prime[i]) {
                rs.add(i);
            }
        }
        
        for(int i=0; i<rs.size(); ++i) {
            int first = rs.get(i);
            int second = n - first;
            if(prime[first] && prime[second]) {
                System.out.println(first + " " + second);
                break;
            }
        }
    }
    
    public static void main (String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        
        for(int i=0; i<t; ++i) {
            int n = in.nextInt();
            func(n);
        }
    }
}
View Code
原文地址:https://www.cnblogs.com/fu11211129/p/5645373.html