#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

#define N 50

long long fibo[N];
long long check[N];

int main(int argc, char *argv[]) {
    int thread_count = 2;
    if (argc > 1) {
        thread_count = (int)strtol(argv[1], NULL, 10);
    }

    /* Serial version, for comparison */
    check[0] = check[1] = 1;
    for (int i = 2; i < N; i++) {
        check[i] = check[i - 1] + check[i - 2];
    }

    /* Parallel version: compiles, but is it correct? */
    fibo[0] = fibo[1] = 1;
#   pragma omp parallel for num_threads(thread_count)
    for (int i = 2; i < N; i++) {
        fibo[i] = fibo[i - 1] + fibo[i - 2];
    }

    int first_wrong = -1;
    for (int i = 0; i < N; i++) {
        if (fibo[i] != check[i]) {
            first_wrong = i;
            break;
        }
    }

    printf("Threads: %d\n", thread_count);
    printf("fibo[%d] = %lld (should be %lld)\n", N - 1, fibo[N - 1], check[N - 1]);
    if (first_wrong < 0) {
        printf("All %d values are correct this time.\n", N);
    } else {
        printf("First wrong value: fibo[%d] = %lld (should be %lld)\n",
               first_wrong, fibo[first_wrong], check[first_wrong]);
    }
    return 0;
}
