Like Tree1Likes
  • 1 Post By Yarin

Good example of some of the perks of Fortran

This is a discussion on Good example of some of the perks of Fortran within the Tech Board forums, part of the Community Boards category; I found some rather non-Fortranic Fortran code on a blog, and after correcting it, thought it'd be a good example ...

  1. #1
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    963

    Good example of some of the perks of Fortran

    I found some rather non-Fortranic Fortran code on a blog, and after correcting it, thought it'd be a good example highlighting some of the perks of Fortran. I'm somewhat of a Fortran evangelist, so I like showing this stuff off. Here's the original code, cleaned up somewhat:

    Code:
    program primes
    implicit none
    integer, parameter :: max = 10000000
    integer :: i, j, k, product(max), imax, jmax, numprime
    
    do k = 1, 100
       do i = 1, max
          product(i) = 0
       end do
       numprime = 0
    
       imax = floor(sqrt(real(max)))
       do i = 2, imax
          if (product(i) == 0) then
             jmax = max/i
             do j = 2, jmax
                product(i*j) = 1
             end do
          end if
       end do
    
       do i = 2, max
          if (product(i) == 0) then
             numprime = numprime + 1
          end if
       end do
    
       print *, 'Number of primes  = ', numprime
    end do
    
    end program primes
    And here would be a more preferred/optimal form:

    Code:
    program primes
    implicit none
    integer, parameter :: pmax = 10000000
    integer :: i, k, imax
    logical :: prod(pmax)
    
    do k = 1, 100
       prod(:) = .TRUE.
       imax = floor(sqrt(real(pmax)))
    
       do i = 2, imax
          if (prod(i)) then
             prod(2*i:pmax:i) = .FALSE.
          end if
       end do
    
       print *, 'Number of primes = ', count(prod)-1
    end do
    
    end program primes
    Changes:
    - product and max are built-in function names, changed names
    - can do a scalar assignment to an entire array
    - can do a masked assignment to an array
    - used the built-in function count appropriately

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    O_o

    Languages with such "list comprehension" are very nice.

    [Edit]
    So, it has been a while for me and FORTRAN...

    Code:
    prod(2*i:pmax:i) = .FALSE.
    That is saying "Recursively invalidate elements by assigning all elements from same elements within the next sparse array."?
    [/Edit]

    Soma
    Last edited by phantomotap; 03-03-2014 at 03:36 PM.
    “Often out of periods of losing come the greatest strivings toward a new winning streak.” -- Fred Rogers
    “Salem Was Wrong!” -- Pedant Necromancer

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,628
    FWIW, the release of F77 coincided with the release of the original Star Wars. And now, the next revision is set to come out with the next episode.
    laserlight likes this.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    FWIW, the release of F77 coincided with the release of the original Star Wars. And now, the next revision is set to come out with the next episode.
    O_o

    That explains the sinister leitmotif which followed one of my teachers: an "old hat" FORTRAN programmer.

    Soma
    “Often out of periods of losing come the greatest strivings toward a new winning streak.” -- Fred Rogers
    “Salem Was Wrong!” -- Pedant Necromancer

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    That is saying "Recursively invalidate elements by assigning all elements from same elements within the next sparse array."?
    O_o

    Colleague answered my question: the selection happens "LHS" while the assignment--"RHS"--is handled iteratively "over" the scalar value.

    So, I misunderstood the code a bit.

    Soma
    “Often out of periods of losing come the greatest strivings toward a new winning streak.” -- Fred Rogers
    “Salem Was Wrong!” -- Pedant Necromancer

  6. #6
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    963
    It basically is the equivalent of:

    Code:
    for (j = 2*i; j < max; j += i)
       prod(j) = false;
    but as far as Fortran goes, the whole idea with getting rid of as many DO loops as possible (in addition to making code smaller/neater), is to let the compiler know what can be vectorized.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++/Fortran on AIX
    By MarkZWEERS in forum C++ Programming
    Replies: 1
    Last Post: 02-18-2009, 09:52 AM
  2. Using a Fortran .lib
    By Mic in forum C++ Programming
    Replies: 5
    Last Post: 12-26-2008, 06:50 AM
  3. Its About Fortran
    By panfilero in forum Tech Board
    Replies: 2
    Last Post: 11-29-2005, 06:49 AM
  4. any good fortran help sites
    By smd in forum C++ Programming
    Replies: 3
    Last Post: 07-02-2002, 06:10 AM
  5. Where are my perks?
    By Generator in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 10-01-2001, 04:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21