# Range, Reverse, Sum!

Given a positive integer n as input, output the reversed range sum of n.

A reversed range sum is created by making an inclusive range up to n, starting with 1 and including n, reversing each of the numbers inside, and summing it.

Example:

Here is what would happen for an input of 10:

Range: [1,2,3,4,5,6,7,8,9,10]

Reverse: [1,2,3,4,5,6,7,8,9,01] (1-char numbers reversed are themselves, 10 reversed is 01 or 1)

Sum: 46

Numbers with 3+ digits are reversed the same way numbers with 2 digits are. For example, 1234 would become 4321.

Test cases:

Input -> Output

10 -> 46
5 -> 15
21 -> 519
58 -> 2350
75 -> 3147
999 -> 454545


Complete text cases to input of 999 can be found here, thanks very much to @fireflame241.

• More test case results (not numbered, sorry, but you can parse through and get their line number if you want): Try it online! Jul 31, 2017 at 23:49
• @StepHen >:D Charcoal is faster Aug 1, 2017 at 0:36
• Relevant Aug 1, 2017 at 1:12
• OEIS A062918 Aug 1, 2017 at 6:37
• -1 because this is uninteresting. It seems like most, if not all, of the submissions are using the same approach. This challenge seems like a bunch of problems that have already been asked, just piped together with no obvious shortcuts. Aug 21, 2017 at 8:23

# Pyth, 8 6 bytes

-2 bytes thanks to FryAmTheEggman!

sms_h


Try it online!

• 1 byte longer version, sms_dS, that does not abuse implicit U at the end. Aug 1, 2017 at 6:44

# Tcl, 66 bytes

time {incr s [regsub ^0+ [string rev $n] ""];incr n -1}$n
puts $s  Try it online! # Neim, 4 bytes Δ𝐫)𝐬  Try it online! # Explanation Δ ) for each element 1 to n (outputs list) 𝐫 reverse 𝐬 sum  • Alternative solution: 𝐈Ψ𝐫𝐬 (create inclusive range, reverse each element, sum) – Okx Aug 1, 2017 at 11:31 • @Okx didn't know that the Ψ token existed! would have definitely used that in hindsight. real nice Aug 1, 2017 at 12:01 # C (gcc), 71 bytes q(n,x,p){p=n?q(n/10,x*10+n%10):x;}f(w,a,e){for(a=0;w;)a+=q(w--,0);e=a;}  Try it online! • Wait... what? How f() returns its result without any return statement? Does the e=a instruction manipulates the registers in such a way that the result is stored in the same register than the one used by returned values? Sep 5, 2017 at 15:47 # Python 3, 50 bytes lambda n:sum(int(str(n)[::-1])for n in range(n))  Quite close to idiomatic Python code, and honestly I don't really see much room for improvement. • Use n surrounded by backticks instead of str(n) for a nice save – Dion Feb 28, 2021 at 7:34 • Backticksyntax is Python 2 only. Plus, the answer is off by 1.. it should be range(n+1) Feb 28, 2021 at 22:25 # Excel VBA, 80 78 49 Bytes Anonymous VBE Immediate Window function that takes input expected type Integer from range [A1] determines all of the values that fall within the range 1:[A1], and outputs the sum of the reversed values to the VBE immediate window For i=1To[A1]:s=s+Val(StrReverse(Str(i))):Next:?s  ## TXR Lisp: 62 56 bytes: (opip(range 1)(mapcar[chain tostring reverse toint])sum)  Interactive: 1> (opip(range 1)(mapcar[chain tostring reverse toint])sum) #<intrinsic fun: 0 param + variadic> 2> [*1 10] 46 3> [*1 999] 454545  The following 44 byte expression, inspired by the Pari/GP solution, is possible; however, it requires the sum macro to be defined: 1> (defmacro sum (var from to expr) (with-gensyms (accum) ^(for ((,var ,from) (,accum 0)) ((<= ,var ,to) ,accum) ((inc ,accum ,expr) (inc ,var))))) sum 2> (do sum x 1 @1 (toint(reverse(tostring x)))) #<interpreted fun: lambda (#:arg-01-0171 . #:rest-0170)> 3> [*2 10] 46 4> [*2 999] 454545  # Gaia, 7 bytes @…)¦v¦Σ  Try it online! Range, Reverse, and sum! @ # push an input. stack: n … # generate range. stack: [0...n-1] )¦ # map over the list with increment. stack: [1...n] v¦ # map over the list with reverse. stack: [1...n], but all digitally reversed Σ # sum the list; output TOS.  # Ruby, 38 35 bytes Similar to the previous Ruby solution, but a full program and also shorter (for less than an hour)! p (?1..gets).sum{|x|x.reverse.to_i}  Shortened by 3 bytes thanks to user akostadinov Try it online! ## Explanation p # Inspect and print. Written as infix notation to avoid using () (?1..gets) # All strings from "1" to "input", based on successive string format .sum{|x| # Enumerable -> Map to value -> Sum by value x.reverse.to_i }  • you can do (?1..gets).sum{...}) Dec 14, 2017 at 18:54 # Excel, 59 bytes =LET(y,COLUMN(A:O),SUM((0&MID(SEQUENCE(A1),y,1))*10^(y-1)))  Explanation =LET( y,COLUMN(A:O) 'y = [1..15] horz.; Excel is accurate to 15 places SUM((0&MID(SEQUENCE(A1),y,1))*10^(y-1)) 'final result SEQUENCE(A1) '[1..A1] vertical (0&MID( ,y,1)) 'array of individual digits; 0& is to avoid errors on blank cells *10^(y-1) 'multiply 2nd column by 10, 3rd 100, etc. SUM( ) 'sum the array  • Just an FYI, your answers are popping up in the Low Quality Posts review queue as their length catches the system filter. I'd suggest adding in a brief explanation in order to avoid this :) Apr 11, 2021 at 13:41 • Thank you for letting me know. I will start adding some explanations. Apr 11, 2021 at 15:53 # Python 3, 52 bytes lambda n:sum(int(str(n+1)[::-1])for n in range(n)) lambda n:sum(int(f'{n+1}'[::-1])for n in range(n))  The second is equivalent to the first. # Pyke, 7 bytes SF_b)s  Try it online! S - range(1, input) F ) - for i in ^:  - str(i) _ - reversed(^) b - int(^) s - sum(^)  # MATL, 5 bytes :VPUs  Try it online! # Pyth - 7 bytes sms_dS  Explanation sms_dSQ Q added implicitly s Sum of m map s_d integer representation of reversal of string representation to SQ Range from 1 to input  # Pyt, 3 bytes ř₫Ʃ  Try it online! The characters perform the following operations: range, reverse, sum. • Dammit. You beat me to it. +1 Feb 22, 2018 at 3:55 • @mudkip201 Yeah I feel like its almost a race to see who can get the most trivial arithmetic challenges answered Feb 22, 2018 at 14:56 # Canvas, 7 bytes Ｒ｛ŗ±┤］∑  Try it here! # Lua (59 bytes) function r(u)return u<1 and 0 or(u..""):reverse()+r(u-1)end  # PowerShell, 43 bytes 1.."$args"-join'+'|% t*y|%{$s=$_+$s}$s|iex


Try it online!

This script converts a range [1,2,3,4,5,6,7,8,9,10] to the reversed string 01+9+8+7+6+5+4+3+2+1 and evaluates the string

# R, 89 75 bytes

sum(scan(t=sapply(1:scan(),function(x)intToUtf8(rev(utf8ToInt(c(x,'')))))))


Try it online!

Thanks to JayCe and Dominic van Essen for the golfs.

• I know it's an old one but what about this for 82 bytes? Jun 1, 2018 at 17:53
• or even this for 75 bytes? Apr 10, 2021 at 12:44
• @DominicvanEssen thanks! Apr 10, 2021 at 18:43
• @JayCe Doesn't seem like you're active any longer, but thank you for the golf, and apologies I didn't respond sooner! Apr 10, 2021 at 18:44

# Python 3, 127 bytes

def f(n):
s=0
for i in range(1,n+1):
r=""
k=str(i)
for j in range(0,len(k)):
r=r+k[len(k)-1-j]
s+=int(r)
return s


# Java (OpenJDK 8), 81 bytes

long g(int n){return n<1?0:new Long(""+new StringBuffer(""+n).reverse())+g(n-1);}


Try it online!

# Arn-x, 4 bytes

P▀g³


Try it!

# Explained

Unpacked: ~)@.<

    ~    One-range to
_  Input; implied
)
@        Map with
.<     Reverse fix
Implicit sum


# Factor + math.unicode, 39 bytes

[ [1..b] [ >dec reverse dec> ] map Σ ]


Using build 2166 for the somewhat recent addition of >dec.

# Burlesque, 7 bytes

ro)<-++


Try it online!

ro  # Range
)<- # Reverse
++  # Sum


# Desmos, 72 70 bytes

l=floor(logx)
f(y)=∑_{x=1}^y∑_{n=0}^lmod(floor(x/10^n),10)10^{l-n}


Try it on Desmos!

-2 bytes thanks to Aiden Chow

# Julia 1.0, 38 bytes

!x=sum(Meta.parse∘join∘digits,1:x)


Try it online!

this works because digits returns an array with the digits starting with the least significants first (digits(123) == [3, 2, 1])

#### alternative, also 38 bytes (but more chars and not as nice)

!x=sum(@.parse(Int,join(digits(1:x))))


Try it online!

# K (ngn/k), 1514 13 bytes

{+/.'|'$1+!x}  Try it online! -1 byte thanks to coltim • You could use .' instead of I$ to save a byte. A couple more could be saved by making this tacit, i.e. +/.'|'$1+!: May 15, 2022 at 18:04 # J-uby, 16 bytes :+|:sum+(S|:~|Z)  Attempt This Online! ## Explanation Same idea as the Ruby answers but J-uby helps us out with some handy shortcuts. :+ | :sum + (S | :~ | Z) :+ | # Make range 1..n, then :sum + ( ) # Sum of items... S | # Converted to string, then :~ | # Reversed, then Z # Converted to int  # ><>, 68 bytes 1:{:})?^:9)?v:1+! a:v?(1:[2$0:<.17-%1:,a}+*a{%
0.>~]\$d
=?n+a3.>~{~l1


Try it online!

# Japt-x, 4 bytes

õÈìÔ


Try it

õÈìÔ     :Implicit input of integer
õ        :Range [1,input]
È       :Map
ì      :  Convert to digit array
Ô     :  Reverse and implicitly convert back
:Implicit output of sum of resulting array