Advertisement
Guest User

Untitled

a guest
Feb 13th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. // Next(n) returns the number that follows n in a hailstone sequence.
  2. // For example, next(7) = 22 and next(8) = 4.
  3. //
  4. // Next requires n > 1, since there is no number that follows 1.
  5. int next(int n)
  6. { if(n > 1)
  7. {
  8. if((n%2)==0)
  9. {
  10. n = n/2;
  11. }
  12.  
  13. else
  14. {
  15. n = 3*n+1;
  16. }
  17. }
  18. return n;
  19. }
  20.  
  21. //writeHailstoneSequence returns the
  22. void writeHailstoneSequence(int n)
  23. {
  24. int m = n;
  25. if(m == 1)
  26. {
  27. printf("%i", m);
  28. }
  29. else
  30. {
  31. printf("%i ", m);
  32. writeHailstoneSequence(next(n));
  33. }
  34.  
  35. }
  36.  
  37. //reads int n from the 'next' function
  38. //counts every time we calculate in
  39. //'next' and returns that value
  40. int lengthHailstone(int n)
  41. {
  42. int l = n;
  43. if(l == 1)
  44. {
  45. return 1;
  46. }
  47. else
  48. {
  49.  
  50. return lengthHailstone(next(n))+1;
  51. }
  52.  
  53. }
  54.  
  55. //calculates every int n and compares
  56. //it to the int largestValue and if n
  57. //is larger then largestValue takes
  58. //same value as n
  59. int largestHailstoneValue(int n)
  60. {
  61. int v = n;
  62. if(v == 1)
  63. {
  64. return 1;
  65. }
  66. else
  67. {
  68. return max(largestHailstoneValue(next(n)), v);
  69. }
  70.  
  71. }
  72.  
  73. //if int n is 1 then returns 1
  74. //calculates the longest length
  75. //seqeunce from 1 to n
  76. int longestLength(int n)
  77. {
  78. if(n > 1)
  79. {
  80. return max(lengthHailstone(n), longestLength(n-1));
  81. }
  82. else
  83. {
  84. return 1;
  85. }
  86. }
  87.  
  88. //uses lengthHailstone function to
  89. //compare each int up to int n to find
  90. //the first value of the longest sequence
  91. int firstValueBiggestSequence(int n)
  92. {
  93. int firstValue = 1;
  94. for(int i = 2; i <= n; i++)
  95. {
  96. if(lengthHailstone(i) > lengthHailstone(firstValue))
  97. firstValue = i;
  98.  
  99. }
  100. return firstValue;
  101. }
  102.  
  103. //uses largestHailstoneValue function
  104. //to find the largest value from 1
  105. // to n using a loop from i(1) to n
  106. int biggestOverallValue(int n)
  107. {
  108. int biggestValue = 0;
  109. for(int i = 1; i <= n; i++)
  110. {
  111. if(biggestValue < largestHailstoneValue(i))
  112. {
  113. biggestValue = largestHailstoneValue(i);
  114. }
  115. }
  116. return biggestValue;
  117.  
  118.  
  119. }
  120.  
  121. int main()
  122. {
  123. int n;
  124. printf("What number shall I start with?\n");
  125. scanf("%i", &n);
  126. printf("The hailstone sequence starting at %i is: \n", n);
  127. writeHailstoneSequence(n);
  128. printf("\nThe length of the sequence is %i.\n", lengthHailstone(n));
  129. printf("The largest number in the sequence is %i.\n", largestHailstoneValue(n));
  130. printf("The longest hailstone sequence starting with a number up to ");
  131. printf("%i has length %i.\n", n, longestLength(n));
  132. printf("The longest hailstone sequence starting with a number up to ");
  133. printf("%i begins with %i.\n", n, firstValueBiggestSequence(n));
  134. printf("The largest value in any hailstone sequence starting with ");
  135. printf("a number up to %i is %i.\n", n, biggestOverallValue(n));
  136. return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement