views:

264

answers:

4

what i mean is all the numbers _ _ _ _ _ _ _ are distinct and only values 2 to 9 can be entered. I've tried using array and loops but i just couldn't figure out the solution to display all the numbers. examples 7-digit number among 2 to 9 are: 234567 234568 234569

324567 324568 324569

notice the none of the numbers in those values are repeated. I really have no idea what's going on. please help me!

+1  A: 

Since you didn't specify a language, here's a Haskell answer.

import Control.Monad

selectPerms :: MonadPlus m => [a] -> Int -> m [a]
selectPerms _        0 = return []
selectPerms universe n = do
    (digit, remain) <- selectDigit universe
    xs <- selectPerms remain (n - 1)
    return (digit:xs)

selectDigit :: MonadPlus m => [a] -> m (a, [a])
selectDigit [] = mzero
selectDigit (x:xs) = capture `mplus` next
    where
        capture = return (x, xs)
        next = do
            (digit, remain) <- selectDigit xs
            return (digit, x:remain)

yourAnswer :: [[Int]]
yourAnswer = selectPerms [2..9] 7
bdonlan
Much better (but wordier) than the obvious `import Data.List; nub $ map (take 7) (permutations [1..9])` :) It still reads nicely, though.
ephemient
it's a C language programming...
keitamike
@keitamike You really should say these things somewhere, like in the tags :)
bdonlan
C'mon, how hard can it be to translate Haskell to C. OMG, my eyes are bleeding! :-)
paxdiablo
Come now, it's really quite simple! I just select an arbitrary permutation, then use `[]`'s `MonadPlus` instance to expand that over all possible permutations. Easy! :)
bdonlan
+1  A: 

Since this sounds suspiciously like homework, the education here will be a gradual process. First, try brute force. Here's an algorithm (well, Python really) that will do it:

for n1 in range(2,10):
  for n2 in range(2,10):
    if n2 != n1:
      for n3 in range(2,10):
        if n3 != n2 and n3 != n1:
          for n4 in range(2,10):
            if n4 != n3 and n4 != n2 and n4 != n1:
              for n5 in range(2,10):
                if n5 != n4 and n5 != n3 and n5 != n2 and n5 != n1:
                  for n6 in range(2,10):
                    if n6 != n5 and n6 != n4 and n6 != n3 and n6 != n2 and n6 != n1:
                      for n7 in range(2,10):
                        if n7 != n6 and n7 != n5 and n7 != n4 and n7 != n3 and n7 != n2 and n7 != n1:
                          print "%d%d%d%d%d%d%d"%(n1,n2,n3,n4,n5,n6,n7)

It's basically seven nested loops, one for each digit position, with checks that there's no duplicates at any point. Note that this is not a solution that will scale well for lots of digit positions, but it performs quite well for seven of them. If you want many more, a less brute-force solution would be better.

paxdiablo
thanks for your teaching. i think this works for me.. i'm trying now
keitamike
Shouldn't be too hard to do the same thing in C. I don't want to post the C code in case it's homework since you should be doing *some* of the work :-)
paxdiablo
thanks for your answer. it really do help me in my problems. thanks everybody!
keitamike
+1  A: 

There are 7!=5040 ways to permute abcdefg.

There are 9C7=36 ways to choose 7 numbers out of 123456789.

Given a permutation "bfgdeac" and a set of {1,3,4,5,6,8,9}, there is a natural way to use the permutation to provide an order for the set, i.e. [3,8,9,5,6,1,4].

ephemient
That doesn't really answer the question.
s1n
+1  A: 

Borrowed from Python's itertools docs.

def permutations(iterable, r=None):
    # http://docs.python.org/library/itertools.html#itertools.permutations
    pool = tuple(iterable)
    n = len(pool)
    r = n if r is None else r
    if r > n:
        return
    indices = range(n)
    cycles = range(n, n-r, -1)
    yield tuple(pool[i] for i in indices[:r])
    while n:
        for i in reversed(range(r)):
            cycles[i] -= 1
            if cycles[i] == 0:
                indices[i:] = indices[i+1:] + indices[i:i+1]
                cycles[i] = n - i
            else:
                j = cycles[i]
                indices[i], indices[-j] = indices[-j], indices[i]
                yield tuple(pool[i] for i in indices[:r])
                break
        else:
            return
for number in permutations('23456789',6): # you said 7, but your examples were 6
    print number

Edit: Or if you have Python anyways...

import itertools
for number in itertools.permutations('23456789',6): print number
Nick T