P – smooth numbers in given ranges
Last Updated :
05 Aug, 2022
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++
#include <bits/stdc++.h>
using namespace std;
vector<int> p_smooth;
int maxPrimeDivisor(int n)
{
int MPD = -1;
if (n == 1) {
return 1;
}
while (n % 2 == 0) {
MPD = 2;
n = n / 2;
}
int size = pow(n, 0.5) + 1;
for (int odd = 3; odd < size; odd += 2) {
while (n % odd == 0) {
MPD = odd;
n /= odd;
}
}
MPD = max(n, MPD);
return MPD;
}
void generate_p_smooth(int p, int MAX_LIMIT)
{
for (int i = 2; i < MAX_LIMIT + 1; i++) {
if (maxPrimeDivisor(i) <= p) {
p_smooth.push_back(i);
}
}
}
void find_p_smooth(int L, int R)
{
if (L <= p_smooth[p_smooth.size() - 1]) {
for (int i = 0; i < p_smooth.size(); i++) {
int w = p_smooth[i];
if (w > R)
break;
if (w >= L && w <= R) {
cout << w << " ";
}
}
cout << "\n";
}
}
int main()
{
p_smooth.push_back(1);
int p = 7;
int L = 1;
int R = 100;
int MAX_LIMIT = 1000;
generate_p_smooth(p, MAX_LIMIT);
find_p_smooth(L, R);
}
|
Java
import java.util.*;
class GFG {
static ArrayList<Integer> p_smooth
= new ArrayList<Integer>();
static int maxPrimeDivisor(int n)
{
int MPD = -1;
if (n == 1) {
return 1;
}
while (n % 2 == 0) {
MPD = 2;
n = n / 2;
}
int size = (int)Math.pow(n, 0.5) + 1;
for (int odd = 3; odd < size; odd += 2) {
while (n % odd == 0) {
MPD = odd;
n /= odd;
}
}
MPD = Math.max(n, MPD);
return MPD;
}
static void generate_p_smooth(int p, int MAX_LIMIT)
{
for (int i = 2; i < MAX_LIMIT + 1; i++) {
if (maxPrimeDivisor(i) <= p) {
p_smooth.add(i);
}
}
}
static void find_p_smooth(int L, int R)
{
if (L <= p_smooth.get(p_smooth.size() - 1)) {
for (int i = 0; i < p_smooth.size(); i++) {
int w = p_smooth.get(i);
if (w > R)
break;
if (w >= L && w <= R) {
System.out.print(w + " ");
}
}
System.out.print("\n");
}
}
public static void main(String[] args)
{
p_smooth.add(1);
int p = 7;
int L = 1;
int R = 100;
int MAX_LIMIT = 1000;
generate_p_smooth(p, MAX_LIMIT);
find_p_smooth(L, R);
}
}
|
Python3
p_smooth = [1]
def maxPrimeDivisor(n):
MPD = -1
if n == 1 :
return 1
while n % 2 == 0:
MPD = 2
n = n // 2
size = int(n ** 0.5) + 1
for odd in range( 3, size, 2 ):
while n % odd == 0:
MPD = odd
n = n // odd
MPD = max (n, MPD)
return MPD
def generate_p_smooth(p, MAX_LIMIT):
global p_smooth
for i in range(2, MAX_LIMIT + 1):
if maxPrimeDivisor(i) <= p:
p_smooth.append(i)
def find_p_smooth(L, R):
global p_smooth
if L <= p_smooth[-1]:
for w in p_smooth :
if w > R : break
if w >= L and w <= R :
print(w, end =" ")
print()
p = 7
L, R = 1, 100
MAX_LIMIT = 1000
generate_p_smooth(p, MAX_LIMIT)
find_p_smooth(L, R)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static List<int> p_smooth = new List<int>();
static int maxPrimeDivisor(int n)
{
int MPD = -1;
if (n == 1) {
return 1;
}
while (n % 2 == 0) {
MPD = 2;
n = n / 2;
}
int size = (int)Math.Pow(n, 0.5) + 1;
for (int odd = 3; odd < size; odd += 2) {
while (n % odd == 0) {
MPD = odd;
n /= odd;
}
}
MPD = Math.Max(n, MPD);
return MPD;
}
static void generate_p_smooth(int p, int MAX_LIMIT)
{
for (int i = 2; i < MAX_LIMIT + 1; i++) {
if (maxPrimeDivisor(i) <= p) {
p_smooth.Add(i);
}
}
}
static void find_p_smooth(int L, int R)
{
if (L <= p_smooth[p_smooth.Count - 1]) {
for (int i = 0; i < p_smooth.Count; i++) {
int w = p_smooth[i];
if (w > R)
break;
if (w >= L && w <= R) {
Console.Write(w + " ");
}
}
Console.Write("\n");
}
}
public static void Main(string[] args)
{
p_smooth.Add(1);
int p = 7;
int L = 1;
int R = 100;
int MAX_LIMIT = 1000;
generate_p_smooth(p, MAX_LIMIT);
find_p_smooth(L, R);
}
}
|
Javascript
let p_smooth = [1];
function maxPrimeDivisor(n){
let MPD = -1;
if (n == 1){
return 1;
}
while(n % 2 == 0){
MPD = 2;
n = Math.floor(n/2);
}
let size = Math.floor(n ** 0.5) + 1;
for(let odd = 3; odd < size; odd += 2){
while(n % odd == 0){
MPD = odd;
n = Math.floor(n/odd);
}
}
MPD = Math.max(n, MPD);
return MPD;
}
function generate_p_smooth(p, MAX_LIMIT){
for(let i = 2; i < MAX_LIMIT+1; i++){
if(maxPrimeDivisor(i) <= p){
p_smooth.push(i);
}
}
}
function find_p_smooth(L, R){
if(L <= p_smooth[p_smooth.length-1]){
for(let i = 0; i < p_smooth.length; i++){
let w = p_smooth[i];
if (w > R) break;
if (w >= L && w <= R){
document.write(w + " ");
}
}
document.write("\n");
}
}
let p = 7;
let L = 1;
let R = 100;
let MAX_LIMIT = 1000
generate_p_smooth(p, MAX_LIMIT)
find_p_smooth(L, R)
|
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
Please Login to comment...