Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

I am solving this prime generator problem from SPOJ:

Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!

Input

The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.

Output

For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.

I’m using the Sieve of Eratosthenes. Whenever I submit the following code it gives Time Limit Exceeded. Can anyone please give me some optimizations that can make my code run faster?

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
using ll = long long;
int main() {
 ios_base::sync_with_stdio(false);
 int t = 0;
 cin >> t;
 while (t--) {
 ll first = 0, n = 0;
 cin >> first >> n;
 vector<bool> primes(n+1, 0);
 for (ll i = 2; i <= sqrt(n); ++i) {
 if (primes[i] == 0) {
 for (ll j = 2; (i*j) <= n; ++j) {
 primes[i*j] = 1;
 } 
 }
 }
 
 for (ll i = 2; i <= n; ++i) {
 if (primes[i] == 0 && i >= first) {
 cout << i << '\n';
 }
 }
 
 }
 return 0;
}

I am solving this prime generator problem from SPOJ:

Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!

Input

The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.

Output

For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.

I’m using the Sieve of Eratosthenes. Whenever I submit the following code it gives Time Limit Exceeded. Can anyone please give me some optimizations that can make my code run faster?

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
using ll = long long;
int main() {
 ios_base::sync_with_stdio(false);
 int t = 0;
 cin >> t;
 while (t--) {
 ll first = 0, n = 0;
 cin >> first >> n;
 vector<bool> primes(n+1, 0);
 for (ll i = 2; i <= sqrt(n); ++i) {
 if (primes[i] == 0) {
 for (ll j = 2; (i*j) <= n; ++j) {
 primes[i*j] = 1;
 } 
 }
 }
 
 for (ll i = 2; i <= n; ++i) {
 if (primes[i] == 0 && i >= first) {
 cout << i << '\n';
 }
 }
 
 }
 return 0;
}

I am solving this prime generator problem from SPOJ:

Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!

Input

The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.

Output

For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.

I’m using the Sieve of Eratosthenes. Whenever I submit the following code it gives Time Limit Exceeded. Can anyone please give me some optimizations that can make my code run faster?

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
using ll = long long;
int main() {
 ios_base::sync_with_stdio(false);
 int t = 0;
 cin >> t;
 while (t--) {
 ll first = 0, n = 0;
 cin >> first >> n;
 vector<bool> primes(n+1, 0);
 for (ll i = 2; i <= sqrt(n); ++i) {
 if (primes[i] == 0) {
 for (ll j = 2; (i*j) <= n; ++j) {
 primes[i*j] = 1;
 } 
 }
 }
 
 for (ll i = 2; i <= n; ++i) {
 if (primes[i] == 0 && i >= first) {
 cout << i << '\n';
 }
 }
 
 }
 return 0;
}
[Edit removed during grace period]
Source Link
edited tags
Link
200_success
  • 145.5k
  • 22
  • 190
  • 479
add a copy of the challenge to the question body
Source Link
alexwlchan
  • 8.7k
  • 1
  • 23
  • 55
Loading
deleted 34 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Source Link
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /