fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. struct vec {int *data, cap, size;};
  4. struct vec vec_alloc(int cap) {
  5. struct vec v = {0, 0, 0};
  6. if (0 < cap) v.data = malloc(cap * sizeof (int)), v.cap = cap;
  7. return v;
  8. }
  9. void vec_free(struct vec v) {free(v.data);}
  10. void vec_push(struct vec *v, int value) {
  11. if (!v) return;
  12. void *new_data;
  13. if (v->cap == v->size) {
  14. new_data = realloc(v->data, v->cap * 2 * sizeof (int));
  15. if (!new_data) return;
  16. v->data = new_data;
  17. v->cap *= 2;
  18. }
  19. v->data[v->size++] = value;
  20. }
  21. struct vec p(struct vec v) {
  22. int i;
  23. putchar('[');
  24. for (i = 0; i < v.size; i++) {
  25. if (0 < i) printf(", ");
  26. printf("%d", v.data[i]);
  27. }
  28. puts("]");
  29. return v;
  30. }
  31.  
  32. int collatz(int n) {
  33. return n % 2 ? n * 3 + 1 : n / 2;
  34. }
  35. struct vec chain(int n) {
  36. struct vec v = vec_alloc(8);
  37. do vec_push(&v, n), n = collatz(n); while (1 < v.data[v.size - 1]);
  38. return v;
  39. }
  40. void f(int n) {
  41. vec_free(p(chain(n)));
  42. }
  43. int f884(int a, int b, int c) {
  44. int i, count = 0;
  45. for (i = a; i <= b; i++) {
  46. struct vec v = chain(i);
  47. if (c <= v.size) count++;
  48. vec_free(v);
  49. }
  50. return count;
  51. }
  52. int main() {
  53. f(10), f(30);
  54. printf("%d\n", f884(1, 100, 15));
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
[10, 5, 16, 8, 4, 2, 1]
[30, 15, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1]
71