# Largest number less than N whose each digit is prime number

Given a very large number **N** (1 <= number of digit in N <= 10^{5}). The task is find the largest number X such that X < N and each digit of X is prime number.

Examples:

Input : N = 1000 Output : 777 777 is the largest number less than 1000 which have each digit as prime. Input : N = 11 Output : 7

The idea is to traverse from leftmost digit of the number N to rightmost digit of N. Check if the current digit is prime or not. If it is prime, copy the digit to output number at corresponding digit position. If it is not prime, copy the largest prime number less than current digit.

Now consider if the current digit is ‘0’ or ‘1’. In that case copy ‘7’ to current digit position of output number. Also move to adjacent left digit of current digit and reduce it to largest prime number less than it.

Once we reduce any digit to largest prime number less than the digit, we copy ‘7’ to rest of the right digit in the output number.

Below is C++ implementation of this approach:

`// CPP program to find the largest number smaller ` `// than N whose all digits are prime. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Number is given as string. ` `char` `* PrimeDigitNumber(` `char` `N[], ` `int` `size) ` `{ ` ` ` `char` `* ans = (` `char` `*)` `malloc` `(size * ` `sizeof` `(` `char` `)); ` ` ` `int` `ns = 0; ` ` ` ` ` `// We stop traversing digits, once it become ` ` ` `// smaller than current number. ` ` ` `// For that purpose we use small variable. ` ` ` `int` `small = 0; ` ` ` `int` `i; ` ` ` ` ` `// Array indicating if index i (represents a ` ` ` `// digit) is prime or not. ` ` ` `int` `p[] = { 0, 0, 1, 1, 0, 1, 0, 1, 0, 0 }; ` ` ` ` ` `// Store largest ` ` ` `int` `prevprime[] = { 0, 0, 0, 2, 3, 3, 5, 5, 7, 7 }; ` ` ` ` ` `// If there is only one character, return ` ` ` `// the largest prime less than the number ` ` ` `if` `(size == 1) { ` ` ` `ans[0] = prevprime[N[0] - ` `'0'` `] + ` `'0'` `; ` ` ` ` ` `ans[1] = ` `'\0'` `; ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// If number starts with 1, return number ` ` ` `// consisting of 7 ` ` ` `if` `(N[0] == ` `'1'` `) { ` ` ` `for` `(` `int` `i = 0; i < size - 1; i++) ` ` ` `ans[i] = ` `'7'` `; ` ` ` ` ` `ans[size - 1] = ` `'\0'` `; ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Traversing each digit from right to left ` ` ` `// Continue traversing till the number we ` ` ` `// are forming will become less. ` ` ` `for` `(i = 0; i < size && small == 0; i++) { ` ` ` ` ` `// If digit is prime, copy it simply. ` ` ` `if` `(p[N[i] - ` `'0'` `] == 1) { ` ` ` `ans[ns++] = N[i]; ` ` ` `} ` `else` `{ ` ` ` ` ` `// If not prime, copy the largest prime ` ` ` `// less than current number ` ` ` `if` `(p[N[i] - ` `'0'` `] == 0 && ` ` ` `prevprime[N[i] - ` `'0'` `] != 0) { ` ` ` `ans[ns++] = prevprime[N[i] - ` `'0'` `] + ` `'0'` `; ` ` ` `small = 1; ` ` ` `} ` ` ` ` ` `// If not prime, and there is no largest ` ` ` `// prime less than current prime ` ` ` `else` `if` `(p[N[i] - ` `'0'` `] == 0 && ` ` ` `prevprime[N[i] - ` `'0'` `] == 0) { ` ` ` ` ` `int` `j = i; ` ` ` ` ` `// Make current digit as 7 ` ` ` `// Go left of the digit and make it largest ` ` ` `// prime less than number. Continue do that ` ` ` `// until we found a digit which has some ` ` ` `// largest prime less than it ` ` ` `while` `(j > 0 && p[N[j] - ` `'0'` `] == 0 && ` ` ` `prevprime[N[j] - ` `'0'` `] == 0) { ` ` ` `ans[j] = N[j] = ` `'7'` `; ` ` ` `N[j - 1] = prevprime[N[j - 1] - ` `'0'` `] + ` `'0'` `; ` ` ` `ans[j - 1] = N[j - 1]; ` ` ` `small = 1; ` ` ` `j--; ` ` ` `} ` ` ` ` ` `i = ns; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// If the given number is itself a prime. ` ` ` `if` `(small == 0) { ` ` ` ` ` `// Make last digit as highest prime less ` ` ` `// than given digit. ` ` ` `if` `(prevprime[N[size - 1] - ` `'0'` `] + ` `'0'` `!= ` `'0'` `) ` ` ` `ans[size - 1] = prevprime[N[size - 1] - ` `'0'` `] + ` `'0'` `; ` ` ` ` ` `// If there is no highest prime less than ` ` ` `// current digit. ` ` ` `else` `{ ` ` ` `int` `j = size - 1; ` ` ` `while` `(j > 0 && prevprime[N[j] - ` `'0'` `] == 0) { ` ` ` `ans[j] = N[j] = ` `'7'` `; ` ` ` `N[j - 1] = prevprime[N[j - 1] - ` `'0'` `] + ` `'0'` `; ` ` ` `ans[j - 1] = N[j - 1]; ` ` ` `small = 1; ` ` ` `j--; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Once one digit become less than any digit of input ` ` ` `// replace 7 (largest 1 digit prime) till the end of ` ` ` `// digits of number ` ` ` `for` `(; ns < size; ns++) ` ` ` `ans[ns] = ` `'7'` `; ` ` ` ` ` `ans[ns] = ` `'\0'` `; ` ` ` ` ` `// If number include 0 in the beginning, ignore ` ` ` `// them. Case like 2200 ` ` ` `int` `k = 0; ` ` ` `while` `(ans[k] == ` `'0'` `) ` ` ` `k++; ` ` ` ` ` `return` `ans + k; ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` `char` `N[] = ` `"1000"` `; ` ` ` `int` `size = ` `strlen` `(N); ` ` ` `cout << PrimeDigitNumber(N, size) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

777

This article is contributed by **Anuj Chauhan (anuj0503)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Largest number less than N with digit sum greater than the digit sum of N
- Check if the first and last digit of number N is prime and their sum is less than K
- Largest K digit number divisible by X
- Largest and smallest digit of a number
- Largest even digit number not greater than N
- C++ Program for Largest K digit number divisible by X
- Largest N digit number divisible by given three numbers
- Check if the first and last digit of the smallest number forms a prime
- Java Program for Largest K digit number divisible by X
- Largest number that divides x and is co-prime with y
- Largest number with prime digits
- Largest number in [2, 3, .. n] which is co-prime with numbers in [2, 3, .. m]
- Sum of largest prime factor of each number less than equal to n
- Find largest prime factor of a number
- Sum of largest divisible powers of p (a prime number) in a range