Surd number
A number is said to be Surd if its square root, cube root, etc are not integers. For example, 9 is not a Surd as square root of 9 is 3, but 5 is Surd as square root of 5 is not integer. Similarly 8 is not a Surd (Cube root of 8 is integer), but 7 is.
Given a range, find if it is Surds or not
Examples:
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.
The idea is to try every power of all numbers from 2 to √n and check if any power is equal n or not.
C++
// CPP program to find if a number is // Surds or not #include <bits/stdc++.h> 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
<?php // PHP program to find if // a number is Surds or not // Returns true if // x is Surd number function isSurd( $n ) { for ( $i = 2; $i * $i <= $n ; $i ++) { // Try all powers of i $j = $i ; while ( $j < $n ) $j = $j * $i ; if ( $j == $n ) return false; } return true; } // Driver code $n = 15; if (isSurd( $n )) echo ( "Yes" ); else echo ( "No" ); // This code is contributed // by Shivi_Aggarwal ?> |
Javascript
<script> // Javascript program to find if a // number is Surds or not // Returns true if x // is Surd number function isSurd(n) { for (let i = 2; i * i <= n; i++) { // Try all powers of i let j = i; while (j < n) j = j * i; if (j == n) return false ; } return true ; } // Driver Code let n = 15; if (isSurd(n)) document.write( "Yes" ); else document.write( "No" ); // This code is contributed by rag2127 </script> |
Output:
Yes
Time Complexity: O(√n) as the while loop runs for constant time.
Auxiliary Space: O(1)
Please Login to comment...