Getting TLE in hackerearth
up vote
-4
down vote
favorite
When i submiting this code at hackerearth i am getting TLE.
Any suggestions how can i optimise this
Code.
#include <stdio.h>
#include <stdlib.h>
int checkPrime(int);
int main() {
int a, b,
reminder,sum=0,n;
scanf("%d %d",&a,&b);
while(a <= b) {
n = a;
sum = 0;
reminder = 0;
while (n > 0) {
reminder = n % 10;
sum += reminder;
n = n / 10;
}
if(sum > 1 && checkPrime(sum) == 1 && checkPrime(a) == 1) {
printf("%d ",a);
}
++a;
}
return 0;
}
int checkPrime(int p) {
int i,flag=1;
for(i=2; i <= p / 2; i++){
if(p%i == 0) {
flag = 0;
break;
}
}
return flag;
}
Here is the problem i coded for
And how can i analysis this code and get
Time complexity.
c++ c algorithm primes
add a comment |
up vote
-4
down vote
favorite
When i submiting this code at hackerearth i am getting TLE.
Any suggestions how can i optimise this
Code.
#include <stdio.h>
#include <stdlib.h>
int checkPrime(int);
int main() {
int a, b,
reminder,sum=0,n;
scanf("%d %d",&a,&b);
while(a <= b) {
n = a;
sum = 0;
reminder = 0;
while (n > 0) {
reminder = n % 10;
sum += reminder;
n = n / 10;
}
if(sum > 1 && checkPrime(sum) == 1 && checkPrime(a) == 1) {
printf("%d ",a);
}
++a;
}
return 0;
}
int checkPrime(int p) {
int i,flag=1;
for(i=2; i <= p / 2; i++){
if(p%i == 0) {
flag = 0;
break;
}
}
return flag;
}
Here is the problem i coded for
And how can i analysis this code and get
Time complexity.
c++ c algorithm primes
and what is TLE ?
– u__
Nov 8 at 9:21
1
@u__ Time Limit Exceeded. And that's the problem that needs to be solved. The task at face value is usually not so difficult as finding an effiecient solution.
– Weather Vane
Nov 8 at 9:22
Typically being able to derive the time complexity for a specific algorithm is very useful to improve it.
– Willem Van Onsem
Nov 8 at 9:23
1
There are plenty of examples on SO of more efficient prime algorithms. For example, why are you checking every even divisor? 2 is the only even prime. That change alone will halve the run time.
– Weather Vane
Nov 8 at 9:24
Please give your question a more descriptive title, so that other users with a similar problem can find it.
– m69
Nov 9 at 3:26
add a comment |
up vote
-4
down vote
favorite
up vote
-4
down vote
favorite
When i submiting this code at hackerearth i am getting TLE.
Any suggestions how can i optimise this
Code.
#include <stdio.h>
#include <stdlib.h>
int checkPrime(int);
int main() {
int a, b,
reminder,sum=0,n;
scanf("%d %d",&a,&b);
while(a <= b) {
n = a;
sum = 0;
reminder = 0;
while (n > 0) {
reminder = n % 10;
sum += reminder;
n = n / 10;
}
if(sum > 1 && checkPrime(sum) == 1 && checkPrime(a) == 1) {
printf("%d ",a);
}
++a;
}
return 0;
}
int checkPrime(int p) {
int i,flag=1;
for(i=2; i <= p / 2; i++){
if(p%i == 0) {
flag = 0;
break;
}
}
return flag;
}
Here is the problem i coded for
And how can i analysis this code and get
Time complexity.
c++ c algorithm primes
When i submiting this code at hackerearth i am getting TLE.
Any suggestions how can i optimise this
Code.
#include <stdio.h>
#include <stdlib.h>
int checkPrime(int);
int main() {
int a, b,
reminder,sum=0,n;
scanf("%d %d",&a,&b);
while(a <= b) {
n = a;
sum = 0;
reminder = 0;
while (n > 0) {
reminder = n % 10;
sum += reminder;
n = n / 10;
}
if(sum > 1 && checkPrime(sum) == 1 && checkPrime(a) == 1) {
printf("%d ",a);
}
++a;
}
return 0;
}
int checkPrime(int p) {
int i,flag=1;
for(i=2; i <= p / 2; i++){
if(p%i == 0) {
flag = 0;
break;
}
}
return flag;
}
Here is the problem i coded for
And how can i analysis this code and get
Time complexity.
c++ c algorithm primes
c++ c algorithm primes
asked Nov 8 at 9:18
md. motailab
186
186
and what is TLE ?
– u__
Nov 8 at 9:21
1
@u__ Time Limit Exceeded. And that's the problem that needs to be solved. The task at face value is usually not so difficult as finding an effiecient solution.
– Weather Vane
Nov 8 at 9:22
Typically being able to derive the time complexity for a specific algorithm is very useful to improve it.
– Willem Van Onsem
Nov 8 at 9:23
1
There are plenty of examples on SO of more efficient prime algorithms. For example, why are you checking every even divisor? 2 is the only even prime. That change alone will halve the run time.
– Weather Vane
Nov 8 at 9:24
Please give your question a more descriptive title, so that other users with a similar problem can find it.
– m69
Nov 9 at 3:26
add a comment |
and what is TLE ?
– u__
Nov 8 at 9:21
1
@u__ Time Limit Exceeded. And that's the problem that needs to be solved. The task at face value is usually not so difficult as finding an effiecient solution.
– Weather Vane
Nov 8 at 9:22
Typically being able to derive the time complexity for a specific algorithm is very useful to improve it.
– Willem Van Onsem
Nov 8 at 9:23
1
There are plenty of examples on SO of more efficient prime algorithms. For example, why are you checking every even divisor? 2 is the only even prime. That change alone will halve the run time.
– Weather Vane
Nov 8 at 9:24
Please give your question a more descriptive title, so that other users with a similar problem can find it.
– m69
Nov 9 at 3:26
and what is TLE ?
– u__
Nov 8 at 9:21
and what is TLE ?
– u__
Nov 8 at 9:21
1
1
@u__ Time Limit Exceeded. And that's the problem that needs to be solved. The task at face value is usually not so difficult as finding an effiecient solution.
– Weather Vane
Nov 8 at 9:22
@u__ Time Limit Exceeded. And that's the problem that needs to be solved. The task at face value is usually not so difficult as finding an effiecient solution.
– Weather Vane
Nov 8 at 9:22
Typically being able to derive the time complexity for a specific algorithm is very useful to improve it.
– Willem Van Onsem
Nov 8 at 9:23
Typically being able to derive the time complexity for a specific algorithm is very useful to improve it.
– Willem Van Onsem
Nov 8 at 9:23
1
1
There are plenty of examples on SO of more efficient prime algorithms. For example, why are you checking every even divisor? 2 is the only even prime. That change alone will halve the run time.
– Weather Vane
Nov 8 at 9:24
There are plenty of examples on SO of more efficient prime algorithms. For example, why are you checking every even divisor? 2 is the only even prime. That change alone will halve the run time.
– Weather Vane
Nov 8 at 9:24
Please give your question a more descriptive title, so that other users with a similar problem can find it.
– m69
Nov 9 at 3:26
Please give your question a more descriptive title, so that other users with a similar problem can find it.
– m69
Nov 9 at 3:26
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Your checkprime
function takes a lot of runtime. It runs for N/2
operations.
You are running this for all the numbers, so you are running for N*N/2
operations, which is too much.
I would suggest that you use a better method of generating the primes. Take a look at the Sieve of Eratosthenes
Even checking all odd numbers (and 2) up tosqrt(N)
would probably do the trick. Or all6n+1
and6n-1
if you want to be fancy.
– Max Langhof
Nov 8 at 9:32
Thanks man get accepted sqrt(n) worked #Max Langhof
– md. motailab
Nov 8 at 9:46
Thanks all for your kindness :)
– md. motailab
Nov 8 at 9:47
add a comment |
up vote
1
down vote
There are some primitive ways like this one for instance, looping through the odd numbers and some more optimizations
int isPrime ( int n )
{
if (n <= 1) return 0; // zero and one are not prime
unsigned int i;
for (i=2; i*i<=n; i++) {
if (n % i == 0) return 0;
}
return 1;
}
Seive
is kind of over-kill, in your case ( if you are not taking in to consideration your memory requirements ) because the range could be very very large 1000000
you might want to use some sort of bitmap to generate the Seive.
here is a very loosely written Idea of how to generate and use Seive.
char *seive;
void generate_seive( int n )
{
seive = calloc( 1, n );
if( !seive )
{
printf("E...");
exit(0);
}
// Generate Seive
for( int i = 2; i < n ; i ++)
{
if( seive[i] == 1 )
continue;
// Essentially mark all primes as 0 and
// non-primes as 1's
seive[i] = 0;
for( int j = i + i ; j < n ; j += i )
seive[j] = 1;
}
}
int main()
{
generate_seive(100);
// Iterating through the generated Seive
// should do
for( int i = 2; i < 100 ; i ++ )
if( seive[i] == 0 )
printf("%dn", i);
return 0;
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Your checkprime
function takes a lot of runtime. It runs for N/2
operations.
You are running this for all the numbers, so you are running for N*N/2
operations, which is too much.
I would suggest that you use a better method of generating the primes. Take a look at the Sieve of Eratosthenes
Even checking all odd numbers (and 2) up tosqrt(N)
would probably do the trick. Or all6n+1
and6n-1
if you want to be fancy.
– Max Langhof
Nov 8 at 9:32
Thanks man get accepted sqrt(n) worked #Max Langhof
– md. motailab
Nov 8 at 9:46
Thanks all for your kindness :)
– md. motailab
Nov 8 at 9:47
add a comment |
up vote
1
down vote
accepted
Your checkprime
function takes a lot of runtime. It runs for N/2
operations.
You are running this for all the numbers, so you are running for N*N/2
operations, which is too much.
I would suggest that you use a better method of generating the primes. Take a look at the Sieve of Eratosthenes
Even checking all odd numbers (and 2) up tosqrt(N)
would probably do the trick. Or all6n+1
and6n-1
if you want to be fancy.
– Max Langhof
Nov 8 at 9:32
Thanks man get accepted sqrt(n) worked #Max Langhof
– md. motailab
Nov 8 at 9:46
Thanks all for your kindness :)
– md. motailab
Nov 8 at 9:47
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Your checkprime
function takes a lot of runtime. It runs for N/2
operations.
You are running this for all the numbers, so you are running for N*N/2
operations, which is too much.
I would suggest that you use a better method of generating the primes. Take a look at the Sieve of Eratosthenes
Your checkprime
function takes a lot of runtime. It runs for N/2
operations.
You are running this for all the numbers, so you are running for N*N/2
operations, which is too much.
I would suggest that you use a better method of generating the primes. Take a look at the Sieve of Eratosthenes
answered Nov 8 at 9:23
Rishikesh Raje
5,0261825
5,0261825
Even checking all odd numbers (and 2) up tosqrt(N)
would probably do the trick. Or all6n+1
and6n-1
if you want to be fancy.
– Max Langhof
Nov 8 at 9:32
Thanks man get accepted sqrt(n) worked #Max Langhof
– md. motailab
Nov 8 at 9:46
Thanks all for your kindness :)
– md. motailab
Nov 8 at 9:47
add a comment |
Even checking all odd numbers (and 2) up tosqrt(N)
would probably do the trick. Or all6n+1
and6n-1
if you want to be fancy.
– Max Langhof
Nov 8 at 9:32
Thanks man get accepted sqrt(n) worked #Max Langhof
– md. motailab
Nov 8 at 9:46
Thanks all for your kindness :)
– md. motailab
Nov 8 at 9:47
Even checking all odd numbers (and 2) up to
sqrt(N)
would probably do the trick. Or all 6n+1
and 6n-1
if you want to be fancy.– Max Langhof
Nov 8 at 9:32
Even checking all odd numbers (and 2) up to
sqrt(N)
would probably do the trick. Or all 6n+1
and 6n-1
if you want to be fancy.– Max Langhof
Nov 8 at 9:32
Thanks man get accepted sqrt(n) worked #Max Langhof
– md. motailab
Nov 8 at 9:46
Thanks man get accepted sqrt(n) worked #Max Langhof
– md. motailab
Nov 8 at 9:46
Thanks all for your kindness :)
– md. motailab
Nov 8 at 9:47
Thanks all for your kindness :)
– md. motailab
Nov 8 at 9:47
add a comment |
up vote
1
down vote
There are some primitive ways like this one for instance, looping through the odd numbers and some more optimizations
int isPrime ( int n )
{
if (n <= 1) return 0; // zero and one are not prime
unsigned int i;
for (i=2; i*i<=n; i++) {
if (n % i == 0) return 0;
}
return 1;
}
Seive
is kind of over-kill, in your case ( if you are not taking in to consideration your memory requirements ) because the range could be very very large 1000000
you might want to use some sort of bitmap to generate the Seive.
here is a very loosely written Idea of how to generate and use Seive.
char *seive;
void generate_seive( int n )
{
seive = calloc( 1, n );
if( !seive )
{
printf("E...");
exit(0);
}
// Generate Seive
for( int i = 2; i < n ; i ++)
{
if( seive[i] == 1 )
continue;
// Essentially mark all primes as 0 and
// non-primes as 1's
seive[i] = 0;
for( int j = i + i ; j < n ; j += i )
seive[j] = 1;
}
}
int main()
{
generate_seive(100);
// Iterating through the generated Seive
// should do
for( int i = 2; i < 100 ; i ++ )
if( seive[i] == 0 )
printf("%dn", i);
return 0;
}
add a comment |
up vote
1
down vote
There are some primitive ways like this one for instance, looping through the odd numbers and some more optimizations
int isPrime ( int n )
{
if (n <= 1) return 0; // zero and one are not prime
unsigned int i;
for (i=2; i*i<=n; i++) {
if (n % i == 0) return 0;
}
return 1;
}
Seive
is kind of over-kill, in your case ( if you are not taking in to consideration your memory requirements ) because the range could be very very large 1000000
you might want to use some sort of bitmap to generate the Seive.
here is a very loosely written Idea of how to generate and use Seive.
char *seive;
void generate_seive( int n )
{
seive = calloc( 1, n );
if( !seive )
{
printf("E...");
exit(0);
}
// Generate Seive
for( int i = 2; i < n ; i ++)
{
if( seive[i] == 1 )
continue;
// Essentially mark all primes as 0 and
// non-primes as 1's
seive[i] = 0;
for( int j = i + i ; j < n ; j += i )
seive[j] = 1;
}
}
int main()
{
generate_seive(100);
// Iterating through the generated Seive
// should do
for( int i = 2; i < 100 ; i ++ )
if( seive[i] == 0 )
printf("%dn", i);
return 0;
}
add a comment |
up vote
1
down vote
up vote
1
down vote
There are some primitive ways like this one for instance, looping through the odd numbers and some more optimizations
int isPrime ( int n )
{
if (n <= 1) return 0; // zero and one are not prime
unsigned int i;
for (i=2; i*i<=n; i++) {
if (n % i == 0) return 0;
}
return 1;
}
Seive
is kind of over-kill, in your case ( if you are not taking in to consideration your memory requirements ) because the range could be very very large 1000000
you might want to use some sort of bitmap to generate the Seive.
here is a very loosely written Idea of how to generate and use Seive.
char *seive;
void generate_seive( int n )
{
seive = calloc( 1, n );
if( !seive )
{
printf("E...");
exit(0);
}
// Generate Seive
for( int i = 2; i < n ; i ++)
{
if( seive[i] == 1 )
continue;
// Essentially mark all primes as 0 and
// non-primes as 1's
seive[i] = 0;
for( int j = i + i ; j < n ; j += i )
seive[j] = 1;
}
}
int main()
{
generate_seive(100);
// Iterating through the generated Seive
// should do
for( int i = 2; i < 100 ; i ++ )
if( seive[i] == 0 )
printf("%dn", i);
return 0;
}
There are some primitive ways like this one for instance, looping through the odd numbers and some more optimizations
int isPrime ( int n )
{
if (n <= 1) return 0; // zero and one are not prime
unsigned int i;
for (i=2; i*i<=n; i++) {
if (n % i == 0) return 0;
}
return 1;
}
Seive
is kind of over-kill, in your case ( if you are not taking in to consideration your memory requirements ) because the range could be very very large 1000000
you might want to use some sort of bitmap to generate the Seive.
here is a very loosely written Idea of how to generate and use Seive.
char *seive;
void generate_seive( int n )
{
seive = calloc( 1, n );
if( !seive )
{
printf("E...");
exit(0);
}
// Generate Seive
for( int i = 2; i < n ; i ++)
{
if( seive[i] == 1 )
continue;
// Essentially mark all primes as 0 and
// non-primes as 1's
seive[i] = 0;
for( int j = i + i ; j < n ; j += i )
seive[j] = 1;
}
}
int main()
{
generate_seive(100);
// Iterating through the generated Seive
// should do
for( int i = 2; i < 100 ; i ++ )
if( seive[i] == 0 )
printf("%dn", i);
return 0;
}
answered Nov 8 at 9:33
u__
2,501828
2,501828
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204676%2fgetting-tle-in-hackerearth%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
and what is TLE ?
– u__
Nov 8 at 9:21
1
@u__ Time Limit Exceeded. And that's the problem that needs to be solved. The task at face value is usually not so difficult as finding an effiecient solution.
– Weather Vane
Nov 8 at 9:22
Typically being able to derive the time complexity for a specific algorithm is very useful to improve it.
– Willem Van Onsem
Nov 8 at 9:23
1
There are plenty of examples on SO of more efficient prime algorithms. For example, why are you checking every even divisor? 2 is the only even prime. That change alone will halve the run time.
– Weather Vane
Nov 8 at 9:24
Please give your question a more descriptive title, so that other users with a similar problem can find it.
– m69
Nov 9 at 3:26