开发者

Does (!(i % j)) mean not modulus of i and j = 0?

开发者 https://www.devze.com 2022-12-09 13:51 出处:网络
int main() { int i,j; for (i=1; i<=25; i++) { for (j=2; j<= i/2; j++) if (!(i%j)) break; if (j>i/2) cout << i << \"\\n\";
int main()
{
 int i,j;

 for (i=1; i<=25; i++)
 {
  for (j=2; j<= i/2; j++)
   if (!(i%j)) break;
  if (j>i/2) cout << i << "\n";
 }
 return 0;
}

This program (not written by me) outputs the prime numbers from 1 to 25, including 1 even though 1 isnt prime.

I am having trouble with thi开发者_如何学JAVAs line: if (!(i%j)) break;

Does this say "not modulus of i and j = 0?


!(i%j) is the same as (i%j)==0, or "i is divisible by j"


The two following lines are essentially identical (as far as logic goes):

if (!(i%j))
if ((i % j) == 0)

The way I'd read the first line to make it clearer is "if there is not a remainder from i/j", ie, i is divisible by j.

0

精彩评论

暂无评论...
验证码 换一张
取 消