Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Trips

Subject: Trips

From: Shaun

Date: 19 Mar, 2010 18:48:02

Message: 1 of 6

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

Subject: Trips

From: us

Date: 19 Mar, 2010 19:02:05

Message: 2 of 6

"Shaun " <shaunvon@iastate.edu> wrote in message <ho0gt2$cv$1@fred.mathworks.com>...
> 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

a hint:
- look at this FEX submission by john d'errico, aka jd...

http://www.mathworks.com/matlabcentral/fileexchange/12009

us

Subject: Trips

From: Ashish Uthama

Date: 19 Mar, 2010 19:02:06

Message: 3 of 6

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

Subject: Trips

From: Roger Stafford

Date: 19 Mar, 2010 19:40:23

Message: 4 of 6

"Shaun " <shaunvon@iastate.edu> wrote in message <ho0gt2$cv$1@fred.mathworks.com>...
> 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.
> .........

  That code of yours is rather brute force I would say. Surely your for-loops can be made a little more sophisticated - something like this:

 for i = 1:n^(1/3)
  m = n/i;
  if round(m) == m
   for j = i:m^(1/2)
    k = m/j;
    if round(k) == k
     (Then i,j,k is your next triple)
     . . . . .

See if you can figure out why the n^(1/3) or i:m^(1/2). See if you can show that i, j, and k must be in ascending order. See if you can demonstrate that no valid triple is missing.

Roger Stafford

Subject: Trips

From: Bruno Luong

Date: 19 Mar, 2010 20:32:07

Message: 5 of 6

Here is one way:

% http://www.mathworks.com/matlabcentral/fileexchange/24133-set-partition
% http://www.mathworks.com/matlabcentral/fileexchange/24185

n = 1000;

p3 = partitions([ones(1,3) factor(n)],3); % FEX above
p3 = cellfun(@(c) cellfun(@prod,c), p3, 'uni', 0);
p3 = sort(cat(1,p3{:}),2);
p3 = unique(p3,'rows')

% Bruno

Subject: Trips

From: Bruno Luong

Date: 19 Mar, 2010 20:44:06

Message: 6 of 6

I should add using either

p3 = partitions([ones(1,3) factor(n)],3);
% OR
p3 = SetPartition([ones(1,3) factor(n)],3);

depending on the FEX file is from MF or BL

Bruno

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us