Skip to content

Instantly share code, notes, and snippets.

@Biazus
Created August 22, 2014 14:23
Show Gist options
  • Save Biazus/2071f87f1005c0ad6ee5 to your computer and use it in GitHub Desktop.
Save Biazus/2071f87f1005c0ad6ee5 to your computer and use it in GitHub Desktop.
Desafios: 3n+1
// 3n+1
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int i, j, k, l, n, partial=0, maximum=0, resto, troca=1;
while(cin >> i >> j){
troca=0;
if ( i > j ){swap (i, j); troca = 1;}
for(k=i; k<=j;k++){
n = k;
partial++;
while(1){
if(n==1){
if(partial > maximum)
maximum = partial;
break;
}
resto = n%2;
if(resto != 0){
n = 3*n+1;
partial++;
}
else{
n = n/2;
partial++;
}
}
partial = 0;
}
if(troca)
cout << j << " " << i << " " << maximum ;
else cout << i << " " << j << " " << maximum ;
maximum = 0;
cout << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment