Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>  //std::sort
#include <bitset>     //std::bitset
#include <chrono>     //std::chrono(C++11)
#include <cmath>      //abs,std::pow,sqrt,sin,cos,round,floor,ceil
#include <cstdlib>    //abs
#include <ctime>      //localtime_s
#include <fstream>    //std::ifstream,std::ofstream
#include <functional> //std::greater
#include <iomanip>    //std::setprecision,std::setw,std::setfill
#include <iostream>   //std::cout, std::cin
#include <numeric>    //std::accumulate
#include <queue>      //std::queue
#include <random>     //std::random(C++11)
#include <string>     //std::string,std::to_string(C++11)
#include <valarray>   //std::valarray
#include <vector>     //std::vector

const static double de_PI = 3.14159265358979323846;
const static int de_MOD = 1000000007;
const static int de_MAX = 999999999;

bool Judge_PrimeNumber(const int a) {

  if (a < 2) {
    return false;
  }
  if (a == 2) {
    return true;
  }
  if (a % 2 == 0) {
    return false;
  }

  for (int i = 3; i <= a / i; i += 2) {
    if (a % i == 0) {
      return false;
    }
  }
  return true;
}

int main(void) {

  // std::ifstream in("123.txt");	std::cin.rdbuf(in.rdbuf());
  // std::ofstream ofs("456.csv");
  // std::chrono::system_clock::time_point t_st =
  // std::chrono::system_clock::now();

  int N = 0, ans = 0;
  std::cin >> N;

  for (int i = 0; i < N; i++) {
    int temp = 0;
    std::cin >> temp;
    if (Judge_PrimeNumber((temp))) {
      ans++;
    }
  }

  std::cout << ans << std::endl;

  // std::chrono::system_clock::time_point t_ed =
  // std::chrono::system_clock::now(); std::cout <<
  // std::chrono::duration_cast<std::chrono::milliseconds>(t_ed - t_st).count()
  // << "ms" << std::endl;
}

Error: TLE

Fixed submission:

#include <algorithm>  //std::sort
#include <bitset>     //std::bitset
#include <chrono>     //std::chrono(C++11)
#include <cmath>      //abs,std::pow,sqrt,sin,cos,round,floor,ceil
#include <cstdlib>    //abs
#include <ctime>      //localtime_s
#include <fstream>    //std::ifstream,std::ofstream
#include <functional> //std::greater
#include <iomanip>    //std::setprecision,std::setw,std::setfill
#include <iostream>   //std::cout, std::cin
#include <numeric>    //std::accumulate
#include <queue>      //std::queue
#include <random>     //std::random(C++11)
#include <string>     //std::string,std::to_string(C++11)
#include <valarray>   //std::valarray
#include <vector>     //std::vector

const static double de_PI = 3.14159265358979323846;
const static int de_MOD = 1000000007;
const static int de_MAX = 999999999;

bool Judge_PrimeNumber(const int a) {

  if (a == 2) {
    return true;
  }
  if (a % 2 == 0) {
    return false;
  }

  for (int i = 3; i < a; i += 2) {
    if (a % i == 0) {
      return false;
    }
  }

  return true;
}

int main(void) {

  // std::ifstream in("123.txt");	std::cin.rdbuf(in.rdbuf());
  // std::ofstream ofs("456.csv");
  // std::chrono::system_clock::time_point t_st =
  // std::chrono::system_clock::now();

  int N = 0, ans = 0;
  std::cin >> N;

  for (int i = 0; i < N; i++) {
    int temp = 0;
    std::cin >> temp;
    if (Judge_PrimeNumber((temp))) {
      ans++;
    }
  }

  std::cout << ans << std::endl;

  // std::chrono::system_clock::time_point t_ed =
  // std::chrono::system_clock::now(); std::cout <<
  // std::chrono::duration_cast<std::chrono::milliseconds>(t_ed - t_st).count()
  // << "ms" << std::endl;
}