September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -1,15 +1,11 @@
|
|||
There are several ways to generate a self-referential sequence. One very common one (the [[Look-and-say sequence]]) is to start with a positive integer, then generate the next term by concatenating enumerated groups of adjacent alike digits:
|
||||
|
||||
0, 10, 1110, 3110, 132110, 1113122110, 311311222110 ...
|
||||
|
||||
0, 10, 1110, 3110, 132110, 1113122110, 311311222110 ...
|
||||
The terms generated grow in length geometrically and never converge.
|
||||
|
||||
Another way to generate a self-referential sequence is to summarize the previous term.
|
||||
|
||||
Count how many of each alike digit there is, then concatenate the sum and digit for each of the sorted enumerated digits. Note that the first five terms are the same as for the previous sequence.
|
||||
|
||||
0, 10, 1110, 3110, 132110, 13123110, 23124110 ... see [[oeis:A036058|The On-Line Encyclopedia of Integer Sequences]]
|
||||
|
||||
0, 10, 1110, 3110, 132110, 13123110, 23124110 ...
|
||||
Sort the digits largest to smallest. Do not include counts of digits that do not appear in the previous term.
|
||||
|
||||
Depending on the seed value, series generated this way always either converge to a stable value or to a short cyclical pattern. (For our purposes, I'll use converge to mean an element matches a previously seen element.) The sequence shown, with a seed value of 0, converges to a stable value of 1433223110 after 11 iterations. The seed value that converges most quickly is 22. It goes stable after the first element. (The next element is 22, which has been seen before.)
|
||||
|
|
@ -52,4 +48,8 @@ Sequence: (same for all three seeds except for first element)
|
|||
* [[Number names]]
|
||||
* [[Self-describing numbers]]
|
||||
* [[Spelling of ordinal numbers]]
|
||||
|
||||
|
||||
;Also see:
|
||||
* [[oeis:A036058|The On-Line Encyclopedia of Integer Sequences]].
|
||||
<br><br>
|
||||
|
|
|
|||
1
Task/Self-referential-sequence/00META.yaml
Normal file
1
Task/Self-referential-sequence/00META.yaml
Normal file
|
|
@ -0,0 +1 @@
|
|||
--- {}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
const seen = Dict{Vector{Char}, Vector{Char}}()
|
||||
|
||||
function findnextterm(prevterm)
|
||||
counts = Dict{Char, Int}()
|
||||
reversed = Vector{Char}()
|
||||
for c in prevterm
|
||||
if !haskey(counts, c)
|
||||
counts[c] = 0
|
||||
end
|
||||
counts[c] += 1
|
||||
end
|
||||
for c in sort(collect(keys(counts)))
|
||||
if counts[c] > 0
|
||||
push!(reversed, c)
|
||||
if counts[c] == 10
|
||||
push!(reversed, '0'); push!(reversed, '1')
|
||||
else
|
||||
push!(reversed, Char(UInt8(counts[c]) + UInt8('0')))
|
||||
end
|
||||
end
|
||||
end
|
||||
reverse(reversed)
|
||||
end
|
||||
|
||||
function findsequence(seedterm)
|
||||
term = seedterm
|
||||
sequence = Vector{Vector{Char}}()
|
||||
while !(term in sequence)
|
||||
push!(sequence, term)
|
||||
if !haskey(seen, term)
|
||||
nextterm = findnextterm(term)
|
||||
seen[term] = nextterm
|
||||
end
|
||||
term = seen[term]
|
||||
end
|
||||
return sequence
|
||||
end
|
||||
|
||||
function selfseq(maxseed)
|
||||
maxseqlen = -1
|
||||
maxsequences = Vector{Pair{Int, Vector{Char}}}()
|
||||
for i in 1:maxseed
|
||||
seq = findsequence([s[1] for s in split(string(i), "")])
|
||||
seqlen = length(seq)
|
||||
if seqlen > maxseqlen
|
||||
maxsequences = [Pair(i, seq)]
|
||||
maxseqlen = seqlen
|
||||
elseif seqlen == maxseqlen
|
||||
push!(maxsequences, Pair(i, seq))
|
||||
end
|
||||
end
|
||||
println("The longest sequence length is $maxseqlen.")
|
||||
for p in maxsequences
|
||||
println("\n Seed: $(p[1])")
|
||||
for seq in p[2]
|
||||
println(" ", join(seq, ""))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
selfseq(1000000)
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
/*REXX program generates a self─referential sequence and displays the maximums. */
|
||||
/*REXX pgm generates a self─referential sequence and displays sequences with max length.*/
|
||||
parse arg LO HI . /*obtain optional arguments from the CL*/
|
||||
if LO=='' | LO=="," then LO= 1 /*Not specified? Then use the default.*/
|
||||
if HI=='' | HI=="," then HI=1000000 - 1 /* " " " " " " */
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
import spire.math.SafeLong
|
||||
|
||||
import scala.annotation.tailrec
|
||||
import scala.collection.parallel.immutable.ParVector
|
||||
|
||||
object SelfReferentialSequence {
|
||||
def main(args: Array[String]): Unit = {
|
||||
val nums = ParVector.range(1, 1000001).map(SafeLong(_))
|
||||
val seqs = nums.map{ n => val seq = genSeq(n); (n, seq, seq.length) }.toVector.sortWith((a, b) => a._3 > b._3)
|
||||
val maxes = seqs.takeWhile(t => t._3 == seqs.head._3)
|
||||
|
||||
println(s"Seeds: ${maxes.map(_._1).mkString(", ")}\nIterations: ${maxes.head._3}")
|
||||
for(e <- maxes.distinctBy(a => nextTerm(a._1.toString))){
|
||||
println(s"\nSeed: ${e._1}\n${e._2.mkString("\n")}")
|
||||
}
|
||||
}
|
||||
|
||||
def genSeq(seed: SafeLong): Vector[String] = {
|
||||
@tailrec
|
||||
def gTrec(seq: Vector[String], n: String): Vector[String] = {
|
||||
if(seq.contains(n)) seq
|
||||
else gTrec(seq :+ n, nextTerm(n))
|
||||
}
|
||||
gTrec(Vector[String](), seed.toString)
|
||||
}
|
||||
|
||||
def nextTerm(num: String): String = {
|
||||
@tailrec
|
||||
def dTrec(digits: Vector[(Int, Int)], src: String): String = src.headOption match{
|
||||
case Some(n) => dTrec(digits :+ ((n.asDigit, src.count(_ == n))), src.filter(_ != n))
|
||||
case None => digits.sortWith((a, b) => a._1 > b._1).map(p => p._2.toString + p._1.toString).mkString
|
||||
}
|
||||
dTrec(Vector[(Int, Int)](), num)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue