Day 25: Running Time and Complexity

  • + 0 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"; }

    // 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; }