tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
13
Task/Nth-root/Groovy/nth-root-1.groovy
Normal file
13
Task/Nth-root/Groovy/nth-root-1.groovy
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import static Constants.tolerance
|
||||
import static java.math.RoundingMode.HALF_UP
|
||||
|
||||
def root(double base, double n) {
|
||||
double xOld = 1
|
||||
double xNew = 0
|
||||
while (true) {
|
||||
xNew = ((n - 1) * xOld + base/(xOld)**(n - 1))/n
|
||||
if ((xNew - xOld).abs() < tolerance) { break }
|
||||
xOld = xNew
|
||||
}
|
||||
(xNew as BigDecimal).setScale(7, HALF_UP)
|
||||
}
|
||||
21
Task/Nth-root/Groovy/nth-root-2.groovy
Normal file
21
Task/Nth-root/Groovy/nth-root-2.groovy
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
class Constants {
|
||||
static final tolerance = 0.00001
|
||||
}
|
||||
|
||||
print '''
|
||||
Base Power Calc'd Root Actual Root
|
||||
------- ------ ----------- -----------
|
||||
'''
|
||||
def testCases = [
|
||||
[b:32.0, n:5.0, r:2.0],
|
||||
[b:81.0, n:4.0, r:3.0],
|
||||
[b:Math.PI**2, n:4.0, r:Math.PI**(0.5)],
|
||||
[b:7.0, n:0.5, r:49.0],
|
||||
]
|
||||
|
||||
testCases.each {
|
||||
def r = root(it.b, it.n)
|
||||
printf('%7.4f %6.4f %11.4f %11.4f\n',
|
||||
it.b, it.n, r, it.r)
|
||||
assert (r - it.r).abs() <= tolerance
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue