Number of all prime numbers within n

xiaoxiao2021-03-06  40

Private

SUB LLSTART_LINKCLICKED

BYVAL Sender

As system.object,

ByVal E

AS

_

System.windows.Forms.LinkLabellinkClickedEventArgs_

Handles llstart.linkClicked

IF isnumeric (me.txtmaxnbr.text) =

False

THEN

Msgbox ("Max Nbr Must Be Numeric!", _

MsgboxStyle.exclamation, _

"Max Nbr Not Numeric")

Return

End

IF

DIM N, I, J N = CINT (Me.txtMaxnbr.Text) DIM Z (N) DIM STARTTIME AS dateTime = now

'INITIALIZE ARRAY for i = 0 to N z (i) = 0 Next

'Mark Multiples of i

For i = 2 to n / 2 for j = 2 * i to n Step i z (j) = 1 Next Next

'Count unmarked Numbers, Which Are Primes Dim NBR = 0

For i = 2 to n if z (i) = 0 THEN NBR = 1 end if Next

DIM TS AS New TimeSpan (now.ticks - starttime.ticks)

Me.lblnBrPrimes.Text = nbr me.lbltime.text = ts.tostring

End Sub Reply: This comilated code has determined my confidence in VB.NET.

For i = 2 to n / 2

For j = 2 * i to n Step i

z (j) = 1

NEXT

NEXT

This algorithm means: traversing 2 to N / 2 Number I, for each I, set all the multiple of 1 in the array z. This z is actually expressed in the BOOL type, 1 is the number, 0 is the number.

For i = 2 to n

IF z (i) = 0 THEN

NBR = 1

END IF

NEXT

Traverse Z, NBR to write down how many prime numbers.

Original algorithm, please see: http://www.math.utah.edu/classes/216/assignment-07.html

This algorithm is indeed very excellent. Its algorithm z (j) = 1 The number of steps to perform the number of steps is x, X is less than N number, it is clear X <= n.

转载请注明原文地址:https://www.9cbs.com/read-78978.html

New Post(0)