tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
42
Task/Nth-root/Fortran/nth-root.f
Normal file
42
Task/Nth-root/Fortran/nth-root.f
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
program NthRootTest
|
||||
implicit none
|
||||
|
||||
print *, nthroot(10, 7131.5**10)
|
||||
print *, nthroot(5, 34.0)
|
||||
|
||||
contains
|
||||
|
||||
function nthroot(n, A, p)
|
||||
real :: nthroot
|
||||
integer, intent(in) :: n
|
||||
real, intent(in) :: A
|
||||
real, intent(in), optional :: p
|
||||
|
||||
real :: rp, x(2)
|
||||
|
||||
if ( A < 0 ) then
|
||||
stop "A < 0" ! we handle only real positive numbers
|
||||
elseif ( A == 0 ) then
|
||||
nthroot = 0
|
||||
return
|
||||
end if
|
||||
|
||||
if ( present(p) ) then
|
||||
rp = p
|
||||
else
|
||||
rp = 0.001
|
||||
end if
|
||||
|
||||
x(1) = A
|
||||
x(2) = A/n ! starting "guessed" value...
|
||||
|
||||
do while ( abs(x(2) - x(1)) > rp )
|
||||
x(1) = x(2)
|
||||
x(2) = ((n-1.0)*x(2) + A/(x(2) ** (n-1.0)))/real(n)
|
||||
end do
|
||||
|
||||
nthroot = x(2)
|
||||
|
||||
end function nthroot
|
||||
|
||||
end program NthRootTest
|
||||
Loading…
Add table
Add a link
Reference in a new issue