首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >素数查找程序(c++)的效率

素数查找程序(c++)的效率
EN

Stack Overflow用户
提问于 2022-05-28 07:25:35
回答 3查看 367关注 0票数 0

我想知道我所做的这段代码是否有什么效率低下的地方,或者是否有一种更快的方法来找到素数。

代码语言:javascript
复制
#include <stdio.h>

int main(void)
{
    int count;

    for(int i=3; i<1000; i+=2){//search number in range of 3~999
        count=0;//init count
        for(int j=3; j*j<=i; j+=2){
            if(count==1){//if i has aliquot already, break the loop
                break;
            }
            if(i%j==0){
                count=1;//if i has aliquot, change count to 1
            }
        }
        if(count==0){
            printf("%d ", i);//if there are no aliquot, print i
        }
    }

    return 0;
}
EN

回答 3

Stack Overflow用户

发布于 2022-05-28 08:35:38

似乎您使用的是审判分庭,它需要O(√n)时间来确定单个数字的素数,因此在一个范围内查找所有素数的效率很低。要有效地查找范围内的所有素数,请考虑使用埃拉托斯提尼筛 (具有时间复杂度O(n_log_n_loglog_n))或欧拉筛 (具有时间复杂度O(n))。以下是这两种算法的简单实现。

Eratosthenes筛的实现

代码语言:javascript
复制
bool isPrime[N];

void eratosthenes(int n) {
    for (int i = 2; i <= n; ++i) {
        isPrime[i] = true;
    }
    isPrime[1] = false;
    for (int i = 2; i * i <= n; ++i) {
        if (isPrime[i]) {
            for (int j = i * i; j <= n; j += i) {
                isPrime[j] = false;
            }
        }
    }
}

欧拉筛的实现

代码语言:javascript
复制
bool isPrime[N];
std::vector<int> primes;

void euler(int n) {
    for (int i = 2; i <= n; ++i) {
        isPrime[i] = true;
    }
    isPrime[1] = false;
    for (int i = 2; i <= n; ++i) {
        if (isPrime[i]) primes.push_back(i);
        for (size_t j = 0; j < primes.size() && i * primes[j] <= n; ++j) {
            isPrime[i * primes[j]] = false;
            if (i % primes[j] == 0) break;
        }
    }
}
票数 3
EN

Stack Overflow用户

发布于 2022-05-31 06:56:37

埃拉托斯提尼筛子很酷,但不推荐?为什么不使用我的高级课程呢?它有增量的方法,不使用除法。素类通过它的同余来描述一个数字到下素数。增加素数是为了增加所有同余,如果整数是素数,则创建一个新的同余(所有同余-modulos_-与0不同)。

代码语言:javascript
复制
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>

class Prime {
public :
  Prime () : n_ (2), modulos_ (std::vector<std::pair<int, int> > ())
  {
    if (!modulos_.capacity ()) modulos_.reserve (100000000);
    std::pair<int, int> p (2, 0);
    modulos_.push_back (p);
  }
  ~Prime () {}
  Prime (const Prime& i) : n_ (i.n_), modulos_ (i.modulos_)
   {}
  bool operator == (const Prime& n) const {
    return (n_ == n.n_);
  }
  bool operator != (const Prime& n) const {
    return !operator == (n);
  }
  Prime& operator = (const Prime& i) {
    n_ = i.n_,
    modulos_ = i.modulos_;
    return *this;
  }
  void write (std::ostream& os) const {
    os << n_;
  }
  void operator ++ () {
    int prime (1);
    do {
      ++n_;
      prime = 1;
      std::for_each (modulos_.begin (), modulos_.end (), [&prime] (std::pair<int, int>& p) {
        ++p.second;
        if (p.first == p.second) {
          p.second = 0;
          prime  = 0;
        }
      });
    }
    while (!prime);
    std::pair<int, int> p (n_, 0);
    modulos_.push_back (p);
  }
  bool operator < (const int& s) const {
    return n_ < s;
  }
private :
  int n_;
  std::vector<std::pair<int, int> > modulos_; 
};

用法:

代码语言:javascript
复制
int main (int, char**) {
  Prime p;
  do {
    p.write (std::cout);
    std::cout << std::endl;
    ++p;
  }
  while (p < 20);
}

结果:2 3 5 7 11 13 17 19

票数 0
EN

Stack Overflow用户

发布于 2022-05-31 17:39:39

对于最多1000的素数,一个有效的方法是

代码语言:javascript
复制
cout << "2  3   5   7   11  13  17  19  23
29  31  37  41  43  47  53  59  61  67
71  73  79  83  89  97  101 103 107 109
113 127 131 137 139 149 151 157 163 167
173 179 181 191 193 197 199 211 223 227
229 233 239 241 251 257 263 269 271 277
281 283 293 307 311 313 317 331 337 347
349 353 359 367 373 379 383 389 397 401
409 419 421 431 433 439 443 449 457 461
463 467 479 487 491 499 503 509 521 523
541 547 557 563 569 571 577 587 593 599
601 607 613 617 619 631 641 643 647 653
659 661 673 677 683 691 701 709 719 727
733 739 743 751 757 761 769 773 787 797
809 811 821 823 827 829 839 853 857 859
863 877 881 883 887 907 911 919 929 937
941 947 953 967 971 977 983 991 997" << endl;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72413719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档