1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#define ENABLE_DEBUGGING_CODE

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "my_debugging_utilities.h"   // includes dbg_log.h
#include "dbg_log.h"                  // and AGAIN.  Also, dbg_log.h includes stdio.h AGAIN

bool is_prime(int n) {
    bool n_looks_prime = (n % 2) != 0;  // Assume n is prime unless we find that it is divisible
                                        // by something other than 1 or itself.

    // Try all ODD numbers from 3 to n/2
    if(n_looks_prime) {
        for(int divisor = 3; divisor < n / 2; divisor += 2) {
            dbg_log_int(divisor);
            dbg_printf("n == %d    divisor == %d\n", n, divisor);

            if(n % divisor == 0) {
                n_looks_prime = false;
                break;
            }
        }
    }
    return n_looks_prime;
}

int main(int argc, char* argv[]) {
    int n = 13;
    if( is_prime(n) ) {
        printf("%d is prime", n);  // REQUIRED OUTPUT (by specification for program)
    }
    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2021 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.