P-Smooth Numbers or P-friable Number


A number is p-friable for p-smooth if all of its prime factors are less than or equal to p.

For example, 1620 is a 5-smooth number. Because, the prime factors of 1620 are: 2,3, and 5. As it can be seen, 1620 is also a 7-smooth and 11-smooth number.

Problem Statement

Given two numbers N and P, we have to check if N is a P-friable number or not.

Examples

Input −  N = 50, P = 7
Output −  Yes, 50 is a 7-friable number.

Explanation

50 can be prime factorized as: 5*5*5*5.

Hence, the prime factor of 50 is 5 only.

Since, 5 is smaller than 7, 50 is a 7-friable or 7-smooth number.

Input −  N = 1620, P = 3
Output −  No, 1620 is not a 3-friable number.

Explanation

1620 can be prime factorized as: 2*2*3*3*3*3*5.

Hence, the prime factors of 1620 are 2,3, and 5.

Since, 5 is greater than 3, 1620 is not a 3-friable or 3-smooth number.

Input: N = 30, P = 7
Output: Yes, 30 is a 7-friable number.

Explanation

30 can be prime factorized as: 2*3*5.

Hence, the prime factors are 2,3, and 5.

Since all the numbers are less than 7, 30 is a 7-friable or 7-smooth number.

Approach

We have to prime-factorize the number N and then find the greatest prime factor of N.

To prime factorize the number, we follow the given approach:

  • If the number is divisible by 2, store 2 as the greatest prime factor and divide N by 2.

  • Keep dividing N by 2 till it becomes odd.

  • Now, start a loop from 3 till the square root of N. While i divides N, keep dividing N by i.

  • At the end of the loop, if N is greater than 2, then it is a prime number. Store it as the largest prime factor.

  • Compare the largest prime factor and the input P.

  • If P is greater than or equal to the largest prime factor, then print Yes.

Pseudo Code

main() −

  • Initialize N and P.

  • Function call is_Pfriable(N,P).

  • Print the result.

is_Pfriable(int n, int p) −

  • largest__prime_factor -> -1

  • While n is divisible by 2 −

    • Divide n by 2.

    • largest_prime_factor -> maximum(largest_prime_factor, 2)

  • For i->3 to i->square_root(n):

    • While n is divisible by i:

      • largest_prime_factor -> maximum(largest_prime_factor, i)

      • Divide n by i

  • If n > 2:

    • largest_prime_factor -> maximum(largest_prime_factor, i)

  • If p >= largest_prime_factor:

    • Return True

  • Else:

    • Return False

Example

Below is a C++ program to check if a number is a P-smooth or P-friable number or not.

#include <bits/stdc++.h>
using namespace std;
// Function to check if the number n is a P-smooth or P-friable number or not.
bool is_Pfriable(int n, int p){
   //Variable to store the value of the largest prime factor.
	int largest_prime_factor = -1;
	//While loop to divide N till is becomes indivisible by 2.
	//It will also factorize N by all the multiples of 2.
	while ((n % 2)==0){
	   //Store the maximum value.
		largest_prime_factor = max(largest_prime_factor, 2);
		//Keep dividing by 2.
		n = n/2;
	}
   //For loop till the square root of n, since factors exist in pairs.
   //Checking for all the possible factor of n.
	for (int i = 3; i <= sqrt(n); i += 2){
      //Eliminate all the multiples of i which could be the factors of n. That is, prime factorize n by i.
		while (n % i == 0){
			largest_prime_factor = max(largest_prime_factor,i);
			//Keep dividing by i.
			n = n / i;
		}
	}
   //If n>2, then it is a prime factor, and thus, the largest prime factor of itself.
	if (n > 2){
	   largest_prime_factor = max(largest_prime_factor, n);
	}
	//If p is greater than or equal to the largest prime factor of n, then n is p-friable. Hence, return true.
	if(p>=largest_prime_factor){
	    return true;
	}
	//Return false because n is not p-friable.
	return false;
}
int main(){
   //Initialize input
	int n = 56, p = 5;
	//Function call
	//If the function return true, then n is p-friable.
	if (is_Pfriable(n, p)){
	   cout<<"Yes, "<<n<<" is a "<<p<<" friable number"<<endl;
	}
	else{
	   cout<<"No, "<<n<<" is not a "<<p<<" friable number"<<endl;
	}
	return 0;
}

Output

No, 56 is not a 5 friable number

Analysis

Time Complexity − O(sqrt ( N ) * log N),

The complexity is like this, because of the for loops.

The outer for loop gives the complexity of sqrt(N). And, the inner loop gives the logarithmic complexity. Hence, the final result is the multiplication of them.

Space Complexity − O(1) [Constant]

The space complexity is constant since we haven’t used any extra space.

Conclusion

In this article, we discussed the concept of P-smooth or P-friable number. We solved the problem of whether a given number is a P-friable number or not. We understood the problem statement through some examples, and then saw the approach, pseudo code, and the C++ Program.

Updated on: 16-Aug-2023

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements