如何从11、101、201、301等开始以100的增量增加数组的大小以适合所有素数?

问题描述

#include <bits/stdc++.h> 
    using namespace std;
  
void SieveOfEratosthenes(int n) 
{ 
 
    bool prime[n+1];
    memset(prime,true,sizeof(prime)); 
  
    for (int p=2; P*p<=n; p++) 
    { 

        if (prime[p] == true) 
        { 

            for (int i=P*p; i<=n; i += p) 
                prime[i] = false; 
        } 
    } 

    int index = 0;
    for (int p=2; p<=n; p++) 
       if (prime[p]) 
       {
        index += 1;
            if(index % 100 == 1)
            {
                cout << index << ". " << p << endl;
                
            }
            
       }
           
} 
   
int main() 
{ 
    
    SieveOfEratosthenes(1000000); //if i add another 0 on this function the code won't run
    return 0; 
} 

我正在尝试创建一个程序,从1开始以100为增量打印所有素数。首先打印第一个素数,然后是101st,然后是201st,301st,依此类推,直到素数为小于或等于10 ^ 8。但是,我不能这样做,因为程序会在一段时间后停止打印数字,因为我认为是数组的内存限制。有什么办法可以进一步提高这些限制,以便打印出更多的质数,最高可达10 ^ 8?

输出示例:

1. 2
101. 547
201. 1229
301. 1993
401. 2749
501. 3581
601. 4421
701. 5281
801. 6143
901. 7001
1001. 7927
1101. 8837
1201. 9739
1301. 10663
1401. 11677
//..... (and so on until,)
78301. 997219
78401. 998617 // it stops here

解决方法

逻辑是错误的。与其使用大量的bool标记范围内的 all 数字(当您尝试很大的范围时,它将耗尽内存),只需使用具有足够元素的整数数组来满足期望的素数,并将结果整数存储在其中。

最好使用向量来完成此操作size mismatch for stage4.2.branches.2.3.conv1.weight: copying a param with shape torch.Size([128,3,128]) from checkpoint,the shape in current model is torch.Size([128,128,3]). 。如果您未使用c ++ 11 +,则可能正在考虑使用std::vector<unsigned long long>malloc来获取,保留和调整结果列表的大小。