Welcome to WebmasterWorld Guest from **35.172.195.49**

I have the following problem:

I have a variable $x for example: 2138.25

I have several numbers in an array $numbers[]:

123.23

348.56

1435.33

702.92

744.95

...

Now I need to find out, which combination of the numbers equal $x. Problem is: How many of the given numbers equal $x is also a variable. Sometimes its a combination of two numbers, sometimes its three or four, or five.

So basically I have:

$x = a given sum

$numbers[] = an array of numbers where $y of them equal $x

$y = how many of numbers[] are needed to equal $x

Anybody could point me in the right direction on how to do this in PHP?

[offtopic]Wow, Google has become fast. I searched for a solution in Google just now, and was delighted when it showed up a Search Result with exactly the question I had asked here a few moments ago. I clicked on the result and - ended up here. It was my own question already showing in the SERPS.[/offtopic]

At first I thought you would need to traverse pascal's triangle until a match is met. This will solve if any one number, or any two numbers .... any (arraylength) numbers equal $x

But I don't understand

$numbers[] = an array of numbers where $y of them equal $x

$y = how many of numbers[] are needed to equal $x

It is obvious you should not play Gin Rummy with me, jecac! But getting drunk from time to time might bring inspiration (most of which will be "Why did I do that?" and more importantly "What problem am I trying to solve?"

Your array and query make no sense TO ME. Need a bit more. And I am a number cruncher, hence the humorous "Gin!" remark.

Do you expect only 1 solution with your data? What if there are multiple?

If there are multiple solutions it would be best if I get all of them:

For example if I have:

$NUMBERS = array (5,23,43,411,9,11,34,87,4,12);

$TARGET = 39

$y = 3

Then I would expect the solutions:

5,11,23 and 4,12,23

For:

$NUMBERS = array (52,231,43,93,411,94,11,334,827,12);

$TARGET = 200

$y = 4

I would expect: 52,43,94,11 and 52,43,93,12

I know how I could do this if $y where a fixed value, for example $y = 2. Then I simply would use two nested "for" loops and try all combinations. However I don't know how to do it if $y is a variable.

Ah, is $y a value you give to the function? ie. I want a solution with 4 values ($y = 4)?

Or (this is what I was assuming)... $y is returned from the function, indicating the number of values in the solution (although this is simply a count of the numbers in the solution and doesn't need to be calculated separately).

Ah, is $y a value you give to the function? ie. I want a solution with 4 values ($y = 4)?

Yes, $y is not returned but a given variable. So $y=2 means I want to know which two numbers in $NUMBERS equal $TARGET, if $y=4 I want to know which four numbers in $NUMBERS equal $TARGET. Not up to four numbers but exactly four.

How big can your set grow?

As the size of your set grows the more difficult the problem becomes.

Take a look at :-

[stackoverflow.com...]

[en.wikipedia.org...]

How large can $y be?

I guess that $y would normally be between 2 and 8 and normally there should not be more than 10 elements in $NUMBERS. Maybe I'll just write one function for $y=2, one for $y=3 and so on. This should be relatively easy to do. If $y is much bigger than 8 it will take too much time to process anyway.

- Register For Free! -
**Become a Pro Member!** - See forum categories - Enter the Forum

- Moderator List | Top Contributors:This Week, This Month, Oct, Sept, Archive, Top 100 All Time, Top Voted Members

- Google Updates and SERP Changes - November 2019
- November 2019 AdSense Earnings & Observations
- Return of CPU Vulnerability: ZombieLoad Attack
- .Org Registry Acquired By Private Equity Company
- Google Rolls Out Search Console Speed Report
- Brands Vs Ads
- Google Updates and SERP Changes - October 2019
- Next Google Search Evolution - BERT Models Using NLP
- Windows 7 Extended Support Goes Away January 14, 2020. Are you ready?
- Google September 2019 Core Update

- .Org Registry Acquired By Private Equity Company
- Return of CPU Vulnerability: ZombieLoad Attack
- Google Updates and SERP Changes - November 2019
- Google Rolls Out Search Console Speed Report
- New Microsoft Edge Browser General Availability January 15, 2020
- Google "Site Kit" Plugin for WordPress Sites
- 50 Years since ARPANET to Today's Internet
- Google Alphabet Q3 2019 Revenue up 20pct, to $40.5 billion
- Next Google Search Evolution - BERT Models Using NLP
- Firefox 70 Adds More Privacy and Tracking Reporting