Go: package main

// https://ja.wikipedia.org/wiki/%E3%82%A8%E3%83%A9%E3%83%88%E3%82%B9%E3%83%86%E3%83%8D%E3%82%B9%E3%81%AE%E7%AF%A9

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() (int, error) {
	return strconv.Atoi(nextString())
}

func main() {
	scanner.Split(bufio.ScanWords)

	for {
		n, err := nextInt()
		if err != nil {
			break
		}
		maybePrime := make([]bool, n+1)
		for i := 0; i <= n; i++ {
			maybePrime[i] = true
		}

		maybePrime[0] = false
		maybePrime[1] = false
		for i := 2; i <= int(math.Sqrt(float64(n)))+1; i++ {
			j := i * 2
			for j <= n {
				maybePrime[j] = false
				j += i
			}
		}

		nPrimes := 0
		for _, p := range maybePrime {
			if p {
				nPrimes++
			}
		}
		// fmt.Println(maybePrime)
		fmt.Println(nPrimes)
	}
}

C++: #include <iostream>
#include <cmath>
#define N 1000000

bool m[N];

void init_array() {
    int n = N-1;
    int idx = 2;
    std::fill(m, m+N, true);
    while (idx <= sqrt(n)) {
        int next_itr = -1;
        for (int i=idx+1; i<=n; i++) {
            if (m[i]) {
                if (i % idx == 0) {
                    m[i] = false;
                } else if (next_itr == -1) {
                    next_itr = i;
                }
            }
        }
        idx = next_itr;
    }
}

int main(void) {
    int n;
    int res;
    init_array();
    while( std::cin >> n ) {
        int idx = 2;
        int res=0;

        for (int i=2; i<=n; i++) {
            if (m[i]) res++;
        }

        std::cout << res << std::endl;
    }

    return 0;
}

C++: #include<iostream>
#include<cmath>
using namespace std;
bool prime(int);
int main() {
	int n;
	while (cin >> n) {
		int cnt = 0;
		if (n >= 2) cnt++;
		for(int i = 3; i<=n; i += 2){
			if (prime(i)) {
				cnt++;
			}
		}
		cout << cnt << endl;
	}
	return 0;
	cin >> n;
}

bool prime(int n) {
	for (int i = 3; i <= sqrt(n); i++) {
		if (n % i == 0) return false;
	}
	return true;
}

C++: #include <iostream>
#define N 100000
using namespace std;

int main(void)
{
    bool a[N+1];
    int prime[N], n;
    for(int i=0;i<N+1;i++)
    {
        a[i] = true;
    }
    a[0] = a[1] = false;
    for(int i=2;i*i<=N;i++)
    {
        if(a[i])
        {
            prime[i] = prime[i-1] + 1;
            for(int j=i*i;j<N;j+=i)
                a[j] = false;
        }
        else
            prime[i] = prime[i-1];
    }

    while(cin >> n)
        cout << prime[n] << endl;
    
    return 0;
}

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

int sieve_of_eratosthenes(int n){
  int p = 0; //素数の数
  int primes[n];
  bool is_prime[n+1]; 
  for(int i=0;i<=n;i++) is_prime[i] = true;
  is_prime[0] = is_prime[1] = false; //0,1は素数じゃない
  for(int i=2;i<=n;i++){
    if(is_prime[i]){
      primes[p++] = i;
      for(int j= i*2;j<=n;j+=i) is_prime[j] = false;
    }
  }
  return p;
}

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

C++: #include <bits/stdc++.h>
#define MAX_N 1000001
using namespace std;
bool prime[MAX_N];
int table[MAX_N];

void initialize() {
    int p = 0;
    memset(prime, true, sizeof(prime));
    prime[0] = prime[1] = false;
    for(int r = 2; r * r <= MAX_N; r++) {
        if(prime[r]) {
            table[p++] = r;
            for(int c = r * 2; c <= MAX_N; c+= r) {
                prime[c] = false;
            }
        }
    }
}


int main(void) {
    initialize();
    int num;
    while(cin >> num) {
        int cnt = 0;
        for(int r = 0; table[r] <= num; r++) {
            //cout << table[r];
            //putchar(' ');
            cnt++;
        }
        //putchar('\n');
        cout << cnt << endl;
    }
    return(0);
}

C++: #include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main(){
  int n;
  int cont;
  while(scanf("%d",&n)!=EOF){
    int a[n];
    for(int i=0;i<n;i++){
      a[i]=1;        //1 素数　０素数ではない
    }
    a[0]=0;
    cont=0;
    for(int i=2;i<=sqrt(n);i++){
      if(a[i-1]==1){
	for(int j=2;i*j<=n;j++){
	  //	  cout << j << " " << n << endl;
	  a[i*j-1]=0;
	}
      }
    }
    for(int i=0;i<=n;i++){
      //cout<<i+1<<" "<<a[i]<<endl;
      if(a[i]==1)cont++;
    }
    cout<<cont<<endl;
  }
}

C++: #include <stdlib.h>
#include <stdio.h>
const int MAX_V = 1000000; // 100????????§?´???°??¨?????????
int prime[MAX_V+1]; // 1????´???°???0????´???°??§?????? 
int pcount[MAX_V+1];
int main() {
    int i, k,a,sum=0;
    for(i = 2; i <= MAX_V; i++) {
        prime[i] = 1; // 2??\????´???°??¨??????
    } 
    for(i = 2; i*i <= MAX_V; i++) {
        if(prime[i]) {
            for(k = 2 * i; k <= MAX_V; k += i) {
                prime[k] = 0; // ?´???°2?????\????????°?´???°??§?????? 
            }
            
        } 
        
    }
    pcount[0]=0;
    pcount[1]=0;
    for(i=2;i<MAX_V;i++){
        if(prime[i]){
            sum++;
        }
        pcount[i]=sum;
    }
    for(i=0;i<30;i++){
        scanf("%d",&a);
        printf("%d\n",pcount[a]);
    } 

    return 0; 
    
}

C++: #include<iostream>
using namespace std;
int main(){
  long n;
  int count1,count2;
  while(1){
    cin >>n;
    count2=0;
    for(int i=n;i>1;i--){
      count1=0;
      for(int j=2;j<i;j++){
	if(i%j==0){
	  count1++;
	  break;}
      }
      if(count1==0)count2++;}
      cout <<count2<<endl;
    }
  return 0;
}

C++: 



#include <iostream>
#include <vector>
#include <cmath>
using namespace std;


int main(void)
{
	int n;
	vector<int> vecPrime;

	while (cin >> n)
	{
		// vectorの破棄
		vecPrime.clear();

		// nが2以上の時,2のみ追加
		if (n >= 2)
		{
			vecPrime.push_back(2);
		}

		// 奇数のみ探索
		for (int i = 3; i <= n; i += 2)
		{
			// 割り切れたらtrueにする
			bool flagPrime = false;

			// 奇数しかループさせないため、j = 1からでよい
			int size = vecPrime.size();
			for (int j = 1; j < size && vecPrime[j] < sqrt((double)i); j++)
			{
				// 割り切れたら素数でない
				if (i % vecPrime[j] == 0)
				{
					flagPrime = true;
					break;
				}
			}
			// すべて割り切れなかった場合
			if (flagPrime == false)
			{
				vecPrime.push_back(i);
			}
		}

		cout << vecPrime.size() << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<stack>
using namespace std;
int main() {
	int n,ans=0,i,count=0;
	int dp[100][100];
	for (int i = 0; i < 100; i++) {
		for (int j = 0; j < 100; j++) {
			dp[i][j] = -1;
		}
	}
	while (cin >> n) {
		for (i = 2; i <= n; i++) {
			for (int j = 2; j <= i; j++) {
				if (dp[i][j] == -1) {
					dp[i][j] = i%j;
				}
				if (dp[i][j] == 0 ) {
					count++;
				}
			}
			if (count == 1) {
				ans++;
			}
			count = 0;
		}
		cout << ans << endl;
		ans = 0;
	}
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;

const int maxn = 999999 + 10;
bool is_prime[maxn];
int tot[maxn];

void sieve(int n){
	fill(is_prime, is_prime + n + 1, true);
	is_prime[0] = is_prime[1] = false;
	
	for(int i=2; i*i <= n; i++) if(is_prime[i])
		for(int j=2*i; j <= n; j+=i)
			is_prime[j] = false;	
}

int main(){
//	freopen("data/in.txt", "r", stdin);
	
	sieve(maxn-1);
	int cnt = 0;
	for(int i=0; i<maxn; i++){
		if(is_prime[i]) cnt++;
		tot[i] = cnt;
	}
	
	int n;
	while(1==scanf("%d", &n)){
		printf("%d\n", tot[n]);
	}

	return 0;
}

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

bool prime(int num) {
    if (num < 2) return false;
    else if (num == 2) return true;
    else if (num % 2 == 0) return false;

    for (int i = 3; i * i <= num; i += 2) {
        if (num % i == 0) return false;
    }

    return true;
}

int main() {
    int ans[1000000], n;
    ans[0] = 0;
    for (int i = 1; i <= 999999; ++i) {
        if (prime(i)) ans[i] = ans[i - 1] + 1;
        else ans[i] = ans[i - 1];
    }

    while (cin >> n) {
        cout << ans[n] << endl;
    }
    
    return 0;
}