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.

The input is positive, and the input can be taken as any type, but your program's output must be a number.

Standard loopholes are not allowed, and as usual, since this is code golf, the shortest answer in bytes wins!

  • 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 Oct 20 at 3:16

21 Answers 21

up vote 7 down vote accepted

Japt -x, 1 byte

Takes input as a string.


Try it


á     :Get permutations
      :Implicitly reduce by addition and output

Python 2, 66 bytes

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.

05AB1E, 2 bytes


Try it online!

  • What is the O? I know œ means permutations, but what does O mean? If you answer this, and the program follows the rules, I will accept this answer, because it has the smallest number of bytes. – MilkyWay90 Oct 20 at 14:33
  • 3
    @MilkyWay90 If œ is permutations, O ought to be sum. It's customary to wait at least a week before accepting an answer. – Dennis Oct 20 at 15:28

Perl 6, 30 bytes


Try it online!

It would be nice if this was just *.permutations.sum but Perl 6 doesn't treat strings as lists of characters.


*.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 Oct 20 at 2:03
  • 1
    @MilkyWay90 The name of the language is “Perl 6”. It is a sister language to “Perl 5”. Normally when people say “Perl” they mean “Perl 5”. – Brad Gilbert b2gills Oct 20 at 20:16

CJam, 10 bytes


Try it online!

R, 59 57 bytes


Try it online!

Mathematical approach suggested by Peter Taylor and decribed by Arnauld. -2 bytes by J. Doe.

  • Trivial inline golf for 57 – J.Doe Oct 20 at 15:33

J, 18 bytes


Try it online!


                ":  - convert to string
              &.    - do the verbs in () then convert back to number
       i.           - make a list from 0 to
         @!         - factorial 
           @#       - of the number of digits in the input        
    A.~             - find all permutations, using the list above as permutation index
1#.                 - find the sum by base-1 convertion


Using the formula suggested by Peter Taylor:

J, 30 bytes


Try it online!

JavaScript (ES6), 53 bytes

Recursive version, inspired by @tsh.


Try it online!

JavaScript (ES6), 61 60 bytes

Saved 1 byte thanks to @l4m2


Try it online!


This is based on the formula suggested by Peter Taylor in the sandbox:


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

Python 2, 63 62 bytes

f=lambda n,k=1,s=0:n>9and k*f(n/10,k+1,s+n%10)or 10**k/9*(s+n)

Try it online!

Haskell, 62 bytes

import Data.List

Unfortunately, f must be annotated with a type to avoid ambiguity.

  • 1
    Welcome to PPCG and Haskell golfing in particular! Your code works fine on TIO without the type annotations: Try it online!. Also anonymous functions are allowed, so you can drop the f=. – Laikoni Oct 21 at 0:59

MY, 12 bytes


Try it online!

Some of MY's stupid builtin decisions are coming in handy.

⍞             Input
  ℘                's permutations
   ⌊                                as integers.
    Σ                                           Sum of that
     88ǵ'ƒ⇥(                                               to standard base 10 form
             ↵                                                                      output.

Python 2, 76 bytes

print t

Try it online!

Ruby -nl, 41 bytes


Try it online!

Full program taking input from stdin.

Ruby, 45 bytes


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.

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

Jelly, 5 bytes


Try it online!

Charcoal, 12 bytes


Try it online! Link is to verbose version of code. Explanation: Based on Peter Taylor's formula. ΠEθμ doesn't quite calculate \$ (n - 1)! \$ as Charcoal's indices range from \$ 0 \$ to \$ n - 1 \$ so is used to replace the \$ 0 \$ with the sum of digits of the input, thus implicitly multiplying the two. The outer mapping then creates an array of the length of the original input with that product as elements which is then passed to base 10 conversion which effectively multiplies the product by the appropriate repunit.

    θ           Input
   E            Map over characters
       θ        Input
      E         Map over characters
         μ      Current index
        ∨       Logical Or
           θ    Input
          Σ     Digital sum
     Π          Product
  χ             Predefined variable 10
 ↨              Base conversion
I               Cast to string
                Implicitly print

C (gcc), 89 71 bytes


Try it online!

-18 bytes thanks to @nwellnhof

APL(NARS), 25 chars, 50 bytes

{+/10⊥⍉⍎¨w[110 1‼↑⍴w←⍕⍵]}


  f←{+/10⊥⍉⍎¨w[110 1‼↑⍴w←⍕⍵]}
  f¨202 1024 505
888 46662 2220 

110 1‼k would return as built in all the permutations of 1..k .

Scala (74 bytes)


Try it online

Since permutation on digits will drop the repeated digits, we have to permute on indices.

Pyth, 5 4 bytes


Try it online! Takes input as a string.


sv.pQ - Full program. Implicit Q added.

    Q - Input
  .p  - All permutations of it
 v    - Evaluate each (list of strings into integers)
s     - Sum the list

Pyt, 4 bytes


Try it online!


      Implicit input
ǰ     Join (becomes string)
 ᒆ    All permutations of it
  Ɩ   List of strings to int
   Ʃ  Sum the array

Your Answer


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

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