cpp_lib

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub idat50me/cpp_lib

:heavy_check_mark: Isprime(素数判定) (math/isprime.cpp)

なにこれ

素数判定を行う.

関数

計算量


Verified with

Code

#pragma once

#ifndef call_include
#define call_include
#include <bits/stdc++.h>
using namespace std;
#endif

bool isprime(long long n) {
	if(n == 2 || n == 3 || n == 5) return true;
	if(n < 2 || n % 2 == 0 || n % 3 == 0 || n % 5 == 0) return false;

	long long i = 7;
	while(i * i <= n) {
		for(int k : {4, 2, 4, 2, 4, 6, 2, 6}) {
			if(i * i > n) break;
			if(n % i == 0) return false;
			i += k;
		}
	}

	return true;
}
#line 2 "math/isprime.cpp"

#ifndef call_include
#define call_include
#include <bits/stdc++.h>
using namespace std;
#endif

bool isprime(long long n) {
	if(n == 2 || n == 3 || n == 5) return true;
	if(n < 2 || n % 2 == 0 || n % 3 == 0 || n % 5 == 0) return false;

	long long i = 7;
	while(i * i <= n) {
		for(int k : {4, 2, 4, 2, 4, 6, 2, 6}) {
			if(i * i > n) break;
			if(n % i == 0) return false;
			i += k;
		}
	}

	return true;
}
Back to top page