Click to See Complete Forum and Search --> : Getting the nearest prime number
everard
Aug 15th, 2006, 02:20 PM
Can someone give a simple code on how to get the nearest prime number?
For example:
Key Value: 0...60
Prime Number: 59 (since 59 is the nearest prime number to 60)
Note:
Prime number < key value
triggernum5
Aug 15th, 2006, 02:50 PM
What values might you be dealing with? There are multiple ways of doing this, but the best one will depend on your needs.. Would it be feasible to make a list of the primes in range before beginning? That would drastically speed things up.. Does it need to be the absolute closest prime? Because there are certain sets of primes that can be found relatively easily.. Beyond that, there are beowulf clusters dedicated to calculating large primes.. Its really a cpu intensive process regardless of method..
everard
Aug 15th, 2006, 03:22 PM
I just need the closest prime number, meaning the largest among the prime numbers that is less than the key value.
Dnereb
Aug 15th, 2006, 03:30 PM
I know it ain't code but you should be able to distill it starting at this page
http://en.wikipedia.org/wiki/Primality_test
everard
Aug 15th, 2006, 04:39 PM
Problem Solved!!!
I just used this code:
PrimeNumber = 0
For w = 1 To Val(txtFileSize.Text)
PrimeNumSwitch = 1
For z = 9 To 2 Step -1
If w = z Then
PrimeNumSwitch = 1
ElseIf w Mod z <= 0 Then
PrimeNumSwitch = 0
End If
Next
If PrimeNumSwitch = 1 Then
If w > PrimeNumber Then
PrimeNumber = Val(txtKeyValue(x).Text) Mod w
End If
End If
Next
lblAnswer(x).Caption = PrimeNumber
Thanks for the replies!
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.