📜  P平滑数或P易碎数

📅  最后修改于: 2021-05-05 02:34:55             🧑  作者: Mango

P光滑数P易碎数是一个整数,其最大质数小于或等于P。给定N和P,我们需要编写一个程序来检查它是否为P易碎的。
例子:

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. 

该方法将是对因子进行因子分解,并存储所有因子中的最大值。如果数是可整的,我们首先将其除以2,然后从3迭代到Sqrt(n)以得到素数除以特定次数的次数,该次数每次减少n / i并存储素因数i其数除以N。我们将数n(除以素数因子)除以其对应的最小素数因子,直到n变为1。如果在末尾n> 2,则表示它的素数,因此我们将其存储为素数因子为出色地。最后,将最大因子与p进行比较,以检查是否为p平滑数。

C++
// 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.
?>


Javascript


输出:

yes

参考
http://oeis.org/wiki/P-smooth_numbers