For instance:
4064.
First, rearrange the digits to form the highest and lowest value possible [6440, 0446], subtract the smaller from the larger, and repeat the process.
Within 7 iterations; you'll get Six One Seven Four.
3 digit inputs get you 495.
5 digit numbers, however, get you a series that repeats.
In an effort to learn more python, here's a bunch of code to do 3 and 4 digit patterns. It doesn't cope with recognizing sets, which is a bit beyond me, and it's very hacky foo - there must be some way to use string manipulation functions on numbers without casting here there and everywhere.
import operator
import copy
def kapreka(number, repeatValue="0"):
if int(number) == int(repeatValue):
return
a, b = "", ""
n = len(number)
stack = []
for i in range(n):
stack.append(int(number[i]))
stack.sort(cmp=lambda x,y: cmp(x, y))
sorted = copy.copy(stack)
stack.sort(cmp=lambda x,y: cmp(x, y), reverse=True)
reversed = copy.copy(stack)
for i in range(n):
a = operator.concat(a, str(sorted[i]))
b = operator.concat(b, str(reversed[i]))
result = str(int(b) - int(a))
print result
if int(result) == 0:
return
kapreka(result, number)
kapreka("6565")
Questions:
i. Can you improve the code to make it do the same thing in less code?
ii. Can you add a way to recognise repeating series
iii. Can you modify the code to deal with non-base ten numbers
iv. Can you make pretty graphs?
No comments:
Post a Comment