The sieve of eratosthenes

Fortran

! Sieve of Eratosthenes in Fortran
! Written by Rob Hoelz

      function strlen(str)
        character str(*)
        integer i

        i = 0
10      if (str(i+1) .ne. ' ') then
          i = i + 1
          goto 10
        end if
        strlen = i
        return
      end

      function atoi (str)
        character str(*)
        integer s, i, length

        length = strlen(str)
        s = 0

        do 20 i = 1, length
          s = s * 10
          s = s + (iachar(str(i)) - iachar('0'))
20      continue
        atoi = s
        return
      end

      subroutine generatePrimes(primes, limit)
        logical primes(*)
        integer limit, currPrime, i

        primes(1) = .false.
        do 30 i = 2, limit
            primes(i) = .true.
30      continue

        currPrime = 2
40      if ((currPrime * currPrime) <= limit) then
          call removeMultiples(primes, currPrime, limit)
          call nextPrime(primes, currPrime, limit)
          goto 40
        end if
        return
      end

      subroutine nextPrime(primes, currPrime, limit)
        logical primes(*)
        integer currPrime, limit

        do 50 currPrime = currPrime + 1, limit
            if (primes(currPrime)) then
                return
            end if
50      continue

      return
      end subroutine nextPrime

      subroutine removeMultiples(primes, base, limit)
        logical primes(*)
        integer base, limit, i

        do 60 i = base * 2, limit, base
            primes(i) = .false.
60      continue
        return
      end

      subroutine printPrimes(primes, limit)
        logical primes(*)
        integer limit, i
        character(10) :: fmt

        fmt = "(I0 A $)"
        do 70 i = 2, limit
            if(primes(i)) then
                write(*,fmt) i, ' '
            end if
70      continue
        write(*,*)
        return
      end

      program sieve
        logical, dimension(:), allocatable :: primes
        character(32) limitString
        integer limit


        if (IARGC() > 0) then
          call GETARG(1, limitString)
          limit = atoi(limitString)
          allocate(primes(limit))
          call generatePrimes(primes, limit)
          call printPrimes(primes, limit)
          deallocate(primes)
        end if

        stop
      end

back to Eratosthenes page

stats

It is Friday August 29, 2008 12:50 am
This page served 868 times
This page last modified: April 14, 2008 11:28 am
Your IP address is: 38.103.63.61
You are browsing using: CCBot/1.0 (+http://www.commoncrawl.org/bot.html)
You are browsing from: United States.
badcomputer.org's uptime: 00:50:55 up 29 days, 15:57, 2 users, load average: 0.16, 0.06, 0.02

local

home | unix stuff | dir2ogg | sneetchalizer | wmainfo | q&d guide to permissions | q&d guide to tar and gzip | code | MS rant | browser shootout | linux & iAudio X5 | photos | music | programming poetry | sieve of Eratosthenes | plea | rain | suffer | archive | about | recipes | compaqr3000 | sitemap

search

Google

credits

hacker emblem

This page, and all pages on this site were created and are maintained by Darren Kirby using valid XHTML 1.0 and CSS, and are ©copyright 2002 - 2008. The Penguin image was created by Tukka, and is used by permission. Inspiration for the look of this site was provided by Eric A. Meyer's CSS gallery. This website runs on Gentoo Linux. It is served by Apache. PHP and MySQL hold together the backend.

advertisement