Task: Fibonacci n-step number sequences



C#: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fibonacci
{
    class Program
    {
        static void Main(string[] args)
        {
            PrintNumberSequence("Fibonacci", GetNnacciNumbers(2, 10));
            PrintNumberSequence("Lucas", GetLucasNumbers(10));
            PrintNumberSequence("Tribonacci", GetNnacciNumbers(3, 10));
            PrintNumberSequence("Tetranacci", GetNnacciNumbers(4, 10));
            Console.ReadKey();
        }

        private static IList<ulong> GetLucasNumbers(int length)
        {
            IList<ulong> seedSequence = new List<ulong>() { 2, 1 };
            return GetFibLikeSequence(seedSequence, length);
        }

        private static IList<ulong> GetNnacciNumbers(int seedLength, int length)
        {
            return GetFibLikeSequence(GetNacciSeed(seedLength), length);
        }

        private static IList<ulong> GetNacciSeed(int seedLength)
        {
            IList<ulong> seedSquence = new List<ulong>() { 1 };

            for (uint i = 0; i < seedLength - 1; i++)
            {
                seedSquence.Add((ulong)Math.Pow(2, i));
            }

            return seedSquence;
        }

        private static IList<ulong> GetFibLikeSequence(IList<ulong> seedSequence, int length)
        {
            IList<ulong> sequence = new List<ulong>();

            int count = seedSequence.Count();

            if (length <= count)
            {
                sequence = seedSequence.Take((int)length).ToList();
            }
            else
            {
                sequence = seedSequence;

                for (int i = count; i < length; i++)
                {
                    ulong num = 0;

                    for (int j = 0; j < count; j++)
                    {
                        num += sequence[sequence.Count - 1 - j];
                    }

                    sequence.Add(num);
                }
            }

            return sequence;
        }

        private static void PrintNumberSequence(string Title, IList<ulong> numbersequence)
        {
            StringBuilder output = new StringBuilder(Title).Append("   ");

            foreach (long item in numbersequence)
            {
                output.AppendFormat("{0}, ", item);
            }

            Console.WriteLine(output.ToString());
        }
    }
}






VB: Option Explicit

Sub Main()
Dim temp$, T() As Long, i&
    
    T = Fibonacci_Step(1, 15, 1)
    For i = LBound(T) To UBound(T)
        temp = temp & ", " & T(i)
    Next
    Debug.Print "Fibonacci: " & Mid(temp, 3)
    temp = ""
    
    
    T = Fibonacci_Step(1, 15, 2)
    For i = LBound(T) To UBound(T)
        temp = temp & ", " & T(i)
    Next
    Debug.Print "Tribonacci: " & Mid(temp, 3)
    temp = ""
    
    
    T = Fibonacci_Step(1, 15, 3)
    For i = LBound(T) To UBound(T)
        temp = temp & ", " & T(i)
    Next
    Debug.Print "Tetranacci: " & Mid(temp, 3)
    temp = ""
    
    
    T = Fibonacci_Step(1, 15, 1, 2)
    For i = LBound(T) To UBound(T)
        temp = temp & ", " & T(i)
    Next
    Debug.Print "Lucas: " & Mid(temp, 3)
    temp = ""
End Sub

Private Function Fibonacci_Step(First As Long, Count As Long, S As Long, Optional Second As Long) As Long()
Dim T() As Long, R() As Long, i As Long, Su As Long, C As Long

    If Second <> 0 Then S = 1
    ReDim T(1 - S To Count)
    For i = LBound(T) To 0
        T(i) = 0
    Next i
    T(1) = IIf(Second <> 0, Second, 1)
    T(2) = 1
    For i = 3 To Count
        Su = 0
        C = S + 1
        Do While C >= 0
            Su = Su + T(i - C)
            C = C - 1
        Loop
        T(i) = Su
    Next
    ReDim R(1 To Count)
    For i = 1 To Count
        R(i) = T(i)
    Next
    Fibonacci_Step = R
End Function