# Printing all numbers from N to M in one single expression [closed]

Given the integers N and M, can you print all numbers from N to M comma-seperated? The hitch is to use one single expression.

The output for N=23 and M=42 should look like this:

23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42


puts (23..42).to_a.join(', ')


## closed as unclear what you're asking by Peter Taylor, Johannes Kuhn, manatwork, grc, VolatilityJun 28 '13 at 9:59

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• – ardnew Jun 27 '13 at 20:50
• @YMMD what do you consider a single expression in golfscript? – John Dvorak Jun 27 '13 at 21:05
• so now that the code golf tag has been removed, is there even an objective or winning criteria anymore? – ardnew Jun 27 '13 at 21:16
• Quite poorly worded task. As you can see the answers, there are too many variations: n..m vs 23..42, “,” vs “, ”, quoted vs unquoted output, maybe with or without trailing separator. I know, it is not code-golf anymore, but I think the answer still should be somehow comparable. – manatwork Jun 28 '13 at 7:15
• @manatwork, that's the least of its problems. The biggest problem is that, as Jan pointed out, "expression" is not well defined. – Peter Taylor Jun 28 '13 at 10:06

# k

17 chars. Expression returns 1 but prints output to stdout

1@", "/:$23_!1+42  To return a string with the comma seperated values, simply use: ", "/:$23_!1+42


## Sage CLI, 14

Edit: Sage has a range shorthand.

[n..m][1:-1]


# Ruby: 18 characters

$><<[*23..42]*", "  Sample run: bash-4.1$ ruby -e '$><<[*23..42]*", "' 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42  ## Forcing the rules: 10 characters As seen in the other answers: • variables instead of numeric literals • comma only instead of comma and space • no output, just generated value [*n..m]*?,  Sample run: irb(main):001:0> n=23 => 23 irb(main):002:0> m=42 => 42 irb(main):003:0> [*n..m]*?, => "23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42"  # Bash, 21 bytes echo {23..42}|tr \ ,  • what about seq -s, 23 42 – marcosm May 30 '17 at 13:13 • @marcosm seq is distribution specific and not available everywhere. – Martin York May 30 '17 at 16:50 # Tcl, 63 bytes puts "$n,[join [lmap a [lrepeat [expr {$m-$n}] 0] {incr n}] ,]"

• My answer has the problem of putting new lines: while {$n<$m} {puts $n,;incr n};puts$m – sergiol Dec 18 '16 at 22:43
• Fixed: while {$n<$m} {lappend l $n,;incr n};lappend l$m;puts \$l – sergiol Dec 18 '16 at 22:49
• OK I forgot there is the requirement of 1 single expression; my answers are not acceptable! – sergiol Dec 18 '16 at 22:50

# Python: 39

print(",".join(map(str,range(23,42))))


### J, 28 characters

echo(,', ',])&":/[23}.i.>:42

• i.43 creates an array of 43 values 0..42. Unfortunately, we have to compute 43.
• 23}. drops first 23 of them, leaving 23..42. If precomputation is allowed, 23+i.20 fares better byte-wise (-3 characters)
• ,', ',] concatenates both arguments with ', ' between them
• ...&": does that after converting both arguments to strings
• .../ does that as a reduction step.
• echo is a built-in to perform standard output. If simply returning is enough, you can drop this (-4 characters)

## R: 17 characters

cat(n:m,sep=", ")


Example:

> n=23
> m=42
> cat(n:m,sep=", ")
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42