Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 681381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:28:38+00:00 2026-05-14T01:28:38+00:00

How could I check if a number is a perfect square? Speed is of

  • 0

How could I check if a number is a perfect square?

Speed is of no concern, for now, just working.


See also: Integer square root in python.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:28:39+00:00Added an answer on May 14, 2026 at 1:28 am

    The problem with relying on any floating point computation (math.sqrt(x), or x**0.5) is that you can’t really be sure it’s exact (for sufficiently large integers x, it won’t be, and might even overflow). Fortunately (if one’s in no hurry;-) there are many pure integer approaches, such as the following…:

    def is_square(apositiveint):
      x = apositiveint // 2
      seen = set([x])
      while x * x != apositiveint:
        x = (x + (apositiveint // x)) // 2
        if x in seen: return False
        seen.add(x)
      return True
    
    for i in range(110, 130):
       print i, is_square(i)
    

    Hint: it’s based on the “Babylonian algorithm” for square root, see wikipedia. It does work for any positive number for which you have enough memory for the computation to proceed to completion;-).

    Edit: let’s see an example…

    x = 12345678987654321234567 ** 2
    
    for i in range(x, x+2):
       print i, is_square(i)
    

    this prints, as desired (and in a reasonable amount of time, too;-):

    152415789666209426002111556165263283035677489 True
    152415789666209426002111556165263283035677490 False
    

    Please, before you propose solutions based on floating point intermediate results, make sure they work correctly on this simple example — it’s not that hard (you just need a few extra checks in case the sqrt computed is a little off), just takes a bit of care.

    And then try with x**7 and find clever way to work around the problem you’ll get,

    OverflowError: long int too large to convert to float
    

    you’ll have to get more and more clever as the numbers keep growing, of course.

    If I was in a hurry, of course, I’d use gmpy — but then, I’m clearly biased;-).

    >>> import gmpy
    >>> gmpy.is_square(x**7)
    1
    >>> gmpy.is_square(x**7 + 1)
    0
    

    Yeah, I know, that’s just so easy it feels like cheating (a bit the way I feel towards Python in general;-) — no cleverness at all, just perfect directness and simplicity (and, in the case of gmpy, sheer speed;-)…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How could I go about finding the division remainder of a number in Python?
I was wondering whether you could check a number against all numbers in a
I've been wondering how I could check all my parameters efficiently in any function
If I have a string that contains Cat how could I check before hand
I would try it myself, but I can't. Could someone check for me? I
Could you please check out this piece of code: #include <vector> class A {
Please could someone post an example of how to check if an element exists
was hoping you could help me out. I am trying to check if a
In the following scenaio, how could I make use of AJAX to check if
I'm working on a browser-based RPG for one of my websites, and right now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.