i need to make a program that finds all primes between some numbers
the biggest number is 1000000000.

the code that i have works fine, the only problem is that what i thought that would solve this, is too slow

#include <stdio.h>
int main(){
    FILE *fin=fopen("in.txt","r");
    int t,n1[10],n2[10],i,j,ex,k;
    fscanf(fin,"%d\n",&t);
    for (i=1;i<=t;i++){
         fscanf(fin,"%d %d\n",&n1[i],&n2[i]);
                  }
                  fclose(fin);
                  FILE *fout=fopen("out.txt","w");
for (k=1;k<=t;k++){
    for (i=n1[k];i<=n2[k];i++){
          ex=0;
       for (j=1;j<=i;j++){
           if (i%j==0){
                       ex=ex+1;
                       }
                }
                if (ex==2){
                            fprintf(fout,"%d\n",i);
                           }
                }
     fprintf(fout,"\n");
}
fclose(fout);
    return 0;
}

i want to make it faster, like, 5 secs to find all primes until number 1000000000.

cant think anything right now, well imean a better algorithm, so would appreciate any help provided.

thanks

c is generally slow compared to other programming language.
Gone through your coding and nothing seems wrong.

However instead of reading from the text file and providing input, you can simply input directly which will decrease the time of execution.
Except that nothing is coming in my mind right now.

Comments
lmfao, Troll.
You're just so plain wrong !!!
You're just another annoying sig-link troll posting drivel all over the place just to spam your sig url
wrong.

c is generally slow compared to other programming language.
Gone through your coding and nothing seems wrong.

Just out of curiosity, what languages should be faster?
Assembler, fortran?

Good programmers are capable to write more effective programs practically in any programming languages than bad programmers can do it in machine codes ;)

c is generally slow compared to other programming language.

wrong. C is generally faster compared to other languages. any compiled language is inherently faster than an interpreted language such as Visual Basic or Java.

nothing is coming in my mind right now.

:icon_rolleyes:

i need to make a program that finds all primes between some numbers
the biggest number is 1000000000.

the code that i have works fine, the only problem is that what i thought that would solve this, is too slow

#include <stdio.h>
int main(){
    FILE *fin=fopen("in.txt","r");
    int t,n1[10],n2[10],i,j,ex,k;
    fscanf(fin,"%d\n",&t);
    for (i=1;i<=t;i++){
         fscanf(fin,"%d %d\n",&n1[i],&n2[i]);
                  }
                  fclose(fin);
                  FILE *fout=fopen("out.txt","w");
for (k=1;k<=t;k++){
    for (i=n1[k];i<=n2[k];i++){
          ex=0;
       for (j=1;j<=i;j++){
           if (i%j==0){
                       ex=ex+1;
                       }
                }
                if (ex==2){
                            fprintf(fout,"%d\n",i);
                           }
                }
     fprintf(fout,"\n");
}
fclose(fout);
    return 0;
}

i want to make it faster, like, 5 secs to find all primes until number 1000000000.

cant think anything right now, well imean a better algorithm, so would appreciate any help provided.

thanks

Probably, the file reading eats up some time. Otherwise, To find a better algorithm, I suggest you search only till the squae root of the number .

This article has been dead for over six months. Start a new discussion instead.