Need help improving preformance time

So I am trying to keep doing excersises to practice C++ and have lately been doing the classical problems at http://www.spoj.com. Anyways I'm working on this problem here: http://www.spoj.com/problems/PRIME1/

I was wondering if you guys could look it over and see if there is anything wrong with it or if there can be any improvements made to it. Finding the prime numbers was quite easy but I had a hard time dealing with being able to accept the correct input and storing it correctly. Anyways here is the code look forward to any and all criticism.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <math.h>
#include <deque>
#include <sstream>

bool is_prime(const int &number)
{
    if (number < 2)
        return false;

    for (int i = 2; i < sqrt(number+1); i++)
        if (number%i == 0)
            return false;

    return true;
}

int main()
{
    std::deque<int> outputLimits;
    int testsDone = 0;

    int numTests;
    std::cin >> numTests;

    while (testsDone <= numTests)
    {
        std::string test;
        int first, second;

        getline(std::cin, test);
        std::stringstream ss(test);
        ss >> first >> second;

        outputLimits.push_back(first);
        outputLimits.push_back(second);

        ++testsDone;
    }

    while (!outputLimits.empty())
    {
        int start = outputLimits[0];
        int finish = outputLimits[1];
        while (start <= finish)
        {
            if (is_prime(start))
                std::cout << start << std::endl;
            ++start;
        }
        outputLimits.pop_front();
        outputLimits.pop_front();
        std::cout << std::endl;
    }
    return 0;
}

Last edited on
closed account (3qX21hU5)
I ran the code through and it seems to work for fine for me, are you having any problems with it? Cause I don't see any (At east I don't with a quick look)...

I'll look it over once I get some spare time and see if I can see anything that can be improved.
The code runs fine on codeblocks but when I submit it, it fails because it goes over the 6 second time limit. They are using some ancient machines to do the calculations :(. So basically I'm looking for a way to speed up the program and increase its performance.
Anyone? Been looking for ways but still just learning so I dont know anything about increasing preformance :(
Thank you naraku don't know why I was thinking I needed to use getline to read the space for the input. Also looking at the new function thanks for that also.
Last edited on
closed account (3qX21hU5)
@StillLearning Ya I see what you mean, I just submitted your code for the problem and it exceeded the time limit for me also. Though I don't think I will be able to help any other then what naraku posted.

Thanks for linking that site though seems like it has some interesting problems on it and probably will do a few.

Wish ya the best of luck with it and I'm sure some of the other users will be able to figure it out for ya.
Topic archived. No new replies allowed.