How to check for a perfect square in python?29 Aug 2024 | 3 min read To check whether a given number is a perfect square or not there are several methods using various inbuilt functions and various operators in python. Some of the main methods are as follows: Method 1: Using the sqrt() function from the math moduleYou can use the sqrt() function from the math module to find the square root of a number. The number is said to be a perfect square if the corresponding value of its square root is an integer. Here is an example: Output: The number is a perfect square In this example, we first import the math module and then assign a number to the variable num. After that, we use the sqrt() function to find the square root of num. If the calculated value of the square root is an integer, we print a message indicating that the provided number is a perfect square. If the number is not a perfect square, we print a message to that effect. Method 2: Using the ** operatorThe "**" operator which calculates the value of a number raised to certain power can also be used to determine whether a number is a perfect square or not. Here is an example: Output: The provided number is not a perfect square. Method 3: Using the integer division operator //The integer division operator // can also be used to determine whether a number is a perfect square or not. Here is an example: Output: The number is a perfect square. Method 4: Using a loop to check all possible square rootsYou can use the loop to find the square root of a number. Here is an example: Output: True Method 5: Using binary search to find the square root:You can use the binary search method to find the square root of a number. Here is an example: Output: False In this approach, the square root of the given number is found with the help of binary search. We start by initializing the left and right boundaries to 0 and n respectively. After that, we calculate the midpoint and check if its square is equal to the original number. If the square is less than the original number, we adjust the left boundary to mid + 1. We modify the right boundary to "mid-1" if the square is larger than the starting value. We repeat this process until we find the square root or the left and right boundaries cross each other. |
In the following tutorial, we will understand the Difflib module in the Python programming language. We will discuss the functioning of this module along with some examples based on its classes. So, let's get begun. Understanding the Python Difflib module Difflib is a built-in module in the Python programming...
7 min read
In this tutorial, we will write the Python program to find a permutation of the given string. The problem is a given string S, we need to find all unique permutations of the given string in lexicographically sorted order. Below is an example - Example - 1: Input:...
7 min read
In this tutorial, we will discuss how the users can write a Python program for counting the number of matching characters in the given pair of strings. We will pass the pair of non-empty strings. The program will count the number of matching characters in that pair...
4 min read
We all may have heard about the ASCII encoding or texts & styles that are ASCII encoded, but if any of us have not heard of it, then we don't need to take any stress. American Standard Code for Information Interchange, known as ASCII in short,...
11 min read
Metaprogramming might sound new, but if the user has ever worked with Decorators or metaclasses, they have used metaprogramming in their projects. Therefore, we can say that metaprogramming is the program that is used for manipulating the program. In this tutorial, we will discuss the metaclasses and...
9 min read
In this module, we will create a Python code for rotating the screen and put it into use with a GUI. Using some of the functions from the rotatescreen module, a simple Python library for rotating the screen in a system, the display may be changed to...
4 min read
Introduction Curve fitting is a kind of optimization that finds an optimal parameter set for a defined function appropriate for a provided collection of observations. Different from supervised learning, curve fitting needs us to define the function mapping the examples of inputs to outputs. The function which use to...
8 min read
Euclidian distance between two points on any axes is the shortest distance between them. In other words, it is the displacement length between two points. Given two points, A (a, b) and B (c, d), in a 2-dimensional plane, the Euclidian distance between A and B...
3 min read
One method for learning about anything, like music, is to look for significant groupings or collections. While our friends may arrange music by decade, we may arrange music by genre, and our choice of groups aids in understanding the distinct elements. What is Clustering? One of the unsupervised...
11 min read
The scipy.stats.nbinom() describes the Negative Binomial Discrete Distribution. It is an instance of the rv_discrete class inherited from the generic methods. It completes the techniques by adding details specific to this distribution. The Probability Mass Function, which gives the total number of failures for Negative Binomial Discrete...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India