Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 73687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:14:26+00:00 2026-05-10T20:14:26+00:00

Is this seen as an in efficient prime number generator. It seems to me

  • 0

Is this seen as an in efficient prime number generator. It seems to me that this is pretty efficient. Is it the use of the stream that makes the program run slower?

I am trying to submit this to SPOJ and it tells me that my time limit exceeded…

#include <iostream> #include <sstream>  using namespace std;  int main() {     int testCases, first, second, counter = 0;     bool isPrime = true;     stringstream out;      cin >> testCases;      for (int i = 0; i < testCases; i++) {         // get the next two numbers         cin >> first >> second;          if (first%2 == 0)             first++;          // find the prime numbers between the two given numbers         for (int j = first; j <= second; j+=2) {             // go through and check if j is prime             for (int k = 2; k < j; k++) {                 if (j%k == 0) {                     isPrime = false;                     break;                 }             }             if (isPrime) {                 out << j << '\n';             }             isPrime = true;         }         out << '\n';     }      cout << out.str();      return 0; } 

EDIT: The program is supposed to generate prime numbers between the numbers specified in the input. (See here for more details: Prime Generator Problem )

-Tomek

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:14:27+00:00Added an answer on May 10, 2026 at 8:14 pm

    This is one step (skipping even numbers) above the naive algorithm. I would suggest the Sieve Of Eratosthenes as a more efficient algorithm. From the above link:

    The complexity of the algorithm is O((nlogn)(loglogn)) with a memory requirement of O(n). The segmented version of the sieve of Eratosthenes, with basic optimizations such as wheel factorization, uses O(n) operations and O(n1 / 2loglogn / logn) bits of memory.

    The algorithm you give is somewhere near O(n^2). The speedup you get by skipping evens isn’t that great because you would find an even number not to be prime on the first test. The sieve has a much greater memory requirement, but the runtime complexity is far superior for large N.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen that some Android devs promote this form of declarative layout: <TextView style=@style/my_textview_style
I have seen this question about deploying to WebSphere using the WAS ant tasks.
Has anyone seen this error when working with a PHP application out of dynamic
You've seen this - on an ajax-heavy page, like meebo.com, the browser never stops
I've seen this done with a signed Java Applet, but is it possible with
I've seen this done before, but I'm not sure how. When my page loads,
I've seen this one-liner perl -lane '$_{$F[0]}+=$F[1]}print$_ $_{$_}for keys%_;{' file here: How can I
I've seen this done inside of an event handler directly behind the .xaml file
I have seen this for a long time, and finally decided to put the
I have seen this statement in many of the documention samples, like here This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.