Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Find the integer square root of a positive integer
ResourceFunction["FloorSqrt"][n] computes the floor-integer square root of positive integer n. |
Compute the floor of the square root for an integer:
In[1]:= |
Out[2]= |
Check that it is correct:
In[3]:= |
Out[3]= |
If n is a square then FloorSqrt[n]==Sqrt[n]:
In[4]:= |
Out[4]= |
For large n, FloorSqrt is typically much faster to compute than Sqrt:
In[5]:= |
Out[6]= |
Here is an asymptotically fast divide-and-conquer code based on Zimmermann's Karatsuba method:
In[7]:= |
Compare speed to FloorSqrt on a large input:
In[8]:= |
Out[9]= |
FloorSqrt only supports positive integers:
In[10]:= |
Out[10]= |
Use Floor to convert the input to an integer before using FloorSqrt:
In[11]:= |
Out[11]= |
Wolfram Language 13.0 (December 2021) or above
This work is licensed under a Creative Commons Attribution 4.0 International License