Consulting

Results 1 to 13 of 13

Thread: prime numbers

  1. #1
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    prime numbers

    hello
    the following function should calculate how many numbers in a given range
    are prime numbers- they can be divided only in 1 or themselfs and return
    an integer without a residual.
    [vba]Function isprimer(lowernumber As Integer, highernumber As Integer)
    Dim i As Integer
    For i = lowernumber To highernumber
    If Not i Mod 2 = 0 Or Not i Mod 3 = 0 Or Not i Mod 5 = 0 Or Not i Mod 7 = 0 Then
    isprimer = i + 1
    End If
    Next
    End Function[/vba]
    thanks
    Last edited by Ken Puls; 01-28-2006 at 03:41 PM.
    moshe

  2. #2
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    another function

    hello
    i want to calculate how many prime numbers ther are in a range.
    [vba]Function isprimer1(r As Range)
    Dim cell As Object
    For Each cell In Selection
    If Not cell.Value Mod 2 = 0 Or Not cell.Value Mod 3 = 0 Or Not cell.Value Mod 5 = 0 Or Not cell.Value Mod 7 = 0 Then
    isprimer1 = cell + 1
    End If
    Next cell
    End Function[/vba]
    thats wrong?
    Last edited by Ken Puls; 01-28-2006 at 03:42 PM.
    moshe

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by lior03
    hello
    i want to calculate how many prime numbers ther are in a range.
    Function isprimer1(r As Range)
    Dim cell As Object
    For Each cell In Selection
    If Not cell.Value Mod 2 = 0 Or Not cell.Value Mod 3 = 0 Or Not cell.Value Mod 5 = 0 Or Not cell.Value Mod 7 = 0 Then
    isprimer1 = cell + 1
    End If
    Next cell
    End Function
    thats wrong?
    Lior,

    Try this

    [VBA]
    Function isprimer1(r As Range)
    Dim cell As Object
    For Each cell In r
    isprimer1 = isprimer1 - IsPrime(cell.Value)
    Next cell
    End Function

    Function IsPrime(Num As Single) As Boolean
    Dim i As Long
    If Num < 2 Then
    Exit Function
    ElseIf (Num <> 2 And Num Mod 2 = 0) Then
    Exit Function
    ElseIf Num <> Int(Num) Then
    Exit Function
    End If
    For i = 3 To Sqr(Num) Step 2
    If Num Mod i = 0 Then
    Exit Function
    End If
    Next
    IsPrime = True
    End Function
    [/VBA]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    I remember making something like this a few years back just to see how it could be done.

    You should also add a check that divides by 5 or checks if the last number is a 5 since those will never be prime.

    [VBA] ElseIf Right(Num, 1) = 5 Then
    Exit Function[/VBA]

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by DRJ
    I remember making something like this a few years back just to see how it could be done.

    You should also add a check that divides by 5 or checks if the last number is a 5 since those will never be prime.

    [VBA] ElseIf Right(Num, 1) = 5 Then
    Exit Function[/VBA]
    The excessive MODs on large numbers will quickly cripple it. If you are worried about function efficiency, I would personally create an array of n primes and integer divide each of those into the number to see if it divides exactly.

    There must be a better way that the looping MODs (didn't they used to fight on the UK beaches in the 60s?).

    .
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    prime numbers

    hello
    my second question,regarding prime numbers was about finding how many
    prime number there are between two numbers.
    one is called lowernumber the other is called highernumber.
    for example how many primew number there are between 20 and 40.
    is there a way to build such a function.
    thanks
    moshe

  7. #7
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by lior03
    hello
    my second question,regarding prime numbers was about finding how many
    prime number there are between two numbers ... how many primew number there are between 20 and 40.
    is there a way to build such a function.
    thanks
    Hi Moshe,

    Gauss' theorem on the distribution of primes (the prime number theorem) is by no means exact for small numbers... This theorem states that the number of primes (M) less than a given integer a is given by M = a/ln a.

    So if you look at another integer b, the number of primes less than b is N = b/ln b.

    Hence the number of integers between a and b is the difference between M and N (M-N)

    I whipped this up for you and it will give that difference, but pls note that I have NOT put any error-handling in this[vba]Option Explicit
    Sub NumberOfPrimes()
    Dim k As Double, a As Long, b As Long
    a = InputBox("Largest number?")
    b = InputBox("Smallest number?")
    k = Int(a / Log(a) - b / Log(b))
    MsgBox "There are " & k & " primes between " & b & " and " & a
    End Sub[/vba]HTH,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Use these two functions, and calculate with

    =SUMPRODUCT(--(NumPrimes((A1:A100)))

    [VBA]
    Function NumPrimes(rng) As Variant
    Dim i As Long, j As Long, k As Long
    Dim cell As Range, row As Range
    Dim aryPrimes

    If rng.Areas.Count > 1 Then
    NumPrimes = CVErr(xlErrValue)
    Exit Function
    End If

    If rng.Cells.Count = 1 Then
    aryPrimes = rng
    Else
    aryPrimes = rng.Value
    For Each row In rng.Rows
    i = i + 1
    j = 0
    For Each cell In row.Cells
    j = j + 1
    aryPrimes(i, j) = IsPrime(cell.Value)
    Next cell
    Next row
    End If
    NumPrimes = aryPrimes
    End Function

    Function IsPrime(Num As Single) As Boolean
    Dim i As Long
    If Num < 2 Then
    Exit Function
    ElseIf (Num <> 2 And Num Mod 2 = 0) Then
    Exit Function
    ElseIf Num <> Int(Num) Then
    Exit Function
    End If
    For i = 3 To Sqr(Num) Step 2
    If Num Mod i = 0 Then
    Exit Function
    End If
    Next
    IsPrime = True
    End Function
    [/VBA]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi Moshe,

    Here's a couple more to try:

    To get a list of prime numbers...[vba]Sub GetPrimes()
    Dim Count As Integer
    Dim BegNum As Single 'Integer limits to < 32768
    Dim EndNum As Single
    Dim Prime As Single
    Dim flag As Integer
    Dim IntCheck As Single
    Count = 0
    Columns(1).ClearContents
    Do
    BegNum = _
    Application.InputBox(Prompt:="Type beginning number.", Type:=1)
    'Force entry of integers greater than 0.
    IntCheck = BegNum - Int(BegNum)
    If BegNum = 0 Then
    Exit Sub
    'Cancel is 0 -- allow Cancel.
    ElseIf BegNum < 1 Then
    MsgBox "Please enter an integer greater than zero."
    ElseIf IntCheck > 0 Then
    MsgBox "Please enter an integer -- no decimals."
    End If
    'Loop until entry of integer greater than 0.
    Loop While BegNum <= 0 Or IntCheck > 0
    Do
    EndNum = _
    Application.InputBox(Prompt:="Type ending number.", Type:=1)
    'Force entry of integers greater than 0.
    IntCheck = EndNum - Int(EndNum)
    If EndNum = 0 Then
    Exit Sub
    'Cancel is 0 -- allow Cancel.
    ElseIf EndNum < BegNum Then
    MsgBox "Please enter an integer larger than " & BegNum
    ElseIf EndNum < 1 Then
    MsgBox "Please enter an integer greater than zero."
    ElseIf IntCheck > 0 Then
    MsgBox "Please enter an integer -- no decimals."
    End If
    'Loop until entry of integer greater than 0.
    Loop While EndNum < BegNum Or EndNum <= 0 Or IntCheck > 0

    For y = BegNum To EndNum
    flag = 0
    z = 1
    Do Until flag = 1 Or z = y + 1
    'Put message into Status Bar indicating the integer and _
    divisor in each loop.
    Application.StatusBar = y & " / " & z
    Prime = y Mod z
    If Prime = 0 And z <> y And z <> 1 Then
    flag = 1
    End If
    z = z + 1
    Loop

    If flag = 0 Then
    'Enter the factor into a column starting with the active cell.
    [A1].Offset(Count, 0).Value = y
    'Increase the amount to offset for next value.
    Count = Count + 1
    End If
    Next y
    'Restore Status Bar.
    Application.StatusBar = "Ready"
    End Sub[/vba]
    To get all the factors of a number...
    [vba]Sub GetFactors()
    Dim Count As Integer
    Dim NumToFactor As Single 'Integer limits to < 32768
    Dim Factor As Single
    Dim y As Single
    Dim IntCheck As Single
    Columns(1).ClearContents
    Count = 0
    Do
    NumToFactor = _
    Application.InputBox(Prompt:="Type integer", Type:=1)
    'Force entry of integers greater than 0.
    IntCheck = NumToFactor - Int(NumToFactor)
    If NumToFactor = 0 Then
    Exit Sub
    'Cancel is 0 -- allow Cancel.
    ElseIf NumToFactor < 1 Then
    MsgBox "Please enter an integer greater than zero."
    ElseIf IntCheck > 0 Then
    MsgBox "Please enter an integer -- no decimals."
    End If
    'Loop until entry of integer greater than 0.
    Loop While NumToFactor <= 0 Or IntCheck > 0
    For y = 1 To NumToFactor
    'Put message in status bar indicating the integer being checked.
    Application.StatusBar = "Checking " & y
    Factor = NumToFactor Mod y
    'Determine if the result of division with Mod is without _
    remainder and thus a "factor".
    If Factor = 0 Then
    'Enter the factor into a column starting with the active cell.
    [A1].Offset(Count, 0).Value = y
    'Increase the amount to offset for next value.
    Count = Count + 1
    End If
    Next
    'Restore Status Bar.
    Application.StatusBar = "Ready"
    End Sub[/vba]HTH,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  10. #10
    VBAX Regular
    Joined
    Jan 2006
    Location
    Germany
    Posts
    7
    Location

    Smile

    Hi Everybody, Hi John!

    Thank you for the clear coding.

    I am here a bit late for the following questions


    • I want to double click on my number in word2003 to select it then click on a button to do a simple division by a relatively small prime and write in a new line after my selected number the result! Is it possible?
    • The second question can be a challenge: my number is over thousand digits
    Summary: divide a very large number by a small number without leaving word

    Would be very pleased if you could help?
    Matpen
    Keyboard not found, please type F1 to exit!

  11. #11
    VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    What will be the maximum range of the prime number?
    Please marked your thread "Solved" using the "Thread Tool" above when you manage to find a solution.
    - Leonard

  12. #12
    VBAX Regular
    Joined
    Jan 2006
    Location
    Germany
    Posts
    7
    Location
    Hi Leo!
    My divisors will be always in the interval: 2-20 (99% dayly) and very rarely crossing the limit of 110 000. (1% once to twice a week).

    I can't use my javascripts in WORD (2003) and will be EXTREMELY thrilled if you could help!!!!!!!!

    Matpen
    Keyboard not found, please type F1 to exit!

  13. #13
    VBAX Regular
    Joined
    Jan 2006
    Location
    Germany
    Posts
    7
    Location
    Hi Leo!
    Sorry, it is me again, I forgot something maybe important, the biggest digits I'll be working with next, are over 100 000 digits.
    Keyboard not found, please type F1 to exit!

Posting Permissions

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