Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- long int F[1000000];
- long int time = 1;
- long int num;
- bool check = false;
- long int calculateCollatz (long int n)
- {
- if (check==false)
- {
- num = n;
- check = true;
- }
- if (n==1)
- {
- time++;
- F[num] = time;
- time = 1;
- check = false;
- return F[num];
- }
- else if (F[num]!=-1)
- {
- return F[num];
- }
- else
- {
- time++;
- if (n%2==0)
- {
- return calculateCollatz(n/2);
- }
- else
- {
- return calculateCollatz(n*3+1);
- }
- }
- }
- int main(){
- for (long int i = 0; i<1000000; i++)
- {
- F[i] = -1;
- }
- for (long int j = 1; j<1000000; j++)
- {
- calculateCollatz(j);
- }
- long int maxVal = F[0];
- long int result;
- for (long int k = 0; k<1000000; k++)
- {
- if (F[k]>maxVal)
- {
- maxVal = F[k];
- result = k;
- }
- }
- cout << result;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement