Prime Numbers

A

Alan

Hi,
I've been using this formula to identify whether a number is a prime number
or not. It works, but it's a bit long and incongruous.
=IF(A1<8,"2, 3, 5, and 7 are prime numbers. Please enter a number greater
than
8",IF(OR(MOD($A$1,2)=0,MOD($A$1,3)=0,MOD($A$1,4)=0,MOD(A1,5)=0,MOD(A1,6)=0,MOD(A1,7)=0,MOD(A1,8)=0,MOD(A1,9)=0),"Not
Prime","Prime"))
Can anyone suggest a better way?
Thanks in advance,
Alan.
 
C

Chip Pearson

Try a formula like the following array formula:

=IF(AND((MOD($A$1,ROW(INDIRECT("2:"&$A$1-1)))<>0)),"prime","not prime")

This will return "prime" if A1 is prime or "not prime" if it is not. Note
that this is a array formula, so you must press CTRL SHIFT ENTER rather than
just ENTER when you first enter the formula and whenever you edit it later.
If you do this properly, Excel will display the formula enclosed in curly
braces {}. The formula will not work if you do not enter it with CTRL SHIFT
ENTER. See http://www.cpearson.com/Excel/arrayformulas.aspx for much more
info about array formulas.


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 
C

Chip Pearson

Just for fun, the following array formula will test for prime twins,
returning "prime", "prime twin", or "not prime".

=IF(AND((MOD($A$1,ROW(INDIRECT("2:"&$A$1-1)))<>0)),IF(OR(AND((MOD($A$1-2,ROW(INDIRECT("2:"&$A$1-3)))<>0)),AND((MOD($A$1+2,ROW(INDIRECT("2:"&$A$1+1)))<>0))),"prime
twin","prime"),"not prime")

Enter with CTRL SHIFT ENTER.


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 
B

Bruce Sinclair

Just for fun, the following array formula will test for prime twins,
returning "prime", "prime twin", or "not prime".

=IF(AND((MOD($A$1,ROW(INDIRECT("2:"&$A$1-1)))<>0)),IF(OR(AND((MOD($A$1-2,ROW(IN
DIRECT("2:"&$A$1-3)))<>0)),AND((MOD($A$1+2,ROW(INDIRECT("2:"&$A$1+1)))<>0))),"p
rime
twin","prime"),"not prime")

Enter with CTRL SHIFT ENTER.

... and in case anyone else is interested, a 'prime twin' or 'twin prime' is
2 prime numbers that differ by 2 (e.g. 5 and 7; 11 and 13) - the smallest
number they can differ by (one exception ... 2 and 3).
Thanks ... a fascinating formula ... and I learned something today. :)
 
G

George McCabe

The problem with your algorythm is that if you square a prime, you get a number divisible only by 1, the prime and the squared number itself. (Ex. 11 squared is 121) This number will not be found by your application, but is NOT a prime. What you need to add is a line to check whether or not the square root of the number is equal to the integer of the square root. If not, then, together with your process, you'll be fine.



Alan wrote:

Prime Numbers
14-Sep-08

Hi
I've been using this formula to identify whether a number is a prime number
or not. It works, but it's a bit long and incongruous
=IF(A1<8,"2, 3, 5, and 7 are prime numbers. Please enter a number greater
than
8",IF(OR(MOD($A$1,2)=0,MOD($A$1,3)=0,MOD($A$1,4)=0,MOD(A1,5)=0,MOD(A1,6)=0,MOD(A1,7)=0,MOD(A1,8)=0,MOD(A1,9)=0),"Not
Prime","Prime")
Can anyone suggest a better way
Thanks in advance
Alan.

Previous Posts In This Thread:

Prime Numbers
Hi
I've been using this formula to identify whether a number is a prime number
or not. It works, but it's a bit long and incongruous
=IF(A1<8,"2, 3, 5, and 7 are prime numbers. Please enter a number greater
than
8",IF(OR(MOD($A$1,2)=0,MOD($A$1,3)=0,MOD($A$1,4)=0,MOD(A1,5)=0,MOD(A1,6)=0,MOD(A1,7)=0,MOD(A1,8)=0,MOD(A1,9)=0),"Not
Prime","Prime")
Can anyone suggest a better way
Thanks in advance
Alan.

Try a formula like the following array
Try a formula like the following array formula

=IF(AND((MOD($A$1,ROW(INDIRECT("2:"&$A$1-1)))<>0)),"prime","not prime"

This will return "prime" if A1 is prime or "not prime" if it is not. Note
that this is a array formula, so you must press CTRL SHIFT ENTER rather than
just ENTER when you first enter the formula and whenever you edit it later.
If you do this properly, Excel will display the formula enclosed in curly
braces {}. The formula will not work if you do not enter it with CTRL SHIFT
ENTER. See http://www.cpearson.com/Excel/arrayformulas.aspx for much more
info about array formulas

--
Cordially
Chip Pearso
Microsoft Most Valuable Professiona
Excel Product Grou
Pearson Software Consulting, LL
www.cpearson.co
(email on web site



Just for fun, the following array formula will test for prime twins, returning
Just for fun, the following array formula will test for prime twins,
returning "prime", "prime twin", or "not prime"

=IF(AND((MOD($A$1,ROW(INDIRECT("2:"&$A$1-1)))<>0)),IF(OR(AND((MOD($A$1-2,ROW(INDIRECT("2:"&$A$1-3)))<>0)),AND((MOD($A$1+2,ROW(INDIRECT("2:"&$A$1+1)))<>0))),"prime
twin","prime"),"not prime"

Enter with CTRL SHIFT ENTER

--
Cordially
Chip Pearso
Microsoft Most Valuable Professiona
Excel Product Grou
Pearson Software Consulting, LL
www.cpearson.co
(email on web site




Re: Prime Numbers

... and in case anyone else is interested, a 'prime twin' or 'twin prime' is
2 prime numbers that differ by 2 (e.g. 5 and 7; 11 and 13) - the smallest
number they can differ by (one exception ... 2 and 3).
Thanks ... a fascinating formula ... and I learned something today. :)

Re: Prime Numbers
Perhaps have a look a

http://www.dailydoseofexcel.com/archives/2005/06/30/is-this-number-prime


-
met vriendelijke groetje

(e-mail address removed)


Submitted via EggHeadCafe - Software Developer Portal of Choice
Creating a WPF Custom Control
http://www.eggheadcafe.com/tutorial...9-c0b45fb68b78/creating-a-wpf-custom-con.aspx
 

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