Self numbers: Difference between revisions
Content added Content deleted
Catskill549 (talk | contribs) |
|||
Line 123: | Line 123: | ||
selfNumbers(9.7777777792E+10) |
selfNumbers(9.7777777792E+10) |
||
--> {9.99999999997E+11}</lang> |
--> {9.99999999997E+11}</lang> |
||
=={{header|AWK}}== |
|||
<lang AWK> |
|||
# syntax: GAWK -f SELF_NUMBERS.AWK |
|||
# converted from Go (low memory example) |
|||
BEGIN { |
|||
print("HH:MM:SS INDEX SELF") |
|||
print("-------- ---------- ----------") |
|||
count = 0 |
|||
digits = 1 |
|||
i = 1 |
|||
last_self = 0 |
|||
offset = 9 |
|||
pow = 10 |
|||
while (count < 1E8) { |
|||
is_self = 1 |
|||
start = max(i-offset,0) |
|||
sum = sum_digits(start) |
|||
for (j=start; j<i; j++) { |
|||
if (j + sum == i) { |
|||
is_self = 0 |
|||
break |
|||
} |
|||
sum = ((j+1) % 10 != 0) ? ++sum : sum_digits(j+1) |
|||
} |
|||
if (is_self) { |
|||
last_self = i |
|||
if (++count <= 50) { |
|||
selfs = selfs i " " |
|||
} |
|||
} |
|||
if (++i % pow == 0) { |
|||
pow *= 10 |
|||
digits++ |
|||
offset = digits * 9 |
|||
} |
|||
if (count ~ /^10*$/ && arr[count]++ == 0) { |
|||
printf("%8s %10s %10s\n",strftime("%H:%M:%S"),count,last_self) |
|||
} |
|||
} |
|||
printf("\nfirst 50 self numbers:\n%s\n",selfs) |
|||
exit(0) |
|||
} |
|||
function sum_digits(x, sum,y) { |
|||
while (x) { |
|||
y = x % 10 |
|||
sum += y |
|||
x = int(x/10) |
|||
} |
|||
return(sum) |
|||
} |
|||
function max(x,y) { return((x > y) ? x : y) } |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
HH:MM:SS INDEX SELF |
|||
-------- ---------- ---------- |
|||
00:36:35 1 1 |
|||
00:36:35 10 64 |
|||
00:36:35 100 973 |
|||
00:36:35 1000 10188 |
|||
00:36:36 10000 102225 |
|||
00:36:46 100000 1022675 |
|||
00:38:49 1000000 10227221 |
|||
01:03:01 10000000 102272662 |
|||
05:27:35 100000000 1022727208 |
|||
first 50 self numbers: |
|||
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 |
|||
</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |