![]() In this article, we will reduce the time complexity upto for the above algorithm up to O ( s q r t ( n ) ) O(sqrt(n)) O ( s q r t ( n ) ). Executing a loop from 2 to number n takes around n iterations, so the time complexity to check whether the number is prime will be O ( N ) O(N) O ( N ). If n is divisible by i then the number is composite, or else it is prime. To check whether the given number (say n) is prime or not, we can run a simple for loop from 2 to n - 1 using an iterator i and check whether the number n is divisible at each by i or not. IntroductionĪ number that is divisible by only 1 and itself is called a prime number. In this article, we will discuss how to find all prime numbers between two numbers efficiently. All natural numbers other than 1 and prime numbers are called composite numbers. Except 2 every prime number is odd.Įxamples of prime numbers are 2, 3, 5, 7, 11, 13, etc. In other words, a prime number is divisible only by two numbers: 1 and the number itself. A prime number is a number that has only two factors.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |