At the beginning of the program, perform some pre-filtering to speed up the operation and filter out obvious non-square numbers, including negative numbers, numbers with the last 4 digits being 0, and numbers with the last 2 digits meeting certain requirements. The number of the condition (5 or 8 in decimal). For 0, think of it as a square number.
Next, use bitwise techniques to check whether the remainder of modulo 255 = 3 = 3 5 17 is a square number. The array bad255 records whether each remainder is a square number. Just look up the table. .
For numbers that pass the pre-filter, divide by all powers of 2 (in a binary search manner) until the quotient is odd.
The final step is to approximate the square root using a method similar to Hensel's lemma. The inner loop starts with an initial value given by the start array, which provides an approximation of sqrt (mod 8192). This approximation is continuously improved through successive calculations, using bitwise tricks to improve speed.
The rough structure of this method is as follows:
It is worth noting that the author of this algorithm claims that it runs 35% faster than other methods.
The above is the detailed content of How Can We Efficiently Determine if a Large Number is a Perfect Square?. For more information, please follow other related articles on the PHP Chinese website!