- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
NEVERMIND.... I figured it out...
Hi I am working on re-learning modern Fortran by working through the Project Euler examples.
In writing a fibonacci function a "return" is required to actually get the result pushed back, I seem to remember that was required in F77..
I would suspect that
f = fibonacci(x)
Would be enough but through trial and error I found that issuing a "return" at the end of the fibonacci function makes it all work correctly. But this is contrary to just about any fortran example I have seen posted on the web
Thanks ahead of time
! PROBLEM 2
integer function fibonacci(x)
implicit none
integer :: x
integer :: i
integer :: x_m_1, x_m_2
if (x == 0) then
fibonacci = 1
elseif (x == 1) then
fibonacci = 2
else
x_m_1 = 2
x_m_2 = 1
fibonacci = 0
do i = 2,x
fibonacci = x_m_1 + x_m_2
x_m_2 = x_m_1
x_m_1 = fibonacci
if (fibonacci < 0) then
stop
end if
end do
end if
return
end function fibonacci
subroutine problem_2
implicit none
integer :: sum = 0
integer :: even_sum = 0
integer :: x = 0
integer :: f
integer :: fibonacci
do while (f <= 4000000)
f = fibonacci(x)
!print *, "Fibonacci ", x, " = ", f
sum = sum + f
!print *, "sum = ", sum
if (mod(f, 2) == 0) then
even_sum = even_sum + f
end if
!print *, "even_sum = ", even_sum
x = x + 1
end do
print *, "Sum is ", sum
print *, "Even Sum is ", even_sum
end subroutine problem_2
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
F is not initialized before the do while loop so it may never enter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
good point... woops.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page