|
-
Nov 7th, 2007, 02:38 PM
#5
Thread Starter
PowerPoster
Re: A Prime Number Question
 Originally Posted by MaximilianMayrhofer
It would be interesting to see your colleagues code, as it might be possible to modify it for other mathematical functions
This runs faster than his does (my revised VB6 code). On my machine (1.7 Ghz), I can find the first 20,000 primes and throw them into a list box in less than 10 seconds:
Code:
Dim MyNumber As Long, NumFactors As Integer
Private Sub Form_Load()
MyNumber = 1
Do While List1.ListCount < 20000
NumFactors = 0
MyNumber = MyNumber + 1
For I = 1 To Sqr(MyNumber)
If MyNumber / I = MyNumber \ I Then
NumFactors = NumFactors + 1
If NumFactors > 1 Then Exit For ' Not a prime
End If
Next
If NumFactors = 1 Then List1.AddItem Str$(MyNumber) ' Prime Found
Loop
End Sub
If this program runs correctly, the 20,000th prime number is 224,737. The sequence starts with 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, ...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|