0

I have written this simple code to implement Eratosthenes algorithm to compute the prime numbers and type them in some ordered way. The problem is that it doesn't work for numbers larger than 1020. Can somebody tells me the reason of this? When I run it, Eclipse can not launch the exe file and stops computing the numbers. The same code written in Java works well for numbers under one billion, though.

#include <iostream>
#include<math.h>
using namespace std;


int main() {
    int N;
    cin >> N;
    int* a = new int[N];

    int i , j, k, cnt = 0;
        for(a[1] = 0, i = 2; i <= N; i++) a[i] = 1;
    for(i = 2; i <= N/2; i++)
        for(j = 2; j <= N/i; j++)
            a[i*j] = 0;


        for(i = 1; i <= N; i++)
            if(a[i]) {
                cout<< i ;
                int lengthi = (int)floor(log10((float)i));
                int lengthN = (int)floor(log10((float)N)) + 1;
                    for(k = 0; k < lengthN - lengthi + 1; k++)
                         cout<<' ';
                cnt++;
                if(cnt%10==0) cout<<'\n';
            }

    delete [] a;
    return 0;
}
4

1 回答 1

3

您的格式非常糟糕,但是在将其转换为使用std::vector而不是手动管理循环后,它可以正常工作:

http://ideone.com/y2CML7

#include <cmath>
#include <iostream>
#include <vector>

int main() 
{
    int N = 1020;

    std::vector<int> a;
    a.resize(N);

    int i, j, k, cnt = 0;
    a[0] = 0;
    for(i = 1; i < N; i++) // fixed your indexing here
    {   
        a[i] = 1;
    }

    for(i = 2; i <= N/2; i++)
    {
        for(j = 2; j <= N/i; j++)
        {
            a[i*j] = 0; // I haven't done the math, but this may go out of bounds as well
        }
    }


    for(i = 1; i <= N; i++)
    {
        if(a[i]) 
        {
            std::cout << i;
            int lengthi = (int)std::floor(std::log10((float)i));
            int lengthN = (int)std::floor(std::log10((float)N)) + 1;
            for(k = 0; k < lengthN - lengthi + 1; k++)
            {
                std::cout << ' ';
            }
            cnt++;

            if(cnt%10==0)
            {
                std::cout << '\n';
            }
        }
    }

    return 0;
}

或算法的简化版本:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>


struct moduloFunctor
{
    int mod;

    moduloFunctor(int m) : mod(m) {}

    bool operator()(int i)
    {
        return (i % mod == 0 && i != mod);
    }
};


int main()
{
    std::vector<int> numbers;
    int maximum = 1020;
    //std::cin >> maximum;
    numbers.reserve(maximum - 1); // starting at 2
    for (int i = 2; i <= maximum; ++i)
    {
        numbers.push_back(i);
    }

    int cnt = 0;
    do
    {
        moduloFunctor func(numbers[cnt++]);
        numbers.erase(std::remove_if(numbers.begin(), numbers.end(), func), numbers.end()); 
    } while (cnt < numbers.size());

    std::cout << "Primes:  " << std::endl;
    std::copy(numbers.begin(), numbers.end(), std::ostream_iterator<int>(std::cout, " "));

    return 0;
}
于 2013-09-13T18:40:11.813 回答