prithivMLmods/Coder-Stat huggingface dataset:
Problem p00008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>

using namespace std;

int main(void) {
  int n = 0;

  while (scanf("%d", &n) && n != EOF) {
    int ans = 0;

    for (int i = 0; i <= 9; i++) {
      for (int j = 0; j <= 9; j++) {
        for (int k = 0; k <= 9; k++) {
          for (int l = 0; l <= 9; l++) {
            if (n == i + j + k + l) {
              ans++;
            }
          }
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>

using namespace std;

int main(void) {
  int n = 0;

  while (scanf("%d", &n) != EOF) {
    int ans = 0;

    for (int i = 0; i <= 9; i++) {
      for (int j = 0; j <= 9; j++) {
        for (int k = 0; k <= 9; k++) {
          for (int l = 0; l <= 9; l++) {
            if (n == i + j + k + l) {
              ans++;
            }
          }
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}
Problem p00008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <stdio.h>

int main() {
  int n, a, b, c, d, count;
  while (scanf("%d", &n)) {
    count = 0;
    for (a = 0; a < 10; a++) {
      for (b = 0; b < 10; b++) {
        for (c = 0; c < 10; c++) {
          for (d = 0; d < 10; d++) {
            if (a + b + c + d == n)
              count = count + 1;
          }
        }
      }
    }
    printf("%d\n", count);
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <stdio.h>

int main() {
  int n, a, b, c, d, count;
  while (scanf("%d", &n) == 1) {
    count = 0;
    for (a = 0; a < 10; a++) {
      for (b = 0; b < 10; b++) {
        for (c = 0; c < 10; c++) {
          for (d = 0; d < 10; d++) {
            if (a + b + c + d == n)
              count = count + 1;
          }
        }
      }
    }
    printf("%d\n", count);
  }
  return 0;
}
Problem p00008 (C++)

Buggy submission (status: Time Limit Exceeded):
// ↓template↓

#include "bits/stdc++.h"
using namespace std;
#define Would
#define you
#define all(n) n.begin(), n.end()
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
const double pi = acos(-1);
const int SIZE = 1 << 17;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}, alp[30];
long long fac[200005], finv[200005], inv[200005];
vector<long long> dij;
struct edge {
  long long to, cost;
};
vector<vector<edge>> G;

long long mod_pow(long long a, long long b) {
  long long res = 1, mul = a;
  for (int i = 0; i < 31; ++i) {
    if (b >> i & 1) {
      res *= mul;
      res %= MOD;
    }
    mul = (mul * mul) % MOD;
  }

  return res;
}

void addedge(int from, int to, int cost) {
  G[from].push_back({to, cost});
  G[to].push_back({from, cost});
}

// ↑template↑

int main() {
  int n;
  while (cin >> n) {
    int ans = 0;
    for (int i = 0; i <= 9; ++i) {
      for (int j = 0; j = 9; ++j) {
        for (int k = 0; k <= 9; ++k) {
          int mon = n - i - j - k;
          if (0 <= mon && mon <= 9) {
            ++ans;
          }
        }
      }
    }
    cout << ans << endl;
  }
}


Fixed submission:
// ↓template↓

#include "bits/stdc++.h"
using namespace std;
#define Would
#define you
#define all(n) n.begin(), n.end()
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
const double pi = acos(-1);
const int SIZE = 1 << 17;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}, alp[30];
long long fac[200005], finv[200005], inv[200005];
vector<long long> dij;
struct edge {
  long long to, cost;
};
vector<vector<edge>> G;

long long mod_pow(long long a, long long b) {
  long long res = 1, mul = a;
  for (int i = 0; i < 31; ++i) {
    if (b >> i & 1) {
      res *= mul;
      res %= MOD;
    }
    mul = (mul * mul) % MOD;
  }

  return res;
}

void addedge(int from, int to, int cost) {
  G[from].push_back({to, cost});
  G[to].push_back({from, cost});
}

// ↑template↑

int main() {
  int n;
  while (cin >> n) {
    int ans = 0;
    for (int i = 0; i <= 9; ++i) {
      for (int j = 0; j <= 9; ++j) {
        for (int k = 0; k <= 9; ++k) {
          int mon = n - i - j - k;
          if (0 <= mon && mon <= 9) {
            ++ans;
          }
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int dp[40];
int main() {
  for (int i = 0; i <= 9; i++) {
    for (int j = 0; j <= 9; j++) {
      for (int k = 0; k <= 9; k++) {
        for (int l = 0; l <= 9; l++) {
          dp[i + j + k + l]++;
        }
      }
    }
  }
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    cout << dp[n] << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int dp[40];
int main() {
  for (int i = 0; i <= 9; i++) {
    for (int j = 0; j <= 9; j++) {
      for (int k = 0; k <= 9; k++) {
        for (int l = 0; l <= 9; l++) {
          dp[i + j + k + l]++;
        }
      }
    }
  }
  int n;
  while (cin >> n) {
    if (n == 0) {
      break;
    }
    cout << dp[n] << endl;
  }
  return 0;
}
Problem p00008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  int a, b, c, d, n, e = 0;
  for (;;) {
    cin >> n;
    for (a = 0; a < 10; a++) {
      for (b = 0; b < 10; b++) {
        for (c = 0; c < 10; c++) {
          for (d = 0; d < 10; d++) {
            if (a + b + c + d == n)
              e++;
          }
        }
      }
    }
    cout << e << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int a, b, c, d, n, e;
  while (cin >> n) {
    e = 0;
    for (a = 0; a < 10; a++) {
      for (b = 0; b < 10; b++) {
        for (c = 0; c < 10; c++) {
          for (d = 0; d < 10; d++) {
            if (a + b + c + d == n)
              e++;
          }
        }
      }
    }
    cout << e << endl;
  }
  return 0;
}
Problem p00008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int n;

int d[4][61];

int main(void) {

  for (int i = 0; i <= 9; i++)
    d[0][i]++;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j <= 9; j++) {
      for (int k = 0; k <= 9 * (i + 2); k++) {
        d[i + 1][k + j] += d[i][k];
      }
    }
  }

  while (cin >> n, n) {

    cout << d[3][n] << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int n;

int d[4][61];

int main(void) {

  for (int i = 0; i <= 9; i++)
    d[0][i]++;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j <= 9; j++) {
      for (int k = 0; k <= 9 * (i + 2); k++) {
        d[i + 1][k + j] += d[i][k];
      }
    }
  }

  while (cin >> n, !cin.eof()) {

    cout << d[3][n] << endl;
  }

  return 0;
}
Problem p00008 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#define M_PI 3.14159265358979
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n, counter = 0;
  while (scanf("%d", n) == -1) {
    for (int a = 0; a < 10; a++) {
      for (int b = 0; b < 10; b++) {
        for (int c = 0; c < 10; c++) {
          for (int d = 0; d < 10; d++) {
            if (a + b + c + d == n)
              counter++;
          }
        }
      }
    }
    cout << counter << endl;
  }
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#define M_PI 3.14159265358979
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n, counter = 0;
  while (scanf("%d", &n) != -1) {
    counter = 0;
    for (int a = 0; a < 10; a++) {
      for (int b = 0; b < 10; b++) {
        for (int c = 0; c < 10; c++) {
          for (int d = 0; d < 10; d++) {
            if (a + b + c + d == n)
              counter++;
          }
        }
      }
    }
    cout << counter << endl;
  }
  return 0;
}
Problem p00008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  int n, g, ans = 0;
  while (1) {
    cin >> n;
    for (int a = 0; a <= 9; a++) {
      for (int b = 0; b <= 9; b++) {
        for (int c = 0; c <= 9; c++) {
          for (int d = 0; d <= 9; d++) {
            g = a + b + c + d;
            if (g == n)
              ans++;
          }
        }
      }
    }
    cout << ans << endl;
    ans = 0;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n, g, ans = 0;
  while (cin >> n) {
    for (int a = 0; a <= 9; a++) {
      for (int b = 0; b <= 9; b++) {
        for (int c = 0; c <= 9; c++) {
          for (int d = 0; d <= 9; d++) {
            g = a + b + c + d;
            if (g == n)
              ans++;
          }
        }
      }
    }
    cout << ans << endl;
    ans = 0;
  }
  return 0;
}
Problem p00008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

int func(int n) {
  int count = 0;

  for (int a = 0; a <= 9; a++) {
    for (int b = 0; b <= 9; b++) {
      for (int c = 0; c <= 9; c++) {
        for (int d = 0; d <= 9; d++) {
          if (a + b + c + d == n) {
            count++;
          }
        }
      }
    }
  }

  return count;
}

int main() {
  int input;

  while (true) {
    if (std::cin >> input) {
      std::cout << func(input) << "\n";
    }
  }

  return 0;
}

Fixed submission:
#include <iostream>

int func(int n) {
  int count = 0;

  for (int a = 0; a <= 9; a++) {
    for (int b = 0; b <= 9; b++) {
      for (int c = 0; c <= 9; c++) {
        for (int d = 0; d <= 9; d++) {
          if (a + b + c + d == n) {
            count++;
          }
        }
      }
    }
  }

  return count;
}

int main() {
  int input;

  while (std::cin >> input) {
    std::cout << func(input) << "\n";
  }

  return 0;
}
Problem p00008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
#include <string.h>

int dp[2][51];

int main() {
  while (1) {
    int n;
    if (0 == scanf("%d", &n)) {
      break;
    }
    int now = 0;
    for (int i = 0; i <= 50; i++) {
      dp[now][i] = 0;
    }
    dp[now][0] = 1;
    for (int k = 0; k < 4; k++) {
      int nex = (now + 1) % 2;
      for (int i = 0; i <= 50; i++) {
        dp[nex][i] = 0;
      }
      for (int j = 0; j <= 50; j++) {
        for (int i = 0; i <= 9; i++) {
          if (j + i <= 50) {
            dp[nex][j + i] += dp[now][j];
          }
        }
      }
      now = nex;
    }
    printf("%d\n", dp[now][n]);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
#include <string.h>

int dp[2][51];

int main() {
  while (1) {
    int n;
    if (EOF == scanf("%d", &n)) {
      break;
    }
    int now = 0;
    for (int i = 0; i <= 50; i++) {
      dp[now][i] = 0;
    }
    dp[now][0] = 1;
    for (int k = 0; k < 4; k++) {
      int nex = (now + 1) % 2;
      for (int i = 0; i <= 50; i++) {
        dp[nex][i] = 0;
      }
      for (int j = 0; j <= 50; j++) {
        for (int i = 0; i <= 9; i++) {
          if (j + i <= 50) {
            dp[nex][j + i] += dp[now][j];
          }
        }
      }
      now = nex;
    }
    printf("%d\n", dp[now][n]);
  }
  return 0;
}
Problem p00008 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

template <class T> int streamvar(std::istream &in, T &var) {
  if (!in) {
    cerr << "invalid istream" << endl;
    return 1;
  }
  in >> var;
  if (in.fail()) {
    cerr << "no var" << endl;
    return 1;
  }
  return 0;
}
template <class T> int streamvec(std::istream &in, std::vector<T> &v) {
  v.clear();
  string s;

  if (!in) {
    cerr << "invalid istream" << endl;
    return 1;
  }
  getline(in, s);
  if (in.fail()) {
    cerr << "no line" << endl;
    return 1;
  }
  istringstream is(s);
  while (1) {
    T tmp;
    is >> tmp;
    if (is.fail())
      break;
    v.push_back(tmp);
  }
  return 0;
}

int main(void) {
  int n;
  while (!streamvar(cin, n)) {
    int counter = 0;
    for (int a = 0; a < 10; a++)
      for (int b = 0; b < 10; b++)
        for (int c = 0; c < 10; c++)
          for (int d = 0; d < 10; d++)
            if (a + b + c + d == n)
              counter++;
    cout << counter << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

template <class T> int streamvar(std::istream &in, T &var) {
  if (!in) {
    cerr << "invalid istream" << endl;
    return 1;
  }
  in >> var;
  if (in.fail()) {
    // cerr << "no var" << endl;
    return 1;
  }
  return 0;
}
template <class T> int streamvec(std::istream &in, std::vector<T> &v) {
  v.clear();
  string s;

  if (!in) {
    cerr << "invalid istream" << endl;
    return 1;
  }
  getline(in, s);
  if (in.fail()) {
    cerr << "no line" << endl;
    return 1;
  }
  istringstream is(s);
  while (1) {
    T tmp;
    is >> tmp;
    if (is.fail())
      break;
    v.push_back(tmp);
  }
  return 0;
}

int main(void) {
  int n;
  while (!streamvar(cin, n)) {
    int counter = 0;
    for (int a = 0; a < 10; a++)
      for (int b = 0; b < 10; b++)
        for (int c = 0; c < 10; c++)
          for (int d = 0; d < 10; d++)
            if (a + b + c + d == n)
              counter++;
    cout << counter << endl;
  }
  return 0;
}
Problem p00008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int cnt;
  while (1) {
    int num;
    cin >> num;
    cnt = 0;
    for (int a = 0; a < 10; a++) {
      for (int b = 0; b < 10; b++) {
        for (int c = 0; c < 10; c++) {
          for (int d = 0; d < 10; d++) {
            if (a + b + c + d == num) {
              cnt++;
            }
          }
        }
      }
    }
    printf("%d\n", cnt);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int cnt;
  int num;
  while (cin >> num) {
    cnt = 0;
    for (int a = 0; a < 10; a++) {
      for (int b = 0; b < 10; b++) {
        for (int c = 0; c < 10; c++) {
          for (int d = 0; d < 10; d++) {
            if (a + b + c + d == num) {
              cnt++;
            }
          }
        }
      }
    }
    printf("%d\n", cnt);
  }
  return 0;
}
Problem p00008 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <iostream>

using namespace std;

int main() {
  int n;
  while (cin >> n) {
    int cnt = 0;
    for (int i = 0; i <= 9; ++i)
      for (int j = 0; i <= 9; ++j)
        for (int k = 0; k <= 9; ++k)
          for (int l = 0; l <= 9; ++l)
            if (i + j + k + l == n)
              ++cnt;
    cout << cnt << endl;
  }
  return 0;
}

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;
  while (cin >> n) {
    int cnt = 0;
    for (int i = 0; i <= 9; ++i)
      for (int j = 0; j <= 9; ++j)
        for (int k = 0; k <= 9; ++k)
          for (int l = 0; l <= 9; ++l)
            if (i + j + k + l == n)
              ++cnt;
    cout << cnt << endl;
  }
  return 0;
}
Problem p00008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int a, b, c, d, n, x, sum = 0;
  for (;;) {
    cin >> x;
    for (a = 0; a <= 9; a++) {
      for (b = 0; b <= 9; b++) {
        for (c = 0; c <= 9; c++) {
          for (d = 0; d <= 9; d++) {
            n = a + b + c + d;
            if (x == n) {
              sum++;
            }
          }
        }
      }
    }
    cout << sum << endl;
    sum = 0;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int a, b, c, d, n, x, sum = 0, i;
  for (i = 0; i < 50; i++) {
    cin >> x;
    for (a = 0; a <= 9; a++) {
      for (b = 0; b <= 9; b++) {
        for (c = 0; c <= 9; c++) {
          for (d = 0; d <= 9; d++) {
            n = a + b + c + d;
            if (x == n) {
              sum++;
            }
          }
        }
      }
    }
    cout << sum << endl;
    sum = 0;
  }
  return 0;
}
Problem p00008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main(void) {
  int dp[50][4];
  for (int i = 0; i < 50; i++) {
    for (int j = 0; i < 4; j++) {
      if (i < 10 && j == 0)
        dp[i][j] = 1;
      else
        dp[i][j] = 0;
    }
  }

  for (int i = 1; i < 4; i++) {
    for (int j = 0; j < 10; j++) {
      for (int k = 0; k < 37; k++) {
        if (k - j >= 0) {
          dp[k][i] += dp[k - j][i - 1];
        }
      }
    }
  }

  int n;
  while (cin >> n) {
    cout << dp[n][3] << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main(void) {
  int c;
  while (cin >> c) {
    int cnt = 0;
    for (int i = 0; i < 10; i++)
      for (int j = 0; j < 10; j++)
        for (int k = 0; k < 10; k++)
          for (int l = 0; l < 10; l++)
            if (i + j + k + l == c)
              cnt++;
    cout << cnt << endl;
  }
  return 0;
}
Problem p00008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define debug(x) cerr << #x << ": " << x << endl;
#define line() cerr << "---------------" << endl;

int main() {
  while (1) {
    int n;
    cin >> n;

    int cnt = 0;
    for (int i = 0; i < 10; i++)
      for (int j = 0; j < 10; j++)
        for (int k = 0; k < 10; k++)
          for (int l = 0; l < 10; l++) {
            if (i + j + k + l == n) {
              cnt++;
            }
          }

    cout << cnt << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define debug(x) cerr << #x << ": " << x << endl;
#define line() cerr << "---------------" << endl;

int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    int cnt = 0;
    for (int i = 0; i < 10; i++)
      for (int j = 0; j < 10; j++)
        for (int k = 0; k < 10; k++)
          for (int l = 0; l < 10; l++) {
            if (i + j + k + l == n) {
              cnt++;
            }
          }

    cout << cnt << endl;
  }

  return 0;
}
Problem p00008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>

int main(void) {
  int num, cnt = 0;
  int i, j, k, l;

  while (scanf("%d", &num)) {
    for (i = 0; i < 10; i++) {
      for (j = 0; j < 10; j++) {
        for (k = 0; k < 10; k++) {
          for (l = 0; l < 10; l++) {
            if (i + j + k + l == num) {
              cnt++;
            }
          }
        }
      }
    }
    printf("%d\n", cnt);
    cnt = 0;
  }

  return 0;
}

Fixed submission:
#include <stdio.h>

int main(void) {
  int num, cnt = 0;
  int i, j, k, l;

  while (scanf("%d", &num) != EOF) {
    for (i = 0; i < 10; i++) {
      for (j = 0; j < 10; j++) {
        for (k = 0; k < 10; k++) {
          for (l = 0; l < 10; l++) {
            if (i + j + k + l == num) {
              cnt++;
            }
          }
        }
      }
    }
    printf("%d\n", cnt);
    cnt = 0;
  }

  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
using namespace std;

//??¨??????????????????????????§?´???°??¨?????????
//?´???°??¨??¨?´???????????????£?????????????????§??¨????????????

const int MAX_V = 10000000;
int prime[MAX_V + 1]; //?´???°??¨
int sum[MAX_V + 1];   //?´???°?????°???????¨?

int main() {
  int i, k, v;
  prime[0] = prime[1] = 0;
  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;
      }
    }
  }

  sum[0] = 0;
  for (i = 0; i < MAX_V + 1; i++) {
    sum[i + 1] = sum[i] + prime[i + 1];
  }

  int input;
  while (cin >> input) {
    cout << sum[input] << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

//??¨??????????????????????????§?´???°??¨?????????
//?´???°??¨??¨?´???????????????£?????????????????§??¨????????????

const int MAX_V = 999999;
int prime[MAX_V + 1]; //?´???°??¨
int sum[MAX_V + 1];   //?´???°?????°???????¨?

int main() {
  int i, k, v;
  prime[0] = prime[1] = 0;
  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;
      }
    }
  }

  sum[0] = 0;
  for (i = 0; i < MAX_V + 1; i++) {
    sum[i + 1] = sum[i] + prime[i + 1];
  }

  int input;
  while (cin >> input) {
    cout << sum[input] << endl;
  }

  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAX_VALUE = 100007;
bool prime[MAX_VALUE];

int main() {
  FOR(i, 2, MAX_VALUE) {
    if (prime[i])
      continue;
    for (int j = i * 2; j < MAX_VALUE; j += i)
      prime[j] = true;
  }

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

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAX_VALUE = 1000007;
bool prime[MAX_VALUE];

int main() {
  FOR(i, 2, MAX_VALUE) {
    if (prime[i])
      continue;
    for (int j = i * 2; j < MAX_VALUE; j += i)
      prime[j] = true;
  }

  int n;
  while (cin >> n) {
    int cnt = 0;
    FOR(i, 2, n + 1) {
      if (!prime[i])
        cnt++;
    }
    cout << cnt << endl;
  }
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
#define MAX 100000
using namespace std;

int main() {
  // bool num[MAX];
  pair<bool, int> num[MAX];
  int input = 0;
  while (cin >> input) {
    int count = 0;
    for (int i = 2; i < MAX; i++) {
      num[i].first = true;
      num[i].second = i;
    }
    for (int i = 2; i < input + 1; i++) {
      if (num[i].first) {
        // cout << num[i].second <<",";
        count++;
        for (int j = i + i; j < MAX; j += i) {
          num[j].first = false;
        }
      }
    }
    cout << count << endl;
  }
}

Fixed submission:
#include <iostream>
#include <vector>
#define MAX 1000000
using namespace std;

int main() {
  // bool num[MAX];
  pair<bool, int> num[MAX];
  int input = 0;
  while (cin >> input) {
    int count = 0;
    for (int i = 2; i < MAX; i++) {
      num[i].first = true;
      num[i].second = i;
    }
    for (int i = 2; i < input + 1; i++) {
      if (num[i].first) {
        // cout << num[i].second <<",";
        count++;
        for (int j = i + i; j < MAX; j += i) {
          num[j].first = false;
        }
      }
    }
    cout << count << endl;
  }
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  int N;
  bool is_prime[1000000];
  is_prime[0] = 0;
  is_prime[1] = 0;
  for (int i = 2; i < 1000000; i++) {
    is_prime[i] = 1;
  }

  for (int s = 2; s < 1000000; s++) {
    if (is_prime[s] == 1) {
      for (long long i = s * s; i < 1000000; i += s) {
        is_prime[i] = 0;
      }
    }
  }

  while (cin >> N) {

    int ans = 0;
    for (int i = 2; i < N + 1; i++) {
      ans += is_prime[i];
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int N;
  bool is_prime[1000000];
  is_prime[0] = 0;
  is_prime[1] = 0;
  for (int i = 2; i < 1000000; i++) {
    is_prime[i] = 1;
  }

  for (long s = 2; s < 1000000; s++) {
    if (is_prime[s] == 1) {
      for (long long i = s * s; i < 1000000; i += s) {
        is_prime[i] = 0;
      }
    }
  }

  while (cin >> N) {

    int ans = 0;
    for (int i = 2; i < N + 1; i++) {
      ans += is_prime[i];
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  bool a[1000000];

  a[0] = a[1] = false;
  for (int i = 2; i < 1000000; i++) {
    a[i] = true;
  }

  for (int i = 2; i * i < 1000000; i++) {
    if (a[i]) {
      for (int j = i * i; j < 10000000; j += i) {
        a[j] = false;
      }
    }
  }

  int n[1000000];

  int tmp = 0;
  for (int i = 0; i < 1000000; i++) {
    if (a[i]) {
      tmp++;
    }
    n[i] = tmp;
  }

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

    if (cin.eof()) {
      break;
    }

    cout << n[m] << endl;
  }

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

int main() {
  bool a[1000000];

  a[0] = a[1] = false;
  for (int i = 2; i < 1000000; i++) {
    a[i] = true;
  }

  for (int i = 2; i * i < 1000000; i++) {
    if (a[i]) {
      for (int j = i * i; j < 1000000; j += i) {
        a[j] = false;
      }
    }
  }

  int n[1000000];

  int tmp = 0;
  for (int i = 0; i < 1000000; i++) {
    if (a[i]) {
      tmp++;
    }
    n[i] = tmp;
  }

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

    if (cin.eof()) {
      break;
    }

    cout << n[m] << endl;
  }

  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

int main() {
  vector<int> p;
  int n;
  p.push_back(2);
  for (int i = 3; i < 999999; i++) {
    for (int j = 0;; j++) {
      if (i % p[j] == 0)
        break;
      if (i < p[j] * p[j]) {
        p.push_back(i);
        break;
      }
    }
  }
  while (cin >> n) {
    int i = 0;
    while (n >= p[i]) {
      i++;
    }
    cout << i << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int main() {
  vector<int> p;
  int n;
  p.push_back(2);
  for (int i = 3; i < 999999; i++) {
    for (int j = 0;; j++) {
      if (i % p[j] == 0)
        break;
      if (i < p[j] * p[j]) {
        p.push_back(i);
        break;
      }
    }
  }
  p.push_back(9999999);
  while (cin >> n) {
    int i = 0;
    while (n >= p[i]) {
      i++;
    }
    cout << i << endl;
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int f[1000001];
int main() {
  fill_n(f, 1000001, 1);
  f[0] = f[1] = 0;

  for (int i = 2; i < 1000001; i++) {
    f[i] += f[i - 1];
    if (!f[i])
      continue;
    for (int j = i + i; j < 1000001; j += i)
      f[j] = 0;
  }

  int N;

  while (scanf("%d", &N), &N) {
    printf("%d\n", f[N]);
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int f[1000001];
int main() {
  fill_n(f, 1000001, 1);
  f[0] = f[1] = 0;

  for (int i = 2; i < 1000001; i++) {
    f[i] += f[i - 1];
    if (!f[i])
      continue;
    for (int j = i + i; j < 1000001; j += i)
      f[j] = 0;
  }

  int N;

  while (scanf("%d", &N) != EOF) {
    printf("%d\n", f[N]);
  }

  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;
const int maxn = 1e6 + 7;
int prime[maxn];
bool is_prime[maxn];
int n, p = -1;

void Eular() {
  for (int i = 1; i <= maxn - 7; i++)
    is_prime[i] = true;
  for (int i = 2; i <= maxn - 7; i++) {
    if (is_prime[i])
      prime[++p] = i;
    for (int j = 0; j <= p && i * prime[j] <= maxn - 7; j++)
      is_prime[i * prime[j]] = false;
  }
}

int main() {
  Eular();
  while (~scanf("%d", &n)) {
    int i = 0, ans = 0;
    while (prime[i] <= n)
      i++, ans++;
    printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;
const int maxn = 1e6 + 7;
int prime[maxn];
bool is_prime[maxn];
int n, p = -1;

void Eular() {
  for (int i = 1; i <= maxn - 7; i++)
    is_prime[i] = true;
  for (int i = 2; i <= maxn - 7; i++) {
    if (is_prime[i])
      prime[++p] = i;
    for (int j = 0; j <= p && i * prime[j] <= maxn - 7; j++)
      is_prime[i * prime[j]] = false;
  }
}

int main() {
  Eular();
  while (~scanf("%d", &n)) {
    int i = 0, ans = 0;
    while (i <= p && prime[i] <= n)
      i++, ans++;
    printf("%d\n", ans);
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string.h>
using namespace std;
int n, ans;
bool a[10005];
int main() {
  while (cin >> n) {
    memset(a, true, sizeof(a));
    a[0] = false;
    a[1] = false;
    ans = 0;
    for (int i = 2; i <= n; i++) {
      if (a[i])
        for (int j = 2 * i; j <= n; j += i)
          a[j] = false;
    }
    for (int i = 1; i <= n; i++)
      if (a[i])
        ans++;
    cout << ans << endl;
  }
}


Fixed submission:
#include <iostream>
#include <string.h>
using namespace std;
int n, ans;
bool a[1000000];
int main() {
  while (cin >> n) {
    memset(a, true, sizeof(a));
    a[0] = false;
    a[1] = false;
    ans = 0;
    for (int i = 2; i <= n; i++) {
      if (a[i])
        for (int j = 2 * i; j <= n; j += i)
          a[j] = false;
    }
    for (int i = 1; i <= n; i++)
      if (a[i])
        ans++;
    cout << ans << endl;
  }
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
/*
题意：给定整数 n， 请问 n 以内有多少个素数？
思路：如果对许多整数进行素性测试，则采用 埃氏筛法 （枚举 n
以内的素数，辗转相除） 埃氏筛法：辗转相除： 首先，将 2 到 n
范围内的所有整数写下来。其中最小的数字 2 是素数。将表中所有 2 的倍数都划去。
                        表中剩余的最小数字是
3，它不能被更小的数整除，所以是素数。再将表中所有 3 的倍数都划去。
                        以此类推，如果表中剩余的最小数字是 m
时，m就是素数。然后将表中所有 m 的倍数都划去。 像这样反复操作，就能依次枚举 n
以内的素数。
*/

#include <iostream>
#include <stdio.h>
using namespace std;
#define MAX_N 1000

int prime[MAX_N];     // 第 i 个素数
bool is_prime[MAX_N]; // is_prime[i] 为 true 表示 i 为素数

// 返回 n 以内素数的个数
int sieve(int n) {
  int p = 0;
  for (int i = 0; i <= n; i++)
    is_prime[i] = true;

  is_prime[0] = is_prime[1] = false;

  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
  return p;
}

int main() {
  int s;
  //	while (cin  >>  s)
  while (scanf("%d", &s) != EOF) {
    cout << sieve(s) << endl;
  }
  return 0;
}


Fixed submission:
/*
题意：给定整数 n， 请问 n 以内有多少个素数？
思路：如果对许多整数进行素性测试，则采用 埃氏筛法 （枚举 n
以内的素数，辗转相除） 埃氏筛法：辗转相除： 首先，将 2 到 n
范围内的所有整数写下来。其中最小的数字 2 是素数。将表中所有 2 的倍数都划去。
                        表中剩余的最小数字是
3，它不能被更小的数整除，所以是素数。再将表中所有 3 的倍数都划去。
                        以此类推，如果表中剩余的最小数字是 m
时，m就是素数。然后将表中所有 m 的倍数都划去。 像这样反复操作，就能依次枚举 n
以内的素数。
*/

#include <iostream>
#include <stdio.h>
using namespace std;
#define MAX_N 999999

int prime[MAX_N];     // 第 i 个素数
bool is_prime[MAX_N]; // is_prime[i] 为 true 表示 i 为素数

// 返回 n 以内素数的个数
int sieve(int n) {
  int p = 0;
  for (int i = 0; i <= n; i++)
    is_prime[i] = true;

  is_prime[0] = is_prime[1] = false;

  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
  return p;
}

int main() {
  int s;
  //	while (cin  >>  s)
  while (scanf("%d", &s) != EOF) {
    cout << sieve(s) << endl;
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
bool flg[1000000];
int main(void) {
  int n;
  int i, j;
  int cnt = 0;
  while (1) {
    cin >> n;
    if (cin.eof())
      break;
    fill(flg, flg + n + 1, 0);
    for (i = 2; i * i <= n; i++) {
      // cout<<"I="<<i<<endl;
      for (j = 2; j <= n; j++) {
        // cout<<"J="<<j<<endl;
        flg[i * j] = 1;
      }
    }
    cnt = 0;
    for (i = 2; i <= n; i++)
      if (flg[i] == 0)
        cnt++;
    cout << cnt << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
bool flg[1000000];
int main(void) {
  int n;
  int i, j;
  int cnt = 0;
  while (1) {
    cin >> n;
    if (cin.eof())
      break;
    fill(flg, flg + n + 1, 0);
    for (i = 2; i * i <= n; i++) {
      // cout<<"I="<<i<<endl;
      for (j = 2; i * j <= n; j++) {
        // cout<<"J="<<j<<endl;
        flg[i * j] = 1;
      }
    }
    cnt = 0;
    for (i = 2; i <= n; i++)
      if (flg[i] == 0)
        cnt++;
    cout << cnt << endl;
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(n) begin(n), end(n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
int N;
//??¨????????????????????????
bool is_prime[10000 + 1];
vector<int> P;
int E(const int N) {
  int cnt{};
  for (int i = 0; i <= N; i++) {
    is_prime[i] = true; //?????????
  }
  for (int i = 2; i <= N; i++) {
    if (is_prime[i]) {
      for (int j = 2 * i; j <= N; j += i) {
        is_prime[j] = false;
      }
      cnt++;
    }
  }
  return cnt;
}
int main() {
  while (cin >> N) {
    cout << E(N) << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(n) begin(n), end(n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
int N;
//??¨????????????????????????
bool is_prime[999999 + 1];
int E(const int N) {
  int cnt{};
  for (int i = 0; i <= N; i++) {
    is_prime[i] = true; //?????????
  }
  for (int i = 2; i <= N; i++) {
    if (is_prime[i]) {
      for (int j = 2 * i; j <= N; j += i) {
        is_prime[j] = false;
      }
      cnt++;
    }
  }
  return cnt;
}
int main() {
  while (cin >> N) {
    cout << E(N) << endl;
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <string.h>
using namespace std;
int pNum;
bool IsPrime[1000010];
int Prime[1000010];
int GetPrime(int n) {
  memset(IsPrime, true, sizeof(IsPrime));
  IsPrime[0] = IsPrime[1] = false;
  pNum = 0;
  for (int i = 2; i <= n; i++) {
    if (IsPrime[i])
      Prime[++pNum] = i;
    for (int j = 1; j <= pNum && Prime[j] * i <= n; j++) {
      IsPrime[Prime[j] * i] = false;
      if (i % Prime[j] == 0)
        break;
    }
  }
  return pNum;
}
int main() {
  int n;
  while (scanf("%d", &n)) {
    printf("%d\n", GetPrime(n));
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <string.h>
using namespace std;
int pNum;
bool IsPrime[1000010];
int Prime[1000010];
int GetPrime(int n) {
  memset(IsPrime, true, sizeof(IsPrime));
  IsPrime[0] = IsPrime[1] = false;
  pNum = 0;
  for (int i = 2; i <= n; i++) {
    if (IsPrime[i])
      Prime[++pNum] = i;
    for (int j = 1; j <= pNum && Prime[j] * i <= n; j++) {
      IsPrime[Prime[j] * i] = false;
      if (i % Prime[j] == 0)
        break;
    }
  }
  return pNum;
}
int main() {
  int n;
  while (~scanf("%d", &n)) {
    printf("%d\n", GetPrime(n));
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#define REP(i, n) for (unsigned int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define MAX 100001

bool N[MAX] = {};

int M[MAX] = {};

void SoE() {
  REP(i, MAX) N[i] = true;

  N[1] = true;
  for (unsigned int i = 2; i * i <= MAX; i++) {
    for (unsigned int j = i + i; j < MAX; j += i) {
      N[j] = false;
    }
  }
}

void Count() {
  M[1] = 0;
  FOR(i, 2, MAX - 1) {
    int add = 0;
    if (N[i] == true) {
      add = 1;
    }
    M[i] = M[i - 1] + add;
  }
}

int main() {
  SoE();
  Count();
  int tmp;
  while (cin >> tmp) {
    cout << M[tmp] << endl;
  }
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
#define REP(i, n) for (unsigned int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define MAX 1000001

bool N[MAX] = {};

int M[MAX] = {};

void SoE() {
  REP(i, MAX) N[i] = true;

  N[1] = true;
  for (unsigned int i = 2; i * i <= MAX; i++) {
    for (unsigned int j = i + i; j < MAX; j += i) {
      N[j] = false;
    }
  }
}

void Count() {
  M[1] = 0;
  FOR(i, 2, MAX - 1) {
    int add = 0;
    if (N[i] == true) {
      add = 1;
    }
    M[i] = M[i - 1] + add;
  }
}

int main() {
  SoE();
  Count();
  int tmp;
  while (cin >> tmp) {
    cout << M[tmp] << endl;
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rREP(i, k, n) for (int i = (int)(n)-1; i >= k; i--)

#define debug(x) cerr << #x << ": " << x << '\n'

#define fi first
#define se second

#define vi vector<int>
#define pb push_back
#define mp make_pair

#define pcnt __builtin_popcount

typedef long long ll;

const int inf = 100100100;
const int MOD = 998244353;
const double EPS = 1e-9;

bool s[1000000] = {};
int sosu[1000000] = {};
void erast() {
  memset(s, 1, sizeof(s));
  s[1] = false;
  s[0] = false;
  REP(i, 2, 1000) {
    if (s[i]) {
      REP(j, 2, 1000000) {
        if (i * j >= 1000000)
          break;
        s[i * j] = false;
      }
    }
  }
  REP(i, 2, 1000000) sosu[i] = sosu[i - 1] + s[i];
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  erast();
  int n;
  while (cin >> n, n) {
    cout << sosu[n] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rREP(i, k, n) for (int i = (int)(n)-1; i >= k; i--)

#define debug(x) cerr << #x << ": " << x << '\n'

#define fi first
#define se second

#define vi vector<int>
#define pb push_back
#define mp make_pair

#define pcnt __builtin_popcount

typedef long long ll;

const int inf = 100100100;
const int MOD = 998244353;
const double EPS = 1e-9;

bool s[1000000] = {};
int sosu[1000000] = {};
void erast() {
  memset(s, 1, sizeof(s));
  s[1] = false;
  s[0] = false;
  REP(i, 2, 1000) {
    if (s[i]) {
      REP(j, 2, 1000000) {
        if (i * j >= 1000000)
          break;
        s[i * j] = false;
      }
    }
  }
  REP(i, 2, 1000000) sosu[i] = sosu[i - 1] + s[i];
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  erast();
  int n;
  while (cin >> n) {
    cout << sosu[n] << endl;
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <bitset> // compact STL for Sieve, better than vector<bool>!
#include <cstdio>
#include <iostream>
#include <vector>
#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;
  }
}

Fixed submission:
#include <bitset> // compact STL for Sieve, better than vector<bool>!
#include <cstdio>
#include <iostream>
#include <vector>
#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;
  sieve(1000010); // can go up to 10^7 (need few seconds)

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

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  int n;
  static bool c[1000000] = {true};
  static int t[1000000];
  t[0] = 0;
  for (int i = 2; i < 1000000; i++) {
    if (!c[i]) {
      for (int j = i; i * j < 1000000; j++) {
        c[i * j] = true;
      }
    }
    t[i] = t[i - 1] + !c[i];
  }
  for (int n; cin >> n; n++) {
    cout << t[n] << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int n;
  static bool c[1000000] = {true};
  static int t[1000000];
  t[0] = 0;
  for (int i = 2; i < 1000000; i++) {
    for (int j = 2; i * j < 1000000; j++) {
      c[i * j] = true;
    }
    t[i] = t[i - 1] + !c[i];
  }
  for (int n; cin >> n; n++) {
    cout << t[n] << endl;
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int n, prime[1000001], key, lf = 0;
  prime[0] = 2;
  key = 1;
  for (int i = 3; i <= 2000000; i += 2) {
    for (int j = 2; j <= sqrt(i); j++) {
      if (i % j == 0) {
        lf = 1;
        break;
      }
    }
    if (lf == 1) {
      lf = 0;
    } else {
      prime[key] = i;
      key++;
    }
  }

  while (cin >> n) {
    int cou = 0;
    for (int i = 0;; i++) {
      if (prime[i] <= n) {
        cou++;
      } else
        break;
    }

    cout << cou << endl;
  }
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int n, prime[1000001], key, lf = 0;
  prime[0] = 2;
  key = 1;
  for (int i = 3; i <= 1000100; i += 2) {
    for (int j = 2; j <= sqrt(i); j++) {
      if (i % j == 0) {
        lf = 1;
        break;
      }
    }
    if (lf == 1) {
      lf = 0;
    } else {
      prime[key] = i;
      key++;
    }
  }

  while (cin >> n) {
    int cou = 0;
    for (int i = 0;; i++) {
      if (prime[i] <= n) {
        cou++;
      } else
        break;
    }

    cout << cou << endl;
  }
}
Problem p00009 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
#define MAX 1000000
using namespace std;
int main() {
  vector<int> v(MAX);
  for (int i = 0; i < MAX; i++)
    v[i] = true;

  v[0] = v[1] = false;
  for (int i = 2; i < MAX; i++) {
    if (v[i]) {
      for (long long int j = (long long int)i * (long long int)i; j < MAX;
           j += i) {
        cout << i << " " << j << endl;
        v[j] = false;
      }
    }
  }
  for (int i = 2; i < MAX; i++)
    v[i] += v[i - 1];
  while (true) {
    int a;
    cin >> a;
    if (cin.eof())
      break;
    ;
    cout << v[a] << endl;
  }
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
#define MAX 1000000
using namespace std;
int main() {
  vector<int> v(MAX);
  for (int i = 0; i < MAX; i++)
    v[i] = true;

  v[0] = v[1] = false;
  for (int i = 2; i < MAX; i++) {
    if (v[i]) {
      for (long long int j = (long long int)i * (long long int)i; j < MAX;
           j += i) {
        v[j] = false;
      }
    }
  }
  for (int i = 2; i < MAX; i++)
    v[i] += v[i - 1];
  while (true) {
    int a;
    cin >> a;
    if (cin.eof())
      break;
    ;
    cout << v[a] << endl;
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
ll prime[1000001];
ll length = 0;
void insert(ll p) {
  prime[length] = p;
  length++;
  return;
}
int main() {
  insert(2);
  for (ll i = 3; i < 1000000; i += 2) {
    for (ll j = 0; j < length; j++)
      if (i % prime[j] == 0)
        goto cont;
      else if (prime[j] > sqrt(i))
        break;
    insert(i);
  cont:;
  }
  ll n;
  while (cin >> n) {
    ll ans = 0;
    while (prime[ans] <= n) {
      ans++;
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
ll prime[1000001];
ll length = 0;
void insert(ll p) {
  prime[length] = p;
  length++;
  return;
}
int main() {
  insert(2);
  for (ll i = 3; i < 1000000; i += 2) {
    for (ll j = 0; j < length; j++)
      if (i % prime[j] == 0)
        goto cont;
      else if (prime[j] > sqrt(i))
        break;
    insert(i);
  cont:;
  }
  ll n;
  while (cin >> n) {
    ll ans = 0;
    while (prime[ans] <= n && ans < length) {
      ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
// エラトステネスのふるい
int main() {
  int num = 1000;
  int n, prime[num];
  while (cin >> n) {
    for (int i = 0; i < num; i++) {
      prime[i] = 0;
    }
    prime[0] = 1;
    prime[1] = 1;

    int count = 0;
    for (int i = 0; i <= n; i++) {
      if (prime[i] == 0) {
        count++;
        for (int j = i; j <= n; j += i) {
          prime[j] = 1;
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
// エラトステネスのふるい
int main() {
  int num = 1000000;
  int n, prime[num];
  while (cin >> n) {
    for (int i = 0; i < num; i++) {
      prime[i] = 0;
    }
    prime[0] = 1;
    prime[1] = 1;

    int count = 0;
    for (int i = 0; i <= n; i++) {
      if (prime[i] == 0) {
        count++;
        for (int j = i; j <= n; j += i) {
          prime[j] = 1;
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}