fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Test
  5. {
  6. public static List<int> get_primes (int n) {
  7. // is_prime[i] == true, если i -- простое число
  8. bool[] is_prime = new bool[n+1];
  9. for (int i = 2; i <= n; ++i) is_prime[i] = true;
  10. // primes будет содержать все простые числа на отрезке от 1 до n
  11. List<int> primes = new List<int>();
  12. // Алгоритм Решето Эратосфена
  13. for (int i=2; i<=n; ++i)
  14. if (is_prime[i]) {
  15. primes.Add(i);
  16. if (i * i <= n)
  17. for (int j=i*i; j<=n; j+=i)
  18. is_prime[j] = false;
  19. }
  20. return primes;
  21. }
  22.  
  23. public static void Main()
  24. {
  25. List<int> primes = get_primes(20);
  26. Console.WriteLine(string.Join(", ", primes));
  27. }
  28. }
  29.  
Success #stdin #stdout 0.04s 23896KB
stdin
Standard input is empty
stdout
2, 3, 5, 7, 11, 13, 17, 19