Isqrt (integer square root) of X: Difference between revisions
Content added Content deleted
Not a robot (talk | contribs) (Add COBOL) |
(add RPL) |
||
Line 5,700: | Line 5,700: | ||
73 49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407 7,015,820,362,023,593,956,150,476,655,802 |
73 49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407 7,015,820,362,023,593,956,150,476,655,802 |
||
</pre> |
</pre> |
||
=={{header|RPL}}== |
|||
Because RPL can only handle unsigned integers, a light change has been made in the proposed algorithm, |
|||
{{works with|Halcyon Calc|4.2.7}} |
|||
{| class="wikitable" |
|||
! RPL code |
|||
! Comment |
|||
|- |
|||
| |
|||
≪ |
|||
#1 |
|||
'''WHILE''' DUP2 ≥ '''REPEAT''' |
|||
SL SL '''END''' |
|||
#0 |
|||
'''WHILE''' OVER #1 > '''REPEAT''' |
|||
SWAP SR SR SWAP |
|||
DUP2 + |
|||
SWAP SR SWAP |
|||
'''IF''' 4 PICK SWAP DUP2 ≥ '''THEN''' |
|||
- 4 ROLL DROP ROT ROT |
|||
OVER + |
|||
'''ELSE''' DROP2 '''END''' |
|||
'''END''' ROT ROT DROP2 |
|||
≫ |
|||
´'''ISQRT'''’ STO |
|||
| |
|||
'''ISQRT''' ''( #n -- #√n )'' |
|||
q ◄── 1 |
|||
perform while q <= x |
|||
q ◄── q * 4 |
|||
z ◄── x |
|||
r ◄── 0 |
|||
perform while q > 1 |
|||
q ◄── q ÷ 4 |
|||
u ◄── r + q |
|||
r ◄── r ÷ 2 |
|||
if z >= u then do |
|||
z ◄── z - u |
|||
r ◄── r + q |
|||
remove u and copy of z from stack |
|||
end perform, clean stack |
|||
|} |
|||
{{in}} |
|||
<pre> |
|||
≪ { } 0 65 FOR n n R→B ISQRT B→R + NEXT ≫ EVAL |
|||
≪ {} #7 1 11 START DUP ISQRT ROT SWAP + SWAP 49 * NEXT DROP ≫ EVAL |
|||
</pre> |
|||
{{out}} |
|||
<pre> |
|||
2: { 0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 } |
|||
1: { # 2d # 18d # 129d # 907d # 6352d # 44467d # 311269d # 2178889d # 15252229d # 106765608d # 747359260d } |
|||
</pre> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Ruby already has [https://ruby-doc.org/core-2.7.0/Integer.html#method-c-sqrt Integer.sqrt], which results in the integer square root of a positive integer. It can be re-implemented as follows: |
Ruby already has [https://ruby-doc.org/core-2.7.0/Integer.html#method-c-sqrt Integer.sqrt], which results in the integer square root of a positive integer. It can be re-implemented as follows: |