Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Mathematical Challenge (Lottery)
Message
De
08/05/2003 11:15:40
 
 
À
07/05/2003 10:13:53
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00785796
Message ID:
00786269
Vues:
24
Jonathan -

While driving to work today, I thought about your problem. I scratched down some notes while waiting at the Tappan Zee Bridge toll. When I got to my clients' offices, I came up with this:
**	Created by:		George E. Mamunes (george@mamunes.com)
**	Created on:		05/08/2003 11:11 AM
**	Purpose:		Creates a Cursor of Combinations

LOCAL i, j, n, la1(1), la2(1), llLoop, lcSolution, lnLen

n = 4		&& Number of elements

DIMENSION la1(n)		&& Array of possible values
la1(1) = '1'
la1(2) = '2'
la1(3) = '3'
la1(4) = '4'

NOTE: Create a cursor to hold the results:
lnLen = 0
FOR i = 1 TO n
	lnLen = lnLen + LEN(la1(i)) + 1
NEXT i
CREATE CURSOR cTemp (output C(lnLen))
INDEX ON output TAG output		&& Indexed to match the order in the example

NOTE: Loop through each possible value:
FOR i = 0 TO (2^n) - 1	&& There are 2^n ways to combine n elements

	NOTE: This section is here because single and totally empty solutions are invalid in the example
	llLoop = .F.
	FOR j = 0 TO n-1
		IF i = 2^j OR i = 0		&& Each 1, 2, 4, 8, ..., etc. is a single entry in the chart
			llLoop = .T.
			EXIT
		ENDIF
	NEXT j

	IF llLoop
		LOOP
	ENDIF
	NOTE: ENDNOTE
	
	NOTE: Here we build an array of possible solutions:
	lnSoFar = i
	DIMENSION la2(n)
	la2 = .F.
	FOR j = (n - 1) TO 0 STEP -1
		IF lnSoFar >= 2^j
			lnSoFar = lnSoFar - 2^j
			la2(j + 1) = .T.
		ENDIF
	NEXT j
	
	NOTE: Here we pick up the elements from the original array, and concatenate them into a string:
	lcSolution = ''
	FOR j = 1 TO n
		IF la2(j)
			lcSolution = lcSolution + la1(j) + '+'
		ENDIF
	NEXT j
	IF RIGHT(lcSolution, 1) = '+'
		lcSolution = LEFT(lcSolution, LEN(lcSolution) - 1)
	ENDIF
	INSERT INTO cTemp VALUES (lcSolution)
NEXT i
BROWSE NORMAL
Is this what you need?

- George [I hope this helped]


>Hello all,
>
>For a bank reconciliation program, I am trying to simulate a situation which mimics lottery possibilities
>
>For example I have a series of numbers 1, 2, 3 and 4.
>I need to find all the possible combinations of those numbers (in any order), which are:
>1+2, 1+3, 1+4, 1+2+3, 1+2+4, 1+3+4,1+2+3+4, 2+3, 2+4, 2+3+4, 3+4.
>
>I need to write code for that and could not find any solution so far.
>Could someone enlighten me with an idea.
>
>Thanks in advance
>
>Jonathan Feldman
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform