How to resolve the algorithm Fibonacci n-step number sequences step by step in the Kotlin programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Fibonacci n-step number sequences step by step in the Kotlin programming language

Table of Contents

Problem Statement

These number series are an expansion of the ordinary Fibonacci sequence where: For small values of

n

{\displaystyle n}

, Greek numeric prefixes are sometimes used to individually name each series. Allied sequences can be generated where the initial values are changed:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Fibonacci n-step number sequences step by step in the Kotlin programming language

Kotlin Code Explanation:

This Kotlin code generates sequences of numbers known as "n-nacci" sequences, where n represents the number of initial terms used in the sequence.

Function fibN:

  • Parameters:
    • initial: An array of integers representing the initial terms of the sequence.
    • numTerms: The total number of terms to generate.
  • Return: An array containing the first numTerms terms of the n-nacci sequence.
  • Implementation:
    • Verifies that the input parameters are valid.
    • Copies the initial terms into a new array of size numTerms.
    • If numTerms is less than or equal to the number of initial terms, it returns the initial terms.
    • Otherwise, it calculates the remaining terms of the sequence by adding the previous n terms.

Main Function:

  • Input:
    • An array of strings named names containing the names of n-nacci sequences (e.g., Fibonacci, Tribonacci, Tetranacci).
    • An array of integers named initial containing the initial terms for each sequence.
  • Output:
    • Prints a table showing the n-nacci sequences with different values of n.

Printing the Sequences:

  • The code prints the name of the sequence and the first 15 terms of the sequence.
  • For the Lucas sequence (n=2), it uses the initial terms [2, 1].
  • For sequences with n>2, it uses the initial terms specified in the initial array.

Usage:

This code can be used to generate and compare different n-nacci sequences. You can modify the names and initial arrays to explore other sequences.

Source code in the kotlin programming language

// version 1.1.2

fun fibN(initial: IntArray, numTerms: Int) : IntArray {
    val n = initial.size
    require(n >= 2 && numTerms >= 0)
    val fibs = initial.copyOf(numTerms)
    if (numTerms <= n) return fibs
    for (i in n until numTerms) {
        var sum = 0
        for (j in i - n until i) sum += fibs[j]
        fibs[i] = sum
    }
    return fibs
}

fun main(args: Array<String>) {
    val names = arrayOf("fibonacci",  "tribonacci", "tetranacci", "pentanacci", "hexanacci",
                        "heptanacci", "octonacci",  "nonanacci",  "decanacci")
    val initial = intArrayOf(1, 1, 2, 4, 8, 16, 32, 64, 128, 256)
    println(" n  name        values")
    var values = fibN(intArrayOf(2, 1), 15).joinToString(", ")
    println("%2d  %-10s  %s".format(2, "lucas", values))
    for (i in 0..8) {
        values = fibN(initial.sliceArray(0 until i + 2), 15).joinToString(", ")
        println("%2d  %-10s  %s".format(i + 2, names[i], values))
    }
}


  

You may also check:How to resolve the algorithm Ray-casting algorithm step by step in the BASIC programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Jaro-Winkler distance step by step in the jq programming language
You may also check:How to resolve the algorithm Pairs with common factors step by step in the C programming language