Given a number N, the task is to find the largest number less than or equal to the given number N such that on rearranging its digits it can become prime.
Input : N = 99 Output : 98 Explanation : We can rearrange the digits of 98 to 89 and 89 is a prime number. Input : N = 84896 Output : 84896 Explanation : We can rearrange the digits of 84896 to 46889 which is a prime number.
Below is the algorithm to find such a largest number num <= N such that digits of num can be rearranged to get a prime number:
Preprocessing Step: Generate a list of all the prime numbers less than or equal to given number N. This can be done efficiently using the sieve of Eratosthenes.
Main Steps: The main idea is to check all numbers from N to 1, if any of the number can be reshuffled to form a prime. The first such number found will be the answer.
To do this, run a loop from N to 1 and for every number:
- Extract the digits of the given number and store it in a vector.
- Sort this vector to get the smallest number which can be formed using these digits.
- For each permutation of this vector, we would form a number and check whether the formed number is prime or not. Here we make use of the Preprocessing step.
- If it is prime then we stop the loop and this is our answer.
Below is the implementation of the above approach:
# Python 3 Program to find a number less than
# or equal to N such that rearranging
# the digits gets a prime number
from math import sqrt
# Generate the lexicographically
# next permutation inplace.
# Return false if there is no next permutation.
# Find the largest index i such that
# a[i] < a[i + 1]. If no such index exists, # the permutation is the last permutation for i in reversed(range(len(a) - 1)): if a[i] < a[i + 1]: break # found else: # no break: not found return False # no next permutation # Find the largest index j greater than i # such that a[i] < a[j] j = next(j for j in reversed(range(i + 1, len(a))) if a[i] < a[j]) # Swap the value of a[i] with that of a[j] a[i], a[j] = a[j], a[i] # Reverse sequence from a[i + 1] up to and # including the final element a[n] a[i + 1:] = reversed(a[i + 1:]) return True # Preprocessed vector to store primes prime = [True for i in range(1000001)] # Function to generate primes using # sieve of eratosthenese def sieve(): # Applying sieve of Eratosthenes prime = False prime = False for i in range(2,int(sqrt(1000000)) + 1, 1): if (prime[i]): for j in range(i * i, 1000001, i): prime[j] = False # Function to find a number less than # or equal to N such that rearranging # the digits gets a prime number def findNumber(n): v =  flag = False # Run loop from n to 1 num = n while(num >= 1):
x = num
# Extracting the digits
while (x != 0):
v.append(x % 10)
x = int(x / 10)
# Sorting the vector to make smallest
# number using digits
v.sort(reverse = False)
# Check all permutation of current number
# for primality
w = 0
# Traverse vector to for number
for u in v:
w = w * 10 + u
# If prime exists
flag = True
# generating next permutation of vector
if (next_permutation(v) == False):
num -= 1
# Required number
# Driver Code
if __name__ == ‘__main__’:
n = 99
n = 84896
# This code is contributed by
- Largest number not greater than N all the digits of which are odd
- Largest number with prime digits
- Smallest number by rearranging digits of a given number
- Kth prime number greater than N
- Find the Largest number with given number of digits and sum of digits
- Largest even digit number not greater than N
- Find next greater number with same set of digits
- Next greater Number than N with the same quantity of digits A and B
- Smallest Special Prime which is greater than or equal to a given number
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Largest number in [2, 3, .. n] which is co-prime with numbers in [2, 3, .. m]
- Largest number that divides x and is co-prime with y
- Recursive sum of digits of a number is prime or not
- Largest number with the given set of N digits that is divisible by 2, 3 and 5
- Find largest prime factor of a number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : SURENDRA_GANGWAR