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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Jacobsthal numbers step by step in the Wren 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 Wren programming language

Source code in the wren programming language

import "./big" for BigInt
import "./seq" for Lst
import "./fmt" for Fmt

var jacobsthal = Fn.new { |n| ((BigInt.one << n) - ((n%2 == 0) ? 1 : -1)) / 3 }

var jacobsthalLucas = Fn.new { |n| (BigInt.one << n) + ((n%2 == 0) ? 1 : -1) }

System.print("First 30 Jacobsthal numbers:")
var js = (0..29).map { |i| jacobsthal.call(i) }.toList
Fmt.tprint("$,12i", js, 5)

System.print("\nFirst 30 Jacobsthal-Lucas numbers:")
var jsl = (0..29).map { |i| jacobsthalLucas.call(i) }.toList
Fmt.tprint("$,12i", jsl, 5)

System.print("\nFirst 20 Jacobsthal oblong numbers:")
var oblongs = (0..19).map { |i| js[i] * js[i+1] }.toList
Fmt.tprint("$,14i", oblongs, 5)

var primes = js.where { |j| j.isProbablePrime(10) }.toList
var count = primes.count
var i = 31
while (count < 20) {
    var j = jacobsthal.call(i)
    if (j.isProbablePrime(10)) {
        primes.add(j)
        count = count + 1
    }
    i = i + 1
}
System.print("\nFirst 20 Jacobsthal primes:")
for (i in 0..19) Fmt.print("$i", primes[i])


  

You may also check:How to resolve the algorithm Closest-pair problem step by step in the Pascal programming language
You may also check:How to resolve the algorithm Ludic numbers step by step in the Racket programming language
You may also check:How to resolve the algorithm Introspection step by step in the Racket programming language
You may also check:How to resolve the algorithm Loops/Break step by step in the Oforth programming language
You may also check:How to resolve the algorithm Look-and-say sequence step by step in the Picat programming language