Difficulty: Medium, Asked-in: Amazon, VMWare
Given a positive integer n, write a program to check if the number is prime or not.
In many problems, we need to check if a given number is prime or we need to find prime numbers in a range. In this blog, we will learn how to check prime numbers optimally.
One basic idea would be to iterate from 2 to n - 1 and check if any n is divisible by any number. If yes, then we conclude that the given number is not prime. The time complexity of this approach is O(n), as we will iterate through n-2 numbers at max.
Now the critical question is: Can we try to optimize this approach: Is it necessary to iterate through all the n-2 numbers? For example, any integer n can be written as n = a * b, where a and b are equal if n is a perfect square and a = sqrt(n). So if we know one factor a, the other factor will be n/a.
We claim that at least one of a or b cannot be greater than sqrt(n). As if both are greater than sqrt(n), a*b > n, a contradiction. So from this argument, we conclude that it is enough for us to check the divisibility of numbers from 2 to sqrt(n), as if there is a number between 2 to n-1, which divides n, then we are sure that either it will be less than sqrt(n) or it will have another factor, n/number, which is less than or equal to sqrt(n).
The time complexity of this approach is O(sqrt(n)), as we will iterate through sqrt(n) numbers at max. Space complexity is O(1) because constant extra space is used to store variables.
Here are two important conjectures related to prime numbers.
All even natural numbers(> 2) can be represented as the sum of two prime numbers. 2 is the exceptional case here. The mathematician Christian Goldbach first proposed this conjecture in a letter to the mathematician Leonhard Euler in 1742. It is one of the best-known unsolved problems in number theory.
Lemoine’s conjecture is also an unsolved problem in number theory. It has puzzled mathematicians for the last 125 years. Lemoine’s conjecture is as follows: an odd integer n (where n > 5) can be represented in the form of (odd prime + even semiprime).
In mathematical terms, n = p + 2q always has a solution in primes p and q (not necessarily distinct) for n > 5. For example, we can write 47 in the form of 13 + 2 × 17 or 37 + 2 × 5 or 41 + 2 × 3 or 43 + 2 × 2.
In the coming future, we will write a separate blog on the various properties of prime numbers. Enjoy learning, enjoy mathematics, enjoy algorithms!
There are 3 doors behind which are two goats and a car. You pick door 1 hoping for the car but don’t open it right away. Monty Hall, the game show host who knows what's behind the doors, opens door 3, which has a goat. Here's the game: do you want to pick door No. 2? Is it to your advantage to switch your choice?
You’ve got someone working for you for seven days and a gold bar to pay him. The gold bar is segmented into seven connected pieces. You must give them a piece of gold at the end of every day. What is the fewest number of cuts to the bar of gold that will allow you to pay him 1/7th each day?