Quantcast
Channel: Fastest way to determine if an integer's square root is an integer - Stack Overflow
Browsing latest articles
Browse All 38 View Live

Answer by Sajjad Ali Vayani for Fastest way to determine if an integer's...

Square Root of a number, given that the number is a perfect square.The complexity is log(n)/** * Calculate square root if the given number is a perfect square. * * Approach: Sum of n odd numbers is...

View Article



Answer by Viktor for Fastest way to determine if an integer's square root is...

May be the best algorithm for the problem is a fast integer square root algorithm https://stackoverflow.com/a/51585204/5191852There @Kde claims that three iterations of the Newton method would be...

View Article

Answer by MWB for Fastest way to determine if an integer's square root is an...

Here is a divide and conquer solution.If the square root of a natural number (number) is a natural number (solution), you can easily determine a range for solution based on the number of digits of...

View Article

Answer by Albert van der Horst for Fastest way to determine if an integer's...

Calculating square roots by Newton's method is horrendously fast ... provided that the starting value is reasonable. However there is no reasonable starting value, and in practice we end with bisection...

View Article

Answer by aventurin for Fastest way to determine if an integer's square root...

Newton's Method with integer arithmeticIf you wish to avoid non-integer operations you could use the method below. It basically uses Newton's Method modified for integer arithmetic./** * Test if the...

View Article


Answer by MWB for Fastest way to determine if an integer's square root is an...

Not sure if this is the fastest way, but this is something I stumbled upon (long time ago in high-school) when I was bored and playing with my calculator during math class. At that time, I was really...

View Article

Answer by Steve Storck for Fastest way to determine if an integer's square...

Here is the simplest and most concise way, although I do not know how it compares in terms of CPU cycles. This works great if you only wish to know if the root is a whole number. If you really care if...

View Article

Answer by dfeuer for Fastest way to determine if an integer's square root is...

The following simplification of maaartinus's solution appears to shave a few percentage points off the runtime, but I'm not good enough at benchmarking to produce a benchmark I can trust:long goodMask;...

View Article


Answer by Colonel Panic for Fastest way to determine if an integer's square...

An integer problem deserves an integer solution. ThusDo binary search on the (non-negative) integers to find the greatest integer t such that t**2 <= n. Then test whether r**2 = n exactly. This...

View Article


Image may be NSFW.
Clik here to view.

Answer by maaartinus for Fastest way to determine if an integer's square root...

I'm pretty late to the party, but I hope to provide a better answer; shorter and (assuming my benchmark is correct) also much faster.long goodMask; // 0xC840C04048404040 computed below{ for (int i=0;...

View Article

Answer by durron597 for Fastest way to determine if an integer's square root...

I ran my own analysis of several of the algorithms in this thread and came up with some new results. You can see those old results in the edit history of this answer, but they're not accurate, as I...

View Article

Answer by Fractaly for Fastest way to determine if an integer's square root...

I checked all of the possible results when the last n bits of a square is observed. By successively examining more bits, up to 5/6th of inputs can be eliminated. I actually designed this to implement...

View Article

Answer by nabam serbang for Fastest way to determine if an integer's square...

Considering for general bit length (though I have used specific type here), I tried to design simplistic algo as below. Simple and obvious check for 0,1,2 or <0 is required initially.Following is...

View Article


Answer by finnw for Fastest way to determine if an integer's square root is...

This is the fastest Java implementation I could come up with, using a combination of techniques suggested by others in this thread.Mod-256 testInexact mod-3465 test (avoids integer division at the cost...

View Article

Answer by dstibbe for Fastest way to determine if an integer's square root is...

"I'm looking for the fastest way to determine if a long value is a perfect square (i.e. its square root is another integer)."The answers are impressive, but I failed to see a simple check :check...

View Article


Answer by Jonny Heggheim for Fastest way to determine if an integer's square...

I like the idea to use an almost correct method on some of the input. Here is a version with a higher "offset". The code seems to work and passes my simple test case.Just replace your:if(n <...

View Article

Answer by bgiles for Fastest way to determine if an integer's square root is...

Project Euler is mentioned in the tags and many of the problems in it require checking numbers >>2^64. Most of the optimizations mentioned above don't work easily when you are working with an 80...

View Article


Answer by hydrodog for Fastest way to determine if an integer's square root...

The sqrt call is not perfectly accurate, as has been mentioned, but it's interesting and instructive that it doesn't blow away the other answers in terms of speed. After all, the sequence of assembly...

View Article

Answer by Ben for Fastest way to determine if an integer's square root is an...

Regarding the Carmac method, it seems like it would be quite easy just to iterate once more, which should double the number of digits of accuracy. It is, after all, an extremely truncated iterative...

View Article

Answer by paulmurray for Fastest way to determine if an integer's square root...

It ought to be possible to pack the 'cannot be a perfect square if the last X digits are N' much more efficiently than that! I'll use java 32 bit ints, and produce enough data to check the last 16 bits...

View Article

Answer by A. Rex for Fastest way to determine if an integer's square root is...

I figured out a method that works ~35% faster than your 6bits+Carmack+sqrt code, at least with my CPU (x86) and programming language (C/C++). Your results may vary, especially because I don't know how...

View Article


Answer by Brent.Longborough for Fastest way to determine if an integer's...

This a rework from decimal to binary of the old Marchant calculator algorithm (sorry, I don't have a reference), in Ruby, adapted specifically for this question:def isexactsqrt(v) value = v.abs residue...

View Article


Answer by David Lehavi for Fastest way to determine if an integer's square...

You should get rid of the 2-power part of N right from the start.2nd EditThe magical expression for m below should bem = N - (N & (N-1));and not as writtenEnd of 2nd editm = N & (N-1); // the...

View Article

Answer by BobbyShaftoe for Fastest way to determine if an integer's square...

For performance, you very often have to do some compromsies. Others have expressed various methods, however, you noted Carmack's hack was faster up to certain values of N. Then, you should check the...

View Article

Answer by Cyrille Ka for Fastest way to determine if an integer's square root...

Just for the record, another approach is to use the prime decomposition. If every factor of the decomposition is even, then the number is a perfect square. So what you want is to see if a number can be...

View Article


Answer by Hugh Allen for Fastest way to determine if an integer's square root...

It's been pointed out that the last d digits of a perfect square can only take on certain values. The last d digits (in base b) of a number n is the same as the remainder when n is divided by bd, ie....

View Article

Answer by mrzl for Fastest way to determine if an integer's square root is an...

I want this function to work with all positive 64-bit signed integersMath.sqrt() works with doubles as input parameters, so you won't get accurate results for integers bigger than 2^53.

View Article

Answer by Elijah for Fastest way to determine if an integer's square root is...

If speed is a concern, why not partition off the most commonly used set of inputs and their values to a lookup table and then do whatever optimized magic algorithm you have come up with for the...

View Article

Answer by Bill the Lizard for Fastest way to determine if an integer's square...

It should be much faster to use Newton's method to calculate the Integer Square Root, then square this number and check, as you do in your current solution. Newton's method is the basis for the Carmack...

View Article



Answer by John D. Cook for Fastest way to determine if an integer's square...

You'll have to do some benchmarking. The best algorithm will depend on the distribution of your inputs.Your algorithm may be nearly optimal, but you might want to do a quick check to rule out some...

View Article

Answer by Joel Coehoorn for Fastest way to determine if an integer's square...

Don't know about fastest, but the simplest is to take the square root in the normal fashion, multiply the result by itself, and see if it matches your original value.Since we're talking integers here,...

View Article

Answer by Kibbee for Fastest way to determine if an integer's square root is...

I'm not sure if it would be faster, or even accurate, but you could use John Carmack's Magical Square Root, algorithm to solve the square root faster. You could probably easily test this for all...

View Article

Answer by Jon Skeet for Fastest way to determine if an integer's square root...

If you do a binary chop to try to find the "right" square root, you can fairly easily detect if the value you've got is close enough to tell:(n+1)^2 = n^2 + 2n + 1(n-1)^2 = n^2 - 2n + 1So having...

View Article


Answer by chakrit for Fastest way to determine if an integer's square root is...

I was thinking about the horrible times I've spent in Numerical Analysis course.And then I remember, there was this function circling around the 'net from the Quake Source code:float Q_rsqrt( float...

View Article

Answer by Celestial M Weasel for Fastest way to determine if an integer's...

If you want speed, given that your integers are of finite size, I suspect that the quickest way would involve (a) partitioning the parameters by size (e.g. into categories by largest bit set), then...

View Article

Fastest way to determine if an integer's square root is an integer

I'm looking for the fastest way to determine if a long value is a perfect square (i.e. its square root is another integer): I've done it the easy way, by using the built-in Math.sqrt()function, but I'm...

View Article


Answer by FrederickAmpsUp for Fastest way to determine if an integer's square...

static boolean isPerfectSquare (int input) { return Math.sqrt(input) == (int) Math.sqrt(input);}This will return if the integer value of the square root of input is equal to the double value. This...

View Article


Answer by Simon Goater for Fastest way to determine if an integer's square...

This question got me wondering, so I did some simple coding and I'm presenting it here because I think it's interesting, relevant, but I don't know how useful. There's a simple algorithma_n+1 = (a_n +...

View Article
Browsing latest articles
Browse All 38 View Live




Latest Images