29
\$\begingroup\$

Given a number > 0, output the sum with all digits (1 .. n) concatenated and reversed and add them up. For example, with n = 6:

The numbers 1 to 6 concatenated:

123456

Reversed:

654321

Adding them up together will result in: 777777. Another example is n = 11:

1 2 3 4 5 6 7 8 9 10 11 > 1234567891011

and

11 10 9 8 7 6 5 4 3 2 1 > 1110987654321

Adding them up together will result in 2345555545332. This is also known as A078262.

Shortest code wins!

\$\endgroup\$
5
  • \$\begingroup\$ Related. \$\endgroup\$
    – Zgarb
    Commented Feb 22, 2016 at 21:21
  • \$\begingroup\$ Is there a bound to n, or do we have to support arbitrarily large integers? \$\endgroup\$ Commented Feb 22, 2016 at 22:16
  • \$\begingroup\$ I think the default is "bounded by max(256,yourlanguagesdefaultintegertypelimit)". But it should be specified. \$\endgroup\$ Commented Feb 22, 2016 at 22:20
  • \$\begingroup\$ @LegionMammal978 As high as your language supports. \$\endgroup\$
    – Lamaro
    Commented Feb 22, 2016 at 22:43
  • \$\begingroup\$ Important test case: 10, which should give 23333333231. \$\endgroup\$
    – Adnan
    Commented Feb 22, 2016 at 23:04

40 Answers 40

9
\$\begingroup\$

05AB1E, 7 bytes

LDRJsJ+

Try it online.

Explanation

LDRJsJ+

L        range from 1 .. input
 D       duplicate
  R      reverse
   JsJ   convert both arrays to strings
      +  add (coerces both strings to ints)
\$\endgroup\$
3
  • \$\begingroup\$ I feel very confused by the design choice that lead to + on lists doing a nested addition, while for strings it converts to ints and then adds. But I guess it worked out here! :P \$\endgroup\$ Commented Feb 22, 2016 at 21:35
  • \$\begingroup\$ @FryAmTheEggman I'm going to remove nested addition though. It has never been useful since the moment I've implemented it... \$\endgroup\$
    – Adnan
    Commented Feb 22, 2016 at 21:41
  • 3
    \$\begingroup\$ Sheesh, I leave PPCG for two hours and you rename yourself Aqua Tart while I'm gone... Oh, the life of a PPCG user. \$\endgroup\$ Commented Feb 22, 2016 at 23:55
7
\$\begingroup\$

Jelly, 9 bytes

R,U$DF€ḌS

livecoding 

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Is it me or do I see that code secretly stealing some U$D? \$\endgroup\$
    – gcampbell
    Commented May 21, 2016 at 8:20
  • \$\begingroup\$ -3 bytes \$\endgroup\$ Commented Feb 15, 2021 at 1:05
5
\$\begingroup\$

CJam, 15 14 bytes

Thanks to Martin for shaving a byte!

ri,:)_W%si\si+

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ 1 byte less if you flip the string instead of the numeric array: ri,:)s_W%i\i+ \$\endgroup\$
    – Luis Mendo
    Commented Feb 23, 2016 at 1:23
  • \$\begingroup\$ Sorry, I think my version doesn't work for 10 \$\endgroup\$
    – Luis Mendo
    Commented Feb 23, 2016 at 10:55
  • 1
    \$\begingroup\$ This code is secretly happy. :) \$\endgroup\$
    – Cyoce
    Commented Feb 24, 2016 at 0:08
4
\$\begingroup\$

Pyth, 12 10 bytes

ssMjLk_BSQ

Thanks to @FryAmTheEggman for 2 bytes!

Q is the input, S turns it into [1, 2, ..., input()], _B bifurcates it over _ (reverse) to create [rng, rev(rng)], jLk maps it over join by k (which is the "empty string" variable), sM maps int over this resulting array, and s finally calculates the sum.

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

Python 3, 74

Saved 6 bytes thanks to DSM.

Nothing too exciting, join the ranges and then convert to ints and add them.

lambda x:sum(int(''.join(list(map(str,range(1,x+1)))[::i]))for i in(1,-1))
\$\endgroup\$
1
  • \$\begingroup\$ 71 bytes \$\endgroup\$
    – movatica
    Commented May 12, 2021 at 13:17
4
\$\begingroup\$

JavaScript (ES6), 70 67 64 bytes

a=>(z=[...Array(a)].map((b,c)=>c+1)).join``- -z.reverse().join``

Fixed to meet requirement, as old code was made under misunderstanding of the input.

\$\endgroup\$
5
  • \$\begingroup\$ @TimmyD Added an explanation. \$\endgroup\$
    – Mwr247
    Commented Feb 22, 2016 at 21:52
  • \$\begingroup\$ @TimmyD OH! >_< My misunderstanding of the challenge is how... Yeah, I'll have to fix this. \$\endgroup\$
    – Mwr247
    Commented Feb 22, 2016 at 21:55
  • \$\begingroup\$ @TimmyD Took me long enough to get back online. It's fixed now, and thanks for catching that. \$\endgroup\$
    – Mwr247
    Commented Feb 23, 2016 at 16:53
  • \$\begingroup\$ As noted for another answer, this only works if the parameter a is between 1 and 12, that's really too little \$\endgroup\$
    – edc65
    Commented Feb 23, 2016 at 20:26
  • \$\begingroup\$ @edc65 Per OP's comment, that's big enough. \$\endgroup\$
    – Mwr247
    Commented Feb 23, 2016 at 20:29
3
\$\begingroup\$

Retina, 71

Because its blatantly the wrong tool for the job.

.+
$*a:$&$*
+`^(a+)a\b(.*)\b1(1+)$
$1 $& $3
 ?(\w)+ ?
$#1
\d+:?
$&$*c
c

Try it online.

Works for inputs up to 6, but the online interpreter times out after that.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ You can shorten it to 74 by removing the last line and changing (c)+ to c. \$\endgroup\$
    – daavko
    Commented Feb 22, 2016 at 22:52
  • \$\begingroup\$ @daavko yes, of course, thanks! \$\endgroup\$ Commented Feb 22, 2016 at 22:59
  • \$\begingroup\$ Also, $&$*c -> $*c and \d+:? -> \d+ and it's 70. And for some reason it keeps working... \$\endgroup\$
    – daavko
    Commented Feb 22, 2016 at 23:03
3
\$\begingroup\$

Jolf, 9 bytes

Try it here! Replace with \x10.

+P►γzjP_γ
    zj    range 1...j
   γ      γ = ^
  ►        ^ .join("")
 P         as a number
+     P_γ  and γ reversed

I may be able to golf it by moving around the type casting.

\$\endgroup\$
2
  • \$\begingroup\$ You beat pyth and doorknob! \$\endgroup\$
    – Cyoce
    Commented Feb 26, 2016 at 0:05
  • \$\begingroup\$ @Cyoce so I did O_O \$\endgroup\$ Commented Feb 26, 2016 at 0:05
3
\$\begingroup\$

JavaScript (ES6), 67 66 bytes

n=>(a=[...Array(n+1).keys()].slice(1)).join``- -a.reverse().join``

Yes, that's a space. Ugh. At least @Downgoat helped me save a byte.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ You can remove the first + and make the + + -> - - to save a byte \$\endgroup\$
    – Downgoat
    Commented Feb 23, 2016 at 1:07
  • \$\begingroup\$ n=>(a=[...Array(n)].map(_=>n--)).join- -a.reverse().join \$\endgroup\$
    – edc65
    Commented Feb 23, 2016 at 9:07
  • \$\begingroup\$ Note: using simple js arithmetic this is limited to values 1 .. 12 \$\endgroup\$
    – edc65
    Commented Feb 23, 2016 at 9:08
3
+100
\$\begingroup\$

Vyxal s, 5 bytes

ɾḂWvṅ

Try it Online!

ɾ         range: [1 .. x]
 Ḃ        pop x, push x, reversed(x) [bifurcate]
  W       wrap the stack
   v      for each
    ṅ     join on ""

s (flag)  sum

-2 bytes thanks to Razetime

\$\endgroup\$
4
  • \$\begingroup\$ -1: Try it Online! \$\endgroup\$
    – Razetime
    Commented May 11, 2021 at 6:53
  • \$\begingroup\$ @Razetime oh so there is a shorter way to do that. thanks \$\endgroup\$
    – hyperneutrino
    Commented May 11, 2021 at 6:55
  • \$\begingroup\$ s flag should reduce another byte. \$\endgroup\$
    – Razetime
    Commented May 11, 2021 at 6:55
  • \$\begingroup\$ @Razetime guess it's too late to turn back from the flags now, isn't it \$\endgroup\$
    – hyperneutrino
    Commented May 11, 2021 at 6:56
2
\$\begingroup\$

Seriously, 12 bytes

,R;Rεj≈@εj≈+

Try it online!

Explanation:

,R;Rεj≈@εj≈+
,R;           push two copies of range(1, input()+1)
   R          reverse one copy
    εj≈@εj≈   concatenate both and cast both to ints
           +  add
         
\$\endgroup\$
2
\$\begingroup\$

PowerShell, 35 bytes

param($a)+-join(1..$a)+-join($a..1)

Converts the input to ranges with .., then -joins them together, and adds 'em up.

Will work for input numbers up to 138, while 139 will give Infinity, and 140 and above will barf out an awesomely verbose casting error:

Cannot convert value "12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413
5136137138139140" to type "System.Int32". Error: "Value was either too large or too small for an Int32."
\$\endgroup\$
2
\$\begingroup\$

Pyth - 8 bytes

siRT_BSQ

Try it online here.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ I think this doesn't work for 10 or 11 \$\endgroup\$
    – Luis Mendo
    Commented Feb 23, 2016 at 9:59
2
\$\begingroup\$

JavaScript (ES6), 99

This adds digit by digit, so it can handle numbers well above the 53 bits of precision of javascript

n=>eval("for(a=b=c=r='';n;a+=n--)b=n+b;for(i=a.length;i--;r=c%10+r)c=(c>9)-(-a[i]-b[i]);c>9?1+r:r")

Test

f=n=>eval("for(a=b=c=r='';n;a+=n--)b=n+b;for(i=a.length;i--;r=c%10+r)c=(c>9)-(-a[i]-b[i]);c>9?1+r:r")

// Less golfed
U=n=>{
  for(a=b=c=r=''; n; --n)
      b=n+b, a+=n;
  for(i=a.length; i--; r = c%10+r) 
      c=(c>9)-(-a[i]-b[i]);
  return c>9? 1+r : r;
}

function test() {
  var n=+I.value
  R.textContent=f(n)
}  

test()
N: <input id=I value=11 oninput="test()"> -> <span id=R></span>

\$\endgroup\$
2
  • \$\begingroup\$ Doesn't seem to work for 9. Also, why not initialise c with the other variables? \$\endgroup\$
    – Neil
    Commented Feb 22, 2016 at 22:54
  • \$\begingroup\$ You have my upvote. \$\endgroup\$
    – Neil
    Commented Feb 23, 2016 at 9:24
2
\$\begingroup\$

Brachylog, 24 bytes

:1fLrcC,Lc+C=.,{,.:1re?}
\$\endgroup\$
2
\$\begingroup\$

MATL, 13 bytes

:tP2:"wVXvU]+

EDIT (May 20, 2016) The code in the link uses Xz instead of Xv, owing to recent changes in the language.

Try it online!

:                % range [1,2,...,n], where n is input
 tP              % duplicate and flip
   2:"     ]     % do this twice
      w          % swap
       V         % convert array of numbers to string with numbers and spaces
        Xv       % remove spaces
          U      % convert to number
            +    % add the two numbers
\$\endgroup\$
3
  • \$\begingroup\$ Doesn't work for 11 or 10. (Hint: reverse range before converting to string.) \$\endgroup\$ Commented Feb 23, 2016 at 3:32
  • \$\begingroup\$ @ӍѲꝆΛҐӍΛПҒЦꝆ Thanks! Corrected \$\endgroup\$
    – Luis Mendo
    Commented Feb 23, 2016 at 9:57
  • \$\begingroup\$ Great! Have an upvote. \$\endgroup\$ Commented Feb 24, 2016 at 2:00
2
\$\begingroup\$

05AB1E, 5 bytes

LJDR+

Explanation:

L     # Pushes an array containing 1 .. [implicit] input
 J    # Join the array to a string (eg. [1, 2, 3] -> 123)
  D   # Duplicate the array
   R  # Reverse the duplicate
    + # Add them together

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Late comment, but doesn't work for 10 - you are reversing the string after you join, instead of before. \$\endgroup\$
    – Underslash
    Commented May 12, 2021 at 18:30
1
\$\begingroup\$

Bash + coreutils, 39

eval echo {1..$1} + {$1..1}|tr -d \ |bc

Or:

bc<<<`eval printf %s {1..$1} + {$1..1}`

Ideone.

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

Perl 6, 25 bytes

{([~] @_=1..$^n)+[R~] @_}
{
  (
    [~]           # reduce with the string concatenation infix op:
    @_ = 1 .. $^n # the range 1 to input ( also stored in @_ )
  )
  +               # add that to
  [R~] @_         # @_ reduced in reverse
}

Usage:

for 6, 11, 12 -> $n {
  say {([~] @_=1..$^n)+[R~] @_}( $n )
}
777777
2345555545332
244567776755433
\$\endgroup\$
2
  • \$\begingroup\$ I think you can do with $n instead of $^n \$\endgroup\$ Commented Feb 22, 2016 at 22:56
  • \$\begingroup\$ @dev-null Not if I want it to be an input to the block. the -> $n { is a different one to $^n. \$\endgroup\$ Commented Feb 22, 2016 at 22:58
1
\$\begingroup\$

R, 34 60 64 bytes

f=pryr::f;g=f(as.numeric(paste(x,collapse='')));f(g(1:n)+g(n:1))

Assumes pryr package is installed. this gives f as a shorthand for creating functions.

Edit added 26 bytes but returns a function that works, not something entirely wrong.

Edit added another 4 bytes to handle cases above n=10 where strtoi (previously used) was returning NA

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

Lua, 57

a=''b=''for i=1,...do a=a..i b=b.. ...-i+1 end return a+b
\$\endgroup\$
1
\$\begingroup\$

Lua, 53 Bytes

This program takes n as a command-line argument.

s=""r=s for i=1,arg[1]do r,s=i..r,s..i end print(s+r)

I assumed that outputing a number with a decimal part of 0 was okay (in the form 777777.0 because this is the default way to output a number in lua (there's no distinction between integer and float)

\$\endgroup\$
4
  • \$\begingroup\$ Its not the string itself that is reversed, but the digits. Your code fails on n >= 10. \$\endgroup\$
    – Moop
    Commented Feb 23, 2016 at 7:47
  • \$\begingroup\$ @Moop Corrected at the price of 1 byte ^^'. Thanks for the comment ^^' \$\endgroup\$
    – Katenkyo
    Commented Feb 23, 2016 at 8:00
  • \$\begingroup\$ You can save 3 more using ... instead of arg[1] nice work on the reverse concat for r, didn't think of that in my answer. +1 \$\endgroup\$
    – Moop
    Commented Feb 23, 2016 at 8:03
  • \$\begingroup\$ @Moop I saw your post, nice use of it, I didn't even know you could use ... like that! I'll keep it this way for the moment, because I can't use anything else than the online compiler and it can't handle that(I'd like to test it and play with it a little bit before putting it in a answer :)) \$\endgroup\$
    – Katenkyo
    Commented Feb 23, 2016 at 8:07
1
\$\begingroup\$

Perl 5, 37 bytes

25 bytes, plus 1 for -p and 11 for -MList::Gen

$_=<[.]1..$_>+<[R.]1..$_>

Previous solution, 40 bytes: 39, plus one for -p

@a=reverse@_=1..$_;$"=$\;$_="@a"+"@_"
\$\endgroup\$
1
\$\begingroup\$

Perl, 36 bytes

Includes +1 for -p

Run with on STDIN

perl -p reverse.pl <<< 6

reverse.pl

$_=eval join"",map{abs||"+"}-$_..$_
\$\endgroup\$
1
\$\begingroup\$

Haskell, 57 56 48 bytes

This could probably be golfed a bit more, but here it goes:

f x=read y+(read.reverse$y)where y=[1..x]>>=show

Edit: shaved off a space before the where.

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

Dyalog APL, 17 bytes

+/⍎¨∊¨⍕¨¨x(⌽x←⍳⎕)

prompt for input
' enumerate until input
x← store list in x
reverse x
x() prepend reversed list with original list
⍕¨¨ convert each number of each list into character string
∊¨ make each list of character strings into single character strings
⍎¨ convert each character string into a number
+/ sum the two numbers.

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

Factor, 59 bytes

[ [1,b] dup reverse [ [ present ] map concat dec> ] bi@ + ]

Try it online!

  • [1,b] Create a range from 1 to the input, inclusive.
  • dup reverse Make a copy and reverse it.
  • [ [ present ] map concat dec> ] bi@ Convert them both to numbers of concatenated numbers.
  • + Add.
\$\endgroup\$
1
\$\begingroup\$

Python 3, 79 bytes

a=''.join([str(i+1) for i in range(int(input()))])
print(int(a)+(int(a[::-1])))

Try it online!

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

Thunno 2 s, 4 bytes

RJNḲ

Try it online!

Explanation

RJNḲ  # Implicit input
R     # Push [1..input]
 J    # Join into a string
  N   # Convert to integer
   Ḳ  # Duplicate and reverse
      # Sum the stack
      # Implicit output
\$\endgroup\$
0
\$\begingroup\$

Mathematica, 64 bytes

Plus@@FromDigits/@#&[""<>ToString/@#&/@{#,Reverse@#}&[Range@#]]&
\$\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.