PDA

View Full Version : prime numbers



lior03
07-20-2005, 05:16 AM
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.
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
thanks

lior03
07-20-2005, 06:13 AM
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?

Bob Phillips
07-20-2005, 06:30 AM
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


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

Jacob Hilderbrand
07-20-2005, 08:54 AM
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.

ElseIf Right(Num, 1) = 5 Then
Exit Function

Bob Phillips
07-20-2005, 09:43 AM
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.

ElseIf Right(Num, 1) = 5 Then
Exit Function

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?).

.

lior03
07-20-2005, 11:10 PM
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

johnske
07-21-2005, 12:50 AM
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 thisOption 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 SubHTH,
John :)

Bob Phillips
07-21-2005, 02:42 AM
Use these two functions, and calculate with

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


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

johnske
07-21-2005, 06:27 AM
Hi Moshe,

Here's a couple more to try:

To get a list of prime numbers...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
To get all the factors of a number...
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 SubHTH,
John :)

matpen
01-23-2006, 08:42 AM
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

sheeeng
01-28-2006, 10:00 AM
What will be the maximum range of the prime number?

matpen
01-28-2006, 03:27 PM
Hi Leo!:hi:
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!!!!!!!!: pray2:

Matpen

matpen
01-28-2006, 03:53 PM
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.