An add-in that allows you to find the highest prime in a number

J

jrwilmott

I am looking for an add-in that would allow you to find the highest
prime divisor in a number. ie 68, 2x2x17, so 17 is the highest prime.
Any help would be much appreciated...
 
B

Bob Phillips

Here is a function that I adapted from something I found in a Google

Function MaxPrime(limit As Long)
Dim PrimeCnt As Long
Dim y As Long
Dim x As Long
Dim Primes

ReDim Primes(1 To limit)
PrimeCnt = 1
If limit Mod 2 = 0 Then
Primes(PrimeCnt) = 2
PrimeCnt = PrimeCnt + 1
End If
If limit Mod 3 = 0 Then
Primes(PrimeCnt) = 3
PrimeCnt = PrimeCnt + 1
End If
x = 3
Do
x = x + 2
For y = 3 To Sqr(x) Step 2
If x Mod y = 0 Then GoTo noprime
Next y
If limit Mod x = 0 Then
Primes(PrimeCnt) = x
PrimeCnt = PrimeCnt + 1
End If

noprime:
Loop Until x > limit

MaxPrime = Application.Max(Primes)

End Function


--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top