Collatz.mws

Conjectura de Collatz

"começando com um inteiro n qualquer, se iterarmos sucessivamente a função

f(n)=n/2 (se n é par) ou f(n)=3n+1 (se n é ímpar),

chegaremos sempre a 1 ao cabo de um número finito de passos"

 

Procedimento que define a função de Collatz

>    Collatz := proc(n::integer)

>       if type(n,even) then

>          n/2;

>       else

>          3*n+1;

>       fi;

>    end:

Procedimento de contagem das iterações sucessivas da função de Collatz até chegar a 1

>    IC := proc(raiz::integer)

>       local sentinela, contador;

>       contador := 0;

>       sentinela := raiz;

>       while sentinela <> 1 and contador < 1000^1000 do

>           sentinela := Collatz(sentinela);

>           contador := contador + 1;

>       od;

>       RETURN(contador);

>    end:

Verificação da conjectura para os primeiros 200 inteiros

>    seq(IC(i), i=1..200);

0, 1, 7, 2, 5, 8, 16, 3, 19, 6, 14, 9, 9, 17, 17, 4, 12, 20, 20, 7, 7, 15, 15, 10, 23, 10, 111, 18, 18, 18, 106, 5, 26, 13, 13, 21, 21, 21, 34, 8, 109, 8, 29, 16, 16, 16, 104, 11, 24, 24, 24, 11, 11, 1...
0, 1, 7, 2, 5, 8, 16, 3, 19, 6, 14, 9, 9, 17, 17, 4, 12, 20, 20, 7, 7, 15, 15, 10, 23, 10, 111, 18, 18, 18, 106, 5, 26, 13, 13, 21, 21, 21, 34, 8, 109, 8, 29, 16, 16, 16, 104, 11, 24, 24, 24, 11, 11, 1...
0, 1, 7, 2, 5, 8, 16, 3, 19, 6, 14, 9, 9, 17, 17, 4, 12, 20, 20, 7, 7, 15, 15, 10, 23, 10, 111, 18, 18, 18, 106, 5, 26, 13, 13, 21, 21, 21, 34, 8, 109, 8, 29, 16, 16, 16, 104, 11, 24, 24, 24, 11, 11, 1...
0, 1, 7, 2, 5, 8, 16, 3, 19, 6, 14, 9, 9, 17, 17, 4, 12, 20, 20, 7, 7, 15, 15, 10, 23, 10, 111, 18, 18, 18, 106, 5, 26, 13, 13, 21, 21, 21, 34, 8, 109, 8, 29, 16, 16, 16, 104, 11, 24, 24, 24, 11, 11, 1...
0, 1, 7, 2, 5, 8, 16, 3, 19, 6, 14, 9, 9, 17, 17, 4, 12, 20, 20, 7, 7, 15, 15, 10, 23, 10, 111, 18, 18, 18, 106, 5, 26, 13, 13, 21, 21, 21, 34, 8, 109, 8, 29, 16, 16, 16, 104, 11, 24, 24, 24, 11, 11, 1...

Procedimento que calcula a sequência n, f(n), f(f(n)), ..., 1

>    sCollatz := proc(n::integer)

>       local x;

>       x := n;

>       while x<>1 do

>          print(x);

>          x := Collatz(x);

>       od;

>    end:

>    sCollatz(4);

4

2

1

>    sCollatz(27);

27

82

41

124

62

31

94

47

142

71

214

107

322

161

484

242

121

364

182

91

274

137

412

206

103

310

155

466

233

700

350

175

526

263

790

395

1186

593

1780

890

445

1336

668

334

167

502

251

754

377

1132

566

283

850

425

1276

638

319

958

479

1438

719

2158

1079

3238

1619

4858

2429

7288

3644

1822

911

2734

1367

4102

2051

6154

3077

9232

4616

2308

1154

577

1732

866

433

1300

650

325

976

488

244

122

61

184

92

46

23

70

35

106

53

160

80

40

20

10

5

16

8

4

2

1

>    with(plots);

>    pCollatz := proc(n::integer)

>       local w,x,i,p,j;

>       x := n;

>       i:=0;

>       while x<>1 do

>          i:=i+1;

>          w[i]:=Collatz(x);

>          x:=Collatz(x);

>       od;

>    pointplot({seq([j,w[j]],j=1..IC(n))},symbol=circle,symbolsize=12,color=red,axes=boxed):

>    end:

Warning, the name changecoords has been redefined

[animate, animate3d, animatecurve, arrow, changecoords, complexplot, complexplot3d, conformal, conformal3d, contourplot, contourplot3d, coordplot, coordplot3d, cylinderplot, densityplot, display, displ...
[animate, animate3d, animatecurve, arrow, changecoords, complexplot, complexplot3d, conformal, conformal3d, contourplot, contourplot3d, coordplot, coordplot3d, cylinderplot, densityplot, display, displ...
[animate, animate3d, animatecurve, arrow, changecoords, complexplot, complexplot3d, conformal, conformal3d, contourplot, contourplot3d, coordplot, coordplot3d, cylinderplot, densityplot, display, displ...
[animate, animate3d, animatecurve, arrow, changecoords, complexplot, complexplot3d, conformal, conformal3d, contourplot, contourplot3d, coordplot, coordplot3d, cylinderplot, densityplot, display, displ...

>    pCollatz(27);

[Maple Plot]

>    pCollatz(14);

[Maple Plot]