Go: package main

import (
	"fmt"
	"math"
)

func main() {
	var resultArr [100]int
	var inputCount, primeCount, input int
	for {
		fmt.Scanf("%d", &input)
		for i := 0; i <= input; i++ {
			if IsPrime(i) {
				primeCount++
			}
		}
		resultArr[inputCount] = primeCount
		if inputCount == 30 {
			break
		}
		primeCount = 0
		inputCount++
	}
	for _, v := range resultArr {
		fmt.Printf("%d\n", v)
	}
}

func IsPrime(num int) bool {
	switch {
	case num == 2:
		return true
	case num < 2 || num%2 == 0:
		return false
	}
	sqrt := math.Sqrt(float64(num))
	for i := 3; i <= int(sqrt); i += 2 {
		if num%i == 0 {
			return false
		}
	}
	return true
}

C++: #include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
using namespace std;


int main(){
  int prime[100000];
  fill(prime,prime+100000,1) ;
  prime[0] = prime[1] = 0;
  for(int i = 2 ; i*i <= 100000 ; i++ ){
    if(prime[i])for(int j = i*2; j <= 100000; j+=i)prime[j] = 0;
  }

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

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

int table[1000000];

using namespace std;

void init() {
	for (int i = 0; i < 1000000; i++)
		table[i] = 0;
	for (int i = 2; i < 1000000; i++) {
		if (table[i] != -1) {
			table[i] = table[i-1]+1;
			for (int j = i+i; j < 1000000; j+=i)
				table[j] = -1;
		} else {
			table[i] = table[i-1];
		}
	}
}

int main(void) {
	int n;
	init();
	while (cin >> n) {
		cout << table[n] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <math.h>
#include <string>
using namespace std;

int main(){
  int n;
  int i,j,k;
  int era;

  while(cin >> n){
    int count = 0;
    int S[n+1];
    for(i=2 ; i<n+1 ; i++){
      S[i] = i;
    }

    for(i=2 ; i<(
                 (int)sqrt(n)+1) ; i++){
      if(S[i] != 0){
        era = S[i];
        for(j=2 ; (j*era)<(n+1) ; j++){
          S[j*era] = 0;
        }
      }
    }

    for(i=2 ; i<n+1 ; i++){
      if(S[i] != 0){
        count++;
      }
    }
      
    cout <<  count << endl;
  }

    
  return 0;
}

C++: #include <iostream>
#include <vector>
//#include <math.h>
//#include <string>
#include <cmath>
#include <iomanip>

int main();
int count_prime_number(int n);
std::vector<int> return_prime_number(int n);

//最初に素数リストを作成し、入力された値に対してリストを参照する。
int main(){
	std::vector<int> prime_numbers;		//素数リスト
	int input;
	std::vector<int> answer;

	prime_numbers = return_prime_number(999999);
	//for (int i = 0; i < 3; i++){
	//std::cin >> input;
	while (std::cin >> input){
		//素数の探索
		for (int i = 0; i < input; i++){
			if (input == prime_numbers[i]){
				answer.push_back(i + 1);
				break;
			}
			else if (input < prime_numbers[i]){
				answer.push_back(i);
				break;
			}
			else{
				//何もしない
			}
		}
	}

	for (int i = 0; i < answer.size(); i++){
		std::cout << answer[i] << std::endl;
	}

	//while (1);
	return 0;
}


//入力のたびに素数のチェックをする
/*
int main(){
	int input;
	std::vector<int> answer;
	
	//for (int i = 0; i < 3; i++){
		//std::cin >> input;
	while (std::cin >> input){
		answer.push_back(count_prime_number(input));
	}

	for (int i = 0; i < answer.size(); i++){
		std::cout << answer[i] << std::endl;
	}

	//while (1);
	return 0;
}
*/

/**
* nまでの素数の個数を出力する
* args:数値n
* return:素数の個数
* 参考:http://tsg.ne.jp/TT/tsg/c/mansaku/P01.html
*/
int count_prime_number(int n){
	int rtn = 0;

	//2だけ個別チェック
	if (n >= 2){
		rtn++;
	}

	for (int i = 3; i <= n; i += 2)
	{
		bool is_prime_num = true;
		for (int j = 3; j <= sqrt(i); j += 2)		//ルートi以上のiの約数はルートi以下のiの約数と対になっているので範囲はルートiまででいい
		{
			if (i % j == 0)
			{
				is_prime_num = false;
				break;
			}
		}

		if (is_prime_num){
			rtn++;

			//デバッグ
			//std::cout << i << std::endl;
		}
	}

	return rtn;
}

/**
* nまでの素数を返す
* args:任意の数値n
* return:素数を格納したvector
*/
std::vector<int> return_prime_number(int n){
	std::vector<int> rtn;

	//2だけ個別チェック
	if (n >= 2){
		rtn.push_back(2);
	}

	for (int i = 3; i <= n; i += 2)
	{
		bool is_prime_num = true;
		for (int j = 3; j <= sqrt(i); j += 2)		//ルートi以上のiの約数はルートi以下のiの約数と対になっているので範囲はルートiまででいい
		{
			if (i % j == 0)
			{
				is_prime_num = false;
				break;
			}
		}

		if (is_prime_num){
			rtn.push_back(i);

			//デバッグ
			//std::cout << i << std::endl;
		}
	}

	return rtn;
}

C++: #include<stdio.h>
#include<math.h>
#define DB(x) (x)
int prime[80000];
int GetPrime(int num);

int main(){
	int count = GetPrime(1000000);
	DB(printf("%d\n",count));
	int n,ans[100],cor = 0;
	int i;
	while(scanf("%d",&n) != EOF){
		for(i = 0;n >= prime[i] && i < count;i++);
		ans[cor] = i;
		cor++;
	}
	for(i = 0;i < cor;i++)
		printf("%d\n",ans[i]);
	return 0;
}

int GetPrime(int num){
	int i,j,count = 0;
	int sqnum = sqrt((double)num);
	for(i = 2;i <= num;i++){
		for(j = 0;j < count;j++){
			if(prime[j] > sqnum)
				goto COUNT;
			if(i%prime[j] == 0)
				break;
		}
		if(j < count)
			continue;
		COUNT:
		prime[count] = i;
		count++;
	}
	return count;
}

C++: #include<iostream>

using namespace std;

int n;
int prime[1000000];
int checked[1000000];
int primesum[1000000];
int checknum[30];
int datasets;

int main()
{
	prime[0] = 0;
	prime[1] = 0;
	prime[2] = 1;
	primesum[0] = 0;
	primesum[1] = 0;
	primesum[2] = 1;

	for(int a = 3;a<1000000;a++)
	{
		prime[a] = 1;
	}
	
	for(int a = 3;a<1000;a++)
	{
		if(checked[a] == 0)
		{
			checked[a] = 1;
			for(int b = 2;b<a;b++)
			{
				if(a%b==0)
				{
					prime[a] = 0;
					break;
				}
			}
			if(prime[a] == 1)
			{
				for(int c = 2*a ;c<1000000;c+=a)
				{
					checked[c] = 1;
					prime[c] = 0;
				}
			}
		}
	}

	for(int a = 3;a<1000000;a++)
	{
		primesum[a] = primesum[a-1]+prime[a];
	}
	
	while(cin >> n)
	{
		checknum[datasets] = n;
datasets++;
	}

for(int a = 0;a<datasets;a++)
{
cout << primesum[checknum[a]] << endl;
}

	return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
	int N,count=0;
	while(cin >> N){
		count = 0;
	for(int x=2;x<=N;x++){
		int flag = 0;
		for(int y=2;y<x;y++){
			if((x%y)==0)	flag = 1;
		}
		if(flag == 0)	count++;
	}
	cout << count << endl;
	}
	return 0;
}

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

bool isPrime(int x){
	if(x == 0 || x == 1) return false;
	for(int i=2;i<x;i++){
		if(x % i == 0) return false;
	}
	return true;
}

int main(void){
	int n;
	int ans = 0;
	
	while(cin>>n){
		for(int i=2;i<=n;i++){
			if(isPrime(i)) ans++;
		}
		printf("%d\n",ans);
	}

	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;
    int Counter;
sieve(1000000); // can go up to 10^7 (need few seconds)

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