Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

"The program, which was about 50 statements in Visual Basic, checked all of 8991 four digit numbers from 1000 to 9999..."

Whoa, you need 50 statements in VB for that? I need 14 in Python:

    import collections

    def kaprekar(k):
        nr = 0
        while True:
            if k == 6174: return nr
            small = int("".join(sorted(list("%04d" % k))))
            large = int("".join(sorted(list("%04d" % k), reverse=True)))
            k = large - small
            nr += 1
    
    freqs = collections.defaultdict(int)        
    for i in range(1000, 10000):
        if not i % 1111: continue # skip 1111, 2222, etc...
        freqs[kaprekar(i)] += 1

    for k,v in freqs.items():
        print k, v
Outputs:

  0 1
  1 356 
  2 519
  3 2124
  4 1124
  5 1379
  6 1508
  7 1980


And somebody programming in some other language might need even less lines. What's your point?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: