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


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

K (ngn/k), 15 14 bytes

{+/I$|'$1+!x}


Try it online!

• 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 at 18:04

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.

Jelly, 5 bytes

RDUḌS


Try it online!

R      ' [R]ange
D     ' Integer -> [D]ecimal
U    ' [U]pend (vectorized reverse?)
Ḍ   ' [Ḍ]ecimal -> Integer
S  ' [S]um

• Shouldn't you make it [U]pend for completion's sake? Aug 1, 2017 at 0:00
• You right, all better. Thanks! Aug 1, 2017 at 0:01

Ly, 31 bytes

ns[l1-s]pr[s>lSrJs>l<p<p]>>r&+u


Try it online!

Pari/GP, 43 bytes

n->sum(i=1,n,fromdigits(Vecrev(digits(i))))


Try it online!

QBIC, 19 bytes

[:|_F!a$|p=p+!A!}?p  Explanation [:| FOR a = 1 to n (read from cmd line) _F | FLIP and assign to A$
!a$a string cast of our loop counter p=p+!A! increment p with A$ cast back to number
}           NEXT
?p         Print the result


LOGO, 33 bytes

[reduce "+ map "reverse iseq 1 ?]


There is no "Try it online!" link because all online LOGO interpreter does not support template-list.

That is a template-list (equivalent of lambda function in other languages).

Usage:

pr invoke [reduce[?+reverse ?2]iseq 1 ?] 10


(invoke calls the function, pr prints the result)

prints 46.

Explanation:

LOGO stores numbers as words, therefore apply reverse on a number reverse the digits in that number.

reduce "f [a b c d e] (where [a b c d e] is a list) will calculate f(a, f(b, f(c, f(d, e)))). So reduce "+ list will calculate sum of values of list.

Swift 3, 98 bytes

func f(a:Int)->Int{return (0...a).map{Int(String(String($0).characters.reversed()))!}.reduce(0,+)}  • Aug 12, 2017 at 17:33 • Also String($0) can be "\(\$0)"` to save a couple of bytes too. Aug 12, 2017 at 17:37