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.

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

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:

>c is generally slow compared to other programming language.
>Just out of curiosity, what languages should be faster?
Assembler, fortran?
Look at these wonderful benchmarks: http://shootout.alioth.debian.org/
Slow C... ;););) Hot Ice...

commented: cool link. C kicks ass! +10

dude try running each loop only upto square root of the largest number. definitely helps

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 .

FILE READING i dont know how to speed up. but running the loop only upto the square root will make it faster

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.