Advertisement
OgreVorbis

FreeBASIC Primes

Dec 20th, 2021
4,273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SUB PBMAIN ()
  2.  
  3.     DIM strInput AS STRING
  4.     DIM yorn AS STRING
  5.     DIM n AS LONG
  6.     DIM m AS LONG
  7.     DIM lim AS LONG
  8.     DIM s AS LONG
  9.     DIM startTime AS DOUBLE
  10.     DIM endTime AS DOUBLE
  11.     DIM nums() AS BYTE
  12.  
  13.     PRINT "This program will calculate primes using a simple algorithm."
  14.     INPUT "Enter limit for this search (enter for default): ", strInput
  15.     IF strInput = "" THEN strInput = "100000000"
  16.     lim = VAL(strInput)
  17.     s = SQR(lim)
  18.  
  19.     startTime = TIMER
  20.     REDIM nums(lim)
  21.  
  22.     FOR n = 2 TO s
  23.         IF nums(n) = 0 THEN
  24.             m = n * n
  25.             WHILE m <= lim
  26.                 nums(m) = 1
  27.                 m += n
  28.             WEND
  29.         END IF
  30.     NEXT
  31.  
  32.     endTime = TIMER
  33.     PRINT "It took " + STR$(endTime - startTime) + " seconds to complete."
  34.     INPUT "Would you like to display results (Y/N)? ", yorn
  35.     PRINT ""
  36.     IF yorn = "Y" OR yorn = "y" THEN PRINT "The primes up to " + STR$(lim) + " are:"
  37.     m = 0
  38.     FOR n = 2 TO lim
  39.         IF nums(n) = 0 THEN
  40.             IF yorn = "Y" OR yorn = "y" THEN PRINT STR$(n)
  41.             m += 1
  42.         END IF
  43.     NEXT
  44.     PRINT "Total primes count = " + STR$(m)
  45.     PRINT "Press any key to exit. . . ";
  46.     GETKEY
  47.  
  48. END SUB
  49.  
  50. PBMAIN
  51.  
  52. ' ==================================================================================================
  53. ' OLD POWERBASIC VERSION
  54. ' DELETE AFTER THIS LINE IF NOT USING
  55.  
  56. #COMPILE EXE
  57. #OPTIMIZE CODE ON
  58. #OPTIMIZE SPEED
  59. #DIM ALL
  60.  
  61. FUNCTION PBMAIN () AS LONG
  62.  
  63.     DIM strInput AS STRING
  64.     DIM yorn AS STRING
  65.     DIM n AS LONG
  66.     DIM m AS LONG
  67.     DIM lim AS LONG
  68.     DIM s AS LONG
  69.     DIM startTime AS DOUBLE
  70.     DIM endTime AS DOUBLE
  71.     DIM nums() AS BYTE
  72.  
  73.     PRINT "This program will calculate primes using a simple algorithm."
  74.     INPUT "Enter limit for this search (enter for default): ", strInput
  75.     IF strInput = "" THEN strInput = "100000000"
  76.     lim = VAL(strInput)
  77.     s = SQR(lim)
  78.  
  79.     startTime = TIMER
  80.     REDIM nums(lim)
  81.  
  82.     FOR n = 2 TO s
  83.         IF nums(n) = 0 THEN
  84.             m = n * n
  85.             WHILE m <= lim
  86.                 nums(m) = 1
  87.                 m += n
  88.             WEND
  89.         END IF
  90.     NEXT
  91.  
  92.     endTime = TIMER
  93.     PRINT "It took" + STR$(endTime - startTime) + " seconds to complete."
  94.     INPUT "Would you like to display results (Y/N)? ", yorn
  95.     PRINT ""
  96.     IF yorn = "Y" OR yorn = "y" THEN PRINT "The primes up to" + STR$(lim) + " are:"
  97.     m = 0
  98.     FOR n = 2 TO lim
  99.         IF nums(n) = 0 THEN
  100.             IF yorn = "Y" OR yorn = "y" THEN PRINT STR$(n)
  101.             m += 1
  102.         END IF
  103.     NEXT
  104.     PRINT "Total primes count =" + STR$(m)
  105.     PRINT "Press any key to exit. . . ";
  106.     WAITKEY$
  107.  
  108. END FUNCTION
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement