fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct series series;
  5. struct series {
  6. long num;
  7. series *next;
  8. };
  9. series *head = NULL;
  10.  
  11. void addhead(long x)
  12. {
  13. series *p;
  14. p = (series *) malloc(sizeof (series));
  15. p->num = x;
  16. p->next = head;
  17. head = p;
  18. //printf("added: %ld\n", x);
  19. }
  20. void series_generate(long num)
  21. {
  22. long n = 1;
  23. addhead(n);
  24. while (n < num)
  25. {
  26. series *p;
  27. for (p = head; p != NULL; p = p->next)
  28. {
  29. long bf = p->num - 1;
  30. if (p->num%2 == 0 && bf != 0 && bf%3 == 0) {
  31. bf /= 3;
  32. if (bf != 1)
  33. addhead(bf);
  34. if (bf < num)
  35. n++;
  36. //printf("new num: %ld\n", bf);
  37. }
  38. p->num *= 2;
  39. if ( p->num < num)
  40. n++;
  41. //printf("new num: %ld\n", p->num);
  42.  
  43. }
  44. }
  45. }
  46. int main()
  47. {
  48. long num = 1000000;
  49. series_generate(num);
  50. series *p;
  51. for (p = head; p != NULL; p = p->next) {
  52. if (p->num < num) {
  53. printf("%ld\n", p->num);
  54. break;
  55. }
  56. }
  57. return 0;
  58. }
Success #stdin #stdout 0.09s 11840KB
stdin
Standard input is empty
stdout
-178956971