# count of numbers between A and B (inclusive) that have sum of digits equal to S

Find the count of numbers between A and B (inclusive) that have sum of digits equal to S. Also print the smallest such number between A and B (inclusive).

Input:
Single line consisting of A, B, and S.

Output:
Two lines.

In first line, the number of integers between A and B having sum of digits equal to S.

In second line, the smallest such number between A and B.

Constraints:

1 <= A <= B < 10^15

1 <= S <= 135
• shortest-time isn't a good criteria for questions on this site. Jan 4, 2014 at 7:10
• @MtnViewMark - I can't see where shortest time is mentioned in the puzzle. Jan 5, 2014 at 17:54
• @WayneConrad - It was tagged shortest-time. The author has now changed the tag to code-golf. Jan 6, 2014 at 1:03
• Is the intention of the 10^15 limit to rule out solutions that simply test all numbers between A and B? Jan 6, 2014 at 1:04
• Someone (probably a bot) is insisting in trying to edit the answers to add spam, it is not trying to do to random questions, it insists in this one. Someone knows the reason for this? Have this appeared high for some particular google search terms? Feb 23, 2014 at 3:12

# Python, 105

a,b,s=input()
l=filter(lambda n:sum(int(s)for s in list(str(n)))==s,range(a,b+1))
print l[0]
print len(l)

# GolfScript, 35 bytes

~:S;),>{.10base{+}*S=!{;}*}%(\,)]n*

### How it works

~         # Interpret the input string.
:S;       # Save the last integer in variable “S” and pop it from the stack.
),        # Increment it by 1 and create the array “[0 ... B]”
>         # Remove the first “A” elements. This leaves the array “[A ... B]”
{         # For each of those integers:
.10base # Push its digits in base 10.
S=!{;}* # If the sum is not equal to “S”, pop the integer from the stack.
}%        # Collect the results into an array.
(\        # Extract the first and swap it below the array.
,)        # Count the elements of the array of all remaining integers and increment by one.
n\        # Separate the results by a newline.

Ruby, 109

Golfed:

a,b,s=gets.split(',').map(&:to_i);m=(a..b).select{|n|n.to_s.chars.map(&:to_i).reduce(:+)==s};puts m.size,m[0]

Ungolfed:

min, max, target_sum = gets.split(',').map(&:to_i)
matches = (min..max).select do |n|
sum = n.to_s.chars.map(&:to_i).reduce(:+)
sum == target_sum
end
puts matches.size
puts matches.first
• I'd never recommend doing this with real code, but here's your solution golfed down to 87 characters: a,b,s=gets.split',';m=[*a..b].select{|n|eval(n.chars.join'+')==s.to_i};puts m.size,m[0]. Hideous. 😀
– O-I
Jan 6, 2014 at 0:29
• @O-I - Very nice! And hideous. Jan 7, 2014 at 18:46

## GTB, 32

`A,B,Sn?A,B)→L1:SortA(L1~L1=S~L1
• 28? i see 36, 38 if you count as \xe2\x86\x92, but no way 28 Jan 4, 2014 at 23:59
• @mniip Sorry, fixed. Jan 5, 2014 at 17:45
• The tag wiki for code-golf states: Unless the question is specified to be scored by characters, it is scored by bytes. Therefore, this should count as 34 bytes. If you don't know how to check that, an easy way is python3 -c 'print(len(input().encode("utf-8")))' May 4, 2014 at 15:34
• Or just wc -c (for character count, wc -m), but you'll have to subtract one from that because of the trailing newline. May 4, 2014 at 16:46