How to resolve the algorithm Jacobsthal numbers step by step in the Julia programming language
How to resolve the algorithm Jacobsthal numbers step by step in the Julia 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 Julia programming language
This Julia code explores the sequences of Jacobsthal numbers, Jacobsthal-Lucas numbers, oblong Jacobsthal numbers, and Jacobsthal prime numbers. Here's a breakdown of the code:
-
Importing Libraries:
using Lazy
: This imports the Lazy package for creating lazy sequences.using Primes
: This imports the Primes package for working with prime numbers.
-
Defining Functions:
J(n)
: This function computes the Jacobsthal number for indexn
.L(n)
: This function computes the Jacobsthal-Lucas number for indexn
.
-
Creating Lazy Sequences:
Jacobsthal
: This is a lazy sequence that generates Jacobsthal numbers starting from index 0.JLucas
: This is a lazy sequence that generates Jacobsthal-Lucas numbers starting from index 0.Joblong
: This is a lazy sequence that generates oblong Jacobsthal numbers starting from index 0.Jprimes
: This is a lazy sequence that generates Jacobsthal numbers starting from index 0, filtered to include only prime numbers.
-
Helper Function:
printrows
: This is a helper function that prints the elements of a given vector in rows, with customizable column size, number of columns, and left/right justification.
-
Printing Sequences:
- The code uses the
printrows
function to print the first 30 Jacobsthal numbers, 30 Jacobsthal-Lucas numbers, 20 oblong Jacobsthal numbers, and 15 Jacobsthal prime numbers. The output is formatted with specific column sizes and justification.
- The code uses the
Overall, this code demonstrates the use of lazy sequences to efficiently generate and explore different types of Jacobsthal numbers, showcasing the versatility of the Julia programming language for mathematical computations.
Source code in the julia programming language
using Lazy
using Primes
J(n) = (2^n - (-1)^n) ÷ 3
L(n) = 2^n + (-1)^n
Jacobsthal = @>> Lazy.range(0) map(J)
JLucas = @>> Lazy.range(0) map(L)
Joblong = @>> Lazy.range(big"0") map(n -> J(n) * J(n + 1))
Jprimes = @>> Lazy.range(big"0") map(J) filter(isprime)
function printrows(title, vec, columnsize = 15, columns = 5, rjust=true)
println(title)
for (i, n) in enumerate(vec)
print((rjust ? lpad : rpad)(n, columnsize), i % columns == 0 ? "\n" : "")
end
println()
end
printrows("Thirty Jacobsthal numbers:", collect(take(30, Jacobsthal)))
printrows("Thirty Jacobsthal-Lucas numbers:", collect(take(30, JLucas)))
printrows("Twenty oblong Jacobsthal numbers:", collect(take(20, Joblong)))
printrows("Fifteen Jacabsthal prime numbers:", collect(take(15, Jprimes)), 40, 1, false)
You may also check:How to resolve the algorithm Y combinator step by step in the R programming language
You may also check:How to resolve the algorithm Bitwise IO step by step in the Lingo programming language
You may also check:How to resolve the algorithm Sum digits of an integer step by step in the R programming language
You may also check:How to resolve the algorithm Matrix transposition step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm URL decoding step by step in the Icon and Unicon programming language