📜  编号

📅  最后修改于: 2021-04-24 23:59:53             🧑  作者: Mango

如果数字的平方根,立方根等不是整数,则称该数字为Surd。例如,9不是平方根,因为9的平方根是3,但是5是Surd,因为5的平方根不是整数。同样,8不是Surd(8的立方根是整数),而7是。

给定范围,查找是否为Surds
例子:

Input : 4
Output : No
4 is not a Surd number as its square root is
an integer.

Input : 5
Output : Yes
5 is a Surd number as its square root is not an integer.

Input : 8
Output : No
8 is not a Surd number as cube root of 8
is an integer.

这个想法是尝试从2到  n的所有数字的所有幂,并检查是否有任何幂等于n。

C++
// CPP program to find if a number is 
// Surds or not
#include 
using namespace std;
  
// Returns true if x is Surd number
bool isSurd(int n)
{  
    for (int i=2; i*i<=n; i++)
    {
       // Try all powers of i
       int j = i;
       while (j < n)              
          j = j * i;
  
       if (j == n)
          return false;
    }
  
    return true;
}
  
// driver code 
int main()
{  
    int n = 15;
    if (isSurd(n))
       cout << "Yes";
    else
       cout << "No";
    return 0;
}


Java
// Java program to find if a
// number is Surds or not
  
class GFG
{
  
// Returns true if x
// is Surd number
static boolean isSurd(int n)
{ 
    for (int i = 2; 
             i * i <= n; i++)
    {
        // Try all powers of i
        int j = i;
        while (j < n)             
            j = j * i;
      
        if (j == n)
            return false;
    }
  
    return true;
}
  
// Driver Code 
public static void main(String args[])
{ 
    int n = 15;
    if (isSurd(n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed
// by Ankita_Saini


Python3
# Python3 program to find 
# if a number is Surd or not.
  
# define a isSurd function which
# Returns true if x is Surd number.
def isSurd(n) :
      
    i = 2
    for i in range(2, (i * i) + 1) :
          
        # Try all powers of i
        j = i
        while (j < n) :
            j = j * i
  
        if (j == n) :
            return False
      
    return True
  
# Driver code
if __name__ == "__main__" :
      
    n = 15
      
    if (isSurd(n)) :
        print("Yes")
          
    else :
        print("No")
  
# This code is contributed 
# by Ankit Rai1


C#
// C# program to find if a
// number is Surds or not
using System;
  
class GFG
{
  
// Returns true if x
// is Surd number
static bool isSurd(int n)
{ 
    for (int i = 2; 
             i * i <= n; i++)
    {
        // Try all powers of i
        int j = i;
        while (j < n)             
            j = j * i;
      
        if (j == n)
            return false;
    }
  
    return true;
}
  
// Driver Code 
public static void Main()
{ 
    int n = 15;
    if (isSurd(n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
  
// This code is contributed
// by ChitraNayal


PHP


输出:
Yes