sábado, 12 de novembro de 2016

Thue-Morse / Morse-Thue sequence and Phi (Javascript)

<script>
    /*
    Luiz Augusto Prado - Morse-Thue sequence
    The
Thue-Morse / Morse-Thue é obtido assim:
    É dado um valor inicia zero para a construção da sequencia.
    T0 = 0

    O proximo final será o anterior somado com o inverso do anterior. Assim:
    Tf = Tanterior concatenado com o inverso do Tanterior
    NO caso, por exemplo, sendo o T0 sendo o Tanterior teremos:
    Se Ta = T0
        então:
            Tf = T0 + inverso(T0)

    0     = 0
    2     = 1
    4     = 10
    8     = 1001
    16    = 10010110
    32    = 1001011001101001
    64    = 10010110011010010110100110010110

    */

    function inverter(k)
    {
        if(k==0)return 1;
        else return 0;
    }


    //=============================================================================
 // Sequence by arrays. It can be hard if you wanna have only some digits       


 //=================================================================================
    function log2(n)
    {
        return Math.log(n)/Math.log(2);
    }

    //=================================================================================
 // I made this source to get only the digts that I wanna have. Better!

 //=================================================================================
    function getDigit(n, invert)
    {
        if(n==0)return 0
        var pA = Math.floor(log2(n))
        var v  = Math.round( Math.pow(2, pA) )
        var remainder=n-v
        if(remainder>v)
        {
            invert=1-invert  
            remainder-=v
        }
        if(remainder>1) remainder = getDigit(remainder, invert)
        if(invert==0) remainder=1-remainder
        return remainder
    }

    //=================================================================================
 // Example 1 - construindo a seqeuncia passo a passo
 //=================================================================================
    for(var i=0; i<18; i++)
    {
        for(var k=Math.pow(2,i); k<Math.pow(2,i+1); k++)
        {
            document.write( inverter(getDigit(k, 0) ) +"" );
        }
        document.write( "<br>" );
    }
    document.write( "<br>" );

    //=================================================================================
 // Example 2 - pegando até o 120 digito

 //=================================================================================       for(var i=0; i<120; i++)
    {
        document.write( getDigit(i, 0) +"" );
    }
    document.write( "<br>" );

</script>



<script>
// Phi and phi conjugate
// Phi * phi = 1
var Phi= (1+Math.sqrt(5))/2;
var phi= (1-Math.sqrt(5))/2; 
// Find the n number Fibonacci where sequence starting with 0
function Fib(n)
{
 return Math.round(( Math.pow(Phi, n) - Math.pow(phi, n) ) / Math.sqrt(5));
}
// Find the sum of the n first numbers of Fibonacci where sequence starting with 0
function sumPhi(n)
{
 return Fib(n+2)-1;
}
// Find position n of the value where Fib(n)=value
function FindPosition(value)
{
 return Math.floor(Math.log(value*Math.sqrt(5))/Math.log(Phi));
}
document.write("Sum = "+sumPhi(8));
</script>

Enquanto não apagam, podem conferir aqui nesse link também:

Não me perguntem por que não está aparecendo nas pesquisas do Google... Eu faço ideia, mas não tenho certeza. Tirem suas próprias conclusões:

http://www.guj.com.br/t/thue-morse-sequence-dificil/19945
http://www.guj.com.br/t/project-euler/13180/14

Nenhum comentário: