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 9294667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:25:31+00:00 2026-06-18T21:25:31+00:00

A perfect square is taken in binary and some bits are replaced with ?

  • 0

A perfect square is taken in binary and some bits are replaced with “?” for example 1??, the number would be 4.(or 1????000???0000)

I need to find that perfect square.(there will be only such possible number)

number of ‘?’s in the string be n

To find that number what I am doing is iterating through 2**n numbers(111,110,101,100) and checking if it is a perfect square. I am using following function to check if it is a perfect square.

bool issqr(int n){
   int d=(int)(sqrt(n));
   if(d*d==n) return true;
   else return false;
}

Even though in python I did it, it is taking a lot of time, so I shifted to C++ using only bit operations for populating 2**n numbers(which was much faster than the python version)

but this fails if the number has more than 64 bits

How to avoid this problem? How can I do the same thing if a number has say 120 bits.

(10100110???1?1?01?1?011000?1100?00101000?1?11001101100110001010111?0?1??0110?110?01?1100?1?0110?1?10111?01?0111000?10??101?01)

  • 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-06-18T21:25:32+00:00Added an answer on June 18, 2026 at 9:25 pm

    Rather than re-writing in C++ you should first have looked at improving your algorithm. The lowest possible answer is the square root from the original value with all ‘?’ replace by 0 rounded up, the highest possible answer is the square root of the pattern with the ‘?’s replaced by 1 rounded down. Find those two values, iterate through them, square and check against the pattern.

    This is faster both because you are iterating through many fewer numbers and because you aren’t calculating any square roots in the loop: squaring is much easier.

    You don’t need to compare string to check for a match:

    mask = int(pattern.replace('0', '1').replace('?', '0'), 2)
    test = int(pattern.replace('?', '0'), 2)
    
    def is_match(n):
        return (n&mask)==test
    

    So putting it all together:

    def int_sqrt(x):
        if x < 0:
            raise ValueError('square root not defined for negative numbers')
        n = int(x)
        if n == 0:
            return 0
        a, b = divmod(n.bit_length(), 2)
        x = 2**(a+b)
        while True:
            y = (x + n//x)//2
            if y >= x:
                return x
            x = y
    
    def find_match(pattern):
        lowest = int(pattern.replace('?', '0'), 2)
        highest = int(pattern.replace('?', '1'), 2)
        mask = int(pattern.replace('0', '1').replace('?', '0'), 2)
        lowsqrt = int_sqrt(lowest)
        if lowsqrt*lowsqrt != lowest:
                lowsqrt += 1
        highsqrt = int_sqrt(highest)
        for n in range(lowsqrt, highsqrt+1):
            if (n*n & mask)==lowest:
                return n*n
    
    print(find_match('1??1??1'))
    print(find_match('1??0??1'))
    print(find_match('1??????????????????????????????????????????????????????????????????????1??0??1'))
    

    Output:

    121
    81
    151115727461209345152081
    

    N.B. This only works in Python 3.x, the last test will overflow range in Python 2.x

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

Sidebar

Related Questions

I need to write a function that returns the first perfect square that is
Create a program to find out the first perfect square greater than 1 that
How could I check if a number is a perfect square? Speed is of
Need some help with the CSS for generating a grid of perfect squares. Div's
I'm working on a concept of using an image that isn't a perfect square
I'm trying to write a program that counts the number of perfect numbers within
How would you write a code that will remove the perfect squares from a
This is a code to check if a number is perfect square or not.
When i send this perfect square character: 25² And retrieve it in another page
I've got this script for calculating square area, which works perfect: <script language=javascript type=text/javascript>

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.