How to resolve the algorithm Jacobsthal numbers step by step in the SETL programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Jacobsthal numbers step by step in the SETL programming language

Table of Contents

Problem Statement

Jacobsthal numbers are an integer sequence related to Fibonacci numbers. Similar to Fibonacci, where each term is the sum of the previous two terms, each term is the sum of the previous, plus twice the one before that. Traditionally the sequence starts with the given terms 0, 1. Terms may be calculated directly using one of several possible formulas:

Jacobsthal-Lucas numbers are very similar. They have the same recurrence relationship, the only difference is an initial starting value J0 = 2 rather than J0 = 0. Terms may be calculated directly using one of several possible formulas: Jacobsthal oblong numbers is the sequence obtained from multiplying each Jacobsthal number Jn by its direct successor Jn+1.

Jacobsthal primes are Jacobsthal numbers that are prime.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Jacobsthal numbers step by step in the SETL programming language

Source code in the setl programming language

program jacobsthal_numbers;
    print("First 30 Jacobsthal numbers:");
    printseq([j n : n in [0..29]]);
    print;

    print("First 30 Jacobsthal-Lucas numbers:");
    printseq([jl n : n in [0..29]]);
    print;

    print("First 20 Jacobsthal oblong numbers:");
    printseq([jo n : n in [0..19]]);
    print;

    print("First 10 Jacobsthal primes:");
    printseq([j n : n in [0..31] | prime j n]);

    proc printseq(seq);
        loop for n in seq do
            nprint(lpad(str n, 14));
            if (i +:= 1) mod 5 = 0 then print; end if;
        end loop;
    end proc;

    op j(n);
        return (2**n - (-1)**n) div 3;
    end op;

    op jl(n);
        return 2**n + (-1)**n;
    end op;

    op jo(n);
        return j n * j (n+1);
    end op;

    op prime(n);
        if n<=4 then return n in {2,3}; end if;
        return not exists d in [2..floor sqrt n] | n mod d = 0;
    end op;
end program;

  

You may also check:How to resolve the algorithm Move-to-front algorithm step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Memory allocation step by step in the Sinclair ZX81 BASIC programming language
You may also check:How to resolve the algorithm Enumerations step by step in the E programming language
You may also check:How to resolve the algorithm Old lady swallowed a fly step by step in the Racket programming language
You may also check:How to resolve the algorithm Delete a file step by step in the Axe programming language