c - how to make loop in this case? -
i need compute first 100 prime numbers, in output got "9" , other in numbers....................... want compute first 100 prime numbers
{ bool prime; int start, new, kor,k, i,gg; start=1; k=1 ; gg=0; { if (start < 2) {new = 2;} if (start == 2) {new = 3;} if (start > 2) { if ((new % 2) == 0) new--; { prime = true; kor=sqrt(new); new+=2; (i=3;prime&& (i<=kor); i+=2) { if (new % == 0) prime=false;} } while (!prime) ; } gg++; printf("%d->%d\n",gg, new); k++; start++; continue; } while (k<101); }
with
if (start < 2) {new = 2;} if (start == 2) {new = 3;}
you have special cases first , second numbers. next time round do...while
loop skip loop because kor
1, thereby printing 5. didn't check, perhaps got lucky. smells don't check far enough.
next time, after
kor=sqrt(new1); new1+=2;
kor
2, again don't loop, , print 7. next time have same situation. kor still 2 9.
i think if switch new+=2
before kor=sqrt(1);
work. once in part, don't need check if even, since add 2 odd number. btw why continue
last thing in loop? might better (i took liberty of putting in function):
void find_primes() { bool prime; int start, new, kor,k, i,gg; start=1; k=1 ;gg=0; { if (start < 2) {new = 2;} if (start == 2) {new = 3;} if (start > 2) { { prime = true; new+=2; kor=sqrt(new); (i=3;prime&& (i<=kor); i+=2) { if (new % == 0) prime=false; } } while (!prime) ; } gg++; printf("%d->%d\n",gg, new); k++; start++; } while (k<101); }
Comments
Post a Comment