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).

Single line consisting of A, B, and S.

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.


1 <= A <= B < 10^15

1 <= S <= 135
  • 3
    \$\begingroup\$ shortest-time isn't a good criteria for questions on this site. \$\endgroup\$ – MtnViewMark Jan 4 '14 at 7:10
  • 1
    \$\begingroup\$ @MtnViewMark - I can't see where shortest time is mentioned in the puzzle. \$\endgroup\$ – Wayne Conrad Jan 5 '14 at 17:54
  • \$\begingroup\$ @WayneConrad - It was tagged shortest-time. The author has now changed the tag to code-golf. \$\endgroup\$ – MtnViewMark Jan 6 '14 at 1:03
  • \$\begingroup\$ Is the intention of the 10^15 limit to rule out solutions that simply test all numbers between A and B? \$\endgroup\$ – MtnViewMark Jan 6 '14 at 1:04
  • \$\begingroup\$ 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? \$\endgroup\$ – Victor Stafusa Feb 23 '14 at 3:12

Python, 105

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


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.
  {+}*    # Add the digits.
  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


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]


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
puts matches.size
puts matches.first
  • \$\begingroup\$ 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. 😀 \$\endgroup\$ – O-I Jan 6 '14 at 0:29
  • \$\begingroup\$ @O-I - Very nice! And hideous. \$\endgroup\$ – Wayne Conrad Jan 7 '14 at 18:46

GTB, 32

  • \$\begingroup\$ 28? i see 36, 38 if you count as \xe2\x86\x92, but no way 28 \$\endgroup\$ – mniip Jan 4 '14 at 23:59
  • \$\begingroup\$ @mniip Sorry, fixed. \$\endgroup\$ – Timtech Jan 5 '14 at 17:45
  • \$\begingroup\$ 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")))' \$\endgroup\$ – nyuszika7h May 4 '14 at 15:34
  • \$\begingroup\$ Or just wc -c (for character count, wc -m), but you'll have to subtract one from that because of the trailing newline. \$\endgroup\$ – nyuszika7h May 4 '14 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.