Go: package main

import (
        "fmt"
)

const MAX int = 1000000
var primes [MAX]int

func main() {
        Init()
        var n, cnt int
        for {
                _, err := fmt.Scanf("%d", &n)
                if err != nil {
                        break
                }
                cnt = 0
                for i := 2; i <= n; i++ {
                        cnt += primes[i]
                }
                fmt.Printf("%d\n", cnt)
        }
}

func Init() {
        for i := 0; i < MAX; i++ {
                primes[i] = i
        }

        Soe()

        for i := 0; i < MAX; i++ {
                primes[i] %= 2
        }
        primes[1] = 0
        primes[2] = 1
}

func Soe() {
        for i := 2; i < MAX; i++ {
                if primes[i] > 0 {
                        for j := 2; j < MAX/i; j++ {
                                if i * j < MAX {
                                        primes[i*j] = 0
                                }
                        }
                }
        }
}

C++: #include <iostream>
#include <vector>
using namespace std;

long long n;
vector<long long> p(1, 2);

int main()
{
	for (long long i = 3; i < 1000000; i += 2) {
		long long j = 0;
		bool f = 1;
		while(p[j] * p[j] <= i){
			if (i % p[j] == 0) {
				f = 0;
				break;
			}

			j++;
		}

		if (f)
			p.push_back(i);
	}

	while (cin >> n) {
		int m = 0;

		while (m < p.size() && p[m] <= n)
			m++;

		cout << m << endl;
	}

	return 0;
}

C++: #include <iostream>
using namespace std;
  
bool isprime(int n) {
    for(int i=2; i*i <= n; i++) {
        if(n%i==0) return false;
    }
    return n != 1;
}
  
int main() {
int n;
while(cin >> n) {
int cnt = 0;
for(int i=2; i<=n; i++) if(isprime(i)) cnt++;
cout << cnt << endl;
}
return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <iomanip>
#include <vector>
#include <map>

int main() {
	int a;
	while (std::cin >> a) {
		int nSum = 0;
		for (int i = 2;i <= a;i++) {
			int flag = 0;
		for (int j = 2;j < i;j++) {
			if (i%j == 0) {
				flag = 1;
				break;
			}
		}
		if (flag == 0) {
			nSum += 1;
		}
	}
		std::cout << nSum << std::endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
int main(){
	int n;
	while(std::cin>>n){
		int ans=0;
		std::vector<int> vec(n);
		for(int i=0;i<n;i++)vec[i]=1;
		vec[0]=vec[1]=0;
		for(int i=2;i<n;i++){
			if(vec[i] == 1){
				ans++;
				for(int j = 2 * i ; j < n ; j += i){
					vec[j]=0;
				}
			}
		}
		std::cout<<ans<<std::endl;
	}

	return 0;
}

C++: #include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{
    int x;
    int i, j;
    int p[999999] = {};
    p[2] = 1;
    for (i = 3; i < 1000000; i++) {
        for (j = 2; ; j++) {
            if (!(i % j)) {
                break;
            } else if (j > sqrt(i)) {
                p[i] = 1;
                break;
            }
        }
    }
    while (scanf("%d", &x) != EOF) {
        j = 0;
        for (i = 0; i <= x; i++) {
            j += p[i];   
        }
        printf("%d\n", j);
    }
    return 0;
}

C++: #include <bitset> // compact STL for Sieve, better than vector<bool>!
#include <vector>
#include <cstdio>
#include <iostream>
#define ll long long
#define vi vector<int>

using namespace std;

ll _sieve_size; // ll is defined as: typedef long long ll;
bitset<10000010> bs; // 10^7 should be enough for most cases
vi primes; // compact list of primes in form of vector<int>
void sieve(ll upperbound) { // create list of primes in [0..upperbound]
_sieve_size = upperbound + 1; // add 1 to include upperbound
bs.set(); // set all bits to 1
bs[0] = bs[1] = 0; // except index 0 and 1
for (ll i = 2; i <= _sieve_size; i++) if (bs[i]) {
// cross out multiples of i starting from i * i!
for (ll j = i * i; j <= _sieve_size; j += i) bs[j] = 0;
primes.push_back((int)i); // add this prime to the list of primes
} } // call this method in main method
bool isPrime(ll N) { // a good enough deterministic prime tester
if (N <= _sieve_size) return bs[N]; // O(1) for small primes
for (int i = 0; i < (int)primes.size(); i++)
if (N % primes[i] == 0) return false;
return true; // it takes longer time if N is a large prime!
} // note: only work for N <= (last prime in vi "primes")^2
// inside int main()
int main()
{
    int TestCase,x;
sieve(1000010); // can go up to 10^7 (need few seconds)

    while(cin >> TestCase)
    {
        x=0;
        while(TestCase >= primes[x])
        {
            x++;
        }
        cout << x << endl;
    }
}

C++: #include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

typedef long long ll;

template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

// ref. YouTube ABC 152 E
class EratosthenesSieve {
    public:
        const int N;

        EratosthenesSieve(const int N): N(N) {
            numbers.resize(N + 1, 0);
            init();
        }

        // assume that n > 0 && n <= N
        map<int, int> factors(int n) {
            // assert(n > 0 && n <= N); 
            map<int, int> res;
            while (n > 1) {
                res[numbers[n]]++;
                n /= numbers[n];
            }
            return res;
        }

        void primes(vector<int> &p) {
            for (int i = 2; i <= N; i++) {
                if (is_prime(i)) {
                    p.push_back(i);
                }
            }
        }

        // assume that n > 0 && n <= N
        bool is_prime(const int n) {
            // assert(n > 0 && n <= N);
            return numbers[n] == n;
        }

    private:
        vector<int> numbers; // numbers[i] means minimum prime factor for i (e.g. numbers[12] = 2)

        void init() {
            // O(N * log(logN)) ???
            int i = 2;
            for (; i * i <= N; i++) {
                if (numbers[i] == 0) {
                    numbers[i] = i;
                    for (int j = i * i; j <= N; j += i) {
                        if (numbers[j] == 0) {
                            numbers[j] = i;
                        }
                    }
                }
            }
            for (; i <= N; i++) {
                if (numbers[i] == 0) {
                    numbers[i] = i;
                }
            }
        }
};

const int MAX_N = 999999;

// ref. http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0009
int main(void) {
    EratosthenesSieve *sieve = new EratosthenesSieve(MAX_N);
    vector<int> ps;
    sieve->primes(ps);

    char buf[1024];
    while (fgets(buf, 1024, stdin) != NULL) {
        int x = atoi(buf);
        auto y = upper_bound(ps.begin(), ps.end(), x);
        cout << y - ps.begin() << endl;
    }

    delete sieve;
    return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,x,n) for(int i=(x);i<(n);i++)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define ub upper_bound
#define lb lower_bound
#define int long long
using namespace std;
const int MOD=1000000007;
const int INF=1000000009;

bool Is_Prime(int n){
  if(n==1) return false;
  for(int i=2;i*i<=n;i++){
    if(n%i==0){
      return false;
    }
  }
  return true;
}

signed main(){
  int n;
  while(cin>>n){
    int ans=0;
    loop(i,1,n+1)if(Is_Prime(i))ans++;
    cout<< ans<<endl;
  }
  return 0;
}

C++: #include<iostream>

int main(){
  int prime[200000];
  int k,n;
  prime[0]=2;
  while(std::cin >> n){
    if(n==1){
      std::cout << 0 << std::endl;
      continue;
    }
    k=0;
    for(int i=3;i<=n;i++){
      for(int j=0;j<=k;j++){
        if(i%prime[j]==0){
          break;
        }
        if(j==k){
          ++k;
          prime[k]=i;
        }
      }
    }
    std::cout << k+1 << std::endl;
  }
}