# 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 equal to 7, it is P-friable.

Input : N = 22   ,  P = 5
Output : NO
Explanation : The prime divisors are 11 and 2, hence 11>5,
so it is not a P-friable number.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The approach will be to prime factorize the number and store the maximum of all the prime factors. We first divide the number by 2 if it is divisible, then we iterate from 3 to Sqrt(n) to get the number of times a prime number divides a particular number which reduces every time by n/i and store the prime factor i if its divides N. We divide our number n (by prime factors) by its corresponding smallest prime factor till n becomes 1. And if at the end n > 2, it means its a prime number, so we store that as a prime factor as well. At the end the largest factor is compared with p to check if it is p-smooth number or not.

## CPP

 `// CPP program to check if a number is ` `// a p-smooth number or not ` `#include ` `#include ` `using` `namespace` `std; ` ` `  `// function to check if number n ` `// is a P-smooth number or not ` `bool` `check(``int` `n, ``int` `p)  ` `{ ` `    ``int` `maximum = -1; ` `     `  `    ``// prime factorise it by 2 ` `    ``while` `(!(n % 2))  ` `    ``{ ` `        ``// if the number is divisible by 2 ` `        ``maximum = max(maximum, 2); ` `        ``n = n/2; ` `    ``} ` ` `  `    ``// check for all the possible numbers  ` `    ``// that can divide it ` `    ``for` `(``int` `i = 3; i <= ``sqrt``(n); i += 2) ` `    ``{ ` `        ``// prime factorize it by i ` `        ``while` `(n % i == 0)  ` `        ``{    ` `            ``// stores the maximum if maximum  ` `            ``// and i, if i divides the number ` `            ``maximum = max(maximum,i);  ` `            ``n = n / i; ` `        ``} ` `    ``} ` ` `  `    ``// if n at the end is a prime number,  ` `    ``// then it a divisor itself ` `    ``if` `(n > 2) ` `        ``maximum = max(maximum, n); ` `     `  `    ``return` `(maximum <= p); ` `} ` ` `  `// Driver program to test above function ` `int` `main()  ` `{ ` `    ``int` `n = 24, p = 7; ` `     `  `    ``if` `(check(n, p)) ` `        ``cout << ``"yes"``; ` `    ``else` `        ``cout << ``"no"``; ` `     `  `    ``return` `0; ` `} `

## Java

 `// Java program to check if a number is ` `// a p-smooth number or not ` ` `  `import` `java.lang.*; ` ` `  `class` `GFG{ ` ` `  `// function to check if number n ` `// is a P-smooth number or not ` ` `  `static` `boolean` `check(``int` `n, ``int` `p)  ` `{ ` `    ``int` `maximum = -``1``; ` `     `  `    ``// prime factorise it by 2 ` `    ``while` `((n % ``2``) == ``0``)  ` `    ``{ ` `        ``// if the number is divisible by 2 ` `        ``maximum = Math.max(maximum, ``2``); ` `        ``n = n/``2``; ` `    ``} ` ` `  `    ``// check for all the possible numbers  ` `    ``// that can divide it ` `    ``for` `(``int` `i = ``3``; i <= Math.sqrt(n); i += ``2``) ` `    ``{ ` `        ``// prime factorize it by i ` `        ``while` `(n % i == ``0``)  ` `        ``{  ` `            ``// stores the maximum if maximum  ` `            ``// and i, if i divides the number ` `            ``maximum = Math.max(maximum,i);  ` `            ``n = n / i; ` `        ``} ` `    ``} ` ` `  `    ``// if n at the end is a prime number,  ` `    ``// then it a divisor itself ` `    ``if` `(n > ``2``) ` `        ``maximum = Math.max(maximum, n); ` `     `  `    ``return` `(maximum <= p); ` `} ` ` `  `// Driver program to test ` `// above function ` `public` `static` `void` `main(String[] args)  ` `{ ` `    ``int` `n = ``24``, p = ``7``; ` `     `  `    ``if` `(check(n, p)) ` `        ``System.out.println(``"yes"``); ` `    ``else` `        ``System.out.println(``"no"``); ` `     `  `} ` `} ` ` `  `// This code is contributed by ` `// Smitha Dinesh Semwal `

## Python3

 `# Python 3 program to ` `# check if a number is ` `# a p-smooth number or not ` ` `  `import` `math ` ` `  `# function to check if number n ` `# is a P-smooth number or not ` `def` `check(n, p) : ` ` `  `    ``maximum ``=` `-``1` `     `  `    ``# prime factorise it by 2 ` `    ``while` `(``not``(n ``%` `2``)):  ` `     `  `        ``# if the number is divisible by 2 ` `        ``maximum ``=` `max``(maximum, ``2``) ` `        ``n ``=` `int``(n``/``2``) ` `     `  ` `  `    ``# check for all the possible numbers  ` `    ``# that can divide it ` `    ``for` `i ``in` `range``(``3``,``int``(math.sqrt(n)), ``2``): ` `     `  `        ``# prime factorize it by i ` `        ``while` `(n ``%` `i ``=``=` `0``) : ` `          `  `            ``# stores the maximum if maximum  ` `            ``# and i, if i divides the number ` `            ``maximum ``=` `max``(maximum,i)  ` `            ``n ``=` `int``(n ``/` `i) ` `         `  `     `  ` `  `    ``# if n at the end is a prime number,  ` `    ``# then it a divisor itself ` `    ``if` `(n > ``2``): ` `        ``maximum ``=` `max``(maximum, n) ` `     `  `    ``return` `(maximum <``=` `p) ` ` `  ` `  `# Driver program to test above function ` `n ``=` `24` `p ``=` `7` `if` `(check(n, p)): ` `    ``print``( ``"yes"` `) ` `else``: ` `    ``print``( ``"no"` `) ` ` `  `# This code is contributed by ` `# Smitha Dinesh Semwal `

## C#

 `// C# program to check if a number is ` `// a p-smooth number or not ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// function to check if number n ` `    ``// is a P-smooth number or not ` `     `  `    ``static` `bool` `check(``int` `n, ``int` `p)  ` `    ``{ ` `        ``int` `maximum = -1; ` `         `  `        ``// prime factorise it by 2 ` `        ``while` `((n % 2) == 0)  ` `        ``{ ` `            ``// if the number is divisible by 2 ` `            ``maximum = Math.Max(maximum, 2); ` `            ``n = n / 2; ` `        ``} ` `     `  `        ``// check for all the possible numbers  ` `        ``// that can divide it ` `        ``for` `(``int` `i = 3; i <= Math.Sqrt(n); i += 2) ` `        ``{ ` `            ``// prime factorize it by i ` `            ``while` `(n % i == 0)  ` `            ``{  ` `                ``// stores the maximum if maximum  ` `                ``// and i, if i divides the number ` `                ``maximum = Math.Max(maximum, i);  ` `                ``n = n / i; ` `            ``} ` `        ``} ` `     `  `        ``// if n at the end is a prime number,  ` `        ``// then it a divisor itself ` `        ``if` `(n > 2) ` `            ``maximum = Math.Max(maximum, n); ` `         `  `        ``return` `(maximum <= p); ` `    ``} ` `     `  `    ``// Driver program to test ` `    ``// above function ` `    ``public` `static` `void` `Main()  ` `    ``{ ` `        ``int` `n = 24, p = 7; ` `         `  `        ``if` `(check(n, p)) ` `            ``Console.Write(``"yes"``); ` `        ``else` `            ``Console.Write(``"no"``); ` `         `  `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` 2) ` `        ``\$maximum` `= max(``\$maximum``, ``\$n``); ` `     `  `    ``return` `(``\$maximum` `<= ``\$p``); ` `} ` ` `  `// Driver Code ` `\$n` `= 24; ``\$p` `= 7; ` `     `  `if` `(check(``\$n``, ``\$p``)) ` `    ``echo``(``"yes"``); ` `else` `    ``echo``(``"no"``); ` `     `  `// This code is contributed by Ajit. ` `?> `

Output:

```yes
```

Reference:
http://oeis.org/wiki/P-smooth_numbers

My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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.

Improved By : jit_t

Article Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.