Zadatak3.8-Sirakuza algoritam

  1. nacin-Tasa

#include <iostream>

using namespace std;

int main()
{
int n,i,br=0;
cin>>n;
while(n!=1)
{
if(n%2==0)n=n/2;
else n=n*3+1;
br++;
}
cout<<br;
return 0;
}

2. nacin moje

#include <iostream>

using namespace std;

int main()
{
int n,i,br;
cin>>n;
unsigned long long int niz[100];

i=0;
br=0;
while(niz[i]!=1)
{niz[0]=n;
if(niz[i]%2==0)
niz[i+1]=niz[i]/2;
else
if(niz[i]%2!=0)
niz[i+1]=3*niz[i]+1;

br++;
i++;

}
cout<<endl;
cout<<br;
return 0;
}

 

 

Advertisements