Add All Permutations of a Number
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
You have to make something that takes in one input from a default I/O method (as an integer), and prints out the sum of all the permutations of that number (not necessarily unique)
For example:
10
would return 11
because 10
can have 2 permutations (10
and 01
), and the sum of those two numbers would be 11
202
would have 6 permutations (202
, 220
, 022
, 022
, 202
, 220
), so the sum would be 888
.
Standard loopholes are not allowed, and as usual, since this is code golf, the shortest answer in bytes wins!
code-golf
add a comment |Â
up vote
2
down vote
favorite
You have to make something that takes in one input from a default I/O method (as an integer), and prints out the sum of all the permutations of that number (not necessarily unique)
For example:
10
would return 11
because 10
can have 2 permutations (10
and 01
), and the sum of those two numbers would be 11
202
would have 6 permutations (202
, 220
, 022
, 022
, 202
, 220
), so the sum would be 888
.
Standard loopholes are not allowed, and as usual, since this is code golf, the shortest answer in bytes wins!
code-golf
1
Note: Peter Taylor pointed out a simplification of the challenge here. If you are reading this, you may want to think about it first to see if you can get the same (or shorter) solution.
– user202729
4 hours ago
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
You have to make something that takes in one input from a default I/O method (as an integer), and prints out the sum of all the permutations of that number (not necessarily unique)
For example:
10
would return 11
because 10
can have 2 permutations (10
and 01
), and the sum of those two numbers would be 11
202
would have 6 permutations (202
, 220
, 022
, 022
, 202
, 220
), so the sum would be 888
.
Standard loopholes are not allowed, and as usual, since this is code golf, the shortest answer in bytes wins!
code-golf
You have to make something that takes in one input from a default I/O method (as an integer), and prints out the sum of all the permutations of that number (not necessarily unique)
For example:
10
would return 11
because 10
can have 2 permutations (10
and 01
), and the sum of those two numbers would be 11
202
would have 6 permutations (202
, 220
, 022
, 022
, 202
, 220
), so the sum would be 888
.
Standard loopholes are not allowed, and as usual, since this is code golf, the shortest answer in bytes wins!
code-golf
code-golf
asked 6 hours ago
MilkyWay90
144
144
1
Note: Peter Taylor pointed out a simplification of the challenge here. If you are reading this, you may want to think about it first to see if you can get the same (or shorter) solution.
– user202729
4 hours ago
add a comment |Â
1
Note: Peter Taylor pointed out a simplification of the challenge here. If you are reading this, you may want to think about it first to see if you can get the same (or shorter) solution.
– user202729
4 hours ago
1
1
Note: Peter Taylor pointed out a simplification of the challenge here. If you are reading this, you may want to think about it first to see if you can get the same (or shorter) solution.
– user202729
4 hours ago
Note: Peter Taylor pointed out a simplification of the challenge here. If you are reading this, you may want to think about it first to see if you can get the same (or shorter) solution.
– user202729
4 hours ago
add a comment |Â
9 Answers
9
active
oldest
votes
up vote
1
down vote
Perl 6, 30 bytes
*.comb.permutations>>.join.sum
Try it online!
It would be nice if this was just *.permutations.sum
but Perl 6 doesn't treat strings as lists of characters.
Explanation
*.comb.permutations>>.join.sum
*.comb # Convert to list of digits
.permutations # Get all permutations of the list
>>.join # Join all lists of digits
.sum # Get the sum of all numbers
Nice job! I look forward to receiving more responses to this question! Since I am not familiar with Perl, the explanation was very useful!
– MilkyWay90
5 hours ago
add a comment |Â
up vote
1
down vote
JavaScript (ES6), 61 bytes
n=>[...n+(x=s=p='')].map((d,i)=>(p+=1,x=x*i||1,s-=d))&&-s*p*x
Try it online!
How?
This is based on the formula suggested by Peter Taylor in the sandbox:
$$left(sum_i=1^na_iright)frac10^n-19(n-1)!$$
where $a_i$ is the $i$th digit of the input number and $n$ is the total number of digits.
The result of the expression $(10^n-1)/9$ is a number consisting of the digit $1$ repeated $n$ times, which is what is computed in $p$. The factorial is stored in $x$ and the opposite of the sum is stored in $s$.
add a comment |Â
up vote
1
down vote
Python 2, 66 bytes
n=s=0
P=1
for c in input():s+=int(c);n+=1;P*=n
print 10**n/9*s*P/n
Try it online!
Takes input as a string.
Python 2, 70 bytes
f=lambda k,P=1,n=0,s=0:f(k/10,P*-~n,n+1,s+k%10)if k else 10**n/9*s*P/n
Try it online!
An arithmetic method. The base case is hard to deal with because the /n
causes division by zero for the inital value n=0
.
add a comment |Â
up vote
0
down vote
Python 2, 76 bytes
s=map(int,`input()`);n=len(s);t=sum(s)*int('1'*n)
while~-n:n-=1;t*=n
print t
Try it online!
add a comment |Â
up vote
0
down vote
C (clang), 86 + -lm
= 89 bytes
f(a,s,n)for(s=n=0;a;s+=a%10,a/=10,n++);a=tgamma(n)*s*(pow(10,n)-1)/9;printf("%d",a);
Try it online!
add a comment |Â
up vote
0
down vote
CJam, 10 bytes
Abm!:s1b
Try it online!
add a comment |Â
up vote
0
down vote
Ruby -nl
, 41 bytes
p$_.chars.permutation.sumx
Try it online!
Full program taking input from stdin.
Ruby, 45 bytes
->nn.digits.permutation.sumx
Try it online!
Function taking input as integer. digits
can be shortened to chars
if input is acceptable as string, and chars
can be completely removed if input is an array of characters.
add a comment |Â
up vote
0
down vote
Python 2, 76 bytes
lambda x:sum(map(int,x))*int('1'*len(x))*reduce(int.__mul__,range(1,len(x)))
Try it online!
xnor's answer is shorter, but I thought I'd have a go at a one-liner
add a comment |Â
up vote
0
down vote
J, 18 bytes
1#.(A.~i.@!@#)&.":
Try it online!
Alternative:
Using the formula suggested by Peter Taylor:
J, 30 bytes
1#.("."0*9%~!@<:@#*_1+10^#)@":
Try it online!
add a comment |Â
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Perl 6, 30 bytes
*.comb.permutations>>.join.sum
Try it online!
It would be nice if this was just *.permutations.sum
but Perl 6 doesn't treat strings as lists of characters.
Explanation
*.comb.permutations>>.join.sum
*.comb # Convert to list of digits
.permutations # Get all permutations of the list
>>.join # Join all lists of digits
.sum # Get the sum of all numbers
Nice job! I look forward to receiving more responses to this question! Since I am not familiar with Perl, the explanation was very useful!
– MilkyWay90
5 hours ago
add a comment |Â
up vote
1
down vote
Perl 6, 30 bytes
*.comb.permutations>>.join.sum
Try it online!
It would be nice if this was just *.permutations.sum
but Perl 6 doesn't treat strings as lists of characters.
Explanation
*.comb.permutations>>.join.sum
*.comb # Convert to list of digits
.permutations # Get all permutations of the list
>>.join # Join all lists of digits
.sum # Get the sum of all numbers
Nice job! I look forward to receiving more responses to this question! Since I am not familiar with Perl, the explanation was very useful!
– MilkyWay90
5 hours ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Perl 6, 30 bytes
*.comb.permutations>>.join.sum
Try it online!
It would be nice if this was just *.permutations.sum
but Perl 6 doesn't treat strings as lists of characters.
Explanation
*.comb.permutations>>.join.sum
*.comb # Convert to list of digits
.permutations # Get all permutations of the list
>>.join # Join all lists of digits
.sum # Get the sum of all numbers
Perl 6, 30 bytes
*.comb.permutations>>.join.sum
Try it online!
It would be nice if this was just *.permutations.sum
but Perl 6 doesn't treat strings as lists of characters.
Explanation
*.comb.permutations>>.join.sum
*.comb # Convert to list of digits
.permutations # Get all permutations of the list
>>.join # Join all lists of digits
.sum # Get the sum of all numbers
answered 5 hours ago
Jo King
17.5k24197
17.5k24197
Nice job! I look forward to receiving more responses to this question! Since I am not familiar with Perl, the explanation was very useful!
– MilkyWay90
5 hours ago
add a comment |Â
Nice job! I look forward to receiving more responses to this question! Since I am not familiar with Perl, the explanation was very useful!
– MilkyWay90
5 hours ago
Nice job! I look forward to receiving more responses to this question! Since I am not familiar with Perl, the explanation was very useful!
– MilkyWay90
5 hours ago
Nice job! I look forward to receiving more responses to this question! Since I am not familiar with Perl, the explanation was very useful!
– MilkyWay90
5 hours ago
add a comment |Â
up vote
1
down vote
JavaScript (ES6), 61 bytes
n=>[...n+(x=s=p='')].map((d,i)=>(p+=1,x=x*i||1,s-=d))&&-s*p*x
Try it online!
How?
This is based on the formula suggested by Peter Taylor in the sandbox:
$$left(sum_i=1^na_iright)frac10^n-19(n-1)!$$
where $a_i$ is the $i$th digit of the input number and $n$ is the total number of digits.
The result of the expression $(10^n-1)/9$ is a number consisting of the digit $1$ repeated $n$ times, which is what is computed in $p$. The factorial is stored in $x$ and the opposite of the sum is stored in $s$.
add a comment |Â
up vote
1
down vote
JavaScript (ES6), 61 bytes
n=>[...n+(x=s=p='')].map((d,i)=>(p+=1,x=x*i||1,s-=d))&&-s*p*x
Try it online!
How?
This is based on the formula suggested by Peter Taylor in the sandbox:
$$left(sum_i=1^na_iright)frac10^n-19(n-1)!$$
where $a_i$ is the $i$th digit of the input number and $n$ is the total number of digits.
The result of the expression $(10^n-1)/9$ is a number consisting of the digit $1$ repeated $n$ times, which is what is computed in $p$. The factorial is stored in $x$ and the opposite of the sum is stored in $s$.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
JavaScript (ES6), 61 bytes
n=>[...n+(x=s=p='')].map((d,i)=>(p+=1,x=x*i||1,s-=d))&&-s*p*x
Try it online!
How?
This is based on the formula suggested by Peter Taylor in the sandbox:
$$left(sum_i=1^na_iright)frac10^n-19(n-1)!$$
where $a_i$ is the $i$th digit of the input number and $n$ is the total number of digits.
The result of the expression $(10^n-1)/9$ is a number consisting of the digit $1$ repeated $n$ times, which is what is computed in $p$. The factorial is stored in $x$ and the opposite of the sum is stored in $s$.
JavaScript (ES6), 61 bytes
n=>[...n+(x=s=p='')].map((d,i)=>(p+=1,x=x*i||1,s-=d))&&-s*p*x
Try it online!
How?
This is based on the formula suggested by Peter Taylor in the sandbox:
$$left(sum_i=1^na_iright)frac10^n-19(n-1)!$$
where $a_i$ is the $i$th digit of the input number and $n$ is the total number of digits.
The result of the expression $(10^n-1)/9$ is a number consisting of the digit $1$ repeated $n$ times, which is what is computed in $p$. The factorial is stored in $x$ and the opposite of the sum is stored in $s$.
answered 3 hours ago


Arnauld
66.4k583280
66.4k583280
add a comment |Â
add a comment |Â
up vote
1
down vote
Python 2, 66 bytes
n=s=0
P=1
for c in input():s+=int(c);n+=1;P*=n
print 10**n/9*s*P/n
Try it online!
Takes input as a string.
Python 2, 70 bytes
f=lambda k,P=1,n=0,s=0:f(k/10,P*-~n,n+1,s+k%10)if k else 10**n/9*s*P/n
Try it online!
An arithmetic method. The base case is hard to deal with because the /n
causes division by zero for the inital value n=0
.
add a comment |Â
up vote
1
down vote
Python 2, 66 bytes
n=s=0
P=1
for c in input():s+=int(c);n+=1;P*=n
print 10**n/9*s*P/n
Try it online!
Takes input as a string.
Python 2, 70 bytes
f=lambda k,P=1,n=0,s=0:f(k/10,P*-~n,n+1,s+k%10)if k else 10**n/9*s*P/n
Try it online!
An arithmetic method. The base case is hard to deal with because the /n
causes division by zero for the inital value n=0
.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Python 2, 66 bytes
n=s=0
P=1
for c in input():s+=int(c);n+=1;P*=n
print 10**n/9*s*P/n
Try it online!
Takes input as a string.
Python 2, 70 bytes
f=lambda k,P=1,n=0,s=0:f(k/10,P*-~n,n+1,s+k%10)if k else 10**n/9*s*P/n
Try it online!
An arithmetic method. The base case is hard to deal with because the /n
causes division by zero for the inital value n=0
.
Python 2, 66 bytes
n=s=0
P=1
for c in input():s+=int(c);n+=1;P*=n
print 10**n/9*s*P/n
Try it online!
Takes input as a string.
Python 2, 70 bytes
f=lambda k,P=1,n=0,s=0:f(k/10,P*-~n,n+1,s+k%10)if k else 10**n/9*s*P/n
Try it online!
An arithmetic method. The base case is hard to deal with because the /n
causes division by zero for the inital value n=0
.
answered 2 hours ago


xnor
87.9k17182433
87.9k17182433
add a comment |Â
add a comment |Â
up vote
0
down vote
Python 2, 76 bytes
s=map(int,`input()`);n=len(s);t=sum(s)*int('1'*n)
while~-n:n-=1;t*=n
print t
Try it online!
add a comment |Â
up vote
0
down vote
Python 2, 76 bytes
s=map(int,`input()`);n=len(s);t=sum(s)*int('1'*n)
while~-n:n-=1;t*=n
print t
Try it online!
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Python 2, 76 bytes
s=map(int,`input()`);n=len(s);t=sum(s)*int('1'*n)
while~-n:n-=1;t*=n
print t
Try it online!
Python 2, 76 bytes
s=map(int,`input()`);n=len(s);t=sum(s)*int('1'*n)
while~-n:n-=1;t*=n
print t
Try it online!
answered 4 hours ago
Chas Brown
4,4561419
4,4561419
add a comment |Â
add a comment |Â
up vote
0
down vote
C (clang), 86 + -lm
= 89 bytes
f(a,s,n)for(s=n=0;a;s+=a%10,a/=10,n++);a=tgamma(n)*s*(pow(10,n)-1)/9;printf("%d",a);
Try it online!
add a comment |Â
up vote
0
down vote
C (clang), 86 + -lm
= 89 bytes
f(a,s,n)for(s=n=0;a;s+=a%10,a/=10,n++);a=tgamma(n)*s*(pow(10,n)-1)/9;printf("%d",a);
Try it online!
add a comment |Â
up vote
0
down vote
up vote
0
down vote
C (clang), 86 + -lm
= 89 bytes
f(a,s,n)for(s=n=0;a;s+=a%10,a/=10,n++);a=tgamma(n)*s*(pow(10,n)-1)/9;printf("%d",a);
Try it online!
C (clang), 86 + -lm
= 89 bytes
f(a,s,n)for(s=n=0;a;s+=a%10,a/=10,n++);a=tgamma(n)*s*(pow(10,n)-1)/9;printf("%d",a);
Try it online!
answered 3 hours ago
Logern
39115
39115
add a comment |Â
add a comment |Â
up vote
0
down vote
CJam, 10 bytes
Abm!:s1b
Try it online!
add a comment |Â
up vote
0
down vote
CJam, 10 bytes
Abm!:s1b
Try it online!
add a comment |Â
up vote
0
down vote
up vote
0
down vote
CJam, 10 bytes
Abm!:s1b
Try it online!
CJam, 10 bytes
Abm!:s1b
Try it online!
answered 53 mins ago


Esolanging Fruit
8,11932373
8,11932373
add a comment |Â
add a comment |Â
up vote
0
down vote
Ruby -nl
, 41 bytes
p$_.chars.permutation.sumx
Try it online!
Full program taking input from stdin.
Ruby, 45 bytes
->nn.digits.permutation.sumx
Try it online!
Function taking input as integer. digits
can be shortened to chars
if input is acceptable as string, and chars
can be completely removed if input is an array of characters.
add a comment |Â
up vote
0
down vote
Ruby -nl
, 41 bytes
p$_.chars.permutation.sumx
Try it online!
Full program taking input from stdin.
Ruby, 45 bytes
->nn.digits.permutation.sumx
Try it online!
Function taking input as integer. digits
can be shortened to chars
if input is acceptable as string, and chars
can be completely removed if input is an array of characters.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Ruby -nl
, 41 bytes
p$_.chars.permutation.sumx
Try it online!
Full program taking input from stdin.
Ruby, 45 bytes
->nn.digits.permutation.sumx
Try it online!
Function taking input as integer. digits
can be shortened to chars
if input is acceptable as string, and chars
can be completely removed if input is an array of characters.
Ruby -nl
, 41 bytes
p$_.chars.permutation.sumx
Try it online!
Full program taking input from stdin.
Ruby, 45 bytes
->nn.digits.permutation.sumx
Try it online!
Function taking input as integer. digits
can be shortened to chars
if input is acceptable as string, and chars
can be completely removed if input is an array of characters.
edited 33 mins ago
answered 44 mins ago
Kirill L.
2,9261117
2,9261117
add a comment |Â
add a comment |Â
up vote
0
down vote
Python 2, 76 bytes
lambda x:sum(map(int,x))*int('1'*len(x))*reduce(int.__mul__,range(1,len(x)))
Try it online!
xnor's answer is shorter, but I thought I'd have a go at a one-liner
add a comment |Â
up vote
0
down vote
Python 2, 76 bytes
lambda x:sum(map(int,x))*int('1'*len(x))*reduce(int.__mul__,range(1,len(x)))
Try it online!
xnor's answer is shorter, but I thought I'd have a go at a one-liner
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Python 2, 76 bytes
lambda x:sum(map(int,x))*int('1'*len(x))*reduce(int.__mul__,range(1,len(x)))
Try it online!
xnor's answer is shorter, but I thought I'd have a go at a one-liner
Python 2, 76 bytes
lambda x:sum(map(int,x))*int('1'*len(x))*reduce(int.__mul__,range(1,len(x)))
Try it online!
xnor's answer is shorter, but I thought I'd have a go at a one-liner
answered 31 mins ago
Jo King
17.5k24197
17.5k24197
add a comment |Â
add a comment |Â
up vote
0
down vote
J, 18 bytes
1#.(A.~i.@!@#)&.":
Try it online!
Alternative:
Using the formula suggested by Peter Taylor:
J, 30 bytes
1#.("."0*9%~!@<:@#*_1+10^#)@":
Try it online!
add a comment |Â
up vote
0
down vote
J, 18 bytes
1#.(A.~i.@!@#)&.":
Try it online!
Alternative:
Using the formula suggested by Peter Taylor:
J, 30 bytes
1#.("."0*9%~!@<:@#*_1+10^#)@":
Try it online!
add a comment |Â
up vote
0
down vote
up vote
0
down vote
J, 18 bytes
1#.(A.~i.@!@#)&.":
Try it online!
Alternative:
Using the formula suggested by Peter Taylor:
J, 30 bytes
1#.("."0*9%~!@<:@#*_1+10^#)@":
Try it online!
J, 18 bytes
1#.(A.~i.@!@#)&.":
Try it online!
Alternative:
Using the formula suggested by Peter Taylor:
J, 30 bytes
1#.("."0*9%~!@<:@#*_1+10^#)@":
Try it online!
edited 2 mins ago
answered 17 mins ago
Galen Ivanov
5,3021931
5,3021931
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f174335%2fadd-all-permutations-of-a-number%23new-answer', 'question_page');
);
Post as a guest
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
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
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
1
Note: Peter Taylor pointed out a simplification of the challenge here. If you are reading this, you may want to think about it first to see if you can get the same (or shorter) solution.
– user202729
4 hours ago