This question already has answers here:
Printing prime numbers from 1 through 100
(22 answers)
Closed 9 years ago.
So, I made this little program. It gives all the correct outputs, but when uploading it to the online judge it throw me away a Time Limited Exceeded. Any ideas on how to make it more efficient?
Here is my code.
int const MAX = 1000001;
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main()
{
int count = 0, array[MAX], a, b, pi = 0;
bool end = false;
for(int i = 1; i <= MAX; i++)
{
count = 0;
for(int j = 2; j <= sqrt(i); j++)
{
if(i % j == 0)
{
array[i]=0;
count++;
break;
}
}
if(count == 0 && i != 1){
array[i]=1;
}
}
do {
cin >> a >> b;
pi = 0;
if ((a == 0) && (b == 0)) {
end = true;
break;
}
else {
for (int i = a; i <= b; i++) {
if (array[i] == 1) {
pi ++;
}
}
cout << pi << endl;
}
} while (end == false);
cout << endl;
return 0;
}
You may consider using Sieve of Eratosthenes. It is an efficient prime generation algorithm.
Related
#include<iostream>
using namespace std;
int main(){
int n=5;
int i = 2;
for (i; i <= n; i++)
// for all num to n
{
int j = 2;
bool divide = false;
for (j; j <= n - 1; j++)
// for checking each num
{
if (i % j == 0)
{
divide = true;
break;
}
}
if (divide == false)
{
cout << i << " ";
}
}
return 0;
}
my Q is that
//please tell me why it is not working
//it is expected to give ans 2,3,5 which it is not giving why???
maybe I found the issue.
I think that the problem here is:
for (j; j <= n - 1; j++)
Here you did j<=n-1;
So to fix this just do:
for(j; j < i; j++){
//this should fix
So everything should look like this:
#include<iostream>
using namespace std;
int main() {
int n = 5;
int i = 2;
//check prime numbers starting from i and max n using for loop
for (i = 2; i <= n; i++) {
bool divide = false;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
divide = true;
break;
}
}
if (!divide) {
//!divide is equal to divide=false
cout << i << " ";
}
}
}
I've come across this problem where I have to transform a number from the decimal system to the binary and compare if the numbers are equal
Example:
7 is becoming 111
The output is false
and where 5 is 101
The output is true
I've figured out how to transform the numbers with an array
But have no idea how to compare them
#include<iostream>
using namespace std;
int main()
{
int num;
cin >> num;
int arr[8] = {};
if ((num >= 0) && (num <= 255))
{
while (num != 0)
{
for (int i = 0; i < 8; i++)
{
if (num % 2 == 0)
arr[i] = 0;
else
{
arr[i] = 1;
}
num = num / 2;
}
}
for (int i = 7; i >= 0; i--)
cout << arr[i];
cout << endl;
}
else
{
cout << "error" << endl;
return 1;
}
system("pause");
return 0;
}
here is a loop that is supposed to add prime numbers only and ignore non prime numbers but it is not working properly ,my skills are pretty basic , so please try to simplify your answers as much as possible,
#include <iostream>
using namespace std;
int main()
{
int n = 0, a = 0, sum = 0;
cin >> n;
for (int j = 1; j <= n; j++)
{
cin >> a;
if (a == 1)
{
continue;
}
if (a == 2 || a == 3)
{
sum += a;
}
if (a % 2 == 0)
{
continue;
}
for (int i = 3; i < a; i++)
{
if (a % i != 0)
{
sum += a;
}
else
{
continue;
}
}
}
cout << sum;
return 0;
}
I would prefer using this function instead. You should add cmath library:
bool isPrime(int number) {
if (number <= 1)
return false;
for (int i = 2; i <= sqrt(number); i++)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
But if you want to continue with your code, after checking a == 2 || a == 3 you should continue. And last part before for loop you should define some boolean like bool isPrime = true. Then if its finds a divider you should assing it to false and break.
Your full code should be something like this:
#include <iostream>
using namespace std;
int main()
{
int n = 0, a = 0, sum = 0;
cin >> n;
for (int j = 1; j <= n; j++)
{
cin >> a;
if (a == 1)
{
continue;
}
if (a == 2 || a == 3)
{
sum += a;
continue;
}
if (a % 2 == 0)
{
continue;
}
bool isPrime = true;
for (int i = 3; i < a; i++)
{
if (a % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime) {
sum += a;
}
}
cout << sum;
return 0;
}
So I have to make prime factorisation for up to 10^6 different numbers from 1 to 8*10^6. I've done this with Sieve of Erathostenes, but it seems that it's too slow. I have no idea how can I deal with it. Here's my code:
int factors[8000001] = { };
void sieve(int* tab, int max)
{
for (int i = 2; i * i < max; ++i)
{
if(tab[i] == 0)
{
for (int j = i; j * i < max; ++j)
{
if (tab[i*j] > i || tab[i*j] == 0)
tab[i*j] = i;
}
}
}
}
in main():
cin >> number;
while (number > 1)
{
if (factors[number] == 0)
{
cout << number;
number = 1;
}
else if (number != factors[number])
cout << factors[number] << "*";
else
cout << factors[number];
number /= factors[number];
}
cout << "\n";
For smaller tests it works perfectly, but not for large ones. What can I improve? (I'm also using ios_base::sync_with_stdio(0); line)
I have to create a program, which counts bursted baloons, like from ZUMA. If I have a line with 3 or more baloons with the same color this sequence will burst. So in input, I have number of ballons (3 <= N <= 10^5) , and line with numbers (line with baloons color (1 <= сi <= 100) ), with 1 sequence for sure. I have to output number of bursted baloons. I have a programm, but it is working longer than 4000msv sometimes. How can I make it working faster?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int Fmax(int n, const string& f){
int max;
vector<int> k(n);
int i, j, p = 0;
for (i = 0; i <= n; i++)
{
k[i] = 0;
}
for (i = 0; i <= n; i++)
{
for (j = i; j <= n; j++)
{
if (f[i] == f[j])
{
k[p]++;
}
else break;
}
p++;
}
max = k[0];
for (i = 0; i <= p; i++){ if (max <= k[i]){ max = k[i]; } }
return max;
}
string pog(int n){
int d;
string doa;
for (int i = 1; i <= n; i++){
cin >> d;
doa += (char)d;
}
return doa;
}
void main(){
int i, sc = 1, bf = 1;
string f;
int len;
cin >> i;
f = pog(i);
len = i;
while (Fmax(f.length(), f) >= 3){
for (int c = 1; c <= f.length(); c++){
if (f[c] == f[c - 1]){
if (sc == 1){ bf = c - 1; }
sc++;
}
else{
if (sc >= 3){ f.erase(bf, sc); sc = 1; break; }
sc = 1;
}
}
}
cout << len - f.length() << endl;
}
Any help is warmly welcome.
You are leaking memory. Use vectors to avoid that.
Why do you need to create array? Why not use the string directly?
Pass strings which aren't modified by const reference to avoid copies.
Use constant variables for the lengths:
const unsigned int f_length = f.length();
while (Fmax(f_length, f) >= 3){
for (int c = 1; c <= f_length ; c++){
This helps the compiler reduce the number of calls to the length method.