Go: package main

import (
	"fmt"
	"math"
)

func main() {
	var resultArr [31]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 <math.h>
using namespace std;

int main(){
  int a;
  int i, j, k;
  int past=1, a_sqrt;
  int count;
  bool chk[1000000];

  for(i=1; i<1000000; i++) chk[i]=true;
  while(cin>>a){
    count=0;
    if(past>=a){
      for(i=2; i<=a; i++){
        if(chk[i]) count++;
      }
      printf("%d\n", count);
    }
    else {
      a_sqrt=(int)sqrt((double)a);
      for(i=2; i<=a_sqrt; i++){
        for(j=i; j<=a/i; j++){
          chk[j*i]=false;
        }
      }
      past=a;
      for(i=2; i<=a; i++){
        if(chk[i]) count++;
      }
      printf("%d\n", count);
    }
  }

  return 0;
}

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

using namespace std;
   
void primeCal()
{
    const int N = 1000000;
    
    vector<bool> prime(N, true);
    prime[0] = prime[1] = false;
    
    vector<int> count(N);
    
    int n;
    
    for (int i = 2; i*i < N; ++i) 
    {
        if (!prime[i]) 
        { 
            continue; 
        }
        
        for (int j = i*i; j < N; j += i) 
        {
            prime[j] = false;
        }
    }
    
    for (int i = 1; i < N; ++i) 
    {
        count[i] = count[i-1] + prime[i];
    }
    
    while (cin>>n)
    {
        cout << count[n] << endl;
    }
    
    return;
}
     
int main()
{     
    primeCal();
    return 0;
}

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

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

int prime[1000000];

int main()
{
  int n;
  for (int i = 0; i < 1e6; i++) prime[i] = 1;
  prime[0] = prime[1] = 0;
  for (int i = 2; i < 1000000; i++){
    if (prime[i] == 1){
      for (int j = 2 * i; j < 1000000; j += i){
        prime[j] = 0;
      }
    }
  }
  for (int i = 0; i < 1000000; i++){
    prime[i + 1] += prime[i];
  }
  while (cin >> n){
    cout << prime[n] << endl;
  }

  return 0;
}

C++: #include<iostream>
#include<cmath>
using namespace std;
bool isprime(int n) {
	if (n == 2)return true;
	if (n % 2 == 0)return false;
	for (int i = 3; i <= sqrt(n); i += 2) if (n%i == 0)return false;
	return true;
}
int main() {
	long prime[80000];
	prime[0] = 2;
	int num = 1;
	for (int i = 3; i < sqrt(1000000); i += 2) {
		if (isprime(i)) {
			prime[num] = i;
			num++;
		}
	}
	long n; while (cin >> n) {
		int j = 0;
		for (; prime[j] <= n; j++) {}
		cout << j << endl;
	}
	return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

bool table[1000000];
vector<int> primes;

int main() {
  table[2] = true;
  for (int i = 3; i <= 999999; i += 2)
    table[i] = true;
  for (int i = 3; i <= 1000; i += 2)
    if (table[i])
      for (int j = 3 * i; j <= 999999; j += 2 * i)
        table[j] = false;
  primes.push_back(2);
  for (int i = 3; i <= 999999; i += 2)
    if (table[i])
      primes.push_back(i);
  int n;
  while (scanf("%d", &n) == 1) {
    printf("%d\n", upper_bound(primes.begin(), primes.end(), n)-primes.begin());
  }
  return 0;
}

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

int main()
{
	unsigned int  n;
	cin >> n;
	unsigned  int num,count = 0;
	for(int i = 0; i < n; i++)
	{
		cin >> num;
		for(int j = 2; j <=num; j++)
		{
			if(num == j)count++;
			if((num % j) == 0)
			{
				break;
			}
		}
	}
	cout << count << endl;
	return 0;
}

C++: #include <cmath>
#include <cstdio>
#include <map>

bool fermat_test(int n) {
    if (n < 2) {
        return false;
    } else if (n == 2) {
        return true;
    } else {
        return (static_cast<int>(std::pow(2, n - 1)) % n) == 1;
    }
}

bool prime_test(int n) {
    if (n < 2) {
        return false;
    } else if (n == 2) {
        return true;
    } else {
        for (int i = 3; i < static_cast<int>(std::sqrt(n)); i += 2) {
            if ((n %= 2) == 0) {
                return false;
            }
        }
    }
    return true;
}

int main() {
    std::map<int, int> m;
    int n;
    while (~scanf("%d", &n)) {
        for (int i = 2; i <= n; ++i) {
            if (m.count(i) > 0)
                continue;

            m[i] = m[i - 1] + (fermat_test(i) && prime_test(i));
        }
        printf("%d\n", m[n]);
    }
}

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

const int MAXN = 1e6;

bool is_prime[MAXN];
int prime_count[MAXN];

void init() {
	fill(is_prime, is_prime + MAXN, true);
	int cnt = 0;
	for (int i = 2; i < MAXN; ++i) {
		if (is_prime[i]) {
			++cnt;
			for (int j = i + i; j < MAXN; j += i) {
				is_prime[j] = false;
			}
		}
		prime_count[i] = cnt;
	}
}

int main() {
	//freopen("0009.txt", "r", stdin);
	init();
	int n;
	while (cin >> n) {
		cout << prime_count[n] << "\n";
	}
}

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

int main()
{
	vector<int> prime,count;
	int j, k, x, num;
	while (cin >> num){
		prime.push_back(2);
		x = 1;
		k = 1;
		while (prime[k-1] <= num)
		{
			x += 2;
			j = 0;
			while (j < k && x % prime[j] != 0)j++;
			if (j == k)
			{
				prime.push_back(x);
				k++;
			}
		}
		count.push_back(prime.size() - 1);
		prime.clear();
	}
	for (int i = 0; i < count.size(); i++)
	{
		cout << count[i] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<math.h>
using namespace std;
int isPrime(int n) {
	if (n == 2) {
		return 1;
	}
	if ((n <2) || (n % 2 == 0)) {
		return 0;
	}
	int i = 3;
	while (i <= sqrt(n)) {
		if (n % i == 0) {
			return 0;
			
		}
		i += 2;
	}
	return 1;
}
int main()
{
	int x[30];
	int i = 0;
	while (cin >> x[i]) {
		int k = 0;
		for (int j = 0; j < x[i]; j+=2) {
			
			k += isPrime(j+1);
		}
		cout << k+1 << endl;

		i++;
	}

	return 0;
}

C++: #include <cstdio>

using namespace std;

int main() {
    long NUM = 1000000;
	long n = NUM;
	long m = 2;
	bool flag;
	long counter = 0;
	bool primeNum[NUM + 10];

	long i, len = NUM + 10;
	
	for (i = 0; i < len; i++) primeNum[i] = false;
	
    while (m <= n) {
        flag = true;
        len = m / 2;

        for(i = 2; i <= len; i++) {
            if(m % i == 0) {
                flag = false;
                
                break;
            }
        }

        if (flag) primeNum[m] = true;

        m++;
    }
    
    while (scanf("%ld", &n) != EOF) {
        counter = 0;
        
        for (i = 0; i <= n; i++) {
            if (primeNum[i]) counter++;
        }
        
        printf("%ld\n", counter);
    }

    return 0;
}

C++: #include<iostream>

using namespace std;

int main(){
  int n;
  int *prime;
  int i,j;
  while(cin >> n){
    //init
    prime = new int[n+1];
    for(i=0; i<=n; i++) prime[i] = 0;

    //素数判定
    for(i=2; i*i<=n; i++){
      if(prime[i] == 0){
	prime[i] = 1; //iは素数
	for(j=2; i*j<=n; j++){//倍数ふるい落とし
	  prime[i*j] = -1;//i*jは素数じゃない
	}
      }
    }
    for(i=i; i<=n; i++){
      if(prime[i] == 0) prime[i] = 1;
    }
    
    //output
    int ans=0;
    for(i=2; i<=n; i++){
      if(prime[i] == 1) {
	ans ++;
	//cout << "prime:" << i << endl;;
      }
    }
    cout << ans << endl;

    delete[] prime;
  }
  return 0;
}