34
\$\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
    Commented Jul 31, 2017 at 23:49
  • \$\begingroup\$ @StepHen >:D Charcoal is faster \$\endgroup\$
    – ASCII-only
    Commented Aug 1, 2017 at 0:36
  • 1
    \$\begingroup\$ Relevant \$\endgroup\$ Commented Aug 1, 2017 at 1:12
  • 1
    \$\begingroup\$ OEIS A062918 \$\endgroup\$
    – Leaky Nun
    Commented Aug 1, 2017 at 6:37
  • 7
    \$\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\$ Commented Aug 21, 2017 at 8:23

87 Answers 87

2
\$\begingroup\$

RProgN 2, 8 bytes

{Ø.in}S+

Explained

{Ø.in}S+
{    }S # Create a stack in range 0 through the implicit input, using the function defined
 Ø.     # Append nothing, stringifying the number
   i    # Reverse the string
    n   # Convert back to a number
       +# Get the sum of the stack, and output implicitly.

Try it online!

\$\endgroup\$
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
    Commented 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
    Commented 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
    Commented 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
    Commented 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
    Commented 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
    Commented 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\$ Commented Dec 14, 2017 at 18:54
2
\$\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\$
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
    Commented 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\$
    – qqq
    Commented 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
    Commented Jun 1, 2018 at 17:53
  • \$\begingroup\$ or even this for 75 bytes? \$\endgroup\$ Commented Apr 10, 2021 at 12:44
  • \$\begingroup\$ @DominicvanEssen thanks! \$\endgroup\$
    – Giuseppe
    Commented 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
    Commented 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\$

Factor + math.unicode, 39 bytes

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

An image of running the above code in Factor's REPL

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

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

Burlesque, 7 bytes

ro)<-++

Try it online!

ro  # Range
)<- # Reverse
++  # 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\$

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!

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

K (ngn/k), 15 14 13 bytes

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

Try it online!

-1 byte thanks to coltim

\$\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
    Commented May 15, 2022 at 18:04
1
\$\begingroup\$

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

><>, 68 bytes

1:{:})?^:9)?v:1+!
a:v?(1:[2$0:<.17-%1:,a}+*a{%
0.>~]$d
=?n+a3.>~{~l1

Try it online!

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.