Go: package main

import (
	"fmt"
	"math"
)

func main() {
	var primeCount, input int
	for {
		fmt.Scanf("%d", &input)
		for i := 0; i <= input; i++ {
			if IsPrime(i) {
				primeCount++
			}
		}
		fmt.Printf("%d\n", primeCount)
		primeCount = 0
	}
}

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>

using namespace std;
const int max_d = 30;

int sieve(int n) {
    int res = 0;
    bool prime[n + 1];
    for(int i = 0; i <= n; i++) {
        prime[i] = true;
    }
    for(int i = 2; i <= n ; i++) {
        if(prime[i]) {
            res++;
            for(int j = 2 * i; j <= n; j += i) {
                prime[j] = false;
            }
        }
    }
    return res;
}

int main() {
    int n;
    int dc = 0;
    while(cin >> n) {
        dc++;
        if(dc > max_d) break;
        cout << sieve(n) << endl;
    }
    return 0;
}

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

using namespace std;

#define MAX_N 10000000

int sosuu[MAX_N+1];

int main() {
	for (int i = 0; i < MAX_N; ++i)
		sosuu[i] = 1;

	sosuu[0] = sosuu[1] = 0;

	for (int i = 2; i < MAX_N; ++i)
		if (sosuu[i])
			for (int j = 2*i; j < MAX_N; j += i)
				sosuu[j] = 0;

	for (int i = 0; i < MAX_N; ++i)
		sosuu[i+1] += sosuu[i];

	int n;
	while ( scanf("%d", &n) != EOF )
		cout << sosuu[n] << endl;
}

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

int main(){
	int i=0,k=2,source[30];
	while(cin >> source[i]) i++;
	int m = max(source[0],source[i]);
	bool hurui[1000000];
	memset(hurui+2,1,sizeof(hurui));
	for (int j=2;j<=m;j++,k=2)
	{
		if (!hurui[j]) continue;
		while(j*k < m)hurui[j*k]=false,k++;
	}
	for (int j=0;j<i;j++){
		int count=0;
		for (k=2;k<=source[j];k++) count += !!hurui[k];
		cout << count << endl;
	}
}

C++: #include <iostream>
#include <math.h>
#define MAX 1000
using namespace std;

int main(){
	int n;
	int prime[MAX],i,j;
	int count;
	bool flag;

	while((cin >> n)!= NULL){
		count = 0;
		for(i = 3; i <= n; i+=2){
			flag = true;
			for(j = 0; j < count; j++){
				if(i % prime[j] == 0){
					flag = false;
					break;
				}
			}
			if(flag == true){
				prime[count++] = i;
			}
		}
		if(n > 1)	count++; //2
		cout << count << endl;
		//delete[] prime;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <limits>
#include <map>
#include <string>
using namespace std;

bool is_prime(int n){
for(int i=2;i*i<=n;i++){
if(n % i == 0) return false;
}
return n!=1;
}

int main(){
int n;


while(~scanf("%d",&n)){
int* table =  new int[n+1];
fill((int*)table,(int*)table+sizeof(table),1);
table[0]=0;
table[1]=0;

for(int i=2;i*i<=n;i++){
if(!is_prime(i)) {
for(int j=i;j<=n;j*=i){
table[j] = 0;
}
}
}
printf("%d\n",count(table,table+1000000,1));
}
}

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

using namespace std;

int N,ans=1;

int IsPrime(int p) {
	double n = sqrt(p);
	for (int i = 3; i <= n; i++) {
		if (p%i == 0)return 0;
	}
	return 1;
}

int main() {
	while (cin >> N) {
		//double n = sqrt(N);
		for (int i = 3; i <= N; i = i + 2) {
			ans += IsPrime(i);
		}
		cout << ans << endl;
		ans = 1;
	}
	return 0;


}

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

map<int, bool> memo_of_prime;

bool check_prime_number (int n) {
    if ( n <= 1 ) return false;
    if (memo_of_prime.find(n) != memo_of_prime.end()) {
        return memo_of_prime[n];
    }

    for (int i = 2; i <= sqrt(n); ++i) {
        if (n % i == 0) return memo_of_prime[n] = false;
    }

    return memo_of_prime[n] = true;
}

int count_prime_number (int n) {

    int count = 0;
    if (n == 1) return 0;
    for (int i = 2; i <= n; ++i) {
        if (check_prime_number(i)) {
            count++;
        }
    }

    return count;
}

int main() {
    int n;
    while (cin >> n) {
        cout << count_prime_number(n);
    }
    return 0;
}

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

int main(){
	int x=100000;
	bool isPrime[x];
	isPrime[0]=isPrime[1]=false;
	for(int i=2; i<x; i++)
		isPrime[i]=true;
	
	for(int i=2; i*i<x; i++){
		if(isPrime[i]){
			for(int j=i*i; j<x;j+=i)
				isPrime[j]=false;
		}
	}

	int nPrime[x];
	int cnt=0;

	for(int i=0; i<x; i++){
		if(isPrime[i])
			cnt++;

		nPrime[i]=cnt;
	}

	while(true){
		int n;
		cin >> n;

		if(cin.eof())
			break;

		cout << nPrime[n] << endl;
	}
	
	return 0;
}

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

using namespace std;

bool isprime(int n)
{
	for (int i = 2; i <= sqrt(n); i++)
	{
		if (n % i == 0) return false;
	}

	return true;
}

int main()
{
	int N[31];
	int C[31]; memset(N, 0, sizeof(N));

	for (int i = 0; i < 3; i++)
	{
		for (int j = 2; j <= N[i]; j++)
		{
			if (isprime(j) == true)
			{
				C[i] += 1;
			}
		}
	}

	for (int i = 0; i < 31; i++)
	{
		cout << C[i] << endl;
	}

	return 0;
}

C++: #include <stdio.h>

int main(){
	int sosuu[100000] = { 2, 3, 5 }, i, x, y = 3,n=0,z;
	
	while (scanf("%d", &n)!=EOF){
		z = 0; 
		for (i = 3; i < n + 1; i += 2){
			for (x = 0; x < y; x++){
				if (i % sosuu[x] == 0)break;
				if (x + 1 == y){ sosuu[y] = i, y++;}
			}
		}
		for (i = 0; i < y; i++){ if (sosuu[i] > n)break; z++; }
		printf("%d\n", z);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int main(){
    vector<int> primes;
    primes.PB(2);
    FOR(i,3,10000){
	bool flg = true;
	REP(j,primes.size()) if(i % primes[j] == 0 ) flg = false;
	if(flg) primes.PB(i);
    }

    int n;
    while(true){
	cin >> n;
	int res = upper_bound(ALL(primes),n) - primes.begin();
	cout << res << endl;
    }

    return 0;
}

C++: #include <stdio.h>

long check_prime(int a)
{
   long c;

   for ( c = 2 ; c <= a-1; c++ )
   {
      if ( a%c == 0 )
      {
	    return 0;
      }

   }
   if ( c == a )
      return 1;
}

int main ()
{
    long  n=0,i=0,prime=0,result=0;

    scanf("%ld",&n);

 for(i=2;i<=n;i++)
 {

   result = check_prime(i);

   if ( result == 1 )
     prime = prime +1;
 }

 printf("%ld",prime);
 printf("\n");
 return 0;
}

C++: #include <iostream>
#include <map>
using namespace std;
#define MAX_N 999999
int main()
{
    int n;
    bool notPrime[MAX_N+2];
    int count[MAX_N+2];
    for (int i = 2 ; i <= MAX_N ; i++)
    {
        if (!notPrime[i])
        {
            for (int g = i+i ; g <= MAX_N ; g += i)
            {
                notPrime[g] = true;
            }
        }
    }
    int cnt = 0;
    count[1] = 0;
    for (int g = 2 ; g <= MAX_N ; g++)
    {
        if (!notPrime[g])
            cnt ++;
        count[g] = cnt;
    }
    while (cin >> n)
    {
        cout << count[n] << endl;
    }
    
}

C++: def sieve_prime(n): # return prime numbers within [1, n]
  primes = []
  is_prime = [True] * (n+1) # [0, n]
  for i in range(2, n+1):
    if not is_prime[i]:
      continue # need only primes

    primes.append(i)
    for j in range(i*2, n+1, i): # range(i*2, n+1, i) = {i*2, i*3, ...}
      is_prime[j] = False

    """
    Compare the above code with the below one in the view of time complexity
    for j in range(i*2, n+1):
      if j % i == 0:
        is_prime[j] = False
    """
        
  return primes
      
while True:
  try:
    n = int(input())
    print(len(sieve_prime(n))) # because T <= 30, you can simply use sieve_prime every time
  except EOFError:
    break