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.
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.
C++
#include <iostream>
#include<math.h>
using namespace std;
bool check(int n, int p)
{
int maximum = -1;
while (!(n % 2))
{
maximum = max(maximum, 2);
n = n/2;
}
for (int i = 3; i <= sqrt(n); i += 2)
{
while (n % i == 0)
{
maximum = max(maximum,i);
n = n / i;
}
}
if (n > 2)
maximum = max(maximum, n);
return (maximum <= p);
}
int main()
{
int n = 24, p = 7;
if (check(n, p))
cout << "yes";
else
cout << "no";
return 0;
}
|
Java
import java.lang.*;
class GFG{
static boolean check(int n, int p)
{
int maximum = -1;
while ((n % 2) == 0)
{
maximum = Math.max(maximum, 2);
n = n/2;
}
for (int i = 3; i <= Math.sqrt(n); i += 2)
{
while (n % i == 0)
{
maximum = Math.max(maximum,i);
n = n / i;
}
}
if (n > 2)
maximum = Math.max(maximum, n);
return (maximum <= p);
}
public static void main(String[] args)
{
int n = 24, p = 7;
if (check(n, p))
System.out.println("yes");
else
System.out.println("no");
}
}
|
Python3
import math
def check(n, p) :
maximum = -1
while (not(n % 2)):
maximum = max(maximum, 2)
n = int(n/2)
for i in range(3,int(math.sqrt(n)), 2):
while (n % i == 0) :
maximum = max(maximum,i)
n = int(n / i)
if (n > 2):
maximum = max(maximum, n)
return (maximum <= p)
n = 24
p = 7
if (check(n, p)):
print( "yes" )
else:
print( "no" )
|
C#
using System;
class GFG {
static bool check(int n, int p)
{
int maximum = -1;
while ((n % 2) == 0)
{
maximum = Math.Max(maximum, 2);
n = n / 2;
}
for (int i = 3; i <= Math.Sqrt(n); i += 2)
{
while (n % i == 0)
{
maximum = Math.Max(maximum, i);
n = n / i;
}
}
if (n > 2)
maximum = Math.Max(maximum, n);
return (maximum <= p);
}
public static void Main()
{
int n = 24, p = 7;
if (check(n, p))
Console.Write("yes");
else
Console.Write("no");
}
}
|
PHP
<?php
function check($n, $p)
{
$maximum = -1;
while (!($n % 2))
{
$maximum = max($maximum, 2);
$n = $n / 2;
}
for ($i = 3; $i <= sqrt($n); $i += 2)
{
while ($n % $i == 0)
{
$maximum = max($maximum, $i);
$n = $n / $i;
}
}
if ($n > 2)
$maximum = max($maximum, $n);
return ($maximum <= $p);
}
$n = 24; $p = 7;
if (check($n, $p))
echo("yes");
else
echo("no");
?>
|
Javascript
<script>
function check(n, p)
{
let maximum = -1;
while (!(n % 2))
{
maximum = Math.max(maximum, 2)
n = n/2;
}
var i;
for (i = 3; i*i <= n; i += 2)
{
while (n % i == 0)
{
maximum = Math.max(maximum, i);
n = n / i;
}
}
if (n > 2)
maximum = Math.max(maximum, n);
if (maximum <= p)
return true;
else
return false;
}
let n = 24, p = 7;
if (check(n, p))
document.write("yes");
else
document.write("no");
</script>
|
Output:
yes
Time Complexity: O(sqrt(N)*logN), as we are using nested loops to traverse sqrt(N)*logN times.
Auxiliary Space: O(1), as we are not using any extra space.
Reference:
http://oeis.org/wiki/P-smooth_numbers
Last Updated :
08 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...