CPP Prime Generator SPOJ Sieve - c++

I did implement Sieve prime generator. The code is pretty fast and consumes less memory. https://www.spoj.com/problems/PRIME1/
But I get "Wrong Answer" when I submit the solution. People online seem to just make the set the max to 32000 and run the solution. I don't exactly get where am I actually wrong? Or Is it just an extra newline (if possible) that makes the solution to be incorrect?
#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
unordered_map<long long int, bool> notPrime;
notPrime[0] = true;
notPrime[1] = true;
for (long long int x = 2; x < 100000; x++) {
if (notPrime[x]) continue;
for (long long int u = 2 * x; u < 100000; u += x)
notPrime[u] = true;
}
int n;
cin >> n;
while (n--) {
long long int s, e;
cin >> s >> e;
if (s < 0)
s = 0;
for (long long int i = s; i <= e; i++) {
if (!notPrime[i]) {
cout << i << '\n';
}
}
if (n)
puts("");
}
return 0;
}

Related

Trying to implement Willans' formula for the n-th prime, what's the problem with my code?

The formula is listed in the following article: https://en.wikipedia.org/wiki/Formula_for_primes. I am trying to implement it but to no success, for whatever reason the code is producing number which seem to be nth power of two + 1, which is obviously not what I want to achieve.
#include <iostream>
#include <cmath>
using namespace std;
int nth_prime(int n) {
double s = 1;
for (int i = 1; i <= pow(2, n); i++) {
double c = 0;
for (int j = 1; j <= i; j++) {
double f = (tgamma(j)+1)/j;
c+=floor(pow(cos(M_PI*f), 2));
}
s+=floor(pow(n/c, 1/n));
}
return s;
}
int main() {
int n;
while (cin >> n) {
cout << nth_prime(n) << endl;
}
return 0;
}

Why do I get a SIGABRT error while using bitset in stl?

The following code is running perfectly well with my g++ compiler. But on submitting it on an online judge, gives SIGABRT error. I read that stl elements can generate this error if they try to access too much memory. I cannot see any such use of memory. Is it the count? I have implemented my own count, but it still gives the same error.
#pragma GCC optimize ("-O2")
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define M 100
void range_change(bitset<M>& x, int lower, int upper, bool change){
if (change){
for (unsigned i = lower; i <= upper; ++i)
x.set(i);
}
else{
for (unsigned i = lower; i <= upper; ++i)
x.reset(i);
}
}
int count_Set(bitset<M> & x){
int count = 0;
for(int i = 0; i< 100; i++){
if(1 & x[i]) count++;
}
return count;
}
bitset<100> houses;
int main(){
fastio;
int t;
cin>> t;
int m, x, y;
int reach{0};
int lower{0}, upper{0};
// int t = 1;
while(t--){
houses.set();
// cout<< houses;
cin >> m >> x >> y;
reach = x*y;
vector<int> cat(m, 0);
for(auto& u: cat){
cin>> u;
lower = u - reach -1;
if(lower < 0) lower = 0;
upper = u + reach-1;
if(upper > 100) upper = 99;
range_change(houses, lower, upper, false);
}
// cout<< houses << "\n";
cout<< houses.count() << "\n";
// cout<< count_Set(houses) << "\n";
}
return 0;
}
Just skimming the source code, this is what I see:
#define M 100
...
bitset<100> houses;
Seems like you want to use M instead of 100 for safety.
Then here:
if(upper > 100) upper = 99;
This is going to break at upper = 100 Suggest:
if(upper >= M) upper = M - 1;

Error because of using scientific notation

I attempted a Hackerearth tutorial question and was able to solve it correctly using following code:
#include <iostream>
using namespace std;
int main() {
const long int MOD = 1000000007;
const long int SIZE = 100001;
long int t, n;
long int cache[SIZE];
cache[0] = 1;
for (long int i = 1; i < SIZE; i++) {
cache[i] = ( i * cache[i-1] ) % MOD;
}
cin >> t;
while (t--) {
cin >> n;
cout << cache[n] << endl;
}
return 0;
}
However when using scientific notation to write MOD or SIZE, online judge reports incorrect answers. What am I missing here?
#include <iostream>
using namespace std;
int main() {
const long int MOD = 10e9+7;
const long int SIZE = 100001;
long int t, n;
long int cache[SIZE];
cache[0] = 1;
for (long int i = 1; i < SIZE; i++) {
cache[i] = ( i * cache[i-1] ) % MOD;
}
cin >> t;
while (t--) {
cin >> n;
cout << cache[n] << endl;
}
return 0;
}
Scientific notation means “constant multiplied by ten to the power of x”. If you want 1000000007 you need 1e9, meaning “one followed by nine zeroes.” Now you use 10e9 which is “ten followed by nine zeroes” or “one followed by ten zeroes” so you’re off by a factor of ten.

Sieve of Eratosthenes not working beyond 200,000

My C++ program to calculate all the prime numbers using sieve of Eratosthenes method stops after 200,000. But I need to calculate the primes up to 2 million. Help would be appreciated if someone could tell me where I went wrong with my code.
#include <iostream>
#include<math.h>
using namespace std;
void isprime(long long int prime[],long int n)
{
for(long long int i=0;i<=n;i++)
{
prime[i]=1;
}
prime[0]=prime[1]=0;
for(long long int i=2;i<=sqrt(n);i++)
{
if(prime[i]==1)
{
for(long long int j=2;i*j<=n;j++)
prime[i*j]=0;
}
}
for(long long int i=0;i<=n;i++)
{
if(prime[i]==1)
cout<<i<<endl;
}
}
int main()
{
long long int n;
cout<<"enter number";
cin>>n;
long long int prime[n+1];
isprime(prime,n);
return 0;
}
Since each sieve element contains only a 0 or 1, there is no need to use a long long int to store each one. std::vector<bool> potentially uses 1 bit per element and thus is optimal for memory efficiency.
Here is your code with a very few modifications to use a std::vector<bool>. Since some bit manipulation is required to get and set individual elements, this version may be slower than code which uses one byte or int per sieve element. You can benchmark various versions and decide the right trade-off for your needs.
#include <cmath>
#include <cstddef>
#include <exception>
#include <iostream>
#include <string>
#include <vector>
// returns the number of primes <= n
long isprime(long n) {
std::vector<bool> prime(n + 1);
for (long i = 0; i <= n; i++) {
prime[i] = 1;
}
prime[0] = prime[1] = 0;
long upper_bound = std::sqrt(n);
for (long i = 2; i <= upper_bound; i++) {
if (prime[i] == 1) {
for (long j = 2; i * j <= n; j++)
prime[i * j] = 0;
}
}
long num_primes = 0;
for (long i = 0; i <= n; i++) {
if (prime[i] == 1) {
++num_primes;
// std::cout << i << std::endl;
}
}
return num_primes;
}
int main() {
std::cout << "Enter the sieve size: ";
std::string line;
std::getline(std::cin, line);
std::cout << std::endl;
long len = std::stol(line);
long num_primes = isprime(len);
std::cout << "There are " << num_primes << " primes <= " << len << std::endl;
return 0;
}

Saving unknown amount of integers without taking too much time/memory

#include <iostream>
using namespace std;
unsigned long long divsum(unsigned long long x);
int main()
{
unsigned long long x;
cin >> x;
unsigned long long y[200000];
for (unsigned int i = 0; i < x; i++)
cin >> y[i];
for (unsigned int i = 0; i < x; i++){
cout << divsum(y[i]) << endl;
}
return 0;
}
unsigned long long divsum(unsigned long long x){
int sum = 0;
for(unsigned int i = 1; i <= x/2; i++){
if(x % i == 0)
sum += i;
}
return sum;
}
I'm doing an online exercise and it says there are possible 2000000 cases in the first line, so I made an array of that amount, however, when I submit the solution it exceeds the time.. so I was wondering what is an alternative and faster way to do this? The program works fine right now, except it exceeds the time limit of the website.
You can allocate the array dynamically, so it will work better in cases where x < 200000
int main()
{
unsigned long long x;
cin >> x;
unsigned long long *y = new unsigned long long[x];
// you can check if new didn't throw an exception here
for (unsigned int i = 0; i < x; i++)
cin >> y[i];
for (unsigned int i = 0; i < x; i++){
cout << divsum(y[i]) << endl;
}
delete[] y;
return 0;
}
Since you know the size of the array, try vector and reserve.
int main()
{
unsigned long long x;
cin >> x;
unsigned long long var;
vector<unsigned long long> y;
y.reserve(x);
for (unsigned int i = 0; i < x; i++){
cin >> y[i];
}for (unsigned int i = 0; i < x; i++){
cout << divsum(var) << endl;
}
return 0;
}
And deal in const &
const unsigned long long & divsum(const unsigned long long & x){
int sum = 0;
unsigned long long x2 = x/2
for(unsigned int i = 1; i <= x2; i++){
if(x % i == 0)
sum += i;
}
return sum;
}
I think your assignment is more complex than you think. Your divsum(x) function should return sum of all divisors of x, right? In this case it's better to factorize the x, and calculate this sum using all the prime numbers (with powers), product of which is equal to the x. Take a look at:
http://en.wikipedia.org/wiki/Divisor_function
There are methods for recursive factorization - for example, if you have factorized all numbers until n, you can quickly find factorization for (n + 1). You also need to generate primes, Erathosphene sieve for first 2000000 numbers would be fine.