Guest User

Untitled

a guest
Nov 17th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. The following iterative sequence is defined for the set of positive integers:
  2.  
  3. n → n/2 (n is even) n → 3n + 1 (n is odd)
  4.  
  5. Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
  6.  
  7. It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet, it is thought that all starting numbers finish at 1.
  8.  
  9. Here's a program that tries to find out which starting number, under one million, produces the longest chain.
  10.  
  11. NOTE: Once the chain starts the terms are allowed to go above one million.
  12. #include <stdio.h>
  13.  
  14. int main (void)
  15. {
  16. int maxc = 0;
  17. int maxi = 0;
  18. int i = 0;
  19.  
  20. for (i = 0; i < 1000000; i++) {
  21. int n = i;
  22. int c = 1;
  23. while (n > 1) {
  24. if (n % 2 == 0) {
  25. n = n / 2;
  26. } else {
  27. n = 3*n + 1;
  28. }
  29. c++;
  30. }
  31. if (c > maxc) {
  32. maxc = c;
  33. maxi = i;
  34. }
  35. }
  36. printf("%d %d\n", maxi, maxc);
  37. return 0;
  38. }
  39.  
  40. Find the bug in this program.
Add Comment
Please, Sign In to add comment