home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 February / PCWK0297.iso / envelop / envelop.6 / Tools / Bootcamp / advanced / sieve / SIEVE.ETO < prev    next >
Text File  |  1996-07-08  |  10KB  |  392 lines

  1. Type GenericSieve
  2.  
  3.   ' METHODS for object: GenericSieve
  4.   Sub ListPrimes(LstBox As ListBox)
  5.   End Sub
  6.  
  7.   Sub Run()
  8.   End Sub
  9.  
  10.   Sub SetLimit(ByVal Lim As Integer)
  11.   End Sub
  12.  
  13. End Type
  14.  
  15. Type BetterSieve From GenericSieve
  16.   Dim Prime As New DataBuffer
  17.  
  18.   ' METHODS for object: BetterSieve
  19.   Sub ListPrimes(LBox As ListBox)
  20.     Dim I As Integer
  21.   
  22.     For I = 1 To Prime.Size - 1
  23.       If Prime.GetByte(I) Then LBox.AddItem(I)
  24.     Next I
  25.     SieveForm.LblNumOfPrimes.Caption = LBox.ListCount
  26.   End Sub
  27.  
  28.   Sub Run()
  29.     Dim I, J, Finish As Integer
  30.   
  31.     Prime.FillBytes(0, Prime.Size, True)
  32.     Finish = int(sqr(Prime.Size - 1) + 1)
  33.   
  34.     For I = 2 To Finish
  35.       If Prime.GetByte(I) Then 
  36.         J = I + I
  37.         While J < Prime.Size
  38.           If Prime.GetByte(J) Then Prime.SetByte(J, False)
  39.           J = J + I
  40.         Wend
  41.       End If
  42.     Next I
  43.   End Sub
  44.  
  45.   Sub SetLimit(Lim As Integer)
  46.     Prime.Size = Lim + 1
  47.   End Sub
  48.  
  49. End Type
  50.  
  51. Type BestSieve From GenericSieve
  52.   Dim Prime As New DataBuffer
  53.  
  54.   ' METHODS for object: BestSieve
  55.   Sub ListPrimes(LBox As ListBox)
  56.     Dim I, N As Integer
  57.   
  58.     LBox.AddItem(1)
  59.     LBox.AddItem(2)
  60.     N = Prime.Size \ 2 - 1
  61.     For I = 1 To N
  62.       If Prime.GetByte(I) Then LBox.AddItem((2 * I) + 1)
  63.     Next I
  64.     SieveForm.LblNumOfPrimes.Caption = LBox.ListCount
  65.   End Sub
  66.  
  67.   Sub Run()
  68.     Dim I, J, N, N2, K As Integer
  69.     Dim Finish As Integer
  70.   
  71.     Prime.FillBytes(0, Prime.Size, True)
  72.   
  73.     Finish = int(sqr(Prime.Size - 1) + 1)
  74.     I = 1
  75.     For N = 3 To Finish Step 2
  76.       If Prime.GetByte(I) Then 
  77.         N2 = N + N
  78.         J = N2 + N
  79.         While J < Prime.Size
  80.           K = J \ 2
  81.           If Prime.GetByte(K) Then Prime.SetByte(K, False)
  82.           J = J + N2
  83.         Wend
  84.       End If
  85.       I = I + 1
  86.     Next N
  87.   
  88.   End Sub
  89.  
  90.   Sub SetLimit(Lim As Integer)
  91.     Prime.Size = Lim + 1
  92.   End Sub
  93.  
  94. End Type
  95.  
  96. Type GreedySieve From GenericSieve
  97.   Dim Prime As New DataBuffer
  98.  
  99.   ' METHODS for object: GreedySieve
  100.   Sub ListPrimes(LBox As ListBox)
  101.     Dim I As Integer
  102.   
  103.     For I = 1 To Prime.Size - 1
  104.       If Prime.GetByte(I) Then LBox.AddItem(I)
  105.     Next I
  106.     SieveForm.LblNumOfPrimes.Caption = LBox.ListCount
  107.   End Sub
  108.  
  109.   Sub Run()
  110.     Dim I, J, N As Integer
  111.   
  112.     Prime.FillBytes(0, Prime.Size, True)
  113.   
  114.     N = Prime.Size - 1
  115.     For I = 2 To N
  116.       If Prime.GetByte(I) Then 
  117.         J = I + I
  118.         While J < Prime.Size
  119.           If Prime.GetByte(J) Then Prime.SetByte(J, False)
  120.           J = J + I
  121.         Wend
  122.       End If
  123.     Next I
  124.   End Sub
  125.  
  126.   Sub SetLimit(Lim As Integer)
  127.     Prime.Size = Lim + 1
  128.   End Sub
  129.  
  130. End Type
  131.  
  132. Type SieveForm From SampleMasterForm
  133.   Type BtnSieve1 From Button
  134.   End Type
  135.   Type BtnSieve2 From Button
  136.   End Type
  137.   Type BtnSieve3 From Button
  138.   End Type
  139.   Dim LstPrimes As New ListBox
  140.   Dim LblNumOfPrimes As New Label
  141.   Dim LblNumOfPrimesLbl As New Label
  142.   Type LblSieve1 From Label
  143.   End Type
  144.   Type LblSieve2 From Label
  145.   End Type
  146.   Type LblSieve3 From Label
  147.   End Type
  148.   Dim LblSizeLbl As New Label
  149.   Dim LblSize As New Label
  150.   Dim SbSizeSelector As New ScrollBar
  151.   Dim font As New Font
  152.   Dim LblGreedyTime As New Label
  153.   Dim LblBetterTime As New Label
  154.   Dim LblBestTime As New Label
  155.   Dim font2 As New Font
  156.  
  157.   ' METHODS for object: SieveForm
  158.   Sub BtnSieve1_Click()
  159.     Dim I as integer
  160.   
  161.     LstPrimes.Clear
  162.     LblNumOfPrimes.Caption = ""
  163.     LblGreedyTime.Caption = ""
  164.   
  165.     GreedySieve.SetLimit(SieveForm.SbSizeSelector.Value)
  166.   
  167.     CPUStopClock.Start
  168.     GreedySieve.Run
  169.     CPUStopClock.Finish
  170.   
  171.     LblGreedyTime.Caption = Right$(CPUStopClock.ElapsedTime, 6)
  172.     GreedySieve.ListPrimes(LstPrimes)
  173.   
  174.     CPUStopClock.Reset
  175.   End Sub
  176.  
  177.   Sub BtnSieve2_Click()
  178.     Dim I as integer
  179.   
  180.     LstPrimes.Clear
  181.     LblNumOfPrimes.Caption = ""
  182.     LblBetterTime.Caption = ""
  183.   
  184.     BetterSieve.SetLimit(SieveForm.SbSizeSelector.Value)
  185.   
  186.     CPUStopClock.Start
  187.     BetterSieve.Run
  188.     CPUStopClock.Finish
  189.   
  190.     LblBetterTime.Caption = Right$(CPUStopClock.ElapsedTime, 6)
  191.     BetterSieve.ListPrimes(LstPrimes)
  192.   
  193.     CPUStopClock.Reset
  194.   End Sub
  195.  
  196.   Sub BtnSieve3_Click()
  197.     Dim I as integer
  198.   
  199.     LstPrimes.Clear
  200.     LblNumOfPrimes.Caption = ""
  201.     LblBestTime.Caption = ""
  202.   
  203.     BestSieve.SetLimit(SieveForm.SbSizeSelector.Value)
  204.   
  205.     CPUStopClock.Start
  206.     BestSieve.Run
  207.     CPUStopClock.Finish
  208.   
  209.     LblBestTime.Caption = Right$(CPUStopClock.ElapsedTime, 6)
  210.     BestSieve.ListPrimes(LstPrimes)
  211.   
  212.     CPUStopClock.Reset
  213.   
  214.   End Sub
  215.  
  216.   Sub ResetApplication_Click()
  217.     LoadForm
  218.     SbSizeSelector.Value = 20
  219.     LblSize.Caption = SbSizeSelector.Value
  220.     LblNumOfPrimes.Caption = ""
  221.     LstPrimes.Clear
  222.     LblGreedyTime.Caption = ""
  223.     LblBetterTime.Caption = ""
  224.     LblBestTime.Caption = ""
  225.   End Sub
  226.  
  227.   Sub Resize()
  228.     LstPrimes.Height = ScaleHeight - LstPrimes.Top - 135
  229.   End Sub
  230.  
  231.   Sub SbSizeSelector_Change()
  232.     LblSize.Caption = SieveForm.SbSizeSelector.Value
  233.     LblNumOfPrimes.Caption = ""
  234.     LblGreedyTime.Caption = ""
  235.     LblBetterTime.Caption = ""
  236.     LblBestTime.Caption = ""
  237.     LstPrimes.Clear
  238.   End Sub
  239.  
  240.   Sub SbSizeSelector_Scroll()
  241.     LblSize.Caption = SieveForm.SbSizeSelector.Value
  242.   End Sub
  243.  
  244. End Type
  245.  
  246. Begin Code
  247. ' Reconstruction commands for object: GenericSieve
  248. '
  249.   With GenericSieve
  250.   End With  'GenericSieve
  251. ' Reconstruction commands for object: BetterSieve
  252. '
  253.   With BetterSieve
  254.     With .Prime
  255.     End With  'BetterSieve.Prime
  256.   End With  'BetterSieve
  257. ' Reconstruction commands for object: BestSieve
  258. '
  259.   With BestSieve
  260.     With .Prime
  261.     End With  'BestSieve.Prime
  262.   End With  'BestSieve
  263. ' Reconstruction commands for object: GreedySieve
  264. '
  265.   With GreedySieve
  266.     With .Prime
  267.     End With  'GreedySieve.Prime
  268.   End With  'GreedySieve
  269. ' Reconstruction commands for object: SieveForm
  270. '
  271.   With SieveForm
  272.     .Caption := "Sieve"
  273.     .ForeColor := 16711680
  274.     .Font := SieveForm.font
  275.     .Move(3765, 1875, 5685, 4680)
  276.     .SampleDir := "W:\bootcamp\advanced\sieve\"
  277.     .SampleName := "sieve"
  278.     With .BtnSieve1
  279.       .Caption := "Greedy"
  280.       .ForeColor := 0
  281.       .ZOrder := 8
  282.       .Move(120, 975, 1050, 450)
  283.     End With  'SieveForm.BtnSieve1
  284.     With .BtnSieve2
  285.       .Caption := "Better"
  286.       .ForeColor := 0
  287.       .ZOrder := 7
  288.       .Move(120, 2070, 1050, 450)
  289.     End With  'SieveForm.BtnSieve2
  290.     With .BtnSieve3
  291.       .Caption := "Best"
  292.       .ForeColor := 0
  293.       .ZOrder := 6
  294.       .Move(120, 3120, 1050, 450)
  295.     End With  'SieveForm.BtnSieve3
  296.     With .LstPrimes
  297.       .Caption := "Primes"
  298.       .ForeColor := 128
  299.       .ZOrder := 2
  300.       .Move(4320, 1020, 1140, 2730)
  301.       .Sorted := False
  302.     End With  'SieveForm.LstPrimes
  303.     With .LblNumOfPrimes
  304.       .ForeColor := 128
  305.       .ZOrder := 4
  306.       .Move(4320, 555, 1050, 270)
  307.     End With  'SieveForm.LblNumOfPrimes
  308.     With .LblNumOfPrimesLbl
  309.       .Caption := "# of Primes:"
  310.       .ZOrder := 9
  311.       .Move(2730, 555, 1500, 270)
  312.       .Alignment := "Right"
  313.     End With  'SieveForm.LblNumOfPrimesLbl
  314.     With .LblSieve1
  315.       .Caption := "This is an implementation of Eratosthenes' sieve, which finds prime numbers using a nested loop to eliminate multiples of previously known primes. Times are in CPU seconds."
  316.       .ForeColor := 16711680
  317.       .Font := SieveForm.font2
  318.       .ZOrder := 10
  319.       .Move(1320, 960, 2850, 960)
  320.     End With  'SieveForm.LblSieve1
  321.     With .LblSieve2
  322.       .Caption := "The better algorithm optimizes by stopping the outer loop at the square root of 'size', since at that point all non-primes up to 'size' have been found."
  323.       .ForeColor := 16711680
  324.       .Font := SieveForm.font2
  325.       .ZOrder := 13
  326.       .Move(1320, 2070, 2850, 900)
  327.     End With  'SieveForm.LblSieve2
  328.     With .LblSieve3
  329.       .Caption := "The best algorithm optimizes time and space by not representing or testing any even numbers. "
  330.       .ForeColor := 16711680
  331.       .Font := SieveForm.font2
  332.       .ZOrder := 12
  333.       .Move(1320, 3120, 2850, 900)
  334.     End With  'SieveForm.LblSieve3
  335.     With .LblSizeLbl
  336.       .Caption := "Size:"
  337.       .ZOrder := 11
  338.       .Move(3510, 135, 750, 300)
  339.       .Alignment := "Right"
  340.     End With  'SieveForm.LblSizeLbl
  341.     With .LblSize
  342.       .Caption := "20"
  343.       .ForeColor := 128
  344.       .ZOrder := 14
  345.       .Move(4350, 150, 1050, 300)
  346.     End With  'SieveForm.LblSize
  347.     With .SbSizeSelector
  348.       .ZOrder := 15
  349.       .Move(135, 135, 3240, 300)
  350.       .SmallChange := 1
  351.       .LargeChange := 10
  352.       .Min := 20
  353.       .Max := 1000
  354.       .Value := 20
  355.       .Orientation := "Horizontal"
  356.       .Move(135, 135, 3240, 300)
  357.     End With  'SieveForm.SbSizeSelector
  358.     With .font
  359.       .FaceName := "MS Sans Serif"
  360.       .Size := 12.000000
  361.       .Bold := False
  362.       .Italic := False
  363.       .Strikethru := False
  364.     End With  'SieveForm.font
  365.     With .LblGreedyTime
  366.       .ForeColor := 128
  367.       .ZOrder := 5
  368.       .Move(120, 1425, 1050, 300)
  369.     End With  'SieveForm.LblGreedyTime
  370.     With .LblBetterTime
  371.       .ForeColor := 128
  372.       .ZOrder := 3
  373.       .Move(120, 2520, 1050, 300)
  374.     End With  'SieveForm.LblBetterTime
  375.     With .LblBestTime
  376.       .ForeColor := 128
  377.       .ZOrder := 1
  378.       .Move(120, 3570, 1050, 300)
  379.     End With  'SieveForm.LblBestTime
  380.     With .font2
  381.       .FaceName := "MS Sans Serif"
  382.       .Size := 8.000000
  383.       .Bold := False
  384.       .Italic := False
  385.       .Strikethru := False
  386.     End With  'SieveForm.font2
  387.     With .helpfile
  388.       .FileName := "W:\bootcamp\advanced\sieve\sieve.hlp"
  389.     End With  'SieveForm.helpfile
  390.   End With  'SieveForm
  391. End Code
  392.