生成质数表-cpp

vb.net太慢>_< c++重写一遍, 心情爽! [cpp] #include<fcntl.h> #include<unistd.h> #include<cstring> #include<iostream> using std::cout; using std::endl; typedef unsigned long long uint64; #define UINT32_MAX 0x100000000L void writeBool(int fd, uint64 pos, bool b) { if (pos<0 || pos>UINT32_MAX) { return; } char buf; lseek(fd, pos>>3, SEEK_SET); size_t len=read(fd, &buf, 1); //if not set yet if (len<=0) buf=0; if (b) buf |= 1<<(pos % 8); else buf &= ~(1<<(pos % 8)); lseek(fd, pos>>3, SEEK_SET); write(fd, &buf, 1); } bool readBool(int fd, uint64 pos) { if (pos<0 || pos>UINT32_MAX) { return false; } char buf; lseek(fd, pos>>3, SEEK_SET); size_t len=read(fd, &buf, 1); //if not set yet if (len<=0) buf=0; return (buf>>(pos % 8))&1; } void writeFF(int fd, size_t block_size, size_t count) { char buf[block_size]; memset(buf, 0xFF, sizeof(buf)); lseek(fd, 0, SEEK_SET); for(int i=0;i<count;++i) write(fd, buf, block_size); } int main() { int fd=open("./pt.bin", O_RDWR | O_CREAT, 0644); //init writeFF(fd, 4096, UINT32_MAX/8/4096); cout<<"init done"<<endl; writeBool(fd, 0, false); writeBool(fd, 1, false); for(int i=2;i<=UINT32_MAX;++i) { if (readBool(fd, i) && ((UINT32_MAX/i)>i) ) { for(int j=i*i;j<=UINT32_MAX;j+=i) { writeBool(fd, j, false); } } } cout<<readBool(fd, 0)<<" "<<readBool(fd, 2)<<endl; close(fd); return 0; } [/cpp]

发表评论?

4 条评论。

  1. 这玛尼难道是Linux C++程序?看上去好高端

  2. 这是啥高亮,居然可以点击编辑

发表评论

注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :?: :-| :-x :-o :-P :-D :-? :) :( :!: 8-O 8)

本文链接:https://twd2.me/archives/4479QrCode