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<stdio.h>
#include<string>
#include<map>
#include<vector>
#include<stdlib.h>
typedef long long ll;
using namespace std;
#define INF 100000000000
#include<algorithm>
#include<string.h>
bool num[1000000];
int main(){
    int n;
    while(cin >> n){
        memset(num,true,sizeof(num));
        num[0] = num[1] = false;
        for(int i = 2;i<=n;i++){
            if(num[i] == true){
                for(int j = 2*i;j<=n;j+=i){
                    num[j] = false;
                }
            }

        }
        int ans = 0;
        for(int i =2;i<=n;i++){
            if(num[i]){ans++;}
        }
        cout<<ans<<endl;
    }

}

C++: #include<iostream>
#include<stdio.h>
#include<math.h>

using namespace std;

int HowManyPrimeNumbers1(const int);
int HowManyPrimeNumbers2(const int);
int MySqrt(const int);

int main(){
	int n;

	while(cin >> n){
		printf("%d\n",HowManyPrimeNumbers2(n));
	}

	return 0;
}

int HowManyPrimeNumbers1(const int n){
	int i,j,buf,cnt = 1;

	for(i=3;i<=n;i+=2){
		buf = (int)sqrt((double)i);

		for(j=3;j<=buf;j+=2){
			if((i % j) == 0){
				cnt--;
				break;
			}
		}

		cnt++;
	}

	return cnt;
}

int HowManyPrimeNumbers2(const int n){
	int i,j,cnt,root,rt;

	// 3 から n までの奇数の個数 + 1 を n に代入
	cnt = ((int)((((double)(n - 2)) / 2) + 0.5)) + 1;

	// 最大処理数を求める
	root = (int)sqrt((double)n);

	// a
	for(i=3;i<=root;i+=2){
		rt = MySqrt(i);
		for(j=3;j<=rt;j+=2){
			if((i % j) == 0){
				cnt += ((int)(n / i));
				break;
			}
		}
		cnt -= ((int)(n / i));
	}

	return n;
}

int MySqrt(const int i){
	int n;

	n = i - 1;

	return (int)(1 + (1 / 2) * (double)(i - 1) - (1 / 8) * (double)(i - 1) * (i - 1) + (1 / 16) * (double)(i - 1) * (i - 1) * (i - 1));
}

C++: #include <iostream>
#include <cmath>
using namespace std;
#define MAX 1000000

int main(){
	bool p[MAX+1];
	int n, ans;

	for(int i=0 ; i<=MAX ; i++){
		p[i] = (i<=1)? false : true ;
	}

	for(int i=2 ; i<=sqrt((double)MAX)+1 ; i++){
		if(p[i]){
			for(int j=i*2 ; j<=MAX ; j+=i){
				p[j] = false;
			}
		}
	}

	while( cin >> n ){		
		ans = 0;
		for(int i=0 ; i<=n ; i++){
			if(p[i]){
				ans++;
			}
		}
		cout << ans << endl;
	}
}

C++: #include <cstdio>
#define max_n 999999
#define INF 1000000
int prime[max_n];
bool is_prime[max_n+1];
int main(){
    for(int i=0; i<max_n; i++){
        prime[i] = INF;
        is_prime[i]=true;
    }
    int p = 0;
    is_prime[1]=false;
    is_prime[0]=false;
    for(int i=2;i<max_n;i++){
        if(is_prime[i]){
            prime[p++] = i;
            for(int j=2*i;j<max_n;j+=i)is_prime[j]=false;
        }
    }
    int n;
    while(scanf("%d",&n)!=EOF){
        //printf("%d\n",n);
        int ans = 0;
        for(int i =0; i<max_n; i++){
            if(prime[i]>n){
                ans = i;
                break;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

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

int main() {
  constexpr int max = 1000000;
  vector<bool> isPrime(max, true);
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i * i < max; i++) {
    if (isPrime[i]) {
      for (int j = i * 2; j < max; j += i) {
        isPrime[j] = false;
      }
    }
  }
  int n;
  while (cin >> n) {
    int ans = 0;
    for (int i = 0; i <= n; i++) {
      if (isPrime[i]) ans++;
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <list>
#include <map>
#include <algorithm>
#include <stack>
#include <string>

using namespace std;


int main(){
	int n = 0;
	int count;
	bool flag = false;

	while(cin >> n){
		count = 1;
		for(int i = 3; i <= n; i += 2){
			for(int j = 3; j * j <= i; j += 2){
				if(i % j == 0){
					flag = true;
					break;
				}
			}
			if(flag != true){
				count++;
			}
			else{
				flag = false;
			}
		}
		cout << count << endl;
	}

	return 0;
}

C++: //求n以内素数个数以及所有素数
//复杂度O(n*loglogn)
#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

typedef long long ll;
const int MAXN = 1000000+5;

int prime[MAXN]; //第i个素数
bool is_prime[MAXN+1]; //is_prime[i]为true表示i是素数，否则不是

//返回n以内素数个数
int seive(int n){
    int p=0;
    for(int i=0;i<=n;++i) is_prime[i]=true;
    is_prime[0]=is_prime[1]=false;
    for(int i=2;i<=n;++i){
        if(is_prime[i]){
            prime[p++]=i;
            for(int j=2*i;j<=n;j+=i) is_prime[j]=false;
        }
    }
    return p;
}

bool is_prime_large[MAXN]; // is_prime_large[i-a]=true <--> i是素数
//求区间[a,b)之间的素数及个数, a<b<=10^12, b-a<=10^6
int segment_sieve(ll a, ll b){
    seive((int)sqrt(b)+1);
    for(int i=0;i<b-a;++i) is_prime_large[i]=true;

    for(int i=2;(ll)i*i<b;++i){
        if(is_prime[i]){
            for(ll j=max(2LL,(a+i-1)/i)*i; j<b; j+=i) is_prime_large[j-a]=false;
        }
    }

    int p=0;
    for(ll i=a;i<b;++i){
        if(is_prime_large[i-a]) p++; 
    }
    return p;
}

int main(){
/*    ll a=22801763489, b=22801787297;*/
    //int p=segment_sieve(a,b);
    //cout<<"segment_sieve:"<<p<<endl;

    int n;
    while(cin>>n) {
        cout<<seive(n)<<endl;
    }
}

C++: #include<cstdio>
//#include<cmath>
//#include<iostream>
using namespace std;

#define MAX 1000000

int main(){
	int i,j,k;
	bool n[MAX];
		i=MAX; while(i--) n[i]=true;

	while(~scanf("%d", &i)){

	for(j=2; j<i; j++){
		if(n[j]==true){
		for(k=2; j*k<i; k++){
			n[j*k]=false;
		
		}
		}
	}

	for(j=2, k=0; j<i; j++) if(n[j]==true) k++;

	printf("%d\n", k);
	}
 return 0;
}

C++: #include <iostream>
using namespace std;
int main() {
  long n;
  while (cin >> n) {
    if (n == 1) {
      cout << 0 << endl;
      continue;
    }
    int x = 1;
    if (n == 2) {
      cout << x << endl;
      continue;
    }
    x++;
    for (int i = 3; i <= n; i += 2)
      for (int j = 2; j * j <= i; j++) {
        if (i % j == 0) break;
        if ((j + 1) * (j + 1) > i) x++;
      }
    cout << x << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define REP(i,N) for(int i = 0; i < (int)N; i++)

int main(){
	int max = 1000100;
	bool prime[1000100];
	int dp[1000100];
	prime[0] = prime[1] = false;
	int ret=0;
	memset(prime,true,sizeof(prime));
	memset(dp,0,sizeof(dp));
	int hako[30];
	memset(hako,0,sizeof(hako));
	REP(i,30) cin >> hako[i];
	for(int i=2;i<sqrt(max);i++)for(int j=i*2;j<max;j=j+i){
		prime[j] = false;
	}
	for(int i=1; i<max ;i++){
		dp[i]=dp[i-1];
		if(prime[i])dp[i]++;
	}
	REP(i,30)	cout << dp[hako[i]] << endl;
	return 0;
}