-
-
Save kmyk/355add6455ffe5974f7b to your computer and use it in GitHub Desktop.
指摘を受け入出力を減らした
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; $ sbcl --load a.cl --eval '(sb-ext:save-lisp-and-die "a.out" :toplevel #'\''main :executable t)' && time ( echo 100000000 | ./a.out ) | |
; ( echo 100000000 | ./a.out; ) 4.30s user 0.15s system 99% cpu 4.468 total | |
; $ sbcl --version | |
; SBCL 1.2.6 | |
(defun main () | |
(declare (optimize (speed 3) (debug 0) (safety 0) (compilation-speed 0))) | |
(let* ((n (1+ (the fixnum (read)))) | |
(is-prime (make-array n :element-type 'boolean :initial-element t))) | |
(declare (type fixnum n) | |
(type (simple-array boolean (*)) is-prime)) | |
(setf (svref is-prime 0) nil) | |
(setf (svref is-prime 1) nil) | |
(dotimes (i n) | |
(declare (type fixnum i)) | |
(if (svref is-prime i) | |
(do ((j (* 2 i) (+ j i))) | |
((>= j n)) | |
(declare (type fixnum j)) | |
(setf (svref is-prime j) nil)))) | |
(let ((m 0)) | |
(dotimes (i n) | |
(declare (type fixnum i)) | |
(if (svref is-prime i) | |
(setq m (1+ m)))) | |
(write m) | |
(write-char #\newline)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// $ clang++ -std=c++11 -O2 a.cpp && time ( echo 100000000 | ./a.out ) | |
// 2.10s user 0.00s system 99% cpu 2.116 total | |
// $ g++ -std=c++11 -O2 a.cpp && time ( echo 100000000 | ./a.out ) | |
// 2.13s user 0.00s system 99% cpu 2.140 total | |
// $ g++ --version | |
// g++ (GCC) 4.9.2 20141224 (prerelease) | |
// $ clang++ --version | |
// clang version 3.5.1 (tags/RELEASE_351/final) | |
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
int main() { | |
ios_base::sync_with_stdio(false); | |
size_t n; cin >> n; | |
vector<bool> is_prime(n+1, true); | |
is_prime[0] = is_prime[1] = false; | |
for (size_t i = 0; i < n+1; ++i) { | |
if (is_prime[i]) { | |
for (size_t j = 2*i; j < n+1; j += i) { | |
is_prime[j] = false; | |
} | |
} | |
} | |
cout << count(is_prime.begin(), is_prime.end(), true) << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment