The Wayback Machine - https://web.archive.org/web/20240611162241/https://www.geeksforgeeks.org/p-smooth-numbers-in-given-ranges/
Open In App

P – smooth numbers in given ranges

Last Updated : 05 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

C++




// C++ program to display p-smooth
// number in given range.
// P-smooth numbers' array
#include <bits/stdc++.h>
using namespace std;
 
vector<int> p_smooth;
 
// Returns Maximum Prime
//  Divisor of n
int maxPrimeDivisor(int n)
{
 
    int MPD = -1;
 
    if (n == 1) {
        return 1;
    }
 
    while (n % 2 == 0) {
        MPD = 2;
        n = n / 2;
    }
 
    // math.sqrt(n) + 1
    int size = pow(n, 0.5) + 1;
    for (int odd = 3; odd < size; odd += 2) {
        while (n % odd == 0) {
            // Make sure no multiples
            // of prime, enters here
            MPD = odd;
            n /= odd;
        }
    }
 
    // When n is prime itself
    MPD = max(n, MPD);
 
    return MPD;
}
 
// generates p-smooth numbers.
void generate_p_smooth(int p, int MAX_LIMIT)
{
 
    for (int i = 2; i < MAX_LIMIT + 1; i++) {
        if (maxPrimeDivisor(i) <= p) {
            // Satisfies the condition
            // of p-smooth number
            p_smooth.push_back(i);
        }
    }
}
 
// finds p-smooth number in the
// given [L:R] range.
void find_p_smooth(int L, int R)
{
    if (L <= p_smooth[p_smooth.size() - 1]) {
        // If user input exceeds MAX_LIMIT
        // range, no checking
        for (int i = 0; i < p_smooth.size(); i++) {
            int w = p_smooth[i];
            if (w > R)
                break;
            if (w >= L && w <= R) {
                // Print P-smooth numbers
                // within range : L to R.
                cout << w << " ";
            }
        }
        cout << "\n";
    }
}
 
// Driver Code
int main()
{
    p_smooth.push_back(1);
 
    // p_smooth number : p = 7
    // L <= R
    int p = 7;
    int L = 1;
    int R = 100;
 
    // Maximum possible value of R
    int 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);
}
 
// The code is contributed by phasing17


Java




// Java program to display p-smooth
// number in given range.
// P-smooth numbers' array
 
import java.util.*;
 
class GFG {
    static ArrayList<Integer> p_smooth
        = new ArrayList<Integer>();
 
    // Returns Maximum Prime
    //  Divisor of n
    static int maxPrimeDivisor(int n)
    {
 
        int MPD = -1;
 
        if (n == 1) {
            return 1;
        }
 
        while (n % 2 == 0) {
            MPD = 2;
            n = n / 2;
        }
 
        // math.sqrt(n) + 1
        int size = (int)Math.pow(n, 0.5) + 1;
        for (int odd = 3; odd < size; odd += 2) {
            while (n % odd == 0) {
                // Make sure no multiples
                // of prime, enters here
                MPD = odd;
                n /= odd;
            }
        }
 
        // When n is prime itself
        MPD = Math.max(n, MPD);
 
        return MPD;
    }
 
    // generates p-smooth numbers.
    static void generate_p_smooth(int p, int MAX_LIMIT)
    {
 
        for (int i = 2; i < MAX_LIMIT + 1; i++) {
            if (maxPrimeDivisor(i) <= p) {
                // Satisfies the condition
                // of p-smooth number
                p_smooth.add(i);
            }
        }
    }
 
    // finds p-smooth number in the
    // given [L:R] range.
    static void find_p_smooth(int L, int R)
    {
        if (L <= p_smooth.get(p_smooth.size() - 1)) {
            // If user input exceeds MAX_LIMIT
            // range, no checking
            for (int i = 0; i < p_smooth.size(); i++) {
                int w = p_smooth.get(i);
                if (w > R)
                    break;
                if (w >= L && w <= R) {
                    // Print P-smooth numbers
                    // within range : L to R.
                    System.out.print(w + " ");
                }
            }
            System.out.print("\n");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        p_smooth.add(1);
 
        // p_smooth number : p = 7
        // L <= R
        int p = 7;
        int L = 1;
        int R = 100;
 
        // Maximum possible value of R
        int 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);
    }
}
 
// The code is contributed by phasing17


Python3




# 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)


C#




// C# program to display p-smooth
// number in given range.
// P-smooth numbers' array
using System;
using System.Collections.Generic;
 
class GFG {
    static List<int> p_smooth = new List<int>();
 
    // Returns Maximum Prime
    //  Divisor of n
    static int maxPrimeDivisor(int n)
    {
 
        int MPD = -1;
 
        if (n == 1) {
            return 1;
        }
 
        while (n % 2 == 0) {
            MPD = 2;
            n = n / 2;
        }
 
        // math.sqrt(n) + 1
        int size = (int)Math.Pow(n, 0.5) + 1;
        for (int odd = 3; odd < size; odd += 2) {
            while (n % odd == 0) {
                // Make sure no multiples
                // of prime, enters here
                MPD = odd;
                n /= odd;
            }
        }
 
        // When n is prime itself
        MPD = Math.Max(n, MPD);
 
        return MPD;
    }
 
    // generates p-smooth numbers.
    static void generate_p_smooth(int p, int MAX_LIMIT)
    {
 
        for (int i = 2; i < MAX_LIMIT + 1; i++) {
            if (maxPrimeDivisor(i) <= p) {
                // Satisfies the condition
                // of p-smooth number
                p_smooth.Add(i);
            }
        }
    }
 
    // finds p-smooth number in the
    // given [L:R] range.
    static void find_p_smooth(int L, int R)
    {
        if (L <= p_smooth[p_smooth.Count - 1]) {
            // If user input exceeds MAX_LIMIT
            // range, no checking
            for (int i = 0; i < p_smooth.Count; i++) {
                int w = p_smooth[i];
                if (w > R)
                    break;
                if (w >= L && w <= R) {
                    // Print P-smooth numbers
                    // within range : L to R.
                    Console.Write(w + " ");
                }
            }
            Console.Write("\n");
        }
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        p_smooth.Add(1);
 
        // p_smooth number : p = 7
        // L <= R
        int p = 7;
        int L = 1;
        int R = 100;
 
        // Maximum possible value of R
        int 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);
    }
}
 
// The code is contributed by phasing17


Javascript




// JavaScript program to display p-smooth
// number in given range.
// P-smooth numbers' array
let p_smooth = [1];
 
// Returns Maximum Prime
//  Divisor of n
function maxPrimeDivisor(n){
     
    let MPD = -1;
     
    if (n == 1){
        return 1;
    }
             
    while(n % 2 == 0){
        MPD = 2;
        n = Math.floor(n/2);       
    }
 
     
    // math.sqrt(n) + 1
    let size = Math.floor(n ** 0.5) + 1;
    for(let odd = 3; odd < size; odd += 2){
        while(n % odd == 0){       
            // Make sure no multiples
            // of prime, enters here
            MPD = odd;
            n = Math.floor(n/odd);             
        }
     
    }
 
    // When n is prime itself
    MPD = Math.max(n, MPD);
     
    return MPD;
}
 
 
// generates p-smooth numbers.
function generate_p_smooth(p, MAX_LIMIT){
 
    for(let i = 2; i < MAX_LIMIT+1; i++){
        if(maxPrimeDivisor(i) <= p){
            //Satisfies the condition
            // of p-smooth number
            p_smooth.push(i);              
        } 
    }
}
     
 
// finds p-smooth number in the
// given [L:R] range.
function find_p_smooth(L, R){
    if(L <= p_smooth[p_smooth.length-1]){       
        // If user input exceeds MAX_LIMIT
        // range, no checking
        for(let i = 0; i < p_smooth.length; i++){
            let w = p_smooth[i];
            if (w > R) break;
            if (w >= L && w <= R){           
                // Print P-smooth numbers
                // within range : L to R.
                document.write(w + " ");              
            }
 
        }
        document.write("\n");
    }
}
     
 
         
// p_smooth number : p = 7
// L <= R
let p = 7;
let L = 1;
let R = 100;
 
// Maximum possible value of R
let 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)
 
// The code is contributed by Gautam goel (gautamgoel962)


Output: 
1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100


Similar Reads

Count of ways to select exactly K non-disjoint ranges from given N ranges
Given two arrays L[] and R[] of size N, and an integer K, the task is to find the number of ways to select exact K disjoint ranges formed by taking elements present at the same index from the array L[] and R[]. Examples: Input: N = 7, K = 3, L[] = {1, 3, 4, 6, 1, 5, 8}, R[] = {7, 8, 5, 7, 3, 10, 9}Output: 9Explanation: The possible ways of selectin
10 min read
Maximum length intersection of all K ranges among all given ranges
Given an array arr[] consisting of N ranges of the form [L, R], the task is to select K ranges such that the intersection length of the K ranges is maximum. Examples: Input: arr[] = {{5, 15}, {1, 10}, {14, 50}, {30, 70}, {99, 100}}, K = 2Output: 21Explanation: Selecting ranges (14, 50) and (30, 70) would give the maximum answer. Therefore, the maxi
7 min read
Choose points from two ranges such that no point lies in both the ranges
Given two segments [L1, R1] and [L2, R2], the task is to choose two elements x and y from both the ranges (one from range one and other from range two) such that no element belongs to both the ranges i.e. x belongs to first range and y belongs to second range. If no such element exists then print -1 instead. Examples: Input: L1 = 1, R1 = 6, L2 = 3,
4 min read
Check if a pair of integers from two ranges exists such that their Bitwise XOR exceeds both the ranges
Given two integers A and B, the task is to check if there exists two integers P and Q over the range [1, A] and [1, B] respectively such that Bitwise XOR of P and Q is greater than A and B. If found to be true, then print "Yes". Otherwise, print "No". Examples: Input: X = 2, Y = 2 Output: YesExplanation:By choosing the value of P and Q as 1 and 2 r
9 min read
P-Smooth Numbers or P-friable Number
A P-smooth number or P-friable number is an integer whose largest prime factor is less than or equal to P. Given N and P, we need to write a program to check whether it is P-friable or not. Examples: Input : N = 24 , P = 7 Output : YES Explanation : The prime divisors of 24 are 2 and 3 only. Hence its largest prime factor is 3 which is less than or
8 min read
Best 5 Code Editors for Smooth Coding Experience
A code editor is a software used for creating and developing projects. Code editors provide an efficient way to write, debug, and modify source code, develop and handle large scale projects and often come with added functionalities which provide great aid to developers. Unlike IDE's(Integrated Development Environment) are small in size and provide
2 min read
Create Scatter Plot with smooth Line using Python
A curve can be smoothened to reach a well approximated idea of the visualization. In this article, we will be plotting a scatter plot with the smooth line with the help of the SciPy library. To plot a smooth line scatter plot we use the following function: scipy.interpolate.make_interp_spline() from the SciPy library computes the coefficients of in
2 min read
Introduction to Smooth Sort
Smooth sort is a sorting algorithm that was introduced by Edsger Dijkstra. It is another version of heapsort that is designed to minimize the number of comparisons performed during the sort. Like heapsort, smooth sort sorts an array by building a heap and repeatedly extracting the maximum element. However, smooth sort uses a different data structur
13 min read
Queries for maximum difference between prime numbers in given ranges
Given n queries of the form range [L, R]. The task is to find the maximum difference between two prime numbers in the range for each query. If there are no prime in the range then print 0. All ranges are below 100005. Examples: Input : Q = 3 query1 = [2, 5] query2 = [2, 2] query3 = [24, 28] Output : 3 0 0 In first query, 2 and 5 are prime number in
9 min read
Count of numbers appearing in the given ranges at-least K times
Given N ranges and a number K, the task is to find the total count of numbers that appear at least K times in the given ranges. Examples: Input: N = 3, K = 2 Range 1: [91, 94] Range 2: [92, 97] Range 3: [97, 99] Output : 4Explanation: Ranges are 91 to 94, 92 to 97, 97 to 99 and the numbers that occurred atleast 2 times are 92, 93, 94, 97. Input : N
12 min read
Article Tags :
Practice Tags :