28
\$\begingroup\$

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.

\$\endgroup\$
6
  • \$\begingroup\$ More test case results (not numbered, sorry, but you can parse through and get their line number if you want): Try it online! \$\endgroup\$
    – Stephen
    Jul 31, 2017 at 23:49
  • \$\begingroup\$ @StepHen >:D Charcoal is faster \$\endgroup\$
    – ASCII-only
    Aug 1, 2017 at 0:36
  • 1
    \$\begingroup\$ Relevant \$\endgroup\$ Aug 1, 2017 at 1:12
  • 1
    \$\begingroup\$ OEIS A062918 \$\endgroup\$
    – Leaky Nun
    Aug 1, 2017 at 6:37
  • 5
    \$\begingroup\$ -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. \$\endgroup\$ Aug 21, 2017 at 8:23

79 Answers 79

2
\$\begingroup\$

Pyth, 8 6 bytes

-2 bytes thanks to FryAmTheEggman!

sms_`h

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 1 byte longer version, sms_`dS, that does not abuse implicit U at the end. \$\endgroup\$
    – Mr. Xcoder
    Aug 1, 2017 at 6:44
2
\$\begingroup\$

Tcl, 66 bytes

time {incr s [regsub ^0+ [string rev $n] ""];incr n -1} $n
puts $s

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Neim, 4 bytes

Δ𝐫)𝐬

Try it online!

Explanation

Δ )              for each element 1 to n (outputs list)
 𝐫               reverse 
   𝐬             sum 
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Alternative solution: 𝐈Ψ𝐫𝐬 (create inclusive range, reverse each element, sum) \$\endgroup\$
    – Okx
    Aug 1, 2017 at 11:31
  • \$\begingroup\$ @Okx didn't know that the Ψ token existed! would have definitely used that in hindsight. real nice \$\endgroup\$
    – space junk
    Aug 1, 2017 at 12:01
2
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ 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? \$\endgroup\$
    – scottinet
    Sep 5, 2017 at 15:47
2
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Use n surrounded by backticks instead of str(n) for a nice save \$\endgroup\$
    – Dion
    Feb 28, 2021 at 7:34
  • \$\begingroup\$ Backticksyntax is Python 2 only. Plus, the answer is off by 1.. it should be range(n+1) \$\endgroup\$
    – movatica
    Feb 28, 2021 at 22:25
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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.
\$\endgroup\$
2
\$\begingroup\$

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
  }
\$\endgroup\$
1
  • 1
    \$\begingroup\$ you can do (?1..gets).sum{...}) \$\endgroup\$ Dec 14, 2017 at 18:54
2
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ 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 :) \$\endgroup\$ Apr 11, 2021 at 13:41
  • \$\begingroup\$ Thank you for letting me know. I will start adding some explanations. \$\endgroup\$
    – Axuary
    Apr 11, 2021 at 15:53
1
\$\begingroup\$

Pyke, 7 bytes

SF`_b)s

Try it online!

S       -   range(1, input)
 F   )  -  for i in ^:
  `     -     str(i)
   _    -    reversed(^)
    b   -   int(^)
     s  - sum(^)
\$\endgroup\$
1
\$\begingroup\$

MATL, 5 bytes

:VPUs

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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
 
\$\endgroup\$
1
\$\begingroup\$

Pyt, 3 bytes

ř₫Ʃ

Try it online!

The characters perform the following operations: range, reverse, sum.

\$\endgroup\$
2
  • \$\begingroup\$ Dammit. You beat me to it. +1 \$\endgroup\$
    – mudkip201
    Feb 22, 2018 at 3:55
  • \$\begingroup\$ @mudkip201 Yeah I feel like its almost a race to see who can get the most trivial arithmetic challenges answered \$\endgroup\$ Feb 22, 2018 at 14:56
1
\$\begingroup\$

Canvas, 7 bytes

R{ŗ±┤]∑

Try it here!

\$\endgroup\$
1
\$\begingroup\$

Lua (59 bytes)

function r(u)return u<1 and 0 or(u..""):reverse()+r(u-1)end
\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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.

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

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
\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

Arn -x, 4 bytes

P▀g³

Try it!

Explained

Unpacked: ~)@.<

    ~    One-range to
      _  Input; implied
  )
@        Map with
  .<     Reverse fix
      Implicit sum
\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

K (ngn/k), 15 14 bytes

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

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ You could use .' instead of `I$ to save a byte. A couple more could be saved by making this tacit, i.e. +/.'|'$1+!: \$\endgroup\$
    – coltim
    May 15 at 18:04
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Shouldn't you make it [U]pend for completion's sake? \$\endgroup\$
    – Adalynn
    Aug 1, 2017 at 0:00
  • \$\begingroup\$ You right, all better. Thanks! \$\endgroup\$
    – nmjcman101
    Aug 1, 2017 at 0:01
0
\$\begingroup\$

Ly, 31 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pari/GP, 43 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Swift 3, 98 bytes

func f(a:Int)->Int{return (0...a).map{Int(String(String($0).characters.reversed()))!}.reduce(0,+)}
\$\endgroup\$
2

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.