Sum of the digits of n is substring of n
fn digit_sum(n: int) -> int {
let sum: int = 0;
while n > 0 {
sum += n % 10;
n /= 10;
}
return sum;
}
fn main() {
println "Numbers under 1,000 whose sum of digits is a substring of themselves:"
let count = 0;
for n in 0..<1000 {
let ns = "";
let ds = "";
if strstr(ns, ds) {
print " ";
if ++count % 8 == 0 { println ""; }
}
}
println "\n such numbers found."
}
Output:
Numbers under 1,000 whose sum of digits is a substring of themselves:
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
48 such numbers found.
Attribution: This is a community solution for the Rosetta Code task Sum of the digits of n is substring of n in Zen C.
This article uses material from the Rosetta Code article Sum of the digits of n is substring of n, which is released under the GNU Free Documentation License 1.3. A list of the original authors can be found in the page history.