|
-
Aug 15th, 2006, 02:20 PM
#1
Thread Starter
Lively Member
Getting the nearest prime number
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
To become a PROFESSIONAL,
Start from SCRATCH... 
-
Aug 15th, 2006, 02:50 PM
#2
Hyperactive Member
Re: Getting the nearest prime number
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..
-
Aug 15th, 2006, 03:22 PM
#3
Thread Starter
Lively Member
Re: Getting the nearest prime number
I just need the closest prime number, meaning the largest among the prime numbers that is less than the key value.
To become a PROFESSIONAL,
Start from SCRATCH... 
-
Aug 15th, 2006, 03:30 PM
#4
Re: Getting the nearest prime number
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
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
-
Aug 15th, 2006, 04:39 PM
#5
Thread Starter
Lively Member
Re: Getting the nearest prime number
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!
To become a PROFESSIONAL,
Start from SCRATCH... 
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
|