Last active
August 29, 2015 14:06
-
-
Save reinaldomendes/69c15ca4344aa93bf9d6 to your computer and use it in GitHub Desktop.
Fibonacci Math
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source /etc/bash_completion.d/git-prompt | |
PS1="${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[1;37m\]\$(__git_ps1)\[\033[00m\]\$" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Fibonacci by Reinaldo Mendes. | |
* | |
* | |
phi ** n - psi ** n | |
---------------------------------- | |
phi - psi | |
*/ | |
function fibonacci(n){ | |
/* Raiz de 5*/ | |
var rad5 = Math.sqrt(5); | |
/* | |
* Número de ouro phi | |
* phi = (1 + ¬5) / 2 | |
*/ | |
var phi = (1+rad5) / 2 ; | |
/* | |
* psi = 1 - phi | |
*/ | |
var psi = 1 - phi; | |
/* | |
* Montado de forma legível | |
*/ | |
var fib = ( Math.pow(phi, n) - Math.pow(psi,n) ) | |
/**********************/ / /*********************/ | |
(phi - psi); | |
/* O computador nem sempre consegue a precisão necessária para o calculo acima, | |
* mas oarrendomento está correto, já que a sequencia de fibonacci é sempre um inteiro. | |
* Pelo menos quando n é um inteiro. | |
*/ | |
return Math.round(fib); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment