11
\$\begingroup\$

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!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – DELETE_ME
    Oct 20, 2018 at 3:16

27 Answers 27

10
\$\begingroup\$

Japt -x, 1 byte

Takes input as a string.

á

Try it


Explanation

á     :Get permutations
      :Implicitly reduce by addition and output
\$\endgroup\$
2
  • \$\begingroup\$ Doesn't the -x count as an extra byte? \$\endgroup\$
    – Adalynn
    Oct 21, 2018 at 2:12
  • 3
    \$\begingroup\$ @Zacharý No, here's the meta. \$\endgroup\$
    – u-ndefined
    Oct 21, 2018 at 2:15
7
\$\begingroup\$

CJam, 10 6 bytes

{m!1b}

Try it online!

Explanation

{    }  Anonymous block taking input as string on stack
 m!     Find all permutations
   1b   Implicitly convert each to integer and sum
\$\endgroup\$
5
  • \$\begingroup\$ Can you add an explanation so I can decide whether to upvote or not? \$\endgroup\$
    – MilkyWay90
    Mar 23, 2019 at 3:19
  • \$\begingroup\$ @MilkyWay90 Added. \$\endgroup\$ Mar 23, 2019 at 3:23
  • \$\begingroup\$ Okay! This seems like a trivial answer, so I will refrain from upvoting. However, still a great answer! Also, are CJam builtins always 2 bytes or is it just a few? \$\endgroup\$
    – MilkyWay90
    Mar 23, 2019 at 3:26
  • 1
    \$\begingroup\$ @MilkyWay90 Most builtins in CJam are one byte, but there are two groups of two-byte builtins (starting with e and m) for "extra" operations and "math" operations respectively. 1b is actually two different operations: 1 pushes the number 1, and b performs base conversion. 1b means "convert from base 1" but it has the effect of summing an array because b doesn't check that the array elements are actually valid digits. Normally one would take the sum with :+ (reduce with addition), but 1b has the advantage that it works on empty arrays and implicitly converts to integer. \$\endgroup\$ Mar 23, 2019 at 3:37
  • \$\begingroup\$ Ah, I see. That does seem to be worth an upvote \$\endgroup\$
    – MilkyWay90
    Mar 23, 2019 at 3:41
6
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

05AB1E, 2 bytes

œO

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$
    – MilkyWay90
    Oct 20, 2018 at 14:33
  • 4
    \$\begingroup\$ @MilkyWay90 If œ is permutations, O ought to be sum. It's customary to wait at least a week before accepting an answer. \$\endgroup\$
    – Dennis
    Oct 20, 2018 at 15:28
4
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ Nice job! I look forward to receiving more responses to this question! Since I am not familiar with Perl, the explanation was very useful! \$\endgroup\$
    – MilkyWay90
    Oct 20, 2018 at 2:03
  • 1
    \$\begingroup\$ @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”. \$\endgroup\$ Oct 20, 2018 at 20:16
3
\$\begingroup\$

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!

\$\endgroup\$
3
\$\begingroup\$

R, 59 57 bytes

sum(gamma(n<-nchar(x<-scan()))*x%/%10^(0:n)%%10)*10^n%/%9

Try it online!

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

\$\endgroup\$
1
  • \$\begingroup\$ Trivial inline golf for 57 \$\endgroup\$
    – J.Doe
    Oct 20, 2018 at 15:33
3
\$\begingroup\$

J-uby, 35 bytes

:digits|:permutation|:sum+(:join|Z)

Try it online!

-3 from Jordan.

\$\endgroup\$
1
  • \$\begingroup\$ You can replace :to_i with Z for -4 bytes. \$\endgroup\$
    – Jordan
    Jul 4, 2023 at 14:56
2
\$\begingroup\$

J, 18 bytes

1#.(A.~i.@!@#)&.":

Try it online!

Explanation:

                ":  - 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

Alternative:

Using the formula suggested by Peter Taylor:

J, 30 bytes

1#.("."0*9%~!@<:@#*_1+10^#)@":

Try it online!

\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), 53 bytes

Recursive version, inspired by @tsh.

f=(n,s=i=0,p='')=>n?(i++||1)*f(n/10|0,s+n%10,p+1):s*p

Try it online!


JavaScript (ES6), 61 60 bytes

Saved 1 byte thanks to @l4m2

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}^n{a_i}\right)\frac{10^n-1}{9}(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\$.

\$\endgroup\$
2
2
\$\begingroup\$

Haskell, 62 bytes

import Data.List
f::Int->Int
f=sum.map read.permutations.show

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

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 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=. \$\endgroup\$
    – Laikoni
    Oct 21, 2018 at 0:59
2
\$\begingroup\$

MY, 12 bytes

⍞℘⌊Σ88ǵ'ƒ⇥(↵

Try it online!

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

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

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!

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

Ruby -nl, 41 bytes

p$_.chars.permutation.sum{|x|x.join.to_i}

Try it online!

Full program taking input from stdin.

Ruby, 45 bytes

->n{n.digits.permutation.sum{|x|x.join.to_i}}

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.

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

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

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

Jelly, 5 bytes

DŒ!ḌS

Try it online!

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

Charcoal, 12 bytes

I↨χEθΠEθ∨μΣθ

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
\$\endgroup\$
1
\$\begingroup\$

C (gcc), 89 71 bytes

g(x,s,p,f,i){for(s=x%10,p=f=i=1;x/=10;s+=x%10)p=p*10+1,f*=i++;x=s*p*f;}

Try it online!

-18 bytes thanks to @nwellnhof

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

APL(NARS), 25 chars, 50 bytes

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

test:

  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 .

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

Scala (74 bytes)

def%(i:Int)=s"$i".indices.permutations.map(_.map(i+"").mkString.toInt).sum

Try it online

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

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

Pyth, 5 4 bytes

sv.p

Try it online! Takes input as a string.

Explanation:

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
\$\endgroup\$
1
\$\begingroup\$

Pyt, 4 bytes

ǰᒆƖƩ

Try it online!

Explanation:

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

Vyxal Ṡs, 3 bytes

ṖvI

Try it Online!

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

Brachylog, 3 bytes

pᶠ+

Try it online!

Explanation

 ᶠ     Find all:
p          permutations
  +    Sum
\$\endgroup\$
1
\$\begingroup\$

Husk, 4 bytes

ṁdPd

Try it online!

Explanation

ṁdPd
   d  digits of input
  P   get all permutations
ṁ     (flat/concat/sum)-map
 d    interpret as digits
\$\endgroup\$
0
\$\begingroup\$

Wolfram Language(Mathematica), 111 bytes

Try it online!

Total[FromDigits/@(Permutations[#]/.Thread[#->IntegerDigits@#2])]&[Unique[]&/@Range@Length@IntegerDigits@#, #]&






Appendix:

Don't allow duplicate(i.e. Multinomial Permutation) \$\quad \$ (50 bytes). Try it online!

Total[FromDigits/@Permutations[IntegerDigits[#]]]&
\$\endgroup\$
0
\$\begingroup\$

Racket -- 93 bytes

#!racket
(for/sum([l(permutations(string->list(~a(read))))])(string->number(apply string l)))

Try it online!


Explanation

User input is a number. We split the number into its individual digits and then we permutate the list of digits. We loop through all the permutations and rejoin them to form a number. Once we have iterated through the entire list, we return the sum of resulting numbers of the permutations.

#lang racket

(for/sum ([lst (permutations (string->list (~a (read))))])
  (string->number (apply string lst)))

Let's do an example with input 50.

  1. Split input into separate digits: '(5 0)
  2. Obtain all permutations of digits: '((5 0) (0 5))
  3. Convert all permutations back into numbers: '(50 05) or '(50 5)
  4. Once all permutations are numbers, add the numbers together: (+ 50 5)
  5. Return the sum: 55
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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