[Edu-sig] Mystery Number 6174

Mark Tolonen metolone+gmane at gmail.com
Sun Jul 6 23:50:24 CEST 2008


"kirby urner" <kirby.urner at gmail.com> wrote in message 
news:f604188c0807061136j32aa6797oef31101da4406d6d at mail.gmail.com...
> Yutaka Nishiyama has this interesting article in the March 2006 issue
> of Plus, entitled 'Mysterious number 6174'.
>
> The theorem in this article is as follows: any 4-digit positive
> integer, stipulating all 4 digits *not* be the same one, may be
> distilled to 6174 by the following
> process: extract the largest and smallest two integers from the 4
> digits and subtract the smaller from the larger, and repeat as
> necessary.
>
>>>> funtopic.converge()
> 2283: 8322 - 2238 == 6084
> 6084: 8640 - 0468 == 8172
> 8172: 8721 - 1278 == 7443
> 7443: 7443 - 3447 == 3996
> 3996: 9963 - 3699 == 6264
> 6264: 6642 - 2466 == 4176
> 4176: 7641 - 1467 == 6174
>>>> funtopic.converge()
> 9822: 9822 - 2289 == 7533
> 7533: 7533 - 3357 == 4176
> 4176: 7641 - 1467 == 6174
>>>> funtopic.converge()
> 6685: 8665 - 5668 == 2997
> 2997: 9972 - 2799 == 7173
> 7173: 7731 - 1377 == 6354
> 6354: 6543 - 3456 == 3087
> 3087: 8730 - 0378 == 8352
> 8352: 8532 - 2358 == 6174
>
> Here's one way to test the result.  Think of a different way?
>
> def somenum(n = 4):
>    digits = range(10)
>    ans = []
>    good = False # no exit pass (yet)
>
>    while True:
>
>        for i in range(n):
>            ans.append(choice(digits)) # pick any n digits
>
>        firstdigit = ans[0]
>        # not much chance all digits the same
>        # compare first to rest, pass test on first
>        # exception
>        for i in range(1,n+1):
>            if firstdigit != ans[i]:
>                good = True  # exit pass
>                break
>
>        if good:  break # has exit pass
>
>    return "".join([str(i) for i in ans])
>
> def converge(closer = 6174):
>    ournum = somenum()
>    while True:
>        smallest = sorted(list(ournum))
>        highest  = reversed(smallest)
>        strhi, strlo = "".join(highest), "".join(smallest)
>        diff = int(strhi) - int(strlo)
>        print( "%s: %s - %s == %s" % (ournum, strhi, strlo, diff) )
>        if diff == closer: return
>        ournum = str(diff)
>
> Kirby
>
> Related reading:
> http://controlroom.blogspot.com/2007/01/aguirre-wrath-of-god-movie-review.html

The following tests all possible 4-digit combos, excluding "all 4 digits 
*not* be the same one".  It turns out seven iterations is the maximum it 
takes to converge for any number.

def converge(n, maxiter=7):
    i = 0  # iteration count
    while n != 6174 and i < maxiter:
        i += 1
        L=list(str(n).zfill(4))
        min_n = int(''.join(sorted(L)))
        max_n = int(''.join(sorted(L, reverse=True)))
        n = max_n - min_n
    return n == 6174

for n in [i for i in xrange(10000) if i % 1111 != 0]:
    if not converge(n):
        print 'FAILED'
        break
else:
    print 'PASSED'




More information about the Edu-sig mailing list