Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Mathematical Challenge (Lottery)
Message
De
09/05/2003 16:45:48
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivie
 
 
À
09/05/2003 05:49:59
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00785796
Message ID:
00786944
Vues:
33
>Hilmar,
>It is just as you described it, a variable number of figures.
>It is the recursive program I am after.
>Thanks
>Jonathan

I just did some testing - it is a little more work than I thought... Anyway, the following seems to work. I will leave fine-tuning (efficiency, comments) to you.

Please note that the number of steps increases exponentially with the amount of numbers. For 10 numbers you will go through the main process 1024 times (probably still manageable), for 20 numbers, more than a million times (perhaps not so manageable, but I leave it to you to test the practical limits).
clear
dimension MyNumbers(5)
dimension WhichNumbersToUse(5)
MyNumbers(1) = 1
MyNumbers(2) = 2
MyNumbers(3) = 3
MyNumbers(4) = 4
MyNumbers(5) = 5

GetExactSum(@MyNumbers, @WhichNumbersToUse, 1, 6)

FUNCTION GetExactSum(taNumbersToAdd, taWhichNumbersToUse, tnCurrentPosition, tnDesiredSum)
local lnSum, i, j, k

for i = 1 to 2
	taWhichNumbersToUse(tnCurrentPosition) = i = 2
	if tnCurrentPosition < alen(taNumbersToAdd)
		GetExactSum(@taNumbersToAdd, @taWhichNumbersToUse, tnCurrentPosition + 1, tnDesiredSum)
	else && last level reached
		lnSum = 0
		for j = 1 to tnCurrentPosition
			if taWhichNumbersToUse(j)
				lnSum = lnSum + taNumbersToAdd(j)
			endif
		next
		if lnSum = tnDesiredSum
			?
			for k = 1 to tnCurrentPosition
				if taWhichNumbersToUse(k)
					?? taNumbersToAdd(k)
				endif
			next
		endif
	endif
next
Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform