Digital Sumorial
up vote
19
down vote
favorite
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 leftlfloor frac{n}{b^i} rightrfloor 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:
code-golf: The submission with the lowest score wins.
code-golf math sequence integer base-conversion
add a comment |
up vote
19
down vote
favorite
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 leftlfloor frac{n}{b^i} rightrfloor 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:
code-golf: The submission with the lowest score wins.
code-golf math sequence integer base-conversion
3
A fun one:227 -> 9999
. And also:1383 -> 345678
.
– Arnauld
Dec 3 at 18:54
add a comment |
up vote
19
down vote
favorite
up vote
19
down vote
favorite
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 leftlfloor frac{n}{b^i} rightrfloor 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:
code-golf: The submission with the lowest score wins.
code-golf math sequence integer base-conversion
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 leftlfloor frac{n}{b^i} rightrfloor 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:
code-golf: The submission with the lowest score wins.
code-golf math sequence integer base-conversion
code-golf math sequence integer base-conversion
asked Dec 3 at 16:31
Oliver
4,6801831
4,6801831
3
A fun one:227 -> 9999
. And also:1383 -> 345678
.
– Arnauld
Dec 3 at 18:54
add a comment |
3
A fun one:227 -> 9999
. And also:1383 -> 345678
.
– Arnauld
Dec 3 at 18:54
3
3
A fun one:
227 -> 9999
. And also: 1383 -> 345678
.– Arnauld
Dec 3 at 18:54
A fun one:
227 -> 9999
. And also: 1383 -> 345678
.– Arnauld
Dec 3 at 18:54
add a comment |
24 Answers
24
active
oldest
votes
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
add a comment |
up vote
7
down vote
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{ntext{ 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]
add a comment |
up vote
6
down vote
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 aroundn
(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
|
show 1 more comment
up vote
5
down vote
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}leftlfloorfrac{n}{b^i}rightrfloorbmod b,$$ which works since $leftlfloorfrac{n}{(n+1)^0}rightrfloorbmod(n+1)=n$.
Try it online!
add a comment |
up vote
5
down vote
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,Zinmathbb 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 $Ytimes1=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:
+/∘∊⊢,⊢(⍴⊤⊣)¨⍳
+1 Impressive explanation. Not shorter, but no parens:+/∘∊⊢,⊢⊤¨⍨⊢⍴¨⍳
– Adám
Dec 4 at 9:19
@Adám Thanks! I was almost sleeping when I wrote it. :-P
– Erik the Outgolfer
Dec 4 at 11:13
@Adám Regarding your version, it looks like it's a tad more difficult to understand. ;-)
– Erik the Outgolfer
Dec 4 at 11:26
add a comment |
up vote
5
down vote
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`
1
There is no need fori
, so... 65 bytes.
– Olivier Grégoire
Dec 5 at 11:01
@OlivierGrégoire Damn, I'm an idiot. Thanks a lot! Hmm, now I also need to golf my derived C and Whitespace answers.. ;)
– Kevin Cruijssen
Dec 5 at 11:03
add a comment |
up vote
4
down vote
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
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
add a comment |
up vote
3
down vote
Japt -x
, 6 bytes
ÆìXÄ x
Try it
õ!ìU c
Try it
add a comment |
up vote
3
down vote
J, 24 23 bytes
+1#.1#.]#.inv"0~2+i.@<:
Try it online!
add a comment |
up vote
3
down vote
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.
add a comment |
up vote
3
down vote
Perl 6, 45 41 bytes
{$^a+sum map {|polymod $a: $_ xx*},2..$a}
-4 bytes thanks to Jo King
Try it online!
36 bytes
– nwellnhof
Dec 6 at 14:39
add a comment |
up vote
3
down vote
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.)
add a comment |
up vote
3
down vote
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 using0:log(n,i)
, couldn't you use0:n
? There's always going to be at most n digits in any base representation of n, and everything after the initiallog(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 forn=144
, since143^143
is around1.6e308
and144^144
evaluates toInf
. Thanks!
– Giuseppe
Dec 4 at 20:23
add a comment |
up vote
2
down vote
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
add a comment |
up vote
2
down vote
APL (Dyalog Unicode), 22 bytes
{1⊥⍵,∊(1+⍳⍵-1)⊥⍣¯1¨⊂⍵}
Try it online!
add a comment |
up vote
2
down vote
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
add a comment |
up vote
2
down vote
Pari/GP, 30 bytes
n->n+sum(b=2,n,sumdigits(n,b))
Try it online!
add a comment |
up vote
2
down vote
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.
add a comment |
up vote
1
down vote
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.
add a comment |
up vote
1
down vote
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
add a comment |
up vote
1
down vote
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
add a comment |
up vote
1
down vote
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.
add a comment |
up vote
1
down vote
Desmos, 127 bytes
$$fleft(nright)=sum_{b=2}^{n+1}sum_{i=0}^{n-1}operatorname{mod}left(operatorname{floor}left(frac{n}{b^i}right),bright)$$
fleft(nright)=sum_{b=2}^{n+1}sum_{i=0}^{n-1}operatorname{mod}left(operatorname{floor}left(frac{n}{b^i}right),bright)
Try it here! Defines a function $f$, called as $fleft(nright)$. Uses the formula given in Dennis's answer.
Here is the resulting graph (point at $(65, 932)$):
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)
add a comment |
up vote
1
down vote
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'
add a comment |
24 Answers
24
active
oldest
votes
24 Answers
24
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
up vote
6
down vote
accepted
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
Canvas, 3 bytes
┬]∑
Try it here!
Canvas beating Jelly?
{ ] map over 1..input (implicit "{")
┬ decode input from that base
∑ sum the resulting (nested) array
edited Dec 3 at 17:08
answered Dec 3 at 17:03
dzaima
14.2k21754
14.2k21754
add a comment |
add a comment |
up vote
7
down vote
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{ntext{ 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]
add a comment |
up vote
7
down vote
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{ntext{ 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]
add a comment |
up vote
7
down vote
up vote
7
down vote
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{ntext{ 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]
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{ntext{ 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]
edited Dec 4 at 0:41
answered Dec 3 at 19:24
BMO
11k21881
11k21881
add a comment |
add a comment |
up vote
6
down vote
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 aroundn
(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
|
show 1 more comment
up vote
6
down vote
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 aroundn
(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
|
show 1 more comment
up vote
6
down vote
up vote
6
down vote
Ruby, 39 37
->n{(2..n).sum{|b|n.digits(b).sum}+n}
The only golfing here is removing some whitespace. Try it online
Ruby, 39 37
->n{(2..n).sum{|b|n.digits(b).sum}+n}
The only golfing here is removing some whitespace. Try it online
edited Dec 4 at 1:14
answered Dec 3 at 23:55
steenslag
1,760919
1,760919
@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 aroundn
(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
|
show 1 more comment
@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 aroundn
(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
@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
@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
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
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
@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
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
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
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
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
|
show 1 more comment
up vote
5
down vote
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}leftlfloorfrac{n}{b^i}rightrfloorbmod b,$$ which works since $leftlfloorfrac{n}{(n+1)^0}rightrfloorbmod(n+1)=n$.
Try it online!
add a comment |
up vote
5
down vote
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}leftlfloorfrac{n}{b^i}rightrfloorbmod b,$$ which works since $leftlfloorfrac{n}{(n+1)^0}rightrfloorbmod(n+1)=n$.
Try it online!
add a comment |
up vote
5
down vote
up vote
5
down vote
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}leftlfloorfrac{n}{b^i}rightrfloorbmod b,$$ which works since $leftlfloorfrac{n}{(n+1)^0}rightrfloorbmod(n+1)=n$.
Try it online!
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}leftlfloorfrac{n}{b^i}rightrfloorbmod b,$$ which works since $leftlfloorfrac{n}{(n+1)^0}rightrfloorbmod(n+1)=n$.
Try it online!
edited Dec 3 at 16:55
answered Dec 3 at 16:45
Dennis♦
185k32295734
185k32295734
add a comment |
add a comment |
up vote
5
down vote
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,Zinmathbb 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 $Ytimes1=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:
+/∘∊⊢,⊢(⍴⊤⊣)¨⍳
+1 Impressive explanation. Not shorter, but no parens:+/∘∊⊢,⊢⊤¨⍨⊢⍴¨⍳
– Adám
Dec 4 at 9:19
@Adám Thanks! I was almost sleeping when I wrote it. :-P
– Erik the Outgolfer
Dec 4 at 11:13
@Adám Regarding your version, it looks like it's a tad more difficult to understand. ;-)
– Erik the Outgolfer
Dec 4 at 11:26
add a comment |
up vote
5
down vote
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,Zinmathbb 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 $Ytimes1=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:
+/∘∊⊢,⊢(⍴⊤⊣)¨⍳
+1 Impressive explanation. Not shorter, but no parens:+/∘∊⊢,⊢⊤¨⍨⊢⍴¨⍳
– Adám
Dec 4 at 9:19
@Adám Thanks! I was almost sleeping when I wrote it. :-P
– Erik the Outgolfer
Dec 4 at 11:13
@Adám Regarding your version, it looks like it's a tad more difficult to understand. ;-)
– Erik the Outgolfer
Dec 4 at 11:26
add a comment |
up vote
5
down vote
up vote
5
down vote
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,Zinmathbb 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 $Ytimes1=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:
+/∘∊⊢,⊢(⍴⊤⊣)¨⍳
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,Zinmathbb 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 $Ytimes1=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:
+/∘∊⊢,⊢(⍴⊤⊣)¨⍳
edited Dec 3 at 23:23
answered Dec 3 at 20:42
Erik the Outgolfer
31k429102
31k429102
+1 Impressive explanation. Not shorter, but no parens:+/∘∊⊢,⊢⊤¨⍨⊢⍴¨⍳
– Adám
Dec 4 at 9:19
@Adám Thanks! I was almost sleeping when I wrote it. :-P
– Erik the Outgolfer
Dec 4 at 11:13
@Adám Regarding your version, it looks like it's a tad more difficult to understand. ;-)
– Erik the Outgolfer
Dec 4 at 11:26
add a comment |
+1 Impressive explanation. Not shorter, but no parens:+/∘∊⊢,⊢⊤¨⍨⊢⍴¨⍳
– Adám
Dec 4 at 9:19
@Adám Thanks! I was almost sleeping when I wrote it. :-P
– Erik the Outgolfer
Dec 4 at 11:13
@Adám Regarding your version, it looks like it's a tad more difficult to understand. ;-)
– Erik the Outgolfer
Dec 4 at 11:26
+1 Impressive explanation. Not shorter, but no parens:
+/∘∊⊢,⊢⊤¨⍨⊢⍴¨⍳
– Adám
Dec 4 at 9:19
+1 Impressive explanation. Not shorter, but no parens:
+/∘∊⊢,⊢⊤¨⍨⊢⍴¨⍳
– Adám
Dec 4 at 9:19
@Adám Thanks! I was almost sleeping when I wrote it. :-P
– Erik the Outgolfer
Dec 4 at 11:13
@Adám Thanks! I was almost sleeping when I wrote it. :-P
– Erik the Outgolfer
Dec 4 at 11:13
@Adám Regarding your version, it looks like it's a tad more difficult to understand. ;-)
– Erik the Outgolfer
Dec 4 at 11:26
@Adám Regarding your version, it looks like it's a tad more difficult to understand. ;-)
– Erik the Outgolfer
Dec 4 at 11:26
add a comment |
up vote
5
down vote
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`
1
There is no need fori
, so... 65 bytes.
– Olivier Grégoire
Dec 5 at 11:01
@OlivierGrégoire Damn, I'm an idiot. Thanks a lot! Hmm, now I also need to golf my derived C and Whitespace answers.. ;)
– Kevin Cruijssen
Dec 5 at 11:03
add a comment |
up vote
5
down vote
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`
1
There is no need fori
, so... 65 bytes.
– Olivier Grégoire
Dec 5 at 11:01
@OlivierGrégoire Damn, I'm an idiot. Thanks a lot! Hmm, now I also need to golf my derived C and Whitespace answers.. ;)
– Kevin Cruijssen
Dec 5 at 11:03
add a comment |
up vote
5
down vote
up vote
5
down vote
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`
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`
edited Dec 5 at 11:03
answered Dec 4 at 8:38
Kevin Cruijssen
35.2k554185
35.2k554185
1
There is no need fori
, so... 65 bytes.
– Olivier Grégoire
Dec 5 at 11:01
@OlivierGrégoire Damn, I'm an idiot. Thanks a lot! Hmm, now I also need to golf my derived C and Whitespace answers.. ;)
– Kevin Cruijssen
Dec 5 at 11:03
add a comment |
1
There is no need fori
, so... 65 bytes.
– Olivier Grégoire
Dec 5 at 11:01
@OlivierGrégoire Damn, I'm an idiot. Thanks a lot! Hmm, now I also need to golf my derived C and Whitespace answers.. ;)
– Kevin Cruijssen
Dec 5 at 11:03
1
1
There is no need for
i
, so... 65 bytes.– Olivier Grégoire
Dec 5 at 11:01
There is no need for
i
, so... 65 bytes.– Olivier Grégoire
Dec 5 at 11:01
@OlivierGrégoire Damn, I'm an idiot. Thanks a lot! Hmm, now I also need to golf my derived C and Whitespace answers.. ;)
– Kevin Cruijssen
Dec 5 at 11:03
@OlivierGrégoire Damn, I'm an idiot. Thanks a lot! Hmm, now I also need to golf my derived C and Whitespace answers.. ;)
– Kevin Cruijssen
Dec 5 at 11:03
add a comment |
up vote
4
down vote
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
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
add a comment |
up vote
4
down vote
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
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
add a comment |
up vote
4
down vote
up vote
4
down vote
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
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
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
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
edited Dec 4 at 20:25
answered Dec 4 at 19:41
Josh Eller
1913
1913
add a comment |
add a comment |
up vote
3
down vote
Japt -x
, 6 bytes
ÆìXÄ x
Try it
õ!ìU c
Try it
add a comment |
up vote
3
down vote
Japt -x
, 6 bytes
ÆìXÄ x
Try it
õ!ìU c
Try it
add a comment |
up vote
3
down vote
up vote
3
down vote
Japt -x
, 6 bytes
ÆìXÄ x
Try it
õ!ìU c
Try it
Japt -x
, 6 bytes
ÆìXÄ x
Try it
õ!ìU c
Try it
answered Dec 3 at 17:20
Shaggy
18.6k21663
18.6k21663
add a comment |
add a comment |
up vote
3
down vote
J, 24 23 bytes
+1#.1#.]#.inv"0~2+i.@<:
Try it online!
add a comment |
up vote
3
down vote
J, 24 23 bytes
+1#.1#.]#.inv"0~2+i.@<:
Try it online!
add a comment |
up vote
3
down vote
up vote
3
down vote
J, 24 23 bytes
+1#.1#.]#.inv"0~2+i.@<:
Try it online!
J, 24 23 bytes
+1#.1#.]#.inv"0~2+i.@<:
Try it online!
edited Dec 3 at 18:40
answered Dec 3 at 17:29
Jonah
2,011816
2,011816
add a comment |
add a comment |
up vote
3
down vote
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.
add a comment |
up vote
3
down vote
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.
add a comment |
up vote
3
down vote
up vote
3
down vote
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.
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.
answered Dec 3 at 20:25
Cowabunghole
1,050418
1,050418
add a comment |
add a comment |
up vote
3
down vote
Perl 6, 45 41 bytes
{$^a+sum map {|polymod $a: $_ xx*},2..$a}
-4 bytes thanks to Jo King
Try it online!
36 bytes
– nwellnhof
Dec 6 at 14:39
add a comment |
up vote
3
down vote
Perl 6, 45 41 bytes
{$^a+sum map {|polymod $a: $_ xx*},2..$a}
-4 bytes thanks to Jo King
Try it online!
36 bytes
– nwellnhof
Dec 6 at 14:39
add a comment |
up vote
3
down vote
up vote
3
down vote
Perl 6, 45 41 bytes
{$^a+sum map {|polymod $a: $_ xx*},2..$a}
-4 bytes thanks to Jo King
Try it online!
Perl 6, 45 41 bytes
{$^a+sum map {|polymod $a: $_ xx*},2..$a}
-4 bytes thanks to Jo King
Try it online!
edited Dec 4 at 1:27
answered Dec 3 at 18:46
Sean
3,28636
3,28636
36 bytes
– nwellnhof
Dec 6 at 14:39
add a comment |
36 bytes
– nwellnhof
Dec 6 at 14:39
36 bytes
– nwellnhof
Dec 6 at 14:39
36 bytes
– nwellnhof
Dec 6 at 14:39
add a comment |
up vote
3
down vote
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.)
add a comment |
up vote
3
down vote
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.)
add a comment |
up vote
3
down vote
up vote
3
down vote
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.)
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.)
answered Dec 4 at 16:13
Kevin Cruijssen
35.2k554185
35.2k554185
add a comment |
add a comment |
up vote
3
down vote
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 using0:log(n,i)
, couldn't you use0:n
? There's always going to be at most n digits in any base representation of n, and everything after the initiallog(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 forn=144
, since143^143
is around1.6e308
and144^144
evaluates toInf
. Thanks!
– Giuseppe
Dec 4 at 20:23
add a comment |
up vote
3
down vote
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 using0:log(n,i)
, couldn't you use0:n
? There's always going to be at most n digits in any base representation of n, and everything after the initiallog(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 forn=144
, since143^143
is around1.6e308
and144^144
evaluates toInf
. Thanks!
– Giuseppe
Dec 4 at 20:23
add a comment |
up vote
3
down vote
up vote
3
down vote
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
.
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
.
edited Dec 4 at 20:26
answered Dec 3 at 18:26
Giuseppe
16.3k31052
16.3k31052
1
I don't know any R, but instead of using0:log(n,i)
, couldn't you use0:n
? There's always going to be at most n digits in any base representation of n, and everything after the initiallog(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 forn=144
, since143^143
is around1.6e308
and144^144
evaluates toInf
. Thanks!
– Giuseppe
Dec 4 at 20:23
add a comment |
1
I don't know any R, but instead of using0:log(n,i)
, couldn't you use0:n
? There's always going to be at most n digits in any base representation of n, and everything after the initiallog(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 forn=144
, since143^143
is around1.6e308
and144^144
evaluates toInf
. Thanks!
– Giuseppe
Dec 4 at 20:23
1
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
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
@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
add a comment |
up vote
2
down vote
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
add a comment |
up vote
2
down vote
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
add a comment |
up vote
2
down vote
up vote
2
down vote
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
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
edited Dec 3 at 17:14
answered Dec 3 at 16:45
Arnauld
71.2k688298
71.2k688298
add a comment |
add a comment |
up vote
2
down vote
APL (Dyalog Unicode), 22 bytes
{1⊥⍵,∊(1+⍳⍵-1)⊥⍣¯1¨⊂⍵}
Try it online!
add a comment |
up vote
2
down vote
APL (Dyalog Unicode), 22 bytes
{1⊥⍵,∊(1+⍳⍵-1)⊥⍣¯1¨⊂⍵}
Try it online!
add a comment |
up vote
2
down vote
up vote
2
down vote
APL (Dyalog Unicode), 22 bytes
{1⊥⍵,∊(1+⍳⍵-1)⊥⍣¯1¨⊂⍵}
Try it online!
APL (Dyalog Unicode), 22 bytes
{1⊥⍵,∊(1+⍳⍵-1)⊥⍣¯1¨⊂⍵}
Try it online!
answered Dec 3 at 18:14
J. Sallé
1,873322
1,873322
add a comment |
add a comment |
up vote
2
down vote
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
add a comment |
up vote
2
down vote
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
add a comment |
up vote
2
down vote
up vote
2
down vote
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
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
answered Dec 3 at 19:06
BMO
11k21881
11k21881
add a comment |
add a comment |
up vote
2
down vote
Pari/GP, 30 bytes
n->n+sum(b=2,n,sumdigits(n,b))
Try it online!
add a comment |
up vote
2
down vote
Pari/GP, 30 bytes
n->n+sum(b=2,n,sumdigits(n,b))
Try it online!
add a comment |
up vote
2
down vote
up vote
2
down vote
Pari/GP, 30 bytes
n->n+sum(b=2,n,sumdigits(n,b))
Try it online!
Pari/GP, 30 bytes
n->n+sum(b=2,n,sumdigits(n,b))
Try it online!
answered Dec 4 at 2:43
alephalpha
21k32888
21k32888
add a comment |
add a comment |
up vote
2
down vote
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.
add a comment |
up vote
2
down vote
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.
add a comment |
up vote
2
down vote
up vote
2
down vote
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.
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.
answered Dec 5 at 5:57
xnor
89.5k18184439
89.5k18184439
add a comment |
add a comment |
up vote
1
down vote
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.
add a comment |
up vote
1
down vote
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.
add a comment |
up vote
1
down vote
up vote
1
down vote
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.
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.
answered Dec 3 at 16:33
Dennis♦
185k32295734
185k32295734
add a comment |
add a comment |
up vote
1
down vote
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
add a comment |
up vote
1
down vote
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
add a comment |
up vote
1
down vote
up vote
1
down vote
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
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
answered Dec 3 at 22:09
Conor O'Brien
28.9k263160
28.9k263160
add a comment |
add a comment |
up vote
1
down vote
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
add a comment |
up vote
1
down vote
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
add a comment |
up vote
1
down vote
up vote
1
down vote
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
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
answered Dec 3 at 23:39
Neil
78.7k744175
78.7k744175
add a comment |
add a comment |
up vote
1
down vote
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.
add a comment |
up vote
1
down vote
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.
add a comment |
up vote
1
down vote
up vote
1
down vote
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.
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.
answered Dec 3 at 23:50
Neil
78.7k744175
78.7k744175
add a comment |
add a comment |
up vote
1
down vote
Desmos, 127 bytes
$$fleft(nright)=sum_{b=2}^{n+1}sum_{i=0}^{n-1}operatorname{mod}left(operatorname{floor}left(frac{n}{b^i}right),bright)$$
fleft(nright)=sum_{b=2}^{n+1}sum_{i=0}^{n-1}operatorname{mod}left(operatorname{floor}left(frac{n}{b^i}right),bright)
Try it here! Defines a function $f$, called as $fleft(nright)$. Uses the formula given in Dennis's answer.
Here is the resulting graph (point at $(65, 932)$):
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)
add a comment |
up vote
1
down vote
Desmos, 127 bytes
$$fleft(nright)=sum_{b=2}^{n+1}sum_{i=0}^{n-1}operatorname{mod}left(operatorname{floor}left(frac{n}{b^i}right),bright)$$
fleft(nright)=sum_{b=2}^{n+1}sum_{i=0}^{n-1}operatorname{mod}left(operatorname{floor}left(frac{n}{b^i}right),bright)
Try it here! Defines a function $f$, called as $fleft(nright)$. Uses the formula given in Dennis's answer.
Here is the resulting graph (point at $(65, 932)$):
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)
add a comment |
up vote
1
down vote
up vote
1
down vote
Desmos, 127 bytes
$$fleft(nright)=sum_{b=2}^{n+1}sum_{i=0}^{n-1}operatorname{mod}left(operatorname{floor}left(frac{n}{b^i}right),bright)$$
fleft(nright)=sum_{b=2}^{n+1}sum_{i=0}^{n-1}operatorname{mod}left(operatorname{floor}left(frac{n}{b^i}right),bright)
Try it here! Defines a function $f$, called as $fleft(nright)$. Uses the formula given in Dennis's answer.
Here is the resulting graph (point at $(65, 932)$):
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)
Desmos, 127 bytes
$$fleft(nright)=sum_{b=2}^{n+1}sum_{i=0}^{n-1}operatorname{mod}left(operatorname{floor}left(frac{n}{b^i}right),bright)$$
fleft(nright)=sum_{b=2}^{n+1}sum_{i=0}^{n-1}operatorname{mod}left(operatorname{floor}left(frac{n}{b^i}right),bright)
Try it here! Defines a function $f$, called as $fleft(nright)$. Uses the formula given in Dennis's answer.
Here is the resulting graph (point at $(65, 932)$):
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)
answered Dec 4 at 1:14
Conor O'Brien
28.9k263160
28.9k263160
add a comment |
add a comment |
up vote
1
down vote
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'
add a comment |
up vote
1
down vote
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'
add a comment |
up vote
1
down vote
up vote
1
down vote
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'
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'
edited Dec 5 at 11:08
answered Dec 4 at 14:16
Kevin Cruijssen
35.2k554185
35.2k554185
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f176948%2fdigital-sumorial%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
A fun one:
227 -> 9999
. And also:1383 -> 345678
.– Arnauld
Dec 3 at 18:54