Hello mates!

I have one problem with Math Utility Test for PrimeNumber case.

The code is:

public bool IsPrimeNumber(int n)
        {
            int i = 0, num = 0;
            while (i <= n)
            {
                if ((n % i) == 0) num = num + 1;
                i = i + 1;
           }

            if (num == 0)
                return true;
            else
                return false;
        }

But I'm not sure if its the correct code for prime numbers.
&
The test method should be what?:

[TestMethod]
        public void Test_IsPrimeNumber()
        {
            bool result = this.IsPrimeNumber(?);
            Assert.AreEqual(?, result);
        }

It needs to be Passed when you run Test in Solution.

Suggestions are welcome.

Regards!

Recommended Answers

All 2 Replies

Instead of a question mark, put a number.

1) Make a list of known primes and loop them through the test
2) Make a list of known NON-primes and loop them through the test
3) Take a list of random numbers and loop them through the test AND compare them to the known and unknown primes list.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.