# P – smooth numbers in given ranges

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*

## Recommended Posts:

- P-Smooth Numbers or P-friable Number
- Choose points from two ranges such that no point lies in both the ranges
- Queries for maximum difference between prime numbers in given ranges
- Count Primes in Ranges
- Number of intersections between two ranges
- Find if it is possible to get a ratio from given ranges of costs and quantities
- Print matrix after applying increment operations in M ranges
- Numbers less than N which are product of exactly two distinct prime numbers
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Count numbers which are divisible by all the numbers from 2 to 10
- Count numbers which can be constructed using two numbers
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime

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.