"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
Whoa, you need 50 statements in VB for that? I need 14 in Python:
Outputs: