Advertisement
YavorJS

Primes in Given Range - 100%

Sep 22nd, 2016
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4.  
  5. class Primes_in_Given_Range
  6. {
  7. static void Main(string[] args)
  8. {
  9. long firstNum = long.Parse(Console.ReadLine());
  10. long lastNum = long.Parse(Console.ReadLine());
  11. List<long> primes = new List<long>();
  12. for (long num = firstNum; num <= lastNum; num++)
  13. {
  14. bool primeOrNot = isPrime(num);
  15. if (primeOrNot) primes.Add(num);
  16. }
  17. Console.WriteLine(string.Join(", ", primes));
  18. }
  19.  
  20. private static bool isPrime(long n)
  21. {
  22. int boundary = (int)Math.Floor(Math.Sqrt(n));
  23.  
  24. if (n == 0) return false;
  25. if (n == 1) return false;
  26. if (n == 2) return true;
  27.  
  28. for (int i = 2; i <= boundary; ++i)
  29. {
  30. if (n % i == 0) return false;
  31. }
  32. return true;
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement