+ Reply to Thread
Results 1 to 5 of 5

Prime numbers

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-21-2006
    Location
    Ossett, West Yorkshire
    MS-Off Ver
    2003
    Posts
    150

    Prime numbers

    Is there a function that will indicate a prime number or if not a prime numberwhat the lowest common denomenator is?

  2. #2
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Hi,

    For prime numbers, you can use the UDF code proposed by Myrna Larson ...

    Function Prime(TestNum As Long) 
      Dim PrimeCnt As Long 
      Dim y As Long 
      Dim x As Long 
      Dim Flag As Boolean 
      Dim Primes() As Long 
      Dim NumStop As Double 
      ReDim Primes(1 To 2) 
      NumStop = Sqr(TestNum) 
      If TestNum = 1 Or _ 
         TestNum = 2 Or _ 
         TestNum = 3 Or _ 
         TestNum = 5 Then 
           Prime = True 
           Exit Function 
      End If 
      Primes(1) = 2 
      Primes(2) = 3 
      PrimeCnt = 2 
      x = 3 
      Do 
        x = x + 2 
        For y = 3 To Sqr(x) Step 2 
          If x Mod y = 0 Then GoTo NoPrime1 
        Next y 
        PrimeCnt = PrimeCnt + 1 
        ReDim Preserve Primes(1 To PrimeCnt) 
        Primes(PrimeCnt) = x 
    NoPrime1: 
      Loop Until Primes(PrimeCnt) > NumStop 
      For i = LBound(Primes) To UBound(Primes) 
        If TestNum Mod Primes(i) = 0 Then 
          Debug.Print i, Primes(i), TestNum / Primes(i) 
          Prime = False 
          Exit Function 
        End If 
      Next 
      Prime = True 
    End Function
    HTH
    Carim


    Top Excel Links

  3. #3
    Forum Contributor
    Join Date
    08-21-2006
    Location
    Ossett, West Yorkshire
    MS-Off Ver
    2003
    Posts
    150

    Smile Prime numbers

    Thanks Carim that works fine
    Regards Howard

  4. #4
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,721
    Quote Originally Posted by Gearcutter
    Is there a function that will indicate a prime number or if not a prime numberwhat the lowest common denomenator is?
    Do you mean the lowest factor (not counting 1)? This formula will generate the lowest factor (not counting 1) of a number in A1

    =MIN(IF(MOD(A1,ROW(INDIRECT("1:"&FLOOR(A1^(1/2),1)))+1)=0,ROW(INDIRECT("1:"&FLOOR(A1^(1/2),1)))+1))

    confirmed with CTRL+SHIFT+ENTER

    If the result is 0 then A1 contains a prime number

    note: it won't work if A1 is greater than 268435455

    ....or here's a UDF, courtesy of Bernard Liengme, which will return either "prime" or give the lowest factor

    Function prime(num)
        Dim j, k, remain As Integer
        j = Int(num)
        If j <> num Then
          prime = "NonInt"
          Exit Function
        End If
        j = Sqr(num)
        For k = 2 To j
         remain = num Mod k
         If remain = 0 Then
            prime = k
            Exit Function
         End If
        Next k
        prime = "Prime"
    End Function

  5. #5
    Forum Contributor
    Join Date
    08-21-2006
    Location
    Ossett, West Yorkshire
    MS-Off Ver
    2003
    Posts
    150

    Prime numbers

    Hi Daddylonglegs, thank you for the formula and the function, this one is even better as it will return the lcd if the number is not a prime the array formula I will certainly try, I won't bore you with what I am trying to do with these numbers but now my task looks a lot easier, thanks once again
    Howard

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1