1

I want to generate all possible sequence for 49 6 lottery

So I need number sequence for 6 ball drawn from group of 1 to 49.

Can I get logic to generate these sequences?

Avinash
  • 5,734
  • 15
  • 53
  • 93
  • 2
    Homework? Most decent programming books could tell you how to write a simple for loop to do it for you. The question is what will you do with the numbers? Store them in a text file? – ccozad Aug 09 '11 at 07:41
  • 1
    Though not an exact duplicate, but I think it should suffice: http://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n – Yoshi Aug 09 '11 at 07:46
  • If it is with replacement it is 49^6 permutations (13,841,287,201). If each ball is not replaced it is 49 * 48 * 47 * 46 * 45 * 44 permutations (10,068,347,520) – ccozad Aug 09 '11 at 07:49
  • 1
    @ccozad: Not an homework, have to store them in DB. and In this case I sequence does not matter so number will be (49*48*47*46*45*44)/(6!) – Avinash Aug 09 '11 at 08:06
  • Ok, thanks knowing that you are looking for combinations without replacements narrows the problem. – ccozad Aug 09 '11 at 08:16

2 Answers2

3

Here is a well written article that covers the various scenarios of combinations and permutations. It also has a nice list of references at the end of the article.

http://www.codingthewheel.com/archives/exhaustively-enumerating-combinations-and-permutations-in-code

ccozad
  • 1,104
  • 8
  • 13
  • Article is now 404, and the homepage is just a background picture. This is why link-only answers are discouraged. Archive version: https://web.archive.org/web/20130131121518/http://www.codingthewheel.com/archives/exhaustively-enumerating-combinations-and-permutations-in-code – flipchart Apr 10 '14 at 17:11
1
<?php

$totalOutcomesEnumerated = 0;

for ($ball1 = 1; $ball1 < 45; $ball1++) {
  for ($ball2 = $ball1 + 1; $ball2 < 46; $ball2++) {
    for ($ball3 = $ball2 + 1; $ball3 < 47; $ball3++) {
      for ($ball4 = $ball3 + 1; $ball4 < 48; $ball4++) {
        for ($ball5 = $ball4 + 1; $ball5 < 49; $ball5++) {
          for ($ball6 = $ball5 + 1; $ball6 < 50; $ball6++) {
            // Each iteration of this loop visits a single outcome
            $totalOutcomesEnumerated++;
            echo $ball1 . ',' . $ball2 . ',' . $ball3 . ',' . $ball4 . ',' . $ball5 . ',' . $ball6 . PHP_EOL;
          }
        }
      }
    }
  }
}

echo 'Total outcomes : ' . $totalOutcomesEnumerated . PHP_EOL ; // 13983816
OlivierLarue
  • 1,883
  • 19
  • 22