PDA

View Full Version : Solved: How to use VBA to test if a number is a prime or not?



qpham26
03-11-2013, 08:30 PM
Hi guys, I am trying to make a simple program that uses uses if statements to determine if the number, which can be obtain from a cell in the excel workbook, and test to see if the number is a prime or not.

I tried to test for divisibility using mod function, for 2 thru 9, but then i figure what if the input number is a square of some prime (13, 17, 23, etc.) or cube or any exponents. Then the if statement for divisibility will not be adequate.


Please help me with this. I have found the code that is written by microsoft for the same purpose, but that is not appropriate for my level, and I dont really understand it.

Thanks:help

Doug Robbins
03-11-2013, 09:14 PM
See http://www.vbtutor.net/VBA/vba16.htm

or http://www.mrexcel.com/archive/VBA/9221.html

qpham26
03-11-2013, 09:28 PM
Thanks a lot, the idea of using loops to test for divisibility of all the number that is less than N is really nice.

Teeroy
03-11-2013, 10:59 PM
I can't see why looping up to N-1 is required, you only need to loop to N/2. Any number greater than N/2 can't have an integer multiplier to N.

qpham26
03-11-2013, 11:12 PM
I can't see why looping up to N-1 is required, you only need to loop to N/2. Any number greater than N/2 can't have an integer multiplier to N.
thanks for pointing that out :beerchug: