Created
August 24, 2017 22:51
-
-
Save butchi/991bff7a1068c93d7a00eea1e92289e5 to your computer and use it in GitHub Desktop.
フィボナッチ列の計算
This file contains hidden or 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
const s = []; | |
s[0] = '0'; | |
s[1] = '01'; | |
function fibonacci_word(n) { | |
let ret; | |
if (n < 2) { | |
ret = s[n]; | |
return ret; | |
} | |
ret = fibonacci_word(n - 1) + fibonacci_word(n - 2); | |
s[n] = ret; | |
return ret; | |
} | |
let num; | |
num = fibonacci_word(5); | |
console.log(num); // => 0100101001001 |
This file contains hidden or 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
function fibonacci_word_sequence(n) { | |
const { sqrt, floor } = Math; | |
const φ = (1 + sqrt(5)) / 2; | |
let ret; | |
ret = 2 + floor((n + 1) * φ) - floor((n + 2) * φ); | |
return ret; | |
} | |
const zeros = (n) => new Array(n).fill(0); | |
const arr = zeros(13).map( (_num, i) => fibonacci_word_sequence(i) ); | |
console.log(arr); // => [0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1] |
This file contains hidden or 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
const { sqrt, floor } = Math; | |
const φ = (1 + sqrt(5)) / 2; | |
const zeros = (n) => new Array(n).fill(0); | |
function fibonacci_word_sequence_array_with_length(n) { | |
const gArr = zeros(floor((n + 1) / φ)).map( (_num, i) => (i + 1) / φ ); | |
const iArr = zeros(floor((n + 1) / φ / φ)).map( (_num, i) => i + 1 ); | |
const cuttingArr = gArr.concat(iArr); | |
cuttingArr.sort( (a, b) => a - b ); | |
let ret; | |
ret = cuttingArr.map( (num) => Number.isInteger(num) ? 1 : 0 ); | |
return ret; | |
} | |
let arr; | |
arr = fibonacci_word_sequence_array_with_length(13); | |
console.log(arr); // => [0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment