Consulting

Results 1 to 5 of 5

Thread: Solved: How to use VBA to test if a number is a prime or not?

  1. #1
    VBAX Regular
    Joined
    Feb 2013
    Posts
    7
    Location

    Question Solved: How to use VBA to test if a number is a prime or not?

    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

  2. #2

  3. #3
    VBAX Regular
    Joined
    Feb 2013
    Posts
    7
    Location
    Thanks a lot, the idea of using loops to test for divisibility of all the number that is less than N is really nice.

  4. #4
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    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.
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

  5. #5
    VBAX Regular
    Joined
    Feb 2013
    Posts
    7
    Location
    Quote Originally Posted by Teeroy
    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

Posting Permissions

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