Sum of the digits of n is substring of n: Difference between revisions

Content added Content deleted
(Realize in F#)
(added AWK)
Line 190: Line 190:
<pre>0 1 2 3 4 5 6 7 8 9 10 20 30 40 50 60 70 80 90 100 109 119 129 139 149 159 169 179 189 199 200 300 400 500 600 700 800 900
<pre>0 1 2 3 4 5 6 7 8 9 10 20 30 40 50 60 70 80 90 100 109 119 129 139 149 159 169 179 189 199 200 300 400 500 600 700 800 900
910 911 912 913 914 915 916 917 918 919</pre>
910 911 912 913 914 915 916 917 918 919</pre>
=={{header|AWK}}==
<lang AWK>
# syntax: GAWK -f SUM_OF_THE_DIGITS_OF_N_IS_SUBSTRING_OF_N.AWK
BEGIN {
start = 0
stop = 999
for (i=start; i<=stop; i++) {
if (i ~ ""sum_digits(i)) { # TAWK needs the ""
printf("%4d%1s",i,++count%10?"":"\n")
}
}
printf("\nSum of the digits of n is substring of n %d-%d: %d\n",start,stop,count)
exit(0)
}
function sum_digits(n, i,sum) {
for (i=1; i<=length(n); i++) {
sum += substr(n,i,1)
}
return(sum)
}
</lang>
{{out}}
<pre>
0 1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90 100
109 119 129 139 149 159 169 179 189 199
200 300 400 500 600 700 800 900 910 911
912 913 914 915 916 917 918 919
Sum of the digits of n is substring of n 0-999: 48
</pre>


=={{header|BASIC}}==
=={{header|BASIC}}==