简体   繁体   中英

How can I generate text file with all the prime numbers in progress 4gl

In the below code i am importing a text file with numbers 3 to 99 .

i have partially written code to find out the prime numbers from the given numbers.

 define variable a as integer.

 define variable b as integer.

 define variable i as integer initial 0.


    INPUT FROM Value ( "C:\src\New folder\bingo.txt"). 
    repeat :

      IMPORT a. 
      b = a mod i.
      if b=0 then
      do:
        if a=i then 
        do:
          message " prime number"a view-as alert-box.
        end.
        else if a<>i then 
        do:
          i=i + 1.
          message " not prime number"a view-as alert-box.

        end.

        else if a < i then 
        do:
          message "not prime number"a view-as alert-box.   
        end.    
      end.  
      END.     
      if b<>0 then
      do:    
        i=i + 1.       
        message "b<>0"b view-as alert-box.      
      end.
      end.
   Output To Value( "C:\src\New folder\even.txt")append. 
   export a space.
   output close.
   end.   
   end.

help me with . to program

I think it will make more sense to store the prime check in a function or a procedure. This prime check isn't necessary perfect but it will work for these conditions.

Also it's best to define and use streams when working with both in- and output.

DEFINE VARIABLE a AS INTEGER     NO-UNDO.

DEFINE VARIABLE b AS INTEGER     NO-UNDO.
DEFINE STREAM strIn.
DEFINE STREAM strOut.

FUNCTION isPrime RETURNS LOGICAL (INPUT iNum AS INTEGER ):

    DEFINE VARIABLE iDiv   AS INTEGER     NO-UNDO.

    DO iDiv = 2 TO INTEGER(SQRT(iNum)):
        IF iNum MOD iDiv = 0 THEN
            RETURN FALSE.
    END.

    RETURN TRUE.
END.

OUTPUT STREAM strOut TO VALUE ("c:\temp\primes.txt").
INPUT STREAM strIn FROM Value ( "C:\temp\bingo.txt"). 
repeat :

    IMPORT STREAM strIn a. 

    IF isPrime(a) THEN DO:
        /*MESSAGE "prime number" a view-as alert-box. */
        PUT STREAM strOut UNFORMATTED a SKIP.
    END. 
    ELSE DO:
        /*MESSAGE "not prime number" a view-as alert-box. */
    END.    

END.
INPUT STREAM strIn CLOSE.
OUTPUT STREAM strOut CLOSE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM