|
On Fri, 19 Mar 2010 14:48:02 -0400, Shaun <shaunvon@iastate.edu> wrote:
> I am needing help with a problem. I posted this yesterday, but without
> my code I had so far. The problem is this: given a positive integer n,
> find all unique combination's of 3 positive integers whose product is
> n. For example, n=24, you have six triplets:
> 1 1 24
> 1 2 12
> 1 3 8
> 1 4 6
> 2 2 6
> 2 3 4
> All of these sets of numbers multiply to 24. None of them repeat.
>
> Here is the code I have so far:
>
> n = input('Please enter a positive integer: ');
>
> for i = 1:n
> for j = 1:n
> for k = 1:n
> if i*j*k == n
> disp([i,j,k]);
> end
> end
> end
> end
>
> This displays only the combination's whose product is n. I can't seem
> to figure out how to display only the unique triplets. I have tried
> using unique( ) but it does some weird stuff to my results. Could
> anybody give me some help on this? Thanks!!
>
> Shaun
If you want to use the code above, dont display the result in the loop.
Store it in an array and then use UNIQUE after you are done with all the
loops.
You might be able to work something cleaner using these functions: FACTOR,
PERMS
|