September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -14,8 +14,8 @@ Sort the digits largest to smallest. Do not include counts of digits that do not
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.)
<b>Task:</b>
;Task:
Find all the positive integer seed values under 1000000, for the above convergent self-referential sequence, that takes the largest number of iterations before converging. Then print out the number of iterations and the sequence they return. Note that different permutations of the digits of the seed will yield the same sequence. For this task, assume leading zeros are not permitted.
<pre>Seed Value(s): 9009 9090 9900
@ -45,4 +45,11 @@ Sequence: (same for all three seeds except for first element)
19281716151413427110
19182716152413228110
</pre>
See also: [[Self-describing numbers]] and [[Look-and-say sequence]]
;Related tasks:
* &nbsp; [[Fours is the number of letters in the ...]]
* &nbsp; [[Look-and-say sequence]]
* &nbsp; [[Number names]]
* &nbsp; [[Self-describing numbers]]
* &nbsp; [[Spelling of ordinal numbers]]
<br><br>

View file

@ -12,16 +12,15 @@ next(text s, integer show)
l -= 1;
e = 0;
u = insert("", 0, character(s, l));
r_g_integer(e, v, u);
u = insert("", 0, s[l]);
r_j_integer(e, v, u);
r_f_integer(v, u, e + 1);
}
if (r_last(v, u)) {
do {
b_paste(d, -1, itoa(r_q_integer(v, u)));
b_paste(d, -1, u);
} while (r_less(v, u, u));
b_plan(d, v[u], u);
} while (rsk_less(v, u, u));
}
if (show) {
@ -38,7 +37,7 @@ depth(text s, integer i, record r)
integer d;
d = 0;
r_g_integer(d, r, s);
r_j_integer(d, r, s);
if (d <= 0) {
i += 1;
if (d) {
@ -48,7 +47,7 @@ depth(text s, integer i, record r)
}
r_f_integer(r, s, d);
i = depth(next(s, 0), i, r);
d = r_q_integer(r, s);
d = r[s];
if (d <= 0) {
d = i + 1;
r_r_integer(r, s, d);

View file

@ -0,0 +1,68 @@
// version 1.1.2
const val LIMIT = 1_000_000
val sb = StringBuilder()
fun selfRefSeq(s: String): String {
sb.setLength(0) // faster than using a local StringBuilder object
for (d in '9' downTo '0') {
if (d !in s) continue
val count = s.count { it == d }
sb.append("$count$d")
}
return sb.toString()
}
fun permute(input: List<Char>): List<List<Char>> {
if (input.size == 1) return listOf(input)
val perms = mutableListOf<List<Char>>()
val toInsert = input[0]
for (perm in permute(input.drop(1))) {
for (i in 0..perm.size) {
val newPerm = perm.toMutableList()
newPerm.add(i, toInsert)
perms.add(newPerm)
}
}
return perms
}
fun main(args: Array<String>) {
val sieve = IntArray(LIMIT) // all zero by default
val elements = mutableListOf<String>()
for (n in 1 until LIMIT) {
if (sieve[n] > 0) continue
elements.clear()
var next = n.toString()
elements.add(next)
while (true) {
next = selfRefSeq(next)
if (next in elements) {
val size = elements.size
sieve[n] = size
if (n > 9) {
val perms = permute(n.toString().toList()).distinct()
for (perm in perms) {
if (perm[0] == '0') continue
val k = perm.joinToString("").toInt()
sieve[k] = size
}
}
break
}
elements.add(next)
}
}
val maxIterations = sieve.max()!!
for (n in 1 until LIMIT) {
if (sieve[n] < maxIterations) continue
println("$n -> Iterations = $maxIterations")
var next = n.toString()
for (i in 1..maxIterations) {
println(next)
next = selfRefSeq(next)
}
println()
}
}

View file

@ -0,0 +1,74 @@
string n = "000000"
function incn()
for i=length(n) to 1 by -1 do
if n[i]='9' then
if i=1 then return false end if
n[i]='0'
else
n[i] += 1
exit
end if
end for
return true
end function
sequence res = {}, bestseen
integer maxcycle = 0
procedure srs()
sequence seen, this = n
integer cycle = 1
while length(this)>1 and this[1]='0' do
this = this[2..$]
end while
integer ch = this[1]
for i=2 to length(this) do
if this[i]>ch then return end if
ch = this[i]
end for
seen = {this}
while 1 do
sequence digits = repeat(0,10)
for i=1 to length(this) do
digits[this[i]-'0'+1] += 1
end for
string next = ""
for i=length(digits) to 1 by -1 do
if digits[i]!=0 then
next &= sprint(digits[i])
next &= i+'0'-1
end if
end for
if find(next,seen) then exit end if
seen = append(seen,next)
this = next
cycle += 1
end while
if cycle>maxcycle then
res = {seen[1]}
maxcycle = cycle
bestseen = seen
elsif cycle=maxcycle then
res = append(res,seen[1])
end if
end procedure
while 1 do
srs()
if not incn() then exit end if
end while
-- add non-leading-0 perms:
for i=length(res) to 1 by -1 do
string ri = res[i]
for p=1 to factorial(length(ri)) do
string pri = permute(p,ri)
if pri[1]!='0' and not find(pri,res) then
res = append(res,pri)
end if
end for
end for
?res
puts(1,"cycle length is ") ?maxcycle
pp(bestseen,{pp_Nest,1})

View file

@ -1,34 +1,34 @@
/*REXX pgm generates a self-referential sequence and lists the maximums.*/
parse arg low high .; maxL=0; seeds=; max$$=
if low=='' then low=1 /*no low? Then use the default*/
if high=='' then high=1000000 /* " high? " " " " */
/*══════════════════════════════════════════════════traipse through #'s.*/
do seed=low to high; n=seed; $.=0; $$=n; $.n=1
/*REXX program generates a self─referential sequence and displays the maximums. */
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 /* " " " " " " */
max$=; seeds=; maxL=0 /*inialize some defaults and counters. */
do j=1 until x==n /*generate a self─referential seq*/
x=n; n=
do k=9 by -1 for 10 /*gen new sequence*/
_=countstr(k,x); if _\==0 then n=n||_||k
end /*k*/
if $.n then leave /*sequence been generated before?*/
$$=$$'-'n; $.n=1 /*add number to sequence & roster*/
end /*j*/
do #=LO to HI; n=#; @.=0; @.#=1 /*loop thru seed; define some defaults.*/
$=n
do c=1 until x==n; x=n /*generate a self─referential sequence.*/
n=; do k=9 by -1 for 10 /*generate a new sequence (downwards). */
_=countstr(k, x) /*obtain the number of sequence counts.*/
if _\==0 then n=n || _ || k /*is count > zero? Then append it to N*/
end /*k*/
if @.n then leave /*has sequence been generated before ? */
$=$'-'n; @.n=1 /*add the number to sequence and roster*/
end /*c*/
if j==maxL then do /*sequence equal to max so far ? */
seeds=seeds seed; maxnums=maxnums n; max$$=max$$ $$
if c==maxL then do; seeds=seeds # /*is the sequence equal to max so far ?*/
max$=max$ $ /*append this self─referential # to $ */
end
else if j>maxL then do /*have found a new best sequence.*/
seeds=seed; maxL=j; maxnums=n; max$$=$$
end
end /*seed*/
/*═══════════════════════════════════════════════════display the output.*/
say 'seeds that had the most iterations =' seeds
hdr=copies('',30); say 'maximum sequence length =' maxL
else if c>maxL then do; seeds=# /*use the new number as the new seed. */
maxL=c; max$=$ /*also, set the new maximum L; max seq.*/
end /* [↑] have we found a new best seq ? */
end /*#*/
do j=1 for words(max$$); say
say hdr "iteration sequence for: " word(seeds,j) ' ('maxL "iterations)"
q=translate(word(max$$,j),,'-')
do k=1 for words(q); say word(q,k)
say ' seeds that had the most iterations: ' seeds
say 'the maximum selfreferential length: ' maxL
do j=1 for words(max$) ; say
say copies('',30) "iteration sequence for: " word(seeds,j) ' ('maxL "iterations)"
q=translate( word( max$, j), ,'-')
do k=1 for words(q); say word(q, k)
end /*k*/
end /*j*/
/*stick a fork in it, we're done.*/
end /*j*/ /*stick a fork in it, we're all done. */

View file

@ -0,0 +1,32 @@
N:=0d1_000_001;
fcn lookAndJustSaying(seed){ // numeric String --> numeric String
"9876543210".pump(String,'wrap(n){
(s:=seed.inCommon(n)) and String(s.len(),n) or ""
});
}
fcn sequence(seed){ // numeric string --> sequence until it repeats
seq:=L();
while(not seq.holds(seed)){ seq.append(seed); seed=lookAndJustSaying(seed); }
seq
}
fcn decending(str) //--> True if digits are in descending (or equal) order
{ (not str.walker().zipWith('<,str[1,*]).filter1()) }
szs:=List.createLong(25); max:=0;
foreach seed in (N){
z:=seed.toString();
if(decending(z)){ // 321 generates same sequence as 312,132,123,213
len:=sequence(z).len();
if(len>max) szs.clear();
if(len>=max){ szs.append(seed.toString()); max=len; }
}
}
// List permutations of longest seeds
// ("9900"-->(((9,0,0,9),...))-->((9,0,0,9),...)-->("9009"...)
// -->remove numbers w/leading zeros-->remove dups
zs:=szs.apply(Utils.Helpers.permute).flatten().apply("concat")
.filter(fcn(s){ s[0]!="0" }) : Utils.Helpers.listUnique(_);
println(max," iterations for ",zs.concat(", "));
zs.pump(Console.println,sequence,T("concat",", "));