Skip to content

Instantly share code, notes, and snippets.

@gonzalezfj
Created February 8, 2014 21:09
Show Gist options
  • Save gonzalezfj/8890409 to your computer and use it in GitHub Desktop.
Save gonzalezfj/8890409 to your computer and use it in GitHub Desktop.
#include<iostream>
using namespace std;
#define TOTAL 1000000
#define MIN(a,b) ((a) < (b) ? (a) : (b))
typedef unsigned short int usint;
usint tabla[TOTAL];
int main()
{
int p,q;
int cont=1;
int i;
for (i = 2; i < TOTAL; i>>=1)
{
tabla[i]=++cont;
}
while (cin>>p>>q)
{
usint min = (usint)TOTAL;
int i;
for (i = p; i <= q; ++i)
{
if(tabla[i]==0)
{
usint n = i;
int contador=1;
while(n>=1)
{
if(tabla[n]!=0)
{
contador += tabla[n];
}
else
{
if (i & 1)
{
n>>=1;
}else
{
n=3*n+1;
}
contador++;
}
}
tabla[i] = contador;
}
min = MIN(min,tabla[i]);
}
cout << p << " " << q << " " << min << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment