Sum of combinations with repetition

8

1

Write the shortest code you can solving the following problem:

Input:

An integer X with 2 <= X and X <= 100

Output:

Total combinations of 2, 3, and 5 (repetition is allowed, order matters) whose sum is equal to X.

Examples:

Input: 8

Output: 6, because the valid combinations are:

3+5
5+3
2+2+2+2
2+3+3
3+2+3
3+3+2

Input: 11

Output: 16, because the valid combinations are

5+3+3
5+2+2+2
3+5+3
3+3+5
3+3+3+2
3+3+2+3
3+2+3+3
3+2+2+2+2
2+5+2+2
2+3+3+3
2+3+2+2+2
2+2+5+2
2+2+3+2+2
2+2+2+5
2+2+2+3+2
2+2+2+2+3

Input: 100

Output: 1127972743581281, because the valid combinations are ... many

Input and output can be of any reasonable form. The lowest byte count in each language wins. Standard rules apply.

anta40

Posted 2018-02-16T17:08:32.493

Reputation: 189

1

Welcome to PPCG! Unfortunately, here we don't answer general programming questions. However, you may be able to get help on [so]. Just be sure to check their help center out before asking. :)

– Erik the Outgolfer – 2018-02-16T17:23:38.923

1Can someone reword this into a challenge? Because this would be a fun one. – Magic Octopus Urn – 2018-02-16T18:44:40.150

I was thinking the same, @MagicOctopusUrn, but then I got to wondering if it's not a dupe. – Shaggy – 2018-02-16T18:59:05.193

1@Shaggy Ugghhh... filtering through the challenges with the word sum in them was not a good idea to try to solve that inquiry... – Magic Octopus Urn – 2018-02-16T19:04:15.600

2I rewrote your question a bit to make it better fit on codegolf. I also changed the result for input 11 from 12 to 16. Of course feel free to fix this if I misunderstood your intention – Ton Hospel – 2018-02-16T19:38:20.803

@TonHospel voted for your edit. – Magic Octopus Urn – 2018-02-16T19:39:57.660

I feel like we've had a challenge like this before, to express a number as a sum from a fixed set, maybe of coins. Don't know how I'd search for a potential dupe here. Anyone remember what I might be thinking of? – xnor – 2018-02-16T19:53:51.010

2

This is https://oeis.org/A079973

– Ton Hospel – 2018-02-16T20:22:30.953

Borderline dupe of https://codegolf.stackexchange.com/q/85/194

– Peter Taylor – 2018-02-16T22:29:28.453

Answers

9

Python 2, 46 45 bytes

thanks to xnor for -1 byte

f=lambda n:n>0and f(n-2)+f(n-3)+f(n-5)or n==0

Try it online!

ovs

Posted 2018-02-16T17:08:32.493

Reputation: 21 408

Looks like and/or works and saves a byte: f=lambda n:n>0and f(n-2)+f(n-3)+f(n-5)or n==0. – xnor – 2018-02-16T20:13:01.847

@xnor thanks a lot. I just tried it the other way round

– ovs – 2018-02-16T20:33:17.903

6

Oasis, 9 bytes

cd5e++VT1

Try it online!

Explanation

        1    # a(0) = 1
       T     # a(1) = 0, a(2) = 1
      V      # a(3) = 1, a(4) = 1

             # a(n) = 
c    +       # a(n-2) +
 d  +        # a(n-3) +
  5e         # a(n-5)

Emigna

Posted 2018-02-16T17:08:32.493

Reputation: 50 798

3

Pyth, 9 bytes

/sM{y*P30

Try it here!

Pyth, 16 bytes

l{s.pMfqT@P30T./

Try it here

How?

  1. Generates the prime factors of 30, namely [2, 3, 5], gets the powerset of it repeated N times, removes duplicate elements, sums each list and counts the occurrences of N in that.

  2. For each integer parition p, it checks whether p equals p ∩ primefac(30). It only keeps those that satisfy this condition, and for each remaining partition k, it gets the list of k's permutations, flattens the resulting list by 1 level, deduplicates it and retrieves the length.

Mr. Xcoder

Posted 2018-02-16T17:08:32.493

Reputation: 39 774

3

Jelly, 11 bytes

5ÆRẋHŒPQḅ1ċ

Try it online!

How it works

5ÆRẋHŒPQḅ1ċ -> Full program. Argument: N, an integer.
5ÆR         -> Pushes all the primes between 2 and 5, inclusively.
   ẋH       -> Repeat this list N / 2 times.
     ŒP     -> Generate the powerset.
       Q    -> Remove duplicate entries.
        ḅ1  -> Convert each from unary (i.e. sum each list)
          ċ -> Count the occurrences of N into this list.

Mr. Xcoder

Posted 2018-02-16T17:08:32.493

Reputation: 39 774

Speed it up by replacing ³ with H (then it will time out at 12 rather than 6) – Jonathan Allan – 2018-02-16T23:03:32.760

@JonathanAllan Done, thanks. – Mr. Xcoder – 2018-02-17T06:19:26.470

2

JavaScript (ES6), 32 bytes

Same algorithm as in ovs' Python answer.

f=n=>n>0?f(n-2)+f(n-3)+f(n-5):!n

Test cases

f=n=>n>0?f(n-2)+f(n-3)+f(n-5):!n

console.log(f(8))
console.log(f(11))
console.log(f(13))

Arnauld

Posted 2018-02-16T17:08:32.493

Reputation: 111 334

2

Perl, 38 bytes

Includes +1 for p

perl -pE '$_=1x$_;/^(...?|.{5})+$(?{$\++})\1/}{' <<< 11; echo

Interesting enough I have to use \1 to force backtracking. Usually I use ^ but the regex optimizer seems too smart for that and gives too low results. I'll probably have to start giving perl version numbers when using this trick since the optimizer can change at every version. This was tested on perl 5.26.1

This 49 is efficient and can actually handle X=100 (but overflows on X=1991)

perl -pe '$\=$F[@F]=$F[-2]+$F[-3]+$F[-5]for($F[5]=1)..$_}{' <<< 100;echo

Ton Hospel

Posted 2018-02-16T17:08:32.493

Reputation: 14 114

2

C, 41 bytes

G(x){return x>0?G(x-2)+G(x-3)+G(x-5):!x;}

Try it online!

0xrgb

Posted 2018-02-16T17:08:32.493

Reputation: 109

2

R, 56 49 47 bytes

Recursive approach from ovs's answer. Giuseppe shaved off those final two bytes to make it 47.

f=pryr::f(+`if`(x<5,x!=1,f(x-2)+f(x-3)+f(x-5)))

Try it online!

rturnbull

Posted 2018-02-16T17:08:32.493

Reputation: 3 689

148 bytes – Giuseppe – 2018-02-19T16:46:19.363

@Giuseppe Very nice improvement! – rturnbull – 2018-02-20T08:49:09.340

1ah, you don't need the 0 (I didn't consider that before), as unary + will coerce to numeric as well. – Giuseppe – 2018-02-20T17:37:50.097

1

MATL, 15 bytes

:"5Zq@Z^!XsG=vs

Very inefficient: required memory is exponential.

Try it online!

How it works

:"       % For each k in [1 2 ... n], where n is implicit input
  5Zq    %   Push primes up to 5, that is, [2 3 5]
  @      %   Push k
  Z^     %   Cartesian power. Gives a matrix where each row is a Cartesian k-tuple
  !Xs    %   Sum of each row
  G=     %   Compare with input, element-wise
  vs     %   Concatenate all stack contents vertically and sum
         % Implicit end. Implicit display

Luis Mendo

Posted 2018-02-16T17:08:32.493

Reputation: 87 464

1

05AB1E, 10 bytes

30fIиæÙOI¢

Try it online!

Mr. Xcoder

Posted 2018-02-16T17:08:32.493

Reputation: 39 774

1

Pari/GP, 36 bytes

f(n)=if(n>0,f(n-2)+f(n-3)+f(n-5),!n)

Try it online!


Longer, but more efficient:

Pari/GP, 37 bytes

n->Vec(1/(1-x^2-x^3-x^5)+O(x^n++))[n]

Try it online!

alephalpha

Posted 2018-02-16T17:08:32.493

Reputation: 23 988

1

Ruby, 41 bytes

f=->n{n<5?n==1?0:1:[n-5,n-2,n-3].sum(&f)}

Try it online!

This is a recursive solution, the recurcive call being: [n-5,n-2,n-3].sum(&f).

MegaTom

Posted 2018-02-16T17:08:32.493

Reputation: 3 787

0

Jelly, 21 bytes

Œṗe€2,3,5$Ạ$ÐfŒ!Q$€ẎL

Try it online!

Surely can be golfed

HyperNeutrino

Posted 2018-02-16T17:08:32.493

Reputation: 26 575

0

Pyth, 12 bytes

l{fqQsTy*P30

This is horrendously inefficient and hits the memory limit for inputs above 5.

Try it online

Explanation

l{fqQsTy*P30
         P30   Get the prime factors of 30 [2, 3, 5].
        *   Q  Repeat them (implicit) input times.
       y       Take the power set...
  fqQsT        ... and filter the ones whose sum is the input.
l{             Count unique lists.

user48543

Posted 2018-02-16T17:08:32.493

Reputation:

0

Proton, 32 bytes

f=n=>n>0?f(n-2)+f(n-3)+f(n-5):!n

Try it online!

Same approach as ovs' answer.

Mr. Xcoder

Posted 2018-02-16T17:08:32.493

Reputation: 39 774

JS polyglot :-) – Arnauld – 2018-02-18T11:15:38.983

0

Wolfram Language (Mathematica), 43 bytes

Tr[Multinomial@@@{2,3,5}~FrobeniusSolve~#]&

Try it online!

Explanation: FrobeniusSolve computes all solutions of the unordered sum 2a + 3b + 5c = n, then Multinomial figures out how many ways we can order those sums.

Or we could just copy everyone else's solution for the same byte count:

f@1=0;f[0|2|3|4]=1;f@n_:=Tr[f/@(n-{2,3,5})]

Not a tree

Posted 2018-02-16T17:08:32.493

Reputation: 3 106

0

Haskell, 40 bytes

f 0=1
f n|n<0=0|1>0=f(n-2)+f(n-3)+f(n-5)

Try it online!

ovs

Posted 2018-02-16T17:08:32.493

Reputation: 21 408