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>
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;
int res=0;
scanf("%d",&n);
for(int i=2;i<=n;i++){
if(is_prime(n)) res++;
}
printf("%d\n",res);
}

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

int main(){
	int i,j,n;
	bool prime[1000001];
	for (i = 3; i <= 1000000; i += 2){
		prime[i] = true;
	}
	prime[2] = true;

	for (i = 3; i <= 1000; i += 2){
		if (prime[i] == 1){
			for (j = 3; j <= 1000000 / i; j += 2){
				prime[i * j] = false;
			}
		}
	}
	while(cin >> n && n){
		int count = 0;
		for(i=2;i<=n;i++){
			if(prime[i] == true) count++;
		}
		cout << count << endl;
	}

	return 0;
}

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

C++: #include<iostream>
#include<cstdio>
using namespace std;
int main(){
    int a,b,c,i,j;
    while(cin>>a){
        b=0;
        for(i=2;i<=a;i++){
            c=0;
            for(j=2;j*j<=i;j++){
                if(i%j==0){c++;break;}
            }
            if(i==2||i==3)c++;
            if(c==0)b++;
            if(i==2||i==3)b++;
        }
        cout<<b<<endl;
    }
    return 0;
}

C++: #include <stdio.h>
#include <iostream>

using namespace std;

#define MAX 10000000
#define LMT 3163
int sieve();
int calculation();

unsigned flag[MAX>>6];
int primes[5761455];

#define ifc(n) (flag[n>>6]&(1<<((n>>1)&31)))
#define isc(n) (flag[n>>6]|=(1<<((n>>1)&31)))

int main()
{
    sieve();
    return 0;
}


int sieve()
{
    unsigned i, j, k;
    for(i=3; i<LMT; i+=2)
        if(!ifc(i))
            for(j=i*i, k=i<<1; j<MAX; j+=k)
                isc(j);
    calculation();
    return 0;
}

int calculation()
{
    int Rang, count;
    unsigned int i;
    int k = 1;
    primes[0] = 2;
    for( i=3; i<MAX; i+=2)
    {
        if(ifc(i)==0)
            primes[k++] = i;
    }
    while(scanf("%d",&Rang)==1)
    {
        count = 0;
        for(i=0; i<Rang; i++)
        {
            if(primes[i]<=Rang)
            {
                count++;
            }
            else if(primes[i]>Rang)
                break;
        }
        cout << count << "\n";
    }
    return 0;
}

C++: #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#pragma warning(disable:4996)

int main()
{
	
	int num, cnt;
	unsigned int i, j, k, data_num;
	int data[30];
	unsigned int sq_num;
	unsigned int *prime;

	for (data_num = 0; data_num <30; data_num++)
	{
		data[data_num] = -1;
		scanf("%d", &data[data_num]);
		if (data[data_num] < 0)
			break;
	}

	for (k = 0; k < data_num; k++)
	{
		sq_num = (int)sqrt((double)data[k]);
		prime = (unsigned int*)malloc(sizeof(unsigned int) * data[k]);

		prime[0] = 0;
		for (i = 1; i < data[k]; i++)
		{
			prime[i] = 1;
		}

		for (i = 1; i < sq_num; i++)
		{

			if (prime[i] == 1)
			{
				for (j = (i + 1); (i + 1)* j <= data[k]; j++)
				{
					prime[(i + 1) * j - 1] = 0;
				}
			}
		}

		cnt = 0; 
		for (i = 0; i < data[k]; i++)
		{
			if (prime[i] == 1)
			{
				cnt++;
			}
		}
		printf("%d\n", cnt);
		free(prime);
	}
	return 0;
}

C++: #include <stdio.h>

int main() {
    int i, k, v,scan,an;
    int MAX_V = 100000;
    int count[MAX_V];
    int sum[MAX_V];
    int prime[MAX_V];
    for(i = 2; i <= MAX_V; i++) {
        prime[i] = 1;
    }
    for(i = 2; i*i <= MAX_V; i++) {
        if(prime[i]) {
            for(k = 2 * i; k <= MAX_V; k += i) {
                prime[k] = 0;   
            } 
        }
    }
    count[0]=0;
    for(i=1;i<=MAX_V;i++){
        count[i]= count[i-1]+prime[i];
    }
    while(scanf("%d",&scan)!= EOF){
        an=count[scan]-count[0];
        printf("%d\n",an);
    }
    return 0; 
}

C++: #include<iostream>
using namespace std;
int main()
{
long n,i,j,*nn;
for(;;){
cin>>n;
if(cin.eof())break;
nn=new long [n+1];
nn[1]=0;nn[2]=1;
for(i=3;i<=n;i+=2)nn[i]=1;
for(i=4;i<=n;i+=2)nn[i]=0;
for(i=3;i<=n;i+=2){
for(j=i;j*j<=n;j+=2){
if(i%j==0){nn[i]=0;break;}
}
}
for(i=2,j=0;i<=n;i++)if(nn[i]==1)j++;
cout<<j<<endl;
delete [] nn;
}
return 0;
}

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

int main();
int count_prime_number(int n);

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;
}

C++: #include <stdio.h>
const int MAX_V = 1000000;
int prime[MAX_V+1],sum[MAX_V+2]={0};
int main(){
  int n,i = 30;
  for(int i = 2;i <= MAX_V;i++){
    prime[i] = 1;
  }
  for(int i = 2;i * i <= MAX_V;i++){
    if(prime[i]){
      for(int k = 2* i;k <= MAX_V;k += i)
      prime[k]=0;
    }
  }
  for(int i = 2;i < MAX_V;i++){
    sum[i] +=sum[i-1]+prime[i];
  }
  while(i < 30){
    scanf("%d",&n);
    printf("%d\n",sum[n]);
    i++;
  }
  return 0;
}

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

int main(void)
{
	int n;

	while (std::cin >> n) {
		std::vector<int> p(n + 1, 0);
		p[0] = p[1] = 1;
		int m = 0;

		for (int i = 1; i <= std::sqrt(n); i++) {
			if (p[i]) continue;
			m++;
			for (int j = i; j <= n; j+=i)
				p[j] = 1;
		}
		std::cout << m + std::count(p.begin(), p.end(), 0) << std::endl;
	}
	return 0;
}