Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > integer cube root ...
User Name
Password
REGISTER NOW! Mark Forums Read




Reply Bookmark and Share
1 20th March 19:09
john w. kennedy
External User
 
Posts: 1
Default integer cube root ...



nth_root: procedure; arg x, p; x = trunc(x); p = trunc(p)
y = 1; y1 = -1; p1 = p - 1
do while (abs(y - y1) > 1)
y1 = y
y = (p1 * y + x % y ** p1) % p
end
if y ** p < x then
return y1
else
return y

The above is not completely stable against overflow, which will not
happen in REXX, but may happen in PL/I.

(I have to thank you for asking this; if you hadn't, I wouldn't have
discovered Open Object REXX; I've missed having REXX since I had to
abandon OS/2.)

--
John W. Kennedy
"But now is a new thing which is very old--
that the rich make themselves richer and not poorer,
which is the true Gospel, for the poor's sake."
-- Charles Williams. "Judgement at Chelmsford"
  Reply With Quote


 


2 20th March 19:10
robin
External User
 
Posts: 1
Default integer cube root ...



Don't recall it being posted,
but it may be that by Heinrich, published in 1996,
in Dr Dobbs' Journal, #4, April.
  Reply With Quote
3 20th March 19:10
n. shamsundar
External User
 
Posts: 1
Default integer cube root ...


Please correct Line-3 to
do while (abs(y - y1) > 0)

To see why, run the original and corrected versions with x = 1027 and p
= 10.

The code is simply an integer version of Newton's method.

N. Shamsundar
University of Houston
  Reply With Quote
4 20th March 23:45
gerard schildberger
External User
 
Posts: 1
Default integer cube root ...


|> Gerard Schildberger wrote:
|>> Below is a subroutine to find the integer squart root (I've
|>> forgetten who posted this via this newsgroup):
|>> __________________________________________________ ____
|>>
|>> /*find integer sqrt of a non-negative #.*/
|>>
|>> isqrt: procedure; parse arg x; x=trunc(x); r=0; q=1
|>>
|>> do while q<=x
|>> q=q*4
|>> end
|>>
|>> do while q>1
|>> q=q%4
|>> _=x-r-q
|>> r=r%2
|>>
|>> if _>=0 then do
|>> x=_
|>> r=r+q
|>> end
|>>
|>> end
|>>
|>> return r
|>> __________________________________________________ _____
|>>
|>>
|>> Does anyone know of (or where can I get it) an integer
|>> cube root routine? ______________________________Gerard S.
|> nth_root: procedure; arg x, p; x = trunc(x); p = trunc(p)
|> y = 1; y1 = -1; p1 = p - 1
|> do while (abs(y - y1) > 1)
|> y1 = y
|> y = (p1 * y + x % y ** p1) % p
|> end
|> if y ** p < x then
|> return y1
|> else
|> return y
|>
|> The above is not completely stable against overflow, which will not
|> happen in REXX, but may happen in PL/I.
|>
|> (I have to thank you for asking this; if you hadn't, I wouldn't have
|> discovered Open Object REXX; I've missed having REXX since I had to
|> abandon OS/2.)

| Please correct Line-3 to |
| do while (abs(y - y1) > 0)
|
| To see why, run the original and corrected versions with x = 1027 and p
| = 10.
|
| The code is simply an integer version of Newton's method.

What I'm looking for is a specific routine just for integer cube roots,
similar to the integer square root (above), which isn't (as far as I
know, an integer version of Newton's method). I know there is one, and
I was hoping that somebody whould know of it (or where it is on the www).
The general routine (above) for any root seems like overkill for what I
wanted to do. __________________________________________________ Gerard S.
  Reply With Quote
5 20th March 23:45
gerard schildberger
External User
 
Posts: 1
Default integer cube root ...


| > Gerard Schildberger wrote:
| >> Below is a subroutine to find the integer squart root (I've
| >> forgetten who posted this via this newsgroup):
| >> __________________________________________________ ____
| >>
| >> /*find integer sqrt of a non-negative #.*/
| >>
| >> isqrt: procedure; parse arg x; x=trunc(x); r=0; q=1
| >>
| >> do while q<=x
| >> q=q*4
| >> end
| >>
| >> do while q>1
| >> q=q%4
| >> _=x-r-q
| >> r=r%2
| >>
| >> if _>=0 then do
| >> x=_
| >> r=r+q
| >> end
| >>
| >> end
| >>
| >> return r
| >> __________________________________________________ _____
| >>
| >>
| >> Does anyone know of (or where can I get it) an integer
| >> cube root routine? ______________________________Gerard S.
| >
| > nth_root: procedure; arg x, p; x = trunc(x); p = trunc(p)
| > y = 1; y1 = -1; p1 = p - 1
| > do while (abs(y - y1) > 1)
| > y1 = y
| > y = (p1 * y + x % y ** p1) % p
| > end
| > if y ** p < x then
| > return y1
| > else
| > return y
| >
| > The above is not completely stable against overflow, which will not
| > happen in REXX, but may happen in PL/I.
| >
| > (I have to thank you for asking this; if you hadn't, I wouldn't have
| > discovered Open Object REXX; I've missed having REXX since I had to
| > abandon OS/2.)
| >
|
| Please correct Line-3 to |
| do while (abs(y - y1) > 0)
|
| To see why, run the original and corrected versions with x = 1027 and p
| = 10.
|
| The code is simply an integer version of Newton's method.

I tried x=99, p=2 and it loops. ______________________________Gerard S.
  Reply With Quote
6 20th March 23:45
glen herrmannsfeldt
External User
 
Posts: 1
Default integer cube root ...


Find a book an how to use an abacus that includes square
and cube roots. I once bought a cheap abacus in a chinatown
store that included a little book.

For the square root algorithm, similar to the integer square
root algorithm you mention, successive odd integers are subtracted
from the high digit, and the number of such is counted.
(The sum of the first n odd integers is n**2.)
(Though with a shift of the decimal point it will work for
fixed point non-integer numbers, too.)

Then there is a correction when you shift to the next lower digits
(one in the accumulating square root, and two in the ever decreasing
starting number.) The square root algorithm comes from the identity
(10A+B)**2=100A+20AB+B**2

The cube root algorithm takes three sets of abacus columns,
accumulating x and x**2 while reducing the original number by x**3.
(For more digits you put two abaci side by side.)

See: http://hem.passagen.se/ceem/china.htm

The cube root algorithm comes from:
(10A+B)**3=1000A**3+300*A**2*B+30*A*B**2+B**3


-- glen
  Reply With Quote
7 20th March 23:45
gerard schildberger
External User
 
Posts: 1
Default integer cube root ...


| > What I'm looking for is a specific routine just for integer cube roots,
| > similar to the integer square root (above), which isn't (as far as I
| > know, an integer version of Newton's method). I know there is one, and
| > I was hoping that somebody whould know of it (or where it is on the www).
| > The general routine (above) for any root seems like overkill for what I
| > wanted to do. __________________________________________________ Gerard S.
|
|
| Find a book an how to use an abacus that includes square
| and cube roots. I once bought a cheap abacus in a chinatown
| store that included a little book.

Oh, great. I ask what time it is, and you tell me to buy a book
on how to build a watch. Not everyone has access to a good book
store. _____Gerard S.


| For the square root algorithm, similar to the integer square
| root algorithm you mention, successive odd integers are subtracted
| from the high digit, and the number of such is counted.
| (The sum of the first n odd integers is n**2.)
| (Though with a shift of the decimal point it will work for
| fixed point non-integer numbers, too.)

I don't see that in the code below, a subroutine to find the
integer squart root:
__________________________________________________ ____

/*find integer sqrt of a non-negative #.*/

isqrt: procedure; parse arg x; x=trunc(x); r=0; q=1

do while q<=x
q=q*4 end
do while q>1
q=q%4
_=x-r-q r=r%2
if _>=0 then do
x=_
r=r+q
end

end

return r
__________________________________________________ _____

(% is integer divide in Rexx). __________________Gerard S.


| Then there is a correction when you shift to the next lower digits
| (one in the accumulating square root, and two in the ever decreasing
| starting number.) The square root algorithm comes from the identity
| (10A+B)**2=100A+20AB+B**2
|
| The cube root algorithm takes three sets of abacus columns,
| accumulating x and x**2 while reducing the original number by x**3.
| (For more digits you put two abaci side by side.)
|
| See: http://hem.passagen.se/ceem/china.htm
|
| The cube root algorithm comes from:
| (10A+B)**3=1000A**3+300*A**2*B+30*A*B**2+B**3
| -- glen
  Reply With Quote
8 20th March 23:45
john w. kennedy
External User
 
Posts: 1
Default integer cube root ...


With your correction, it can fall into a loop. I should have gone with
my first instinct; doing it with integers is a dumb idea.

How to get a cube root:

cube_root: procedure; arg x; x = trunc(x)
return trunc(x ** 1/3) Ta-da!


--
John W. Kennedy
"But now is a new thing which is very old--
that the rich make themselves richer and not poorer,
which is the true Gospel, for the poor's sake."
-- Charles Williams. "Judgement at Chelmsford"
  Reply With Quote
9 20th March 23:45
gerard schildberger
External User
 
Posts: 1
Default integer cube root ...


|> John W. Kennedy wrote:
|>> Gerard Schildberger wrote:
|>>> Below is a subroutine to find the integer squart root (I've
|>>> forgetten who posted this via this newsgroup):
|>>> __________________________________________________ ____
|>>> /*find integer sqrt of a non-negative #.*/
|>>> isqrt: procedure; parse arg x; x=trunc(x); r=0; q=1
|>>> do while q<=x
|>>> q=q*4
|>>> end
|>>> do while q>1
|>>> q=q%4
|>>> _=x-r-q
|>>> r=r%2
|>>> if _>=0 then do
|>>> x=_
|>>> r=r+q
|>>> end
|>>> end
|>>> return r
|>>> __________________________________________________ _____
|>>> Does anyone know of (or where can I get it) an integer
|>>> cube root routine? ______________________________Gerard S. |
| >> nth_root: procedure; arg x, p; x = trunc(x); p = trunc(p)
| >> y = 1; y1 = -1; p1 = p - 1
| >> do while (abs(y - y1) > 1)
| >> y1 = y
| >> y = (p1 * y + x % y ** p1) % p
| >> end
| >> if y ** p < x then
| >> return y1
| >> else
| >> return y
| >>
| >> The above is not completely stable against overflow, which will not
| >> happen in REXX, but may happen in PL/I.
| >>
| >> (I have to thank you for asking this; if you hadn't, I wouldn't have
| >> discovered Open Object REXX; I've missed having REXX since I had to
| >> abandon OS/2.) |
| > Please correct Line-3 to
| > do while (abs(y - y1) > 0)
| > To see why, run the original and corrected versions with x = 1027 and p
| > = 10.

| With your correction, it can fall into a loop. I should have gone with
| my first instinct; doing it with integers is a dumb idea.
|
| How to get a cube root:
|
| cube_root: procedure; arg x; x = trunc(x)
| return trunc(x ** 1/3)
|
| Ta-da!

Nice joke (if you intended it to be). It simply doesn't work in Rexx,
which is where I originally posted from and wanted a REXX solution.

By the way, Robin Vowels gave me a working version (in PL/1) which is
copyrighted, so I won't post it here.
__________________________________________________ _____________Gerard S.
  Reply With Quote
10 20th March 23:46
n. shamsundar
External User
 
Posts: 1
Default integer cube root ...


If you are willing to use RxMath, the solution is:

/* cube root using float/truncate */

call RxFuncAdd"MathLoadFuncs","rxmath","MathLoadFuncs"
call MathLoadFuncs

yo=0
do x=1 to 100000 y=trunc(rxCalcPower(x+0.1,1/3.0))
if y<>yo then say 'Cube root of ' || x || ' is ' || y
yo=y
end
return

If you do not, here is the fixed-up Newton-Raphson integer code (no
copyright claimed) :

/* integer cube root by Newton-Raphson */
yo=0
do x=1 to 100000 y=cubrt(x)
if y<>yo then say 'Cube root of ' || x || ' is ' || y
yo=y
end
return

cubrt: procedure; arg x; x = trunc(x)
y = 1; v=1; v1=x;
if x < 8 then return 1
do while (abs(y*(v-v1)) > 2*v)
v1 = v
y = (2 * y + x % v) % 3
v = y * y
end
if v*y <= x then
return y
else
return y-1

-- N. Shamsundar
-- University of Houston
  Reply With Quote
Reply


Thread Tools
Display Modes


Some other forums that might be of your interest : Development, Ada, Apple script, Assembler, Awk, Beos, Basic, C, C++, C#, C# .net, .net, .net frameworks, Asp .net, Clarion, Clipper, Clos, Clu, Cobol, Coldfusion, Delphi, Dylan, Eiffel, Forth, Fortran, Haskell, Hermes, Icon, Idl, Java, Java script, Jscript .net, Jcl, Linoleum, Lisp, Lotus, Limbo, Logo, Ml, Mumps, Oberon, Postscript, Pop, Pl1, Prolog, Python, Ruby, Pascal, Perl, Php, Rebol, Rexx, Sed, Sather, Scheme, Smalltalk, Tcl, Vhdl, Vrml, Visual basic, Visual basic .net, Yorick, Mysql, Omnis, Postgresql, Xbase, Access, Oracle, Adabas, Berkeley, Btrieve, Filemaker, Gupta, Db2, Informix, Ingres, Mssql server, Object, Olap, Paradox, Rdb, Revelation, Sybase, Theory, Dbase, Html, Java script, Css, Flash, Photoshop, Corel script, Xml, Tech, Beos, Gem, Hp48, Hpux, Linux, Mac, Ms-dos, Os2, Palm, Solaris, Ti99, Windows, Xenix, Aos, Chorus, Geos, Inferno, Lantastic, Lynx, Mach, Minix, Netware, Os9, Parix, Plan9, Psos, Qnx, Xinu, Sco, Unix, Aix, Aux, 386bsd, Bsdi, Freebsd, Netbsd, Openbsd, Ultrix, Amd, Intel, Aptiva, Buz, Deals, Homebuilt, Overclocking, Programming, Extra forums


Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666