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<math.h>
#include<string>
using namespace std;

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

C++: #include <stdio.h>
 
#define MAX_SIZE(1000001)
 
int prime[MAX_SIZE];
 
int main()
{
int n, i, j;
     
for (i = 0; i < MAX_SIZE; i++){
prime[i] = 1;
}
     
prime[0] = prime[1] = 0;
     
for (i = 2; i * i < MAX_SIZE; i++){
if (prime[i] == 1){
for (j = i * i; j < MAX_SIZE; j += i){
prime[j] = 0;
}
}
}
     
for (i = 1; i < MAX_SIZE - 1; i++){
prime[i + 1] += prime[i];
}     
while (scanf("%d", &n) != EOF){
printf("%d\n", prime[n]);
}     
return 0;
}

C++: #include <iostream>
#include <cmath>

typedef long long LL;
using namespace std;

const int X = 1000001;
bool Find[X];

void init(){
  for(int i=0;i<X;i++){
    Find[i] = false;
  }
  for(int i=2;i<=sqrt(X);i++){
    if(Find[i])continue;
    for(int j=i+1;j<X;j++){
      if(Find[j])continue;
      if(j%i==0)Find[j]=true;
    }
  }
}

int main(){
  init();
  int n;
  while(cin >> n){
    int answer = 0;
    for(int i=2;i<n+1;i++){
      if(!Find[i])answer++;
    }
    cout << answer << endl;
  }
  return 0;
}

C++: #include <stdio.h>

int main(){
	int n;
	while(scanf("%d", &n) != EOF){
		int *arr = new int[n + 1];
		
		for(int i = 2; i <= n; i++){
			if(arr[i] == 0){
				for(int j = 2*i; j <= n; j += i){
					arr[j] = 1;
				}
			}
		}
		
		int cnt = 0;
		for(int i = 2; i <= n; i++){
			if(arr[i] == 0){
				cnt++;
			}
		}
		delete [] arr;
		
		printf("%d\n", cnt);
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
using namespace std;
int main(){
  int n;
  while(cin >> n){
    int cnt=0,res=0;
    for(int i=2; i<=n; i++){
      for(int j=1; j<=n; j++){
        if(i%j == 0) cnt++;
      }
      if(cnt==2) res++;
      cnt = 0;
    }
    cout << res << endl;
  }
  return 0;
}

C++: #include <stdio.h>
#include <cmath>
int n;
int num;
int main()
{
    scanf("%d",&n);
    if(n==0||n==1)
    {
        printf("%d\n",num);
        return 0;
    }
    for(int i=2;i<=n;i++)
    {
    // i 是不是 prime
        bool isp=true;
        int m=sqrt(i);
        for(int j=2;j<=m;j++)
        {
            if(i%j==0)
            {
                isp=false;
                break;
            }
        }
        if(isp)
            num++;//n[i]的素数+1
    }
    printf("%d\n",num);
    return 0;
}

C++: #include <iostream>
#include <math>

using namespace std;

int main() {
  int lim;
  int i,n;
  int count=1;
  bool isPrime=true;
  
  scanf("%d",&n);
  
  for(lim=2;lim<=n;lim++) {
  for(i=2;i<(int)(sqrt(lim)+1.0);i++) {
    if((lim%i)==0) {
      isPrime=false;
      count++;
      break;
    }
  }
  }
  printf("%d",n-count);
  return 0;
}

C++: #include <iostream>
#include <cmath>
#include <string.h>
#include <time.h>     // for clock()
using namespace std;

/*
1???2 ?????? n ?????§?????´??°???????????????
2??????????????£???????????°????????§???????°?????????????????????? p ??¨?????????????????????????????????????????°?????? p ??¨?????????
p ??\?????? p ????????°?????¨????¶????????????????
3???2??????????????°?????????????????????p ??? ???n ????¶??????????????????????????????????????????£?????????????´???°???
*/
// ??¨????????????????????????
int SieveOfEratosthenes(int num) {
	
	if (num < 2) {
		return 0;
	}
	// num???????´???°?????°
	int count = 0;
	bool fl[1000001];
	memset(fl, true, sizeof(fl));
	for (int i = 2; i <= sqrt(num); i++) {
		if (fl[i]) {
			for (int j = 2; j*i <= num; j++) {
				fl[i*j] = false;
			}
		}
	}
	for (int i = 2; i <= num; i++) {
		if (fl[i]) {
			count++;
		}
	}
	return count;
}

int main() {

	int num = 0;
	while (cin >> num) {
		//clock_t start = clock();    // ??????????????????
		cout << SieveOfEratosthenes(num) << endl;
		//clock_t end = clock();     // ????????????
		//std::cout << "duration = " << (double)(end - start) / CLOCKS_PER_SEC << "sec.\n";
	}
	return 0;
}

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

#define MAXN 1000000

int primeN[MAXN];

bool isPrime(int num)
{
	if(num < 2) return false; 
	if(num == 2) return true;
	if(num%2 == 0) return false;
	for(int i=3; i < (int)sqrt((double)num) + 1; i++)
	{
		if(num % i == 0) return false;
	}
	return true;
}

void calcPrimeN()
{
	primeN[0] = 0;
	for(int i=1; i<MAXN; i++)
	{
		if(isPrime(i)) primeN[i] = primeN[i-1] + 1;
		else primeN[i] = primeN[i-1];
	}
}

int main()
{
	calcPrimeN();
	
	int data;
	while(cin >> data)
	{
		cout << primeN[data] << endl;
	}

	return 0;
}