Skip to main content
added 347 characters in body
Source Link
xnor
  • 146.6k
  • 26
  • 279
  • 652

Python 3Python 3, 4645 bytes

k=P=1
while k<1e6:P%k and printP%k>0==print(k);P*=k*k;k+=1

Try it online!

By the time the loop reaches testing k, it has iteratively computed the squared-factorial P=(k-1)!^2. If k is prime, then it doesn't appear in the product 1 * 2 * ... * (k-1), so it's not a factor of P. But, if it's composite, all its prime factors are smaller and so in the product. The squaring is only actually needed to stop k=4 from falsely being called prime.

More strongly, it follows from Wilson's Theorem that when k is prime, P%k equals 1. Though we only need that it's nonzero here, it's useful in general that P%k is an indicator variable for whether k is prime.

Thanks to @Sisyphus for 1 byte with P%k>0==print(k) using chained operator short-circuiting in place of P%k and print(k).

Python 3, 46 bytes

k=P=1
while k<1e6:P%k and print(k);P*=k*k;k+=1

By the time the loop reaches testing k, it has iteratively computed the squared-factorial P=(k-1)!^2. If k is prime, then it doesn't appear in the product 1 * 2 * ... * (k-1), so it's not a factor of P. But, if it's composite, all its prime factors are smaller and so in the product. The squaring is only actually needed to stop k=4 from falsely being called prime.

More strongly, it follows from Wilson's Theorem that when k is prime, P%k equals 1. Though we only need that it's nonzero here, it's useful in general that P%k is an indicator variable for whether k is prime.

Python 3, 45 bytes

k=P=1
while k<1e6:P%k>0==print(k);P*=k*k;k+=1

Try it online!

By the time the loop reaches testing k, it has iteratively computed the squared-factorial P=(k-1)!^2. If k is prime, then it doesn't appear in the product 1 * 2 * ... * (k-1), so it's not a factor of P. But, if it's composite, all its prime factors are smaller and so in the product. The squaring is only actually needed to stop k=4 from falsely being called prime.

More strongly, it follows from Wilson's Theorem that when k is prime, P%k equals 1. Though we only need that it's nonzero here, it's useful in general that P%k is an indicator variable for whether k is prime.

Thanks to @Sisyphus for 1 byte with P%k>0==print(k) using chained operator short-circuiting in place of P%k and print(k).

added 1 character in body
Source Link
xnor
  • 146.6k
  • 26
  • 279
  • 652

Python 3, 46 bytes

Python 3, 46 bytes

k=P=1
while k<1e6:P%k and print(k);P*=k*k;k+=1

By the time the loop reaches testing k, it has iteratively computed the squared-factorial P=(k-1)!^2. If k is prime, then it doesn't appear in the product 1 * 2 * ... * (k-1), so it's not a factor of P. But, if it's composite, all its prime factors are smaller and so in the product. The squaring is only actually needed to stop k=4 from falsely being called prime.

More strongly, it follows from Wilson's Theorem that when k is prime, P%k equals 1. Though we only need that it's nonzero here, it's useful in general that P%k is an indicator variable for whether k is prime.

Python 3, 46 bytes

k=P=1
while k<1e6:P%k and print(k);P*=k*k;k+=1

By the time the loop reaches testing k, it has iteratively computed the squared-factorial P=(k-1)!^2. If k is prime, then it doesn't appear in the product 1 * 2 * ... * (k-1), so it's not a factor of P. But, if it's composite, all its prime factors are smaller and so in the product. The squaring is only actually needed to stop k=4 from falsely being called prime.

More strongly, it follows from Wilson's Theorem that when k is prime, P%k equals 1. Though we only need that it's nonzero here, it's useful in general that P%k is an indicator variable for whether k is prime.

Python 3, 46 bytes

k=P=1
while k<1e6:P%k and print(k);P*=k*k;k+=1

By the time the loop reaches testing k, it has iteratively computed the squared-factorial P=(k-1)!^2. If k is prime, then it doesn't appear in the product 1 * 2 * ... * (k-1), so it's not a factor of P. But, if it's composite, all its prime factors are smaller and so in the product. The squaring is only actually needed to stop k=4 from falsely being called prime.

More strongly, it follows from Wilson's Theorem that when k is prime, P%k equals 1. Though we only need that it's nonzero here, it's useful in general that P%k is an indicator variable for whether k is prime.

added 80 characters in body
Source Link
xnor
  • 146.6k
  • 26
  • 279
  • 652

Python 3, 46 bytes

k=P=1
while k<1e6:P%k and print(k);P*=k*k;k+=1

By the time the loop reaches testing k, we'veit has iteratively computed the productsquared-factorial P=(k-1)!^2. If k is prime only if, then it isn'tdoesn't appear in the product 1 * 2 * ... * (k-1), so it's not a factor of this numberP. But, since otherwiseif it's composite, all its prime factors will already have appearedare smaller and so in the product. The squaring is only actually needed to stop k=4 from falsely being called prime.

More strongly, it followfollows from Wilson's Theorem that when k is prime, k!^2P%k equals 1. WhileThough we only need that it's nonzero here, it's useful in general that P%k is an indicator variable for whether k is prime.

Python 3, 46 bytes

k=P=1
while k<1e6:P%k and print(k);P*=k*k;k+=1

By the time the loop reaches testing k, we've computed the product P=(k-1)!^2. k is prime only if it isn't a factor of this number, since otherwise its prime factors will already have appeared in the product. The squaring is only actually needed to stop k=4 from falsely being called prime.

More strongly, it follow from Wilson's Theorem that when k is prime, k!^2 equals 1. While we only need that it's nonzero here, it's useful in general that P%k is an indicator variable for whether k is prime.

Python 3, 46 bytes

k=P=1
while k<1e6:P%k and print(k);P*=k*k;k+=1

By the time the loop reaches testing k, it has iteratively computed the squared-factorial P=(k-1)!^2. If k is prime, then it doesn't appear in the product 1 * 2 * ... * (k-1), so it's not a factor of P. But, if it's composite, all its prime factors are smaller and so in the product. The squaring is only actually needed to stop k=4 from falsely being called prime.

More strongly, it follows from Wilson's Theorem that when k is prime, P%k equals 1. Though we only need that it's nonzero here, it's useful in general that P%k is an indicator variable for whether k is prime.

Cleanup.
Source Link
xnor
  • 146.6k
  • 26
  • 279
  • 652
Loading
Formatted header to ensure it gets read by the leaderboard snippet
Source Link
Alex A.
  • 24.7k
  • 5
  • 38
  • 119
Loading
added 3 characters in body
Source Link
mbomb007
  • 23.5k
  • 7
  • 63
  • 135
Loading
added 452 characters in body
Source Link
xnor
  • 146.6k
  • 26
  • 279
  • 652
Loading
added 158 characters in body
Source Link
xnor
  • 146.6k
  • 26
  • 279
  • 652
Loading
Source Link
xnor
  • 146.6k
  • 26
  • 279
  • 652
Loading