Given multiple ranges **[L, R]** and a prime number p, we need to find all **P-smooth numbers** in given individual ranges.

What is P – smooth number?

An integer is P – smooth number if the largest Prime factor of that number <= p. 1 is considered (by OEIS) as P – smooth number for any possible value of P because it does not have any prime factor.

Examples:

Input : p = 7 ranges[] = {[1, 17], [10, 25]} Output : For first range : 1 2 3 4 5 6 7 8 9 12 14 15 16 For second range : 15 16 18 20 21 24 25 Explanation : Largest prime factors of numbers printed above are less than or equal to 7.

Suppose, we are checking 7 – smooth numbers.

1. Consider an integer 56. Here, 56 = 2 * 2 * 2 * 7.

So, 56 has two prime factors (2 and 7) which are <=7. So, 56 is 7-smooth number.

2. Consider another integer 66. Here, 66 = 2 * 3 * 11.

66 has three prime factors (2, 3 and 11). Where 11>7. So 66 is not 7-smooth number.

**Brute – Force Approach: **Let P and range [L, R] is given. Here L <= R. Create a loop and check for all numbers in inclusive range [L : R]. If that number has largest prime factor <= p. Then print that number (i.e. P-smooth number). Calculate its Largest Prime Factor / Divisor, using **maxPrimeDivisor(n)** function.

**Efficient Approach: ** The idea is to pre-compute p-smooth numbers for maximum value of all ranges. Once we have pre-computed, we can quickly print for all ranges one by one.

`# Python program to display p-smooth ` `# number in given range. ` `# P-smooth numbers' array ` `p_smooth ` `=` `[` `1` `] ` ` ` `def` `maxPrimeDivisor(n): ` ` ` ` ` `# Returns Maximum Prime ` ` ` `# Divisor of n ` ` ` `MPD ` `=` `-` `1` ` ` ` ` `if` `n ` `=` `=` `1` `: ` ` ` `return` `1` ` ` ` ` `while` `n ` `%` `2` `=` `=` `0` `: ` ` ` `MPD ` `=` `2` ` ` `n ` `=` `n ` `/` `/` `2` ` ` ` ` `# math.sqrt(n) + 1 ` ` ` `size ` `=` `int` `(n ` `*` `*` `0.5` `) ` `+` `1` ` ` `for` `odd ` `in` `range` `( ` `3` `, size, ` `2` `): ` ` ` `while` `n ` `%` `odd ` `=` `=` `0` `: ` ` ` ` ` `# Make sure no multiples ` ` ` `# of prime, enters here ` ` ` `MPD ` `=` `odd ` ` ` `n ` `=` `n ` `/` `/` `odd ` ` ` ` ` `# When n is prime itself ` ` ` `MPD ` `=` `max` `(n, MPD) ` ` ` ` ` `return` `MPD ` ` ` ` ` `def` `generate_p_smooth(p, MAX_LIMIT): ` ` ` ` ` `# generates p-smooth numbers. ` ` ` `global` `p_smooth ` ` ` ` ` `for` `i ` `in` `range` `(` `2` `, MAX_LIMIT ` `+` `1` `): ` ` ` `if` `maxPrimeDivisor(i) <` `=` `p: ` ` ` ` ` `# Satisfies the condition ` ` ` `# of p-smooth number ` ` ` `p_smooth.append(i) ` ` ` ` ` `def` `find_p_smooth(L, R): ` ` ` ` ` `# finds p-smooth number in the ` ` ` `# given [L:R] range. ` ` ` `global` `p_smooth ` ` ` `if` `L <` `=` `p_smooth[` `-` `1` `]: ` ` ` ` ` `# If user input exceeds MAX_LIMIT ` ` ` `# range, no checking ` ` ` `for` `w ` `in` `p_smooth : ` ` ` `if` `w > R : ` `break` ` ` `if` `w >` `=` `L ` `and` `w <` `=` `R : ` ` ` ` ` `# Print P-smooth numbers ` ` ` `# within range : L to R. ` ` ` `print` `(w, end ` `=` `" "` `) ` ` ` ` ` `print` `() ` ` ` `# p_smooth number : p = 7 ` `# L <= R ` `p ` `=` `7` `L, R ` `=` `1` `, ` `100` ` ` `# Maximum possible value of R ` `MAX_LIMIT ` `=` `1000` ` ` `# generate the p-smooth numbers ` `generate_p_smooth(p, MAX_LIMIT) ` ` ` `# Find an print the p-smooth numbers ` `find_p_smooth(L, R) ` |

*chevron_right*

*filter_none*

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Choose points from two ranges such that no point lies in both the ranges
- P-Smooth Numbers or P-friable Number
- Queries for maximum difference between prime numbers in given ranges
- Minimum change in given value so that it lies in all given Ranges
- Find if it is possible to get a ratio from given ranges of costs and quantities
- Queries to minimize sum added to given ranges in an array to make their Bitwise AND non-zero
- Count distinct median possible for an Array using given ranges of elements
- Sum of all possible triplet products from given ranges
- Print matrix after applying increment operations in M ranges
- Count Primes in Ranges
- Number of intersections between two ranges
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Check if a given pair of Numbers are Betrothed numbers or not
- Count of numbers upto M divisible by given Prime Numbers
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count numbers formed by given two digit with sum having given digits
- Sum of two numbers if the original ratio and new ratio obtained by adding a given number to each number is given
- Parity of the given mathematical expression using given N numbers
- Count numbers from a given range that contains a given number as the suffix
- Count numbers which can be constructed using two numbers

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.