!Program program euler implicit none integer, dimension(80, 80) :: original, updated integer :: row, col, changed, ans !Commas replaced with spaces in matrix.txt open(unit = 11, file="matrix.txt") do row = 1,80 read(11,*) (original(row, col),col=1,80) end do do row= 1,80 updated(row, 1) = original(row, 1) end do do col = 2,80 do row = 1,80 updated(row, col) = original(row,col) + updated(row, col-1) end do changed = 1 do while (changed .eq. 1) changed = 0 do row = 1,80 if (row > 1) then if (updated(row-1,col) < updated(row,col) - original(row,col)) then updated(row,col) = original(row,col) + updated(row-1,col) changed = 1 end if end if if (row < 80) then if (updated(row+1,col) < updated(row,col) - original(row,col)) then updated(row,col) = original(row,col) + updated(row+1,col) changed = 1 end if end if end do end do end do ans = 10000000 do row = 1,80 if ( updated(row, 80) < ans) then ans = updated(row, 80) end if end do print *, ans end program euler
Saturday, May 3, 2014
Problem 82 - Fortran: huh, maybe this isn't so bad
So, when I decided that I would use Cat in order to free up FORTRAN for problem number 82, I thought oh no, Fortran, that language is supposed to be awful, I remember having issues with it before when all I needed to do was something simple, this must be bad. Well, for the purposes I needed it for, Fortran was actually kind of pleasant to work with for problem 82 (note for anyone who knows Fortran, I am using Fortran 95, which certainly has something to do with it not being as terrible as the stories of fortran 70, etc). The main difficulty was just in loading the matrix: at first I thought it would be best to be lazy and just declare the big 80x80 grid of numbers in code, but it appears that Fortran doesn't like array literals above a certain relatively small size. So, I read them in from a file, which actually was a surprisingly painless task. I will note that I did not have to write any methods (subroutines), which I think contributed a lot to my positive outlook on this problem. Anyway, problem 82 is solved, and Fortran proves itself again to be blazingly fast, my solution (including reading in the 31K text file) runs in 10ms on my machine.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment