[Edu-sig] Python programming challenge
Kirby Urner
urnerk at qwest.net
Thu Jun 2 03:42:32 CEST 2005
> Of course I'm interested in alternate solutions.
>
> Regards,
> Gregor
>
Mine is not especially different. I'm sure this could be streamlined.
def getmaxroot(n):
for i in range(9,0,-1):
if i*i <= n:
return i
def getpairs(n):
sn = str(n)
if len(sn)%2:
yield sn[0:1]
sn = sn[1:] + '00'
while True:
yield sn[0:2]
sn = sn[2:] + '00'
def root2(n):
g = getpairs(n)
pair = g.next()
root = getmaxroot(int(pair))
newval = int(str(int(pair)-root*root) + g.next())
ans = str(root)
while True:
yield ans
prefix = str(2*int(ans))
for i in range(9,-1,-1):
value = int(prefix + str(i)) * i
if value <= newval:
break
ans += str(i)
newval = int(str(int(newval)-value) + g.next())
Kirby
More information about the Edu-sig
mailing list