Skip to content

Instantly share code, notes, and snippets.

@renugasaraswathy
Created March 12, 2015 16:01
Show Gist options
  • Save renugasaraswathy/557b5c324eeb5f1421e8 to your computer and use it in GitHub Desktop.
Save renugasaraswathy/557b5c324eeb5f1421e8 to your computer and use it in GitHub Desktop.
Display all numbers between 1 to 1000 whose sum of digits is 7
#This program produces the expected result in less number of iteration.
x=*(0...100)
x.each do |y|
str=y.to_s
removed_last_digit=str[0..-1]
s=removed_last_digit.split('').collect(&:to_i).reduce(:+)
if s<=7
diff=7-s
number=str.to_s+diff.to_s
puts number
end
end
=begin
Output as follows
07
16
25
34
43
52
61
70
106
115
124
133
142
151
160
205
214
223
232
241
250
304
313
322
331
340
403
412
421
430
502
511
520
601
610
700
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment