home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l074 / 1.ddi / PRIMEG.TRU < prev    next >
Encoding:
Text File  |  1984-12-03  |  1.5 KB  |  63 lines

  1. ! Finds primes by sieving.
  2. !
  3. ! a True BASIC(tm), Inc. product
  4. !
  5. ! ABSTRACT
  6. !    PRIMEG uses True BASIC graphics to display
  7. !    the prime numbers between 1 and 100.
  8. !
  9. ! Copyright (c) 1985 by True BASIC, Inc.
  10.  
  11. SET MODE "graphics"
  12. DIM slot(100)
  13.  
  14. OPEN #1: screen 0,1,.1,.9
  15. SET WINDOW 0,10,0,10
  16. PLOT TEXT, at 2.75,-1: "Prime Number Sieve"
  17. SET COLOR "green"
  18. CALL squares(10,10)
  19. SET COLOR "yellow"
  20. CALL numbers
  21.  
  22. LET upper = 100                   ! primes to 100
  23. LET c = 1                         ! color to mark
  24. CALL mark(1,c)
  25. FOR n = 2 to upper                ! run through numbers
  26.     IF slot(n) = 0 then           ! 0 means it's prime
  27.        IF n*n <= upper then       ! bother to remove multiples
  28.           LET c = mod(c,3)+1      ! next color
  29.           FOR i = n  to upper step n    ! run through all multiples
  30.               LET slot(i) = 1     ! mark as nonprime
  31.               IF i<>n then CALL mark(i,c)   ! graphically
  32.           NEXT i
  33.           PAUSE 1
  34.        END IF
  35.     END IF
  36. NEXT n
  37. END
  38.  
  39. SUB squares(x,y)
  40.     FOR i=0 to x
  41.         PLOT i,0;i,y
  42.     NEXT i
  43.     FOR i=0 to y
  44.         PLOT 0,i;x,i
  45.     NEXT i
  46. END SUB
  47.  
  48. SUB mark(np,c)
  49.     LET n = np-1                  ! ignore 0
  50.     LET x = mod(n,10)
  51.     LET y = 9 - int(n/10)
  52.     SET COLOR c
  53.     BOX AREA x+.2,x+.8,y+.2,y+.8
  54. END SUB
  55.  
  56. SUB numbers                       !  Numbers into squares
  57.     FOR v = 0 to 9
  58.         FOR h = 1 to 10
  59.             PLOT TEXT, at h-.7,9.5-v: str$(v*10+h)
  60.         NEXT h
  61.     NEXT v
  62. END SUB
  63.