main.c 989 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include<stdio.h>
  2. //fwd is x*3+1
  3. void fwd(unsigned long int *x);
  4. //bwd is x:2
  5. void bwd(unsigned long int *x);
  6. //search max value hailstone number
  7. void srchMax (unsigned long int *x);
  8. //logic Collatz
  9. void Collatz (unsigned long int *x);
  10. static unsigned long int max = 0;
  11. int main()
  12. {
  13. unsigned long int x;
  14. unsigned long int *p;
  15. printf("%s", "input x: ");
  16. scanf("%lu", &x);
  17. p=&x;
  18. Collatz(p);
  19. printf("max value: %lu\n", max);
  20. printf("%s\n", "The end...");
  21. }
  22. void fwd(unsigned long int *x)
  23. {
  24. *x = *x * 3 + 1;
  25. }
  26. void bwd(unsigned long int *x)
  27. {
  28. *x = *x / 2;
  29. }
  30. void srchMax (unsigned long int *x)
  31. {
  32. if(*x > max)
  33. {
  34. max = *x;
  35. }
  36. }
  37. void Collatz(unsigned long int *x)
  38. {
  39. unsigned long int i = 0;
  40. while(1)
  41. {
  42. if(*x == 1)
  43. {
  44. printf("finale step %lu\n", i);
  45. break;
  46. }
  47. else if(*x % 2 == 0)
  48. {
  49. printf("%lu. Number is even: %lu\n",i,*x);
  50. bwd(x);
  51. }
  52. else
  53. {
  54. printf("%lu. Number is odd: %lu\n",i,*x);
  55. fwd(x);
  56. }
  57. srchMax(x);
  58. i = i + 1;
  59. }
  60. }