Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

List all the possible combinations of three numbers from a given sample.

Posted on 2013-02-04
9
566 Views
Last Modified: 2013-03-20
I have to work out and write down all the possible combinations of three numbers can be extracted (without repetition) from 80.

as an illustration:

there is only one group of three numbers in 3 (1,2,3)
there are four groups of three numbers in 4 (1,2,3  1,2,4  1,3,4  2,3,4)
there are 10 groups of three numbers in 6

I have for the calculation =COMBIN(80, 3) for excel which gives me 18,424 possibilities, so I know how many combinations are possible

I need code that will list all them written out in a spreadsheet in an list probably in a text document

I would be very grateful for help
0
Comment
Question by:MichaelGlancy
9 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 38852456
This is a duplicate of http:Q_28019535.html
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 38852610
Simple to do...

my @arr = #list of 80 numbers - either entered directly in code or read from file
open OUT, ">output.csv" or die "could not write output: $!";
for my $i (0..79) {
    for my $j (($i+1)..79) {
        for my $k (($j+1)..79) {
            print OUT "$i,$j,$k\n";
        }
    }
}
close OUT;

Open in new window

0
 

Author Comment

by:MichaelGlancy
ID: 38853082
TommySzalapski

it is a duplicate because I was advised to try Perl, so I reposted from excel to Perl
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 12

Expert Comment

by:tel2
ID: 38853119
Hi Michael,

wilcoxon's code looks good to me, except:
- I don't see the point of line 1, since @arr is not referenced elsewhere.
- From your examples, it looks as if you want numbers to be 1 to 80, not 0 to 79.

Here's the code with those minor adjustments:
#!/usr/bin/perl

open OUT, ">output.csv" or die "Could not write output: $!";
for my $i (1..80) {
    for my $j (($i+1)..80) {
        for my $k (($j+1)..80) {
            print OUT "$i,$j,$k\n";
        }
    }
}
close OUT;

Open in new window

Both scripts produce 82,160 rows of output, which is what =COMBIN(80, 3) gives me when I run it in Excel 2003.
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 38853534
Good point - typo in my code.  It says "from 80 numbers" so I'm assuming it is not simply the numbers 1-80 (if it is simply 1-80 then tel2's changes to my code will work).

my @arr = #list of 80 numbers - either entered directly in code or read from file
open OUT, ">output.csv" or die "could not write output: $!";
for my $i (0..79) {
    for my $j (($i+1)..79) {
        for my $k (($j+1)..79) {
            print OUT "$arr[$i],$arr[$j],$arr[$k]\n";
        }
    }
}
close OUT;

Open in new window

0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 38855081
it is a duplicate because I was advised to try Perl, so I reposted from excel to Perl
What you could do next time is just hit the 'Request Attention' button and ask the Admins to add the Perl zone to the old question. That will generate emails to all the Perl experts asking them to check out your question.

I still don't see why you need something other than Excel for this though for 82k possibilities. Try doing it with the VBA code I posted (it's very similar to the Perl code here). It should work.
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 500 total points
ID: 38856458
There are a number of modules on cpan that does this for you very easily.

Unless I'm misunderstanding what you want, I think you'll end up with more combinations than what you think.

Here's my test script.
#!/usr/bin/perl

use strict;
use warnings;
use Math::Combinatorics;

my @set = 1..6;
my $combinat = Math::Combinatorics->new(count => 3, data => [@set]);

my $cnt;
while(my @permu = $combinat->next_combination){
    print join(' ', @permu)."\n";
    $cnt++;
}

print "\n$cnt\n";

Open in new window


Output of:
c:\testing>permutations.pl
6 5 4
6 5 3
6 5 2
6 5 1
6 4 3
6 4 2
6 4 1
6 3 2
6 3 1
6 2 1
5 4 3
5 4 2
5 4 1
5 3 2
5 3 1
5 2 1
4 3 2
4 3 1
4 2 1
3 2 1

20
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 38871655
In Java or C++ like syntax it is, (this is JavaScript)

<script>

n=5
for (i=1;i<=n;i++)
 for (j=i+1;j<=n;j++)
   for (k=j+1;k<=n;k++)
     document.write(i +' '+ j+ ' '+ k +"<br>")

</script>

Open in new window

You can run this by copying and pasting into Notepad, saving as a .htm and opening in a browser.

Test n=5
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5
0
 

Expert Comment

by:Danean
ID: 39005096
I like the Java version where you can open the list in a browser.  Is there also a way to send the output list to a txt file with the java script?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Probability Calculation 2 68
algorithm 15 116
Math homework question 5 98
can any US household electrical device/equipment be made to work in asia.. 9 49
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Article by: Nicole
This is a research brief on the potential colonization of humans on Mars.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question