Skip to content

Instantly share code, notes, and snippets.

@flyotlin
Created November 27, 2019 18:12
Show Gist options
  • Save flyotlin/e8beecd5c1afd9650b350e12ede1f8b4 to your computer and use it in GitHub Desktop.
Save flyotlin/e8beecd5c1afd9650b350e12ede1f8b4 to your computer and use it in GitHub Desktop.

Revisions

  1. flyotlin created this gist Nov 27, 2019.
    48 changes: 48 additions & 0 deletions The 3n+1 problem.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    #include <iostream>

    using namespace std;
    int three_n(int);
    int main()
    {
    int a, b;
    while(cin >> a >> b)
    {
    int temp, aa, bb;
    if(a > b)
    {
    bb = a;
    aa = b;
    }
    else
    {
    aa = a;
    bb = b;
    }
    int max = 0;
    for(int i = aa; i <= bb; i++)
    {
    max = (three_n(i) > max) ? three_n(i) : max;
    }
    cout << a << " " << b << " " << max << endl;
    }

    return 0;
    }
    int three_n(int n)
    {
    int count=0;
    while(n != 1)
    {
    if(n%2 == 1)
    {
    n = 3*n+1;
    count++;
    }
    else
    {
    n /= 2;
    count++;
    }
    }
    return count+1;
    }