We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
const char* is_prime(int n) {
if (n <= 1) {
return "Not prime";
}
if (n <= 3) {
return "Prime";
}
if (n % 2 == 0 || n % 3 == 0) {
return "Not prime";
}
// Check for divisibility from 5 to sqrt(n)
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return "Not prime";
}
}
return "Prime";
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("%s\n", is_prime(n));
return 0;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 25: Running Time and Complexity
You are viewing a single comment's thread. Return to all comments →
include
include
const char* is_prime(int n) { if (n <= 1) { return "Not prime"; } if (n <= 3) { return "Prime"; } if (n % 2 == 0 || n % 3 == 0) { return "Not prime"; }
}
int main() { int n; printf("Enter a number: "); scanf("%d", &n);
return 0; }