Skip to content

Instantly share code, notes, and snippets.

@tuscen
Last active November 12, 2015 23:14
Show Gist options
  • Select an option

  • Save tuscen/2358f0bf7786e57ae35d to your computer and use it in GitHub Desktop.

Select an option

Save tuscen/2358f0bf7786e57ae35d to your computer and use it in GitHub Desktop.
Fibonacci notation to decimal notation converter
// Работает нативно в Node.js 5.0
'use strict';
// Функция, возвращающая генератор бесконечной
// последовательности чисел Фибоначчи. Итерируема
// посредством вызова метода next() на полученном
// объекте.
function fibonacciGenerator() {
let a = 0
,b = 1;
return {
next() {
let bubble = a;
a = b;
b += bubble;
return a;
}
}
}
// Функция для получения последовательности значений из
// коллекции, бесконечно итерируемой при помощи вызова метода
// next на нем. Остановка итерации происходит посредством
// передачи полученного на шаге итерации значения в функцию
// предикат, возвращающую true или false.
function getSeqFromGen(iter_gen, limit) {
let iterator = iter_gen();
let sequence = [];
let iter = (iterator, count, limit, acc) => {
let value = iterator.next();
if (limit(value, count)) {
return acc;
} else {
return iter(iterator, count + 1, limit, acc.concat(value));
}
};
return iter(iterator, 1, limit, []);
}
function decToFib(v) {
if (v === 0) {
return [0];
}
let fibonacciSequence = getSeqFromGen(fibonacciGenerator, value => value > v);
// Решение посредством использования рекурсии
// let iter = (sequence, limit , acc) => {
// switch (sequence.length) {
// case 0:
// return acc;
// default:
// let value = sequence[0];
// let newLimit, newAcc;
// if (limit - value === 0) {
// newAcc = acc.concat(1);
// newLimit = limit - value;
// } else if (limit - value > 0) {
// newAcc = acc.concat(1);
// newLimit = limit - value;
// } else if (limit - value < 0) {
// newAcc = acc.concat(0);
// newLimit = limit;
// }
// return iter(sequence.slice(1), newLimit, newAcc);
// }
// };
// let result = iter(fibonacciSequence.reverse(), v, []);
// result.splice(-1, 1);
// return result;
let result = fibonacciSequence.reverse().reduce((acc, fibNum) => {
let newAcc = {};
if (acc.limit - fibNum === 0 ||
acc.limit - fibNum > 0) {
newAcc.value = acc.value.concat(1);
newAcc.limit = acc.limit - fibNum;
} else if (acc.limit - fibNum < 0) {
newAcc.value = acc.value.concat(0);
newAcc.limit = acc.limit;
}
return newAcc;
}, {value: [], limit: v}).value;
result.splice(-1, 1);
return result;
}
var fibToDec = function(number) {
let length = number.length;
let fibonacciSequence = getSeqFromGen(fibonacciGenerator, (_, count) => count > length + 1);
fibonacciSequence = fibonacciSequence.slice(1);
return number.split('').reverse().reduce((acc, char, index) => {
if (char === '1') {
return acc + fibonacciSequence[index];
}
return acc;
}, 0);
};
console.log('156754 ==', fibToDec(decToFib(156754).join('')));
console.log('46 ==', fibToDec(decToFib(46).join('')));
console.log('57 ==', fibToDec(decToFib(57).join('')));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment