Java Fibonacci Sequence Get toFibonacciSumSequences(long i)

Here you can find the source of toFibonacciSumSequences(long i)

Description

to Fibonacci Sum Sequences

License

Open Source License

Declaration

public static long[] toFibonacciSumSequences(long i) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import static java.lang.Math.*;

public class Main {
    public static final double GOLDEN_RATIO = (1 + sqrt(5)) / 2;

    public static long[] toFibonacciSumSequences(long i) {
        return toFibonacciSum(true, new long[64], 0, i, 0);
    }/*w  w w .  jav a  2  s .c o m*/

    public static long[] toFibonacciSum(long i) {
        return toFibonacciSum(false, new long[64], 0, i, 0);
    }

    public static long[] toFibonacciSum(boolean seq, long[] nums, int counter, long l, long prevSeq) {
        if (l < 0)
            return null;

        if (l == 0) {
            long[] result = new long[counter];
            System.arraycopy(nums, 0, result, 0, counter);
            return result;
        }

        int nearestSeq = getFibonacciSeq(l);
        if (prevSeq - nearestSeq == 1)
            nearestSeq--;

        long nearestFibonacci = getFibonacciFast(nearestSeq);
        nums[counter++] = seq ? nearestSeq : nearestFibonacci;
        return toFibonacciSum(seq, nums, counter, l - nearestFibonacci, nearestSeq);
    }

    public static int getFibonacciSeq(long f) {
        return (int) Math.floor(Math.log(f * sqrt(5) + 0.5) / Math.log(GOLDEN_RATIO));
    }

    public static long getFibonacciFast(int n) {
        return (long) Math.floor(pow(GOLDEN_RATIO, n) / sqrt(5) + 0.5);
    }
}

Related

  1. getFibonacci(int number)
  2. getFibonacciSeq(long f)
  3. getFibonnaci(long i)
  4. isFibonacci(long f)
  5. makeFibonacciSupplier()