きったんの頭

/*
 * pe7.c
 * https://mind.kittttttan.info/c/pe7
 */

/* https://mind.kittttttan.info/c/prime */
#include "prime.h"

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

int pe7(int n) {
  int *ps;

  if (n < 1) {
    return 0;
  }

  ps = (int*)malloc(sizeof(int) * n);
  if (!ps) {
    fprintf(stderr, "%s:%d: failed malloc\n", __FILE__, __LINE__);
    return 0;
  }

  if (sieve(ps, n)) {
    printf("%dth prime is %d\n", n, ps[n - 1]);
  }
  free(ps);

  return 1;
}

static void help() {
  puts("  pe7 n(n > 0)");
}

int main(int argc, char **argv) {
  int n;

  if (argc > 1) {
    n = atoi(argv[1]);
  } else {
    n = 0;
  }

  if (n > 0) {
    puts("Problem 7:");
    pe7(n);
  } else {
    help();
  }

  return 0;
}