Computer Science Canada

how to generate 4 numbers without repetition

Author:  Blue_angel00 [ Sat Jun 05, 2004 7:44 pm ]
Post subject:  how to generate 4 numbers without repetition

hi i'm new to this site and i haven't been doing programming for a while so i really need help.
i need to write a program to generate all the possibilities of 4 numbers from 1 to 40 in order and without repetition.
the first number has to be smaller than the second number and so on.
for example: 1,2,3,4 then next line has to be 1,2,3,5 all the way to 37,38,39,40.

if anyone knows how to write this program i would be greatly appreciated. Razz

Author:  Delos [ Sat Jun 05, 2004 8:06 pm ]
Post subject: 

Store the generated numbers temporarily in an array.
Each time a new number is generated, check through that array to see if it is already there...if it isn't, then add the next number to it.
Otherwise, generate a new number.

Author:  Cervantes [ Sat Jun 05, 2004 8:09 pm ]
Post subject: 

arrays? dude, this isn't that difficullt Laughing

code:

const max_num := 40
var counter := 0
for a : 1 .. max_num
    for b : 1 .. max_num
        if b > a then
            for c : 1 .. max_num
                if c > b then
                    for d : 1 .. max_num
                        if d > c then
                            counter += 1
                        end if
                    end for
                end if
            end for
        end if
    end for
end for
put counter


all logic 8)

Author:  Delos [ Sat Jun 05, 2004 8:21 pm ]
Post subject: 

Cervantes wrote:
arrays? dude, this isn't that difficullt Laughing


I really should read the posts more carefully...hehehe.

C (40, 4) works.

code:

const maxNum : int := 40
const rowSize : int := 4
var count : int := 0

function factorial (num : real) : real
    var temp : real := num
    if index (realstr (temp, 0), ".") > 0 then
        result - 1
    end if
    if temp > 169 then
        result - 1
    end if
    var n : real := temp - 1
    if temp = 0 then
        result 1
    else
        temp := temp * factorial (n)
    end if
   
    result temp
end factorial

function comb (n : real, r : real) : real
    % n choose r.

    var temp : real

    temp := round (factorial (n) / (factorial (n - r) * (factorial (r))))

    result temp
end comb

put comb (maxNum, rowSize)



(I used the real type as opposed to ints as Turing's ints don't go all that high...)


: