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
Printable View
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
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..
I just need the closest prime number, meaning the largest among the prime numbers that is less than the key value.
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
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!