Here's one of many of mine:
EDITED to do something useful with our calculations!
Code: (Select All)
_Title " A Simple Prime Sieve and Factor of 4 Digit Numbers" ' mod 2024-12-27 bplus
' sieving marks composites ONLY, primes are what is left when composites are crossed off
DefLng A-Z
topN = 9999 ' any 4 digit number
Dim ff(topN) ' need array to hold numbers crossed off, ff stands for first (prime) factor
For pcandidate = 2 To topN ' absolutely no optimation here!!!!
If ff(pcandidate) = 0 Then ' this is next prime discovered so cross off all composites
For i = pcandidate * pcandidate To topN Step pcandidate ' check all multiples of pcand
If ff(i) = 0 Then ff(i) = pcandidate ' going to use ff to factor numbers
Next
End If
Next
For i = 2 To topN
If ff(i) = 0 Then c = c + 1
Next
Print c; " prime numbers 2 to 9999."
Do
Input "Enter a 4 digit number to factor or see if prime ", n
m = n
While ff(m)
Print ff(m);
m = m / ff(m)
Wend
If m = n Then Print n; "is prime." Else Print m
Loop Until n < 2
EDITED to do something useful with our calculations!
b = b + ...