home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / rexx / 667 < prev    next >
Encoding:
Text File  |  1992-07-28  |  1.7 KB  |  53 lines

  1. Newsgroups: comp.lang.rexx
  2. Path: sparky!uunet!wupost!darwin.sura.net!cs.ucf.edu!ocoee!raitano
  3. From: raitano@ocoee.cs.ucf.edu (Tony Raitano )
  4. Subject: rexx n'th root function
  5. Message-ID: <1992Jul29.035717.26082@cs.ucf.edu>
  6. Sender: news@cs.ucf.edu (News system)
  7. Organization: University of Central Florida, Orlando
  8. Date: Wed, 29 Jul 1992 03:57:17 GMT
  9. Lines: 42
  10.  
  11. /* calc n'th root */
  12. /* this uses the standard square root finding method by taking the */
  13. /* average of the initial guess and the number/guess, and refines  */
  14. /* the guess each time, but THIS procedure divides the guess (n-1) */
  15. /* times, and averages that value with the guess.  The convergance */
  16. /* is a little faster for larger roots.  If the number of          */
  17. /* iterations gets too high, it simply returns a 0.  NOTE this is  */
  18. /* in exec form - to create a function, change the 'say' commands  */
  19. /* to 'return' commands, and take out the error messages, if needed*/
  20. /* written by michael campbell - released into public domain       */
  21. /* July 20, 1992.  Knock yourselves out. =)                        */
  22.  
  23.  
  24.  
  25. parse arg number base
  26. if base <> format(base,,0) | base < 0 then do
  27.    say 'root must be positive integer'
  28.    return 1
  29. end
  30.  
  31. if number < 0 then do
  32.    say 'number must be positive'
  33.    return 1
  34. end
  35.  
  36. lps = 0
  37. guess = number / base
  38. div = number / (guess ** (base - 1))
  39. delta = abs(div - guess)
  40. do while delta > .0000001     /* this can be changed for greater */
  41.                               /* or lesser accuracy              */
  42.    guess = (guess * (base - 1) + div) / base
  43.    div = number / (guess ** (base - 1)) /* divide guess n-1 times */
  44.    lps = lps + 1
  45.    delta = abs(div - guess)
  46.    if lps > 100 then do    /* number of iterations way too high */
  47.       say 0
  48.       exit
  49.    end
  50. end
  51. say guess
  52. exit
  53.