Created
October 3, 2018 01:23
-
-
Save SYZYGY-DEV333/d2e364eacccd0ef6154e714f2e9b96dc to your computer and use it in GitHub Desktop.
Nth root of A. `gst nth-root-a.st -a N A`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Number extend [ | |
nthRoot: n [ | |
|x0 m x1| | |
x0 := (self / n) asFloatD. | |
m := n - 1. | |
[true] whileTrue: [ | |
x1 := ((m * x0) + (self/(x0 raisedTo: m))) / n. | |
((x1 - x0) abs) < ((x0 * 1e-9) abs) | |
ifTrue: [ ^ x1 ]. | |
x0 := x1. | |
] | |
] | |
]. | |
(((Smalltalk getArgv: 2) asNumber) nthRoot: ((Smalltalk getArgv: 1) asNumber)) displayNl. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pseudocode: