EKG sequence convergence: Difference between revisions

Content added Content deleted
(Realıze ın F#-Defıne convergence not to depend on programmes ınternal state-Add extra credıt)
Line 15: Line 15:


;Convergence
;Convergence
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.<br>
Two EKG generators with differing starts can converge to produce the same sequence after initial differences.<br>
<code>EKG(N1)</code> and <code>EKG(N2)</code> are said to to have converged at and after generation <code>a(c)</code> if <code>state_of(EKG(N1).a(c)) == state_of(EKG(N2).a(c))</code>.
<code>EKG(N1)</code> and <code>EKG(N2)</code> are said to to have converged at and after generation <code>a(c)</code> ıf the set (unordered collectıon of ıtems) of digıts used so far by <code>EKG(N1)</code> and <code>EKG(N2)</code> are equal and <code>a(c)</code> ıs the same for both.




Line 27: Line 27:
# Calculate and show here at which term <code>EKG(5)</code> and <code>EKG(7)</code> converge &nbsp; ('''stretch goal''').
# Calculate and show here at which term <code>EKG(5)</code> and <code>EKG(7)</code> converge &nbsp; ('''stretch goal''').


;Extra Credıt:
Fınd the position ın sequence <code>EKG(2)</code> of the 10000th prıme number 104729


;Related Tasks:
;Related Tasks:
Line 125: Line 127:
</pre>
</pre>


=={{header|F_Sharp|F#}}==
===The Function===
<lang fsharp>
// Generate EKG Sequences. Nigel Galloway: October 6th., 2017
let EKG n=seq{
let fN,fG=let i=System.Collections.Generic.Dictionary<int,int>()
let fN g=(if not (i.ContainsKey g) then i.[g]<-g);(g,i.[g])
((fun e->i.[e]<-i.[e]+e), (fun l->l|>List.map fN))
let fU l= pCache|>Seq.takeWhile(fun n->n<=l)|>Seq.filter(fun n->l%n=0)|>List.ofSeq
let rec EKG l (α,β)=seq{let b=fU β in if (β=n||β<snd((fG b|>List.maxBy snd))) then fN α; yield! EKG l (fG l|>List.minBy snd)
else fN α;yield β;yield! EKG b (fG b|>List.minBy snd)}
yield! seq[1;n]; let g=fU n in yield! EKG g (fG g|>Seq.minBy snd)}
</lang>
===The Task===
<lang fsharp>
EKG 2 |> Seq.take 45 |> Seq.iter(printf "%2d, ")
let EKGconv n g=Seq.zip(EKG n)(EKG g)|>Seq.skip 2|>Seq.scan(fun(n,i,g,e)(l,β)->(Set.add l n,Set.add β i,l,β))(set[1;n],set[1;g],0,0)|>Seq.takeWhile(fun(n,i,g,e)->g<>e||n<>i)
printfn "%d" (let n,_,_,_=EKGconv 2 5|>Seq.last in ((Set.count n)+1)
</lang>
{{out}}
<pre>
45
</pre>
===Extra Credit===
<lang fsharp>
prıntfn "%d" (EKG 2|>Seq.takeWhile(fun n->n<>104729) ((Seq.length n)+1)
</lang>
{{out}}
<pre>
203786
</pre>
=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<lang go>package main