Mid From PDF Coding C# Coding Interview

Check if a Number is Prime?

bool IsPrime(int n)

{
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i += 6)
{
if (n % i == 0 || n % (i + 2) == 0)
return false;

Follow on:

}
return true;
}

Explanation:

Check divisibility by 2, 3, then test possible divisors of form 6k ± 1 up to √n.

More from C# Programming Tutorial

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details