You have N pennies. Write a Matlab script that will reveal how many different ways you can break up those pennies. For example, with 5 pennies, there are seven different ways you can divide them:
The order of the coins does not matter, so (2,2,1) is considered the same combination as (2,1,2). You can assume that N will always be a positive integer.
Test case p(300)=9253082936723604 exceeds the maximum integer that can be represented exactly as a double precision number (namely, 2^53=9007199254740992); the fact that some solutions work for this case is just luck.
Changed to p(299), which is <2^53. Thanks for the heads up.
Interestingly, p(299) had some issues as well. Changed again to p(199), which seems to be working OK.
Test 2: function y = partitions(x) works!
Test 1: function y = partitions(n) fails
isn't it suppose to be partitions(x)?
But this is just function definition.
Either partitions(x) or partitions(n) should not matter in the function definition.
Strange. The problem you were having had something to do with the error checking suite that was above the solutions. Your solution passed the error checks, but failed to actually work. Now that I have removed the error checks, your solution with partitions(n) passes. I may have to muck around with this a bit later on.
The problem was with the following line in the error checking:
st = sprintf('function y = partitions(x)\n%%%s', st);
This automatically changed the first line of your script to use partitions(x) instead of partitions(n). That was done to prevent folks from using the "function ans=blahblahblah(x)" technique when that still worked, That was just a holdover that happened to trip you up. Now that that line (and the rest of the suite0 has been removed, partitions(n) should work just fine.
@James oops! I was so careless that I didn't notice that single-line error checking (I thought your check was intended to forbid the usage of functions such as eval, str2num, etc, and thus I completely ignored that when I was solving the problem). Now I realized this is not a strange behavior or bug of Cody. Thanks for your clarification.
Project Euler: Problem 1, Multiples of 3 and 5
1063 Solvers
Back to basics 6 - Column Vector
809 Solvers
Back to basics 9 - Indexed References
348 Solvers
160 Solvers
413 Solvers