Date created: Saturday, October 13, 2012 8:37:13 PM. Last modified: Saturday, August 6, 2016 10:10:53 PM

Problem 14

#include 
#include 

using namespace std;

int main() {

  long long ubound = 1000000, lbound = 500000;
  long long testnumber, highestnumber = 0, lastnumber = 0; 
  int length = 0, longest = 0;

  while (ubound>=lbound) {
    testnumber = ubound;
    length = 1;
    while (testnumber>1) {
      if(testnumber%2==0) {
        testnumber = testnumber/2;
      } else {
        testnumber = (testnumber*3)+1;
      }
      length++;
    }
    if (length>longest) {
      longest = length;
      highestnumber = ubound;
      lastnumber = testnumber;
    }
    ubound--;
  }
  cout << "longest: " <<  longest << " highest: " << highestnumber 
       << " last number: " << lastnumber << endl;
return 0;
}

Previous page: Problem 13
Next page: Problem 16