Given an input n, write a program or function that outputs/returns the sum of the digital sums of n for all the bases 1 to n.

$$n + \sum_{b=2}^n \sum_{i=0}^\infty \left\lfloor \frac{n}{b^i} \right\rfloor \bmod b$$

Example:

n = 5


Create the range [1...n]: [1,2,3,4,5]


For each element x, get an array of the base-x digits of n: [[1,1,1,1,1],[1,0,1],[1,2],[1,1],[1,0]]

bijective base-1 of 5 is [1,1,1,1,1]

base-2 (binary) of 5 is [1,0,1]

base-3 of 5 is [1,2]

base-4 of 5 is [1,1]

base-5 of 5 is [1,0]


Sum the digits: 13


Test cases:

1    1
2    3
3    6
4    8
5    13
6    16
7    23
8    25
9    30
10   35

36   297
37   334

64   883
65   932

The sequence can be found on OEIS: A131383

Scoring:

: The submission with the lowest score wins.

  • 3
    A fun one: 227 -> 9999. And also: 1383 -> 345678. – Arnauld Dec 3 at 18:54
  • Any feedback for the downvote would be appreciated. – Oliver 2 days ago

24 Answers 24

up vote 6 down vote accepted

Canvas, 3 bytes

┬]∑

Try it here!

Canvas beating Jelly?

{ ]   map over 1..input (implicit "{")
 ┬      decode input from that base
   ∑  sum the resulting (nested) array

Haskell, 46 bytes

f n=sum$do a<-[1..n];mapM(pure[0..a])[1..n]!!n

Try it online!

Explanation

The function \b n -> mapM(pure[0..b])[1..n], generates all strings \$[0 \dotsc b]^n\$ in lexicographic order. For example:

mapM(pure[0..2])[0..1] == [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]]

By indexing into it with (!!n) this can be used to convert n to base b+1, however this won't work for unary (base-\$1\$), but we're summing the results.. We can even save some bytes with a <- [1..n] and using base-\$(n+1)\$ rather than a work-around for base-\$1\$ since we're missing \$[\underset{n\text{ times}}{\underbrace{1,\dotsc,1}}]\$ which is the same as \$[n]\$ when summing.

Using do-notation just concatenates all the lists instead of nesting them:

λ [ mapM (pure [0..a]) [1..5] !! n | a <- [1..5] ]
[[0,0,1,0,1],[0,0,0,1,2],[0,0,0,1,1],[0,0,0,1,0],[0,0,0,0,5]]

λ do a <- [1..5]; mapM (pure [0..a]) [1..5] !! n
[0,0,1,0,1,0,0,0,1,2,0,0,0,1,1,0,0,0,1,0,0,0,0,0,5]

Ruby, 39 37

->n{(2..n).sum{|b|n.digits(b).sum}+n}

The only golfing here is removing some whitespace. Try it online

  • @Giuseppe The OP starts with: " Given an input n,(...)" . I haven' t been around here for a long time. Is there a list somewhere of requirements to a solution? – steenslag Dec 4 at 0:46
  • 2
    Usually it must be a full program or a function (named or unnamed), here's more information: loopholes and i/o defaults. I don't know ruby, but this seems the shortest fix. – BMO Dec 4 at 0:52
  • @BMO Thanks. For someone who doesn't know Ruby, you are creating and calling a lambda with the greatest of ease! – steenslag Dec 4 at 1:04
  • 1
    you can remove the parentheses around n (37b): ->n{(2..n).sum{|b|n.digits(b).sum}+n} – Conor O'Brien Dec 4 at 1:06
  • 1
    Well, google "ruby lambda" did the trick ;P But I stand corrected in that you were able to save two bytes. – BMO Dec 4 at 12:42

Python 2, 57 bytes

lambda n:sum(n/(k/n+2)**(k%n)%(k/n+2)for k in range(n*n))

This uses the formula $$a(n)=\sum_{b=2}^{n+1}\sum_{i=0}^{n-1}\left\lfloor\frac{n}{b^i}\right\rfloor\bmod b,$$ which works since \$\left\lfloor\frac{n}{(n+1)^0}\right\rfloor\bmod(n+1)=n\$.

Try it online!

APL (Dyalog Unicode), 14 bytes

+/∘∊⊢,⊢(⍴⊤⊣)¨⍳

Try it online!

Explanation

Some parentheses are implied and can be added (lighter than the "official" parenthesizing):

+/∘∊(⊢,(⊢(⍴⊤⊣)¨⍳))

This is a monadic atop. Given an argument Y, this function behaves like:

+/∘∊(⊢,(⊢(⍴⊤⊣)¨⍳))Y

The two functions are applied in order. We'll start from the right one:

⊢,(⊢(⍴⊤⊣)¨⍳)

The are three functions in this train, so this is a fork. Given an argument Y, it acts as:

(⊢Y),((⊢Y)(⍴⊤⊣)¨⍳Y)

We can easily reduce to this (monadic returns its argument, hence called identity):

Y,Y(⍴⊤⊣)¨⍳Y

Now, we know that Y is an integer (simple scalar, i.e. number or character), since we're given one. Therefore ⍳Y, with ⎕IO=1, returns 1 2 ... Y. ⍳Y actually returns an array with shape Y (Y must be a vector), where every scalar is the index of itself in the array (that's why monadic is called the index generator). These indices are vectors, except for the case where 1≡⍴Y, where they are scalars (this is our case).

Let's parse the middle function, (⍴⊤⊣)¨, next. ⍴⊤⊣ is the operand of ¨ (each), and the function is dyadic, so the ¨ operator will first reshape each length-1 argument to the shape of the other (that is, take the element and use it to replace every scalar in the other argument), and then apply the function to each pair of the two arguments. In this case, ⍳Y is a vector and Y is a scalar, so, if n≡⍴⍳Y, then Y will be converted to n⍴Y ( represents the shape (monadic) and reshape (dyadic) functions). That is, in simpler terms, Y will be converted to an array containing Y times Y.

Now, for each pair, let's call the left argument X and the right Z (so that we don't conflict with the input Y). ⍴⊤⊣ is a dyadic fork, so it will expand to:

(X⍴Z)⊤X⊣Z

Let's make the easy first step of reducing X⊣Z to X (dyadic is the left function):

(X⍴Z)⊤X

The in X⍴Z is, again, the reshape function, so X⍴Z, in our case, is simply X times Z. is the encode function. Given two arrays of numbers, where the left array is the base of each digit in the result (doesn't need to be integer or positive), i.e. the encoding, and the right is an array of numbers, returns the transposed array of those numbers in the specified encoding (transposition is the reversal of an array's dimensions relative to its elements). The representation of a digit is based on the quotient of the division of the number and the product of the less significant bases. If any base is 0, it acts as base +∞. The arguments' scalars are all simple. Since X is a positive integer, and X⍴Z is a vector of equal elements, this is really just a case of converting X to base Z and reshaping to X digits. For \$X,Z\in\mathbb N\$, \$X_Z\$ (\$X\$ in base \$Z\$) can't have more than \$X\$ digits, since \$X_1\$ has \$X\$ digits. Therefore, X⍴Z is enough for our purposes.

The result of Y(⍴⊤⊣)¨⍳Y is, therefore, Y converted to each base from 1 to Y, possibly with leading zeroes. However, there is one issue: in APL, base 1 isn't special-cased, while this challenge does special-case it, so we have to include the sum of the base-1 digits of Y ourselves. Fortunately, this sum is just Y, since \$Y_1=\underbrace{[1,1,...,1]}_Y\$, so the sum is simply \$Y\times1=Y\$. It follows that we have to add Y somewhere into the array. This is how we do it:

Y,Y(⍴⊤⊣)¨⍳Y

I have already included this part here. Dyadic , is the catenate function, it concatenates its arguments on their last axes, and errors if that's not possible. Here, we simply concatenate the scalar Y to the vector Y(⍴⊤⊣)¨⍳Y, so that we increment the sum we're going to calculate by Y, as explained above.

The final part is the left function of our atop, +/∘∊:

+/∘∊Y,Y(⍴⊤⊣)¨⍳Y

is the compose operator. f∘g Y is the same as f g Y. However, we're using it here so that our train doesn't fork on the . So, we can reduce:

+/∊Y,Y(⍴⊤⊣)¨⍳Y

Now, it's time for the sum, but wait... there's a problem. The array isn't flat, so we can't just sum its elements before flattening it first. The enlist function flattens an array. Now that the array has been flattened, we finally use +/ to sum it. / is the reduce operator, it applies a dyadic function between an array's elements on its second-to-last axis, with right-to-left priority. If the rank (number of dimensions, i.e. length of shape) of the array doesn't decrease, the array is then enclosed, although this is not the case here. The function that's applied here is +, which is the plus function that adds the pairs on the last axes of two arrays (and errors if the arrays can't be added like that). Here, it simply adds two numbers a number of times so that the reduce is completed.

Lo and behold, our train:

+/∘∊⊢,⊢(⍴⊤⊣)¨⍳

Java 8, 76 65 bytes

n->{int r=n,b=n,N;for(;b>1;b--)for(N=n;N>0;N/=b)r+=N%b;return r;}

-11 bytes thanks to @OlivierGrégoire.

Try it online.

Explanation:

n->{           // Method with integer as both parameter and return-type
  int r=n,     //  Result-sum, starting at the input to account for Base-1
      b=n,     //  Base, starting at the input
      N;       //  Temp integer
  for(;b>1     //  Loop the Base `b` in the range [n, 1):
      ;b--)    //    After every iteration: Go to the next Base (downwards)
    for(N=n;   //   Set `N` to the input `n`
        N>0;   //   Loop as long as `N` isn't 0 yet:
        N/=b)  //     After every iteration: Divide `N` by the Base `b`
      r+=N%b;  //    Increase the result `r` by `N` modulo the Base `b`
  return r;}   //  Return the result-sum `r`

SAS, 81 74 bytes

data;input n;s=n;do b=2to n;do i=0to n;s+mod(int(n/b**i),b);end;end;cards;

Input is entered after the cards; statement, on newlines, like so:

data;input n;s=n;do b=2to n;do i=0to n;s+mod(int(n/b**i),b);end;end;cards;
1
2
3
4
5
6
7
8
9
10
36
37
64
65

Outputs a dataset containing the answer s (along with helper variables), with a row for each input value

enter image description here

Ungolfed:

data; /* Implicit dataset creation */
input n; /* Read a line of input */

s=n; /* Sum = n to start with (the sum of base 1 digits of n is equal to n) */
do b=2to n; /* For base = 2 to n */
    do i=0to n; /* For each place in the output base (output base must always have less than or equal to n digits) */

        /* Decimal value of current place in the output base = b^i      */
        /* Remainder = int(b / decimal_value)                           */
        /* Current place digit = mod(remainder, base)                   */
        s+mod(int(n/b**i),b);
    end;
end;

cards;
1
2
3

Japt -x, 6 bytes

ÆìXÄ x

Try it

õ!ìU c

Try it

J, 24 23 bytes

+1#.1#.]#.inv"0~2+i.@<:

Try it online!

05AB1E (legacy), 5 bytes

LвOO+

Try it online!

Explanation:

LвOO+  //Full program
L      //push [1 .. input]
 в     //for each element b, push digits of input converted to base b
  O    //sum each element
   O   //sum each sum
    +  //add input

In 05AB1E (legacy), base 1 of 5 is [0,0,0,0,0], not [1,1,1,1,1]. Therefore after summing the range, add the input to account for the missing base 1.

I am using 05AB1E (legacy) because in current 05AB1E, base 1 of 5 is [1]. In order to account for this, I would either need to decrement the result by 1 or remove the first element of the range, both of which would cost 1 byte.

Perl 6, 45 41 bytes

{$^a+sum map {|polymod $a: $_ xx*},2..$a}

-4 bytes thanks to Jo King

Try it online!

Whitespace, 153 bytes

[S S S N
_Push_0][S N
S _Duplicate_0][T   N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input][S N
S _Duplicate_input][N
S S N
_Create_Label_OUTER_LOOP][S N
S _Duplicate_top][S S S T   N
_Push_1][T  S S T   _Subtract][N
T   S S N
_If_0_Jump_to_Label_PRINT][S S S N
_Push_0][S N
S _Duplicate_0][T   T   T   _Retrieve_input][N
S S T   N
_Create_Label_INNER_LOOP][S N
S _Duplicate][N
T   S S S N
_If_0_Jump_to_Label_AFTER_INNER_LOOP][S N
T   _Swap_top_two][S T  S S T   N
_Copy_1st_item_to_top][S T  S S T   T   N
_Copy_3rd_item_to_top][T    S T T   _Modulo][T  S S S _Add][S N
T   _Swap_top_two][S T  S S T   S N
_Copy_2nd_item_to_top][T    S T S _Integer_divide][N
S N
T   N
_Jump_to_Label_INNER_LOOP][N
S S S S N
_Create_Label_AFTER_INNER_LOOP][S N
N
_Discard_top][S T   S S T   S N
_Copy_2nd_item_to_top][T    S S S _Add][S N
T   _Swap_top_two][S S S T  N
_Push_1][T  S S T   _Subtract][N
S N
N
_Jump_to_Label_OUTER_LOOP][N
S S S N
_Create_Label_PRINT][S N
N
_Discard_top][T N
S T _Print_as_integer]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Port of my Java 8 answer, because Whitespace has no Base conversion builtins at all.

Example run: input = 3

Command    Explanation                     Stack            Heap   STDIN  STDOUT  STDERR

SSSN       Push 0                          [0]
SNS        Duplicate 0                     [0,0]
TNTT       Read STDIN as integer           [0]              {0:3}  3
TTT        Retrieve input from heap 0      [3]              {0:3}
SNS        Duplicate 3                     [3,3]            {0:3}
NSSN       Create Label_OUTER_LOOP         [3,3]            {0:3}
 SNS       Duplicate 3                     [3,3,3]          {0:3}
 SSSTN     Push 1                          [3,3,3,1]        {0:3}
 TSST      Subtract (3-1)                  [3,3,2]          {0:3}
 NTSSN     If 0: Jump to Label_PRINT       [3,3]            {0:3}
 SSSN      Push 0                          [3,3,0]          {0:3}
 SNS       Duplicate 0                     [3,3,0,0]        {0:3}
 TTT       Retrieve input from heap 0      [3,3,0,3]        {0:3}
 NSSTN     Create Label_INNER_LOOP         [3,3,0,3]        {0:3}
  SNS      Duplicate 3                     [3,3,0,3,3]      {0:3}
  NTSSSN   If 0: Jump to Label_AFTER_IL    [3,3,0,3]        {0:3}
  SNT      Swap top two                    [3,3,3,0]        {0:3}
  STSSTN   Copy (0-indexed) 1st            [3,3,3,0,3]      {0:3}
  STSSTTN  Copy (0-indexed) 3rd            [3,3,3,0,3,3]    {0:3}
  TSTT     Modulo (3%3)                    [3,3,3,0,0]      {0:3}
  TSSS     Add (0+0)                       [3,3,3,0]        {0:3}
  SNT      Swap top two                    [3,3,0,3]        {0:3}
  STSSTSN  Copy (0-indexed) 2nd            [3,3,0,3,3]      {0:3}
  TSTS     Integer divide (3/3)            [3,3,0,1]        {0:3}
  NSNTN    Jump to LABEL_INNER_LOOP        [3,3,0,1]        {0:3}

  SNS      Duplicate 1                     [3,3,0,1,1]      {0:3}
  NTSSSN   If 0: Jump to Label_AFTER_IL    [3,3,0,1]        {0:3}
  SNT      Swap top two                    [3,3,1,0]        {0:3}
  STSSTN   Copy (0-indexed) 1st            [3,3,1,0,1]      {0:3}
  STSSTTN  Copy (0-indexed) 3rd            [3,3,1,0,1,3]    {0:3}
  TSTT     Modulo (1%3)                    [3,3,1,0,1]      {0:3}
  TSSS     Add (0+1)                       [3,3,1,1]        {0:3}
  SNT      Swap top two                    [3,3,1,1]        {0:3}
  STSSTSN  Copy (0-indexed) 2nd            [3,3,1,1,3]      {0:3}
  TSTS     Integer divide (1/3)            [3,3,1,0]        {0:3}
  NSNTN    Jump to LABEL_INNER_LOOP        [3,3,1,0]        {0:3}

  SNS      Duplicate 0                     [3,3,1,0,0]      {0:3}
  NTSSSN   If 0: Jump to Label_AFTER_IL    [3,3,1,0]        {0:3}
 NSSSSN    Create Label_AFTER_IL           [3,3,1,0]        {0:3}
  SNN      Discard top                     [3,3,1]          {0:3}
  STSSTSN  Copy (0-indexed) 2nd            [3,3,1,3]        {0:3}
  TSSS     Add (1+3)                       [3,3,4]          {0:3}
  SNT      Swap top two                    [3,4,3]          {0:3}
  SSSTN    Push 1                          [3,4,3,1]        {0:3}
  TSST     Subtract (3-1)                  [3,4,2]          {0:3}
  NSNN     Jump to LABEL_OUTER_LOOP        [3,4,2]          {0:3}

 SNS       Duplicate 2                     [3,4,2,2]        {0:3}
 SSSTN     Push 1                          [3,4,2,2,1]      {0:3}
 TSST      Subtract (2-1)                  [3,4,2,1]        {0:3}
 NTSSN     If 0: Jump to Label_PRINT       [3,4,2]          {0:3}
 SSSN      Push 0                          [3,4,2,0]        {0:3}
 SNS       Duplicate 0                     [3,4,2,0,0]      {0:3}
 TTT       Retrieve input from heap 0      [3,4,2,0,3]      {0:3}
 NSSTN     Create Label_INNER_LOOP         [3,4,2,0,3]      {0:3}
  SNS      Duplicate 3                     [3,4,2,0,3,3]    {0:3}
  NTSSSN   If 0: Jump to Label_AFTER_IL    [3,4,2,0,3]      {0:3}
  SNT      Swap top two                    [3,4,2,3,0]      {0:3}
  STSSTN   Copy (0-indexed) 1st            [3,4,2,3,0,3]    {0:3}
  STSSTTN  Copy (0-indexed) 3rd            [3,4,2,3,0,3,2]  {0:3}
  TSTT     Modulo (3%2)                    [3,4,2,3,0,1]    {0:3}
  TSSS     Add (0+1)                       [3,4,2,3,1]      {0:3}
  SNT      Swap top two                    [3,4,2,1,3]      {0:3}
  STSSTSN  Copy (0-indexed) 2nd            [3,4,2,1,3,2]    {0:3}
  TSTS     Integer divide (3/2)            [3,4,2,1,1]      {0:3}
  NSNTN    Jump to LABEL_INNER_LOOP        [3,4,2,1,1]      {0:3}

  SNS      Duplicate 1                     [3,4,2,1,1,1]    {0:3}
  NTSSSN   If 0: Jump to Label_AFTER_IL    [3,4,2,1,1]      {0:3}
  SNT      Swap top two                    [3,4,2,1,1]      {0:3}
  STSSTN   Copy (0-indexed) 1st            [3,4,2,1,1,1]    {0:3}
  STSSTTN  Copy (0-indexed) 3rd            [3,4,2,1,1,1,2]  {0:3}
  TSTT     Modulo (1%2)                    [3,4,2,1,1,1]    {0:3}
  TSSS     Add (1+1)                       [3,4,2,1,2]      {0:3}
  SNT      Swap top two                    [3,4,2,2,1]      {0:3}
  STSSTSN  Copy (0-indexed) 2nd            [3,4,2,2,1,2]    {0:3}
  TSTS     Integer divide (1/2)            [3,4,2,2,0]      {0:3}
  NSNTN    Jump to LABEL_INNER_LOOP        [3,4,2,2,0]      {0:3}

  SNS      Duplicate 0                     [3,4,2,2,0,0]    {0:3}
  NTSSSN   If 0: Jump to Label_AFTER_IL    [3,4,2,2,0]      {0:3}
 NSSSSN    Create Label_AFTER_IL           [3,4,2,2,0]      {0:3}
  SNN      Discard top                     [3,4,2,2]        {0:3}
  STSSTSN  Copy (0-indexed) 2nd            [3,4,2,2,4]      {0:3}
  TSSS     Add (2+4)                       [3,4,2,6]        {0:3}
  SNT      Swap top two                    [3,4,6,2]        {0:3}
  SSSTN    Push 1                          [3,4,6,2,1]      {0:3}
  TSST     Subtract (2-1)                  [3,4,6,1]        {0:3}
  NSNN     Jump to LABEL_OUTER_LOOP        [3,4,6,1]        {0:3}

 SNS       Duplicate 1                     [3,4,6,1,1]      {0:3}
 SSSTN     Push 1                          [3,4,6,1,1,1]    {0:3}
 TSST      Subtract (1-1)                  [3,4,6,1,0]      {0:3}
 NTSSN     If 0: Jump to Label_PRINT       [3,4,6,1]        {0:3}
NSSSSN     Create Label_PRINT              [3,4,6,1]        {0:3}
 SNN       Discard top                     [3,4,6]          {0:3}
 TNST      Print top (6) to STDOUT as int  [3,4]            {0:3}         6
                                                                                  error

Program stops with an error: No exit found. (Although I could add three trailing newlines NNN to get rid of that error.)

R, 60 bytes

function(n){if(n-1)for(i in 2:n)F=F+sum(n%/%i^(0:n)%%i)
n+F}

Try it online!

Fails for n>143 since 144^144 is larger than a double can get. Thanks to Josh Eller for suggesting replacing log(n,i) with simply n.

The below will work for n>143; not sure at what point it will stop working.

R, 67 bytes

function(n){if(n-1)for(i in 2:n)F=F+sum(n%/%i^(0:log(n,i))%%i)
n+F}

Try it online!

Uses the classic n%/%i^(0:log(n,i))%%i method to extract the base-i digits for n for each base b>1, then sums them and accumulates the sum in F, which is initialized to 0, then adding n (the base 1 representation of n) to F and returning the result. For n=1, it skips the bases and simply adds n to F.

  • 1
    I don't know any R, but instead of using 0:log(n,i), couldn't you use 0:n? There's always going to be at most n digits in any base representation of n, and everything after the initial log(n,i) digits should be 0, so it won't affect the sum. – Josh Eller Dec 4 at 20:18
  • @JoshEller I suppose I could. It would start to fail for n=144, since 143^143 is around 1.6e308 and 144^144 evaluates to Inf. Thanks! – Giuseppe Dec 4 at 20:23

C (gcc), 67 56 bytes

b,a,s;e(n){for(b=a=n;a>1;a--)for(s=n;s;s/=a)b+=s%a;n=b;}

Port of my Java 8 answer.
-11 bytes thanks to @OlivierGrégoire's golf on my Java answer.

Try it online.

Explanation:

b,             // Result integer
a,             // Base integer
s;             // Temp integer
e(n){          // Method with integer as parameter
  for(b=a=n;   //  Set the result `b` to input `n` to account for Base-1
               //  And set Base `a` to input `n` as well
      a>1      //  Loop the Base `a` in the range [input, 1):
      ;a--)    //    After every iteration: Go to the next Base (downwards)
    for(s=n;   //   Set `s` to input `n`
        s;     //   Inner loop as long as `s` is not 0 yet:
        s/=a)  //     After every iteration: Divide `s` by Base `a`
      b+=s%a;  //    Add `s` modulo Base `a` to the result `b`
  n=b;}        //  Set input `n` to result `b` to 'return it'

JavaScript (ES6), 42 bytes

This version is almost identical to my main answer but relies on arithmetic underflow to stop the recursion. The highest supported value depends on the size of the call stack.

f=(n,b=1,x)=>b>n?n:~~x%b+f(n,b+!x,x?x/b:n)

Try it online!


JavaScript (ES6),  51 48  44 bytes

f=(n,b=2,x=n)=>b>n?n:x%b+f(n,b+!x,x?x/b|0:n)

Try it online!

Commented

f = (             // f = recursive function taking:
  n,              // - n = input
  b = 2,          // - b = current base, initialized to 2
  x = n           // - x = current value being converted in base b
) =>              //
  b > n ?         // if b is greater than n:
    n             //   stop recursion and return n, which is the sum of the digits of n
                  //   once converted to unary
  :               // else:
    x % b +       //   add x modulo b to the final result
    f(            //   and add the result of a recursive call:
      n,          //     pass n unchanged
      b + !x,     //     increment b if x = 0
      x ?         //     if x is not equal to 0:
        x / b | 0 //       update x to floor(x / b)
      :           //     else:
        n         //       reset x to n
    )             //   end of recursive call

APL (Dyalog Unicode), 22 bytes

{1⊥⍵,∊(1+⍳⍵-1)⊥⍣¯1¨⊂⍵}

Try it online!

Husk, 6 bytes

I really wish that there was something like M for cmap :(

Σ§ṁ`Bḣ

Try it online or test all!

Explanation

Σ§ṁ`Bḣ  -- example input: 5
 §      -- fork the argument..
     ḣ  -- | range: [1,2,3,4,5]
   `B   -- | convert argument to base: (`asBase` 5)
  ṁ     -- | and concatMap: cmap (`asBase` 5) [1,2,3,4,5]
        -- : [1,1,1,1,1,1,0,1,1,2,1,1,1,0]
Σ       -- sum: 13

Alternatively, 6 bytes

ΣΣṠMBḣ

Try it online or test all!

Explanation

ΣΣṠMBḣ  -- example input: 5
  Ṡ     -- apply .. 
     ḣ  -- | range: [1,2,3,4,5]
           .. to function applied to itself
   MB   -- | map over left argument of base
        -- : [[1,1,1,1,1],[1,0,1],[1,2],[1,1],[1,0]]
 Σ      -- flatten: [1,1,1,1,1,1,0,1,1,2,1,1,1,0]
Σ       -- sum: 13

Desmos, 127 bytes

$$f\left(n\right)=\sum_{b=2}^{n+1}\sum_{i=0}^{n-1}\operatorname{mod}\left(\operatorname{floor}\left(\frac{n}{b^i}\right),b\right)$$

f\left(n\right)=\sum_{b=2}^{n+1}\sum_{i=0}^{n-1}\operatorname{mod}\left(\operatorname{floor}\left(\frac{n}{b^i}\right),b\right)

Try it here! Defines a function \$f\$, called as \$f\left(n\right)\$. Uses the formula given in Dennis's answer.

Here is the resulting graph (point at \$(65, 932)\$):

graph generated on desmos.com

Desmos, 56 bytes

This might not work on all browsers, but it works on mine. Just copy and paste the formula. This is \$f_2(n)\$ in the above link.

f(n)=\sum_{b=2}^{n+1}\sum_{i=0}^{n-1}mod(floor(n/b^i),b)

Pari/GP, 30 bytes

n->n+sum(b=2,n,sumdigits(n,b))

Try it online!

Python 2, 61 bytes

f=lambda n,b=2.:n and n%b+f(n//b,b)+(n//b>1/n==0and f(n,b+1))

Try it online!

Though this is longer the Dennis's solution off which it's based, I find the method too amusing to not share.

The goal is to recurse both on lopping off the last digit n->n/b and incrementing the base b->b+1, but we want to prevent the base from being increased after one or more digits have been lopped off. This is achieved by make the base b a float, so that after the update n->n//b, the float b infects n with its floatness. In this way, whether n is a float or not is a bit-flag for whether we've removed any digits from n.

We require that the condition 1/n==0 is met to recurse into incrementing b, which integers n satisfy because floor division is done, but floats fails. (n=1 also fails but we don't want to recurse on it anyway.) Otherwise, floats work just like integers in the function because we're careful to do floor division n//b, and the output is a whole-number float.

Jelly, 4 bytes

bRFS

Try it online!

How it works

bRFS  Main link. Argument: n

 R    Range; yield [1, ..., n].
b     Convert n to back k, for each k to the right.
  F   Flatten the resulting 2D array of digits.
   S  Take the sum.

Attache, 25 bytes

{Sum!`'^^ToBase[_,2:_]'_}

Try it online!

Explanation

{Sum!`'^^ToBase[_,2:_]'_}
{                       }   anonymous lambda, input: _
                            example: 5
         ToBase[_,   ]      convert `_`
                  2:_       ...to each base from 2 to `_`
                            example: [[1, 0, 1], [1, 2], [1, 1], [1, 0]]
                      '_    append `_`
                            example: [[1, 0, 1], [1, 2], [1, 1], [1, 0], 5]
       ^^                   fold...
     `'                     the append operator over the list
                            example: [1, 0, 1, 1, 2, 1, 1, 1, 0, 5]
 Sum!                       take the sum
                            example: 13

Charcoal, 12 bytes

IΣEIθΣ↨Iθ⁺²ι

Try it online! Link is to verbose version of code. Charcoal can't convert to base 1, but fortunately the digit sum is the same as the conversion to base \$n + 1\$. Explanation:

    θ           First input
   I            Cast to integer
  E             Map over implicit range
        θ       First input
       I        Cast to integer
      ↨         Converted to base
           ι    Current index
         ⁺      Plus
          ²     Literal 2
     Σ          Sum
 Σ              Grand total
I               Cast to string
                Implicitly print

Retina 0.8.2, 49 bytes

.+
$*
1
11$`;$_¶
+`\b(1+);(\1)+
$1;$#2$*1,
1+;

1

Try it online! Link includes test cases. Explanation:

.+
$*

Convert to unary.

1
11$`;$_¶

List all the numbers from 2 to \$n + 1\$ (since that's easier than base 1 conversion).

+`\b(1+);(\1)+
$1;$#2$*1,

Use repeated divmod to convert the original number to each base.

1+;

Delete the list of bases, leaving just the base conversion digits.

1

Take the sum and convert to decimal.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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