The sieve of eratosthenes

polyglot page

      IDENTIFICATION DIVISION.
      PROGRAM-ID.    SIEVE-PROG.
      * Sieve of Eratosthenes in Cobol
      * Written by Rob Hoelz

        ENVIRONMENT DIVISION.
        CONFIGURATION SECTION.
        INPUT-OUTPUT SECTION.

        DATA DIVISION.
        WORKING-STORAGE SECTION.
        01  Lim                PIC 9(6).
      * Change this number and recompile if you want to generate
      * primes over 1 million.  You'll also need to change the
      * picture clauses for Lim, Offset, CurrPrime, CurrPrime-Squared,
      * and Output-Value.
        01  PrimesTable.
            02  Primes             PIC 9(1) OCCURS 0 TO 1000000 TIMES
            DEPENDING ON Lim.
        01  Offset             PIC 9(6).
        01  CurrPrime          PIC 9(6).
        01  CurrPrime-Squared  PIC 9(6).
        01  Output-Value       PIC Z(5)9.
        PROCEDURE DIVISION.
           ACCEPT Lim FROM ARGUMENT-VALUE
               ON EXCEPTION
                   STOP RUN
           END-ACCEPT.

           SET Primes(1) TO 0.
           SET Offset TO 2.
           PERFORM WITH TEST BEFORE
               UNTIL Offset IS GREATER THAN Lim
               SET Primes(Offset) TO 1
               ADD 1 TO Offset
           END-PERFORM

           SET CurrPrime TO 2.
           MULTIPLY CurrPrime BY CurrPrime GIVING CurrPrime-Squared.
           PERFORM WITH TEST BEFORE
               UNTIL CurrPrime-Squared IS GREATER THAN Lim
               MULTIPLY CurrPrime BY 2 GIVING Offset
               PERFORM WITH TEST BEFORE UNTIL Offset > Lim
                   SET Primes(Offset) TO 0
                   ADD CurrPrime TO Offset
               END-PERFORM

               ADD 1 TO CurrPrime GIVING Offset
               PERFORM WITH TEST BEFORE
                   UNTIL Primes(Offset) IS EQUAL TO 1

                   ADD 1 TO Offset
               END-PERFORM
               SET CurrPrime TO Offset

               MULTIPLY CurrPrime BY CurrPrime GIVING CurrPrime-Squared
           END-PERFORM

           SET Offset TO 1.
           PERFORM WITH TEST BEFORE
               UNTIL Offset IS GREATER THAN Lim

               IF Primes(Offset) IS EQUAL TO 1 THEN
                   SET Output-Value TO Offset
                   DISPLAY Output-Value " " WITH NO ADVANCING
               END-IF

               ADD 1 TO Offset
           END-PERFORM
           DISPLAY "".
        STOP RUN.

back to Eratosthenes page

stats

It is Monday September 08, 2008 1:00 am
This page served 829 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: 01:00:18 up 39 days, 16:07, 2 users, load average: 0.31, 0.09, 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