How to resolve the algorithm Executable library step by step in the Julia programming language
How to resolve the algorithm Executable library step by step in the Julia programming language
Table of Contents
Problem Statement
The general idea behind an executable library is to create a library that when used as a library does one thing; but has the ability to be run directly via command line. Thus the API comes with a CLI in the very same source code file. Task detail Notes:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Executable library step by step in the Julia programming language
This code defines a module Hailstone
that contains a function hailstone
and a function countstones
and a script that uses these functions.
Here is a breakdown of the code:
-
Hailstone
module:- The
hailstone
function takes an integern
as input and returns a list of integers representing the hailstone sequence forn
. - The hailstone sequence for
n
is defined as follows:- If
n
is even, thenn
is replaced byn/2
. - If
n
is odd, thenn
is replaced by3n + 1
. - The sequence continues until
n
reaches 1.
- If
- The
hailstone
function generates the sequence by repeatedly applying the above rules ton
and appending the resulting values to a list. - The
hailstone
function is exported from the module, making it available for use in other modules or scripts.
- The
-
countstones
function:- The
countstones
function takes two integers,mi
andmx
, as input and returns a tuple containing two values:- The most common hailstone sequence length for
n
in the rangemi
tomx
. - The number of times the most common sequence length occurs.
- The most common hailstone sequence length for
- The function generates a dictionary where the keys are hailstone sequence lengths and the values are the number of times that length occurs.
- The function then iterates through the dictionary to find the key (sequence length) with the highest value (number of occurrences).
- The function returns the key and the value as a tuple.
- The
-
Script:
- The script includes the
Hailstone
module and uses thehailstone
andcountstones
functions to calculate the most common hailstone sequence length forn
in the range 1 to 99999. - The script prints the most common sequence length and the number of times it occurs.
- The script includes the
In summary, this code defines a module with functions for generating hailstone sequences and counting the occurrences of different sequence lengths. The script uses these functions to find the most common hailstone sequence length for a given range of integers.
Source code in the julia programming language
############### in file hailstone.jl ###############
module Hailstone
function hailstone(n)
ret = [n]
while n > 1
if n & 1 > 0
n = 3n + 1
else
n = Int(n//2)
end
append!(ret, n)
end
return ret
end
export hailstone
end
if PROGRAM_FILE == "hailstone.jl"
using Hailstone
h = hailstone(27)
n = length(h)
println("The sequence of hailstone(27) is:\n $h.\nThis sequence is of length $n. It starts with $(h[1:4]) and ends with $(h[n-3:end]).")
end
############ in file moduletest.jl ####################
include("hailstone.jl")
using Hailstone
function countstones(mi, mx)
lengths2occurences = Dict()
mostfreq = mi
maxcount = 1
for i in mi:mx
h = hailstone(i)
n = length(h)
if haskey(lengths2occurences, n)
newoccurences = lengths2occurences[n] + 1
if newoccurences > maxcount
maxcount = newoccurences
mostfreq = n
end
lengths2occurences[n] = newoccurences
else
lengths2occurences[n] = 1
end
end
mostfreq, maxcount
end
nlen, cnt = countstones(1,99999)
print("The most common hailstone sequence length for hailstone(n) for 1 <= n < 100000 is $nlen, which occurs $cnt times.")
You may also check:How to resolve the algorithm Add a variable to a class instance at runtime step by step in the Slate programming language
You may also check:How to resolve the algorithm Stack traces step by step in the BASIC programming language
You may also check:How to resolve the algorithm Guess the number step by step in the Brat programming language
You may also check:How to resolve the algorithm Assertions step by step in the NGS programming language
You may also check:How to resolve the algorithm Casting out nines step by step in the EasyLang programming language