📜  平方免费号码

📅  最后修改于: 2021-04-23 19:01:00             🧑  作者: Mango

给定一个数字,检查它是否为无正方形。如果没有一个素因数将其除以一次以上,则该数字被认为是无平方的,即,除以n的素因数的最大乘方为1。前几个平方自由数是1,2,3,5,6,7,7,10,11,13,14,15,17,19,21,22,23,26,29,30,31,33,34, 35、37、38、39 …
例子:

Input : n = 10
Output : Yes
10 can be factorized as 2*5. Since
no prime factor appears more than
once, it is a square free number.

Input :  n = 20
Output : No
20 can be factorized as 2 * 2 * 5.
Since prime factor appears more than
once, it is not a square free number.

这个想法很简单,我们一步一步找到所有主要因素。对于每个素数,我们检查其平方是否也除以n。如果是,则返回false。最后,如果我们找不到一个可以被一次整除的素数,则返回false。

C++
// C++ Program to print
// all prime factors
# include 
using namespace std;
  
// Returns true if n is a square free
// number, else returns false.
bool isSquareFree(int n)
{
    if (n % 2 == 0)
       n = n/2;
  
    // If 2 again divides n, then n is
    // not a square free number.
    if (n % 2 == 0)
       return false;
 
    // n must be odd at this point.  So we can 
    // skip one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i+2)
    {
        // Check if i is a prime factor
        if (n % i == 0)
        {
           n = n/i;
 
           // If i again divides, then
           // n is not square free
           if (n % i == 0)
               return false;
        }
    }
 
    return true;
}
  
/* Driver program to test above function */
int main()
{
    int n = 10;
    if (isSquareFree(n))
       cout << "Yes";
    else
       cout << "No";
    return 0;
}


Java
// Java Program to print
// all prime factors
 
class GFG {
     
    // Returns true if n is a square free
    // number, else returns false.
    static boolean isSquareFree(int n)
    {
        if (n % 2 == 0)
        n = n / 2;
     
        // If 2 again divides n, then n is
        // not a square free number.
        if (n % 2 == 0)
        return false;
     
        // n must be odd at this point. So we can
        // skip one element (Note i = i +2)
        for (int i = 3; i <= Math.sqrt(n); i = i + 2)
        {
            // Check if i is a prime factor
            if (n % i == 0)
            {
                n = n / i;
         
                // If i again divides, then
                // n is not square free
                if (n % i == 0)
                    return false;
            }
        }
     
        return true;
    }
     
    /* Driver program to test above function */
    public static void main(String[] args)
    {
        int n = 10;
        if (isSquareFree(n))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by prerna saini.


Python3
# Python3 Program to print
# all prime factors
from math import sqrt
 
# Returns true if n is
# a square free number,
# else returns false.
def isSquareFree(n):
     
    if n % 2 == 0:
        n = n / 2
 
    # If 2 again divides n,
    # then n is not a square
    # free number.
    if n % 2 == 0:
        return False
 
    # n must be odd at this
    # point. So we can skip
    # one element
    # (Note i = i + 2)
    for i in range(3, int(sqrt(n) + 1)):
         
        # Check if i is a prime
        # factor
        if n % i == 0:
            n = n / i
 
        # If i again divides, then
        # n is not square free
        if n % i == 0:
            return False
    return True
 
# Driver program
n = 10
 
if isSquareFree(n):
    print ("Yes")
else:
    print ("No")
     
# This code is contributed by Shreyanshi Arun.


C#
// C# Program to print
// all prime factors
using System;
 
class GFG {
     
    // Returns true if n is a square free
    // number, else returns false.
    static bool isSquareFree(int n)
    {
        if (n % 2 == 0)
        n = n / 2;
     
        // If 2 again divides n, then n is
        // not a square free number.
        if (n % 2 == 0)
        return false;
     
        // n must be odd at this point. So we can
        // skip one element (Note i = i +2)
        for (int i = 3; i <= Math.Sqrt(n); i = i + 2)
        {
            // Check if i is a prime factor
            if (n % i == 0)
            {
                n = n / i;
         
                // If i again divides, then
                // n is not square free
                if (n % i == 0)
                    return false;
            }
        }
     
        return true;
    }
     
    // Driver program
    public static void Main()
    {
        int n = 10;
        if (isSquareFree(n))
        Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:
Yes

时间复杂度: O(sqrt(N))
在最坏的情况下,如果该数字是一个完美的平方,那么将存在sqrt(n)/ 2次迭代。