Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8360563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:29:57+00:00 2026-06-09T11:29:57+00:00

This is more of a puzzle than anything. I’ve actually found a solution but

  • 0

This is more of a puzzle than anything. I’ve actually found a solution but it is so slow I thought I lost my internet connection (see below).

Here’s the problem:

Let’s say I have an array of numbers, like so:

$numbers_array = array(1, 2, 3, 4, 5, 6, 7, 8, 9);

Let’s also say that I have a number some numbers, stored in variables like so:

$sum = 15;
$sum2 = 24;
$sum3 = 400;

I am trying to create a function that will return true if any of the numbers in $numbers_array can be added together (each only used once) to form the sums:

function is_summable($array_of_nums, $sum_to_check) {
    //What to put here?
}

var_dump(is_summable($numbers_array, $sum));
var_dump(is_summable($numbers_array, $sum2));
var_dump(is_summable($numbers_array, $sum3));

The above should output:

bool(true)
bool(true)
bool(false)

Because 7 + 8 = 15, 7 + 8 + 9 = 24, but no combination of 1-9 can create 200.

Here’s my EXTREMELY slow solution:

function is_summable($numbers, $sum) {
    //Sort provided numbers and assign numerical keys.
    asort($numbers);
    $numbers = array_values($numbers);

    //Var for additions and var for number of provided numbers.
    $total = 0;
    $numbers_length = count($numbers);

    //Empty var to fill below.
    $code = '';

    //Loop and add for() loops.
    for ($i = 0; $i < $numbers_length; $i++) {
        $code .= 'for ($n' . $i . ' = 0; $n' . $i . ' < ' . $numbers_length . '; $n' . $i . '++) {';

        if ($i != 0) {
            $code .= 'if ($n' . $i . ' != $n' . ($i - 1) . ') {';
        }

        $code .= '$total += intval($numbers[$n' . $i . ']);';
        $code .= 'if ($total == $sum) {';
        $code .= 'return true;';
        $code .= '}';
    }

    //Add ending bracket for for() loops above.
    for ($l = 0; $l < $numbers_length; $l++) {
        $code .= '$total -= intval($numbers[$n' . $i . ']);';
        if ($l != 0) {
            $code .= '}';
        }
        $code .= '}';
    }

    //Finally, eval the code.
    eval($code);

    //If "true" not returned above, return false.
    return false;
}

$num_arr = array(1,2,3,4,5,6,7,8,9);
var_dump(is_summable($num_arr, 24));

http://pastebin.com/1nawuwXK

As always, help is appreciated!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:29:59+00:00Added an answer on June 9, 2026 at 11:29 am

    Your problem is in fact a standard algorithmic problem (as Jon mentioned knapsack problem), more specifically Subset sum problem. It can be solved in polynomial time (have look on wiki page).

    Pseudocode:

    initialize a list S to contain one element 0.
    for each i from 1 to N do
      let T be a list consisting of xi + y, for all y in S
      let U be the union of T and S
      sort U
      make S empty 
      let y be the smallest element of U 
      add y to S 
      for each element z of U in increasing order do
         //trim the list by eliminating numbers close to one another
         //and throw out elements greater than s
        if y + cs/N < z ≤ s, set y = z and add z to S 
    if S contains a number between (1 − c)s and s, output yes, otherwise no
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is more a puzzle question for my curiosity than anything else. I'm looking
This is more for information then a problem. (On searching Stack Overflow I found
I'm taking waaaayyy more classes than I normally do this semester and just haven't
I'v always wondered how this works. Does having more slow down the game? for
Disclaimer: this question may not have practical value, it's more of a puzzle/curiosity question.
I want to know this more in detail. Is it a realtime scheduler? An
To make this more clear, I'm going to put code samples: $file = fopen('filename.ext',
UPDATE: I've been playing around with this more, and it seems like tmux's clear-history
I am wondering if there is a way to handle this more elegantly. After
Ok, I'm going to try to make this more clear because my last question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.