# Sum of combinations with repetition

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.

• Welcome to PPCG! Unfortunately, here we don't answer general programming questions. However, you may be able to get help on Stack Overflow. Just be sure to check their help center out before asking. :) – Erik the Outgolfer Feb 16 '18 at 17:23
• Can someone reword this into a challenge? Because this would be a fun one. – Magic Octopus Urn Feb 16 '18 at 18:44
• @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 Feb 16 '18 at 19:04
• I 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 Feb 16 '18 at 19:38
• This is oeis.org/A079973 – Ton Hospel Feb 16 '18 at 20:22

# 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!

• 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 Feb 16 '18 at 20:13
• @xnor thanks a lot. I just tried it the other way round – ovs Feb 16 '18 at 20:33

# 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)


# 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.

# 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.

• Speed it up by replacing ³ with H (then it will time out at 12 rather than 6) – Jonathan Allan Feb 16 '18 at 23:03
• @JonathanAllan Done, thanks. – Mr. Xcoder Feb 17 '18 at 6:19

# 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  # C, 41 bytes G(x){return x>0?G(x-2)+G(x-3)+G(x-5):!x;}  Try it online! # 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)) # R, 5649 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! • 48 bytes – Giuseppe Feb 19 '18 at 16:46 • @Giuseppe Very nice improvement! – rturnbull Feb 20 '18 at 8:49 • ah, you don't need the 0 (I didn't consider that before), as unary + will coerce to numeric as well. – Giuseppe Feb 20 '18 at 17:37 # 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  # 05AB1E, 10 bytes 30fIиæÙOI¢  Try it online! # 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). # 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! # Jelly, 21 bytes Œṗe€2,3,5$Ạ$ÐfŒ!Q$€ẎL


Try it online!

Surely can be golfed

# 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.


# Proton, 32 bytes

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


Try it online!

# 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})]


f 0=1