a$ = "1234567890": h$ = a$
Do
For i = 1 To Len(a$) - 1 Step 2
n1 = Val(Mid(a, i, 1)): If n1 = 0 Then n1 = 10
n2 = Val(Mid(a, i + 1, 1)): If n2 = 0 Then n2 = 10
tot = tot + Abs(n2 - n1)
DoEvents
Next
DoEvents
ct = ct + 1
permute a$
Loop Until a$ = h$
Text1.Text = Text1.Text & tot / ct
Text1.Text = Text1.Text & vbCrLf & " done"
(I note that it really wasn't necessary to convert zero to 10, as the differences overall wouldn't be affected-- 0 to 9 vs 1 to 10.)
This finds the average as 55/3.
More interesting is the general case, so for even number of elements:
2 1 = 3/3
4 10/3
6 7 = 21/3
8 12 = 36/3
10 55/3
It looks like a quadratic and indeed putting 3, 19, 12, 36, 55 into the OEIS, we find n*(2*n + 1) (sequence A014105).
Of course this might be misleading, as it also finds A281153, which continues to match until 11 elements later when an 18 replaces a 528.
But I'll go with the n*(2*n + 1)/3, where n is the length of the permuted sequence divided by 2. That makes the formula L*(L + 1)/6 where L is the length of the permuted sequence. That's the sum of the numbers from 1 to L divided by 3.
|
Posted by Charlie
on 2020-09-01 11:00:36 |