Home » Archimedes archive » Acorn User » AU 1997-10 A.adf » Extras » Apple][e/PD/BOB/ARMBOB/!ArmBob/progs/intsqrt

Apple][e/PD/BOB/ARMBOB/!ArmBob/progs/intsqrt

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » Archimedes archive » Acorn User » AU 1997-10 A.adf » Extras
Filename: Apple][e/PD/BOB/ARMBOB/!ArmBob/progs/intsqrt
Read OK:
File size: 0194 bytes
Load address: 0000
Exec address: 0000
File contents
sqroot (N)
{
 local k2, u, v, u2, n;
 if ( 2 > N) return (N);
 u = N;
 k2 = 0;
 while (u >>= 2) k2++;
 u = 1 << k2;
 v = u;
 u2 = u << k2;
 while (k2--)
 {
  v >>= 1;
  n = (u + u + v)<<k2;
  n += u2;
  if (n <= N)
  {
   u += v;
   u2 = n;
  }
 }
 return (u);
}

main()
{
 print("Positive integer :");
 x = val(input());
 print("Integer part of square root is ",
         sqroot(x),
         "\n");
}
  
00000000  73 71 72 6f 6f 74 20 28  4e 29 0a 7b 0a 20 6c 6f  |sqroot (N).{. lo|
00000010  63 61 6c 20 6b 32 2c 20  75 2c 20 76 2c 20 75 32  |cal k2, u, v, u2|
00000020  2c 20 6e 3b 0a 20 69 66  20 28 20 32 20 3e 20 4e  |, n;. if ( 2 > N|
00000030  29 20 72 65 74 75 72 6e  20 28 4e 29 3b 0a 20 75  |) return (N);. u|
00000040  20 3d 20 4e 3b 0a 20 6b  32 20 3d 20 30 3b 0a 20  | = N;. k2 = 0;. |
00000050  77 68 69 6c 65 20 28 75  20 3e 3e 3d 20 32 29 20  |while (u >>= 2) |
00000060  6b 32 2b 2b 3b 0a 20 75  20 3d 20 31 20 3c 3c 20  |k2++;. u = 1 << |
00000070  6b 32 3b 0a 20 76 20 3d  20 75 3b 0a 20 75 32 20  |k2;. v = u;. u2 |
00000080  3d 20 75 20 3c 3c 20 6b  32 3b 0a 20 77 68 69 6c  |= u << k2;. whil|
00000090  65 20 28 6b 32 2d 2d 29  0a 20 7b 0a 20 20 76 20  |e (k2--). {.  v |
000000a0  3e 3e 3d 20 31 3b 0a 20  20 6e 20 3d 20 28 75 20  |>>= 1;.  n = (u |
000000b0  2b 20 75 20 2b 20 76 29  3c 3c 6b 32 3b 0a 20 20  |+ u + v)<<k2;.  |
000000c0  6e 20 2b 3d 20 75 32 3b  0a 20 20 69 66 20 28 6e  |n += u2;.  if (n|
000000d0  20 3c 3d 20 4e 29 0a 20  20 7b 0a 20 20 20 75 20  | <= N).  {.   u |
000000e0  2b 3d 20 76 3b 0a 20 20  20 75 32 20 3d 20 6e 3b  |+= v;.   u2 = n;|
000000f0  0a 20 20 7d 0a 20 7d 0a  20 72 65 74 75 72 6e 20  |.  }. }. return |
00000100  28 75 29 3b 0a 7d 0a 0a  6d 61 69 6e 28 29 0a 7b  |(u);.}..main().{|
00000110  0a 20 70 72 69 6e 74 28  22 50 6f 73 69 74 69 76  |. print("Positiv|
00000120  65 20 69 6e 74 65 67 65  72 20 3a 22 29 3b 0a 20  |e integer :");. |
00000130  78 20 3d 20 76 61 6c 28  69 6e 70 75 74 28 29 29  |x = val(input())|
00000140  3b 0a 20 70 72 69 6e 74  28 22 49 6e 74 65 67 65  |;. print("Intege|
00000150  72 20 70 61 72 74 20 6f  66 20 73 71 75 61 72 65  |r part of square|
00000160  20 72 6f 6f 74 20 69 73  20 22 2c 0a 20 20 20 20  | root is ",.    |
00000170  20 20 20 20 20 73 71 72  6f 6f 74 28 78 29 2c 0a  |     sqroot(x),.|
00000180  20 20 20 20 20 20 20 20  20 22 5c 6e 22 29 3b 0a  |         "\n");.|
00000190  7d 0a 20 20                                       |}.  |
00000194