Find the smallest twins in given range - GeeksforGeeks
Find the smallest twins in given range Given a range [low..high], print the smallest twin numbers in given range (low and high inclusive). Two numbers are twins if they are primes and there difference is 2. Example: Input: low = 10, high = 100 Output: Smallest twins in given range: (11, 13) Both 11 and 13 are prime numbers and difference between them is two, therefore twins. And these are the smallest twins in [10..100] Input: low = 50, high = 100 Output: Smallest twins in given range: (59, 61) We strongly recommend you to minimize your browser and try this yourself first. A Simple Solution is to start to start from low and for every number x check if x and x + 2 are primes are not. Here x varies from low to high-2. 1) Create a boolean array "mark[0..high]" and initialize all entries in it as false. A value in mark[i] will finally be true if i is not a prime number, else false. 2) Initialize previous prime as 2. Previous prime is going to store first number of twins.Read full article from Find the smallest twins in given range - GeeksforGeeks
No comments:
Post a Comment