Self numbers: Difference between revisions

Add python 2.7
(added Raku programming solution)
(Add python 2.7)
Line 1,066:
completed in 0.1s
</pre>
 
=={{header|Python}}==
{{works with|Python|2.7}}
<lang python>class DigitSumer :
def __init__(self):
sumdigit = lambda n : sum( map( int,str( n )))
self.t = [sumdigit( i ) for i in xrange( 10000 )]
def __call__ ( self,n ):
r = 0
while n >= 10000 :
n,q = divmod( n,10000 )
r += self.t[q]
return r + self.t[n]
 
 
def self_numbers ():
d = DigitSumer()
s = set([])
i = 1
while 1 :
n = i + d( i )
if i in s :
s.discard( i )
else:
yield i
s.add( n )
i += 1
 
import time
p = 100
t = time.time()
for i,s in enumerate( self_numbers(),1 ):
if i <= 50 :
print s,
if i == 50 : print
if i == p :
print '%7.1f sec %9dth = %d'%( time.time()-t,i,s )
p *= 10</lang>
{{out}}
<pre>1 3 5 7 9 20 31 42 53 64 75 86 97 108 110 121 132 143 154 165 176 187 198 209 211 222 233 244 255 266 277 288 299 310 312 323 334 345 356 367 378 389 400 411 413 424 435 446 457 468
0.0 sec 100th = 973
0.0 sec 1000th = 10188
0.1 sec 10000th = 102225
1.0 sec 100000th = 1022675
11.4 sec 1000000th = 10227221
143.4 sec 10000000th = 102272662</pre>
 
=={{header|Raku}}==