How to resolve the algorithm EKG sequence convergence step by step in the Factor programming language
How to resolve the algorithm EKG sequence convergence step by step in the Factor 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 Factor programming language
Source code in the factor programming language
USING: combinators.short-circuit formatting fry io kernel lists
lists.lazy math math.statistics prettyprint sequences
sequences.generalizations ;
: ekg? ( n seq -- ? )
{ [ member? not ] [ last gcd nip 1 > ] } 2&& ;
: (ekg) ( seq -- seq' )
2 lfrom over [ ekg? ] curry lfilter car suffix! ;
: ekg ( n limit -- seq )
[ 1 ] [ V{ } 2sequence ] [ 2 - [ (ekg) ] times ] tri* ;
: show-ekgs ( seq n -- )
'[ dup _ ekg "EKG(%d) = %[%d, %]\n" printf ] each ;
: converge-at ( n m max -- o )
tuck [ ekg [ cum-sum ] [ rest-slice ] bi ] 2bi@
[ swapd [ = ] 2bi@ and ] 4 nfind 4drop dup [ 2 + ] when ;
{ 2 5 7 9 10 } 20 show-ekgs nl
"EKG(5) and EKG(7) converge at term " write
5 7 100 converge-at .
You may also check:How to resolve the algorithm Image noise step by step in the OCaml programming language
You may also check:How to resolve the algorithm Create a file on magnetic tape step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Miranda programming language
You may also check:How to resolve the algorithm Binary search step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Read a file line by line step by step in the PowerShell programming language