How to resolve the algorithm EKG sequence convergence step by step in the Nim programming language
How to resolve the algorithm EKG sequence convergence step by step in the Nim programming language
Table of Contents
Problem Statement
The sequence is from the natural numbers and is defined by: The sequence is called the EKG sequence (after its visual similarity to an electrocardiogram when graphed). Variants of the sequence can be generated starting 1, N where N is any natural number larger than one. For the purposes of this task let us call:
If an algorithm that keeps track of the minimum amount of numbers and their corresponding prime factors used to generate the next term is used, then this may be known as the generators essential state. Two EKG generators with differing starts can converge to produce the same sequence after initial differences. EKG(N1) and EKG(N2) are said to to have converged at and after generation a(c) if state_of(EKG(N1).a(c)) == state_of(EKG(N2).a(c)).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm EKG sequence convergence step by step in the Nim programming language
Source code in the nim programming language
import algorithm, math, sets, strformat, strutils
#---------------------------------------------------------------------------------------------------
iterator ekg(n, limit: Positive): (int, int) =
var values: HashSet[int]
doAssert n >= 2
yield (1, 1)
yield (2, n)
values.incl(n)
var i = 3
var prev = n
while i <= limit:
var val = 2
while true:
if val notin values and gcd(val, prev) != 1:
values.incl(val)
yield (i, val)
prev = val
break
inc val
inc i
#---------------------------------------------------------------------------------------------------
for n in [2, 5, 7, 9, 10]:
var result: array[1..10, int]
for i, val in ekg(n, 10): result[i] = val
let title = fmt"EKG({n}):"
echo fmt"{title:8} {result.join("", "")}"
var ekg5, ekg7: array[1..100, int]
for i, val in ekg(5, 100): ekg5[i] = val
for i, val in ekg(7, 100): ekg7[i] = val
var convIndex = 0
for i in 2..100:
if ekg5[i] == ekg7[i] and sorted(ekg5[1..<i]) == sorted(ekg7[1..<i]):
convIndex = i
break
if convIndex > 0:
echo fmt"EKG(5) and EKG(7) converge at index {convIndex}."
else:
echo "No convergence found in the first {convIndex} terms."
You may also check:How to resolve the algorithm Stirling numbers of the second kind step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Deepcopy step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Sort an array of composite structures step by step in the C# programming language
You may also check:How to resolve the algorithm Nested function step by step in the Julia programming language
You may also check:How to resolve the algorithm Determinant and permanent step by step in the Java programming language