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 655749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:38:18+00:00 2026-05-13T22:38:18+00:00

There’s a blog post comment on codinghorror.com by Paul Jungwirth which includes a little

  • 0

There’s a blog post comment on codinghorror.com by Paul Jungwirth which includes a little programming task:

You have the numbers 123456789, in that order. Between each number, you must insert either nothing, a plus sign, or a multiplication sign, so that the resulting expression equals 2001. Write a program that prints all solutions. (There are two.)

Bored, I thought, I’d have a go, but I’ll be damned if I can get a result for 2001. I think the code below is sound and I reckon that there are zero solutions that result in 2001. According to my code, there are two solutions for 2002. Am I right or am I wrong?

/**
 * Take the numbers 123456789 and form expressions by inserting one of ''
 * (empty string), '+' or '*' between each number.
 * Find (2) solutions such that the expression evaluates to the number 2001
 */

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

// an array of strings representing 8 digit, base 3 numbers
$ops = array();
$numOps = sizeof($input)-1; // always 8
$mask = str_repeat('0', $numOps); // mask of 8 zeros for padding

// generate the ops array
$limit = pow(3, $numOps) -1;
for ($i = 0; $i <= $limit; $i++) {
    $s = (string) $i;
    $s = base_convert($s, 10, 3);
    $ops[] = substr($mask, 0, $numOps - strlen($s)) . $s;
}

// for each element in the ops array, generate an expression by inserting
// '', '*' or '+' between the numbers in $input.  e.g. element 11111111 will
// result in 1+2+3+4+5+6+7+8+9
$limit = sizeof($ops);
$stringResult = null;
$numericResult = null;
for ($i = 0; $i < $limit; $i++) {
    $l = $numOps;
    $stringResult = '';
    $numericResult = 0;
    for ($j = 0; $j <= $l; $j++) {
        $stringResult .= (string) $input[$j];
        switch (substr($ops[$i], $j, 1)) {
            case '0':
                break;
            case '1':
                $stringResult .= '+';
                break;
            case '2':
                $stringResult .= '*';
                break;
            default :
        }
    }

    // evaluate the expression

    // split the expression into smaller ones to be added together
    $temp = explode('+', $stringResult);
    $additionElems = array();
    foreach ($temp as $subExpressions)
    {
        // split each of those into ones to be multiplied together
        $multplicationElems = explode('*', $subExpressions);
        $working = 1;
        foreach ($multplicationElems as $operand) {
            $working *= $operand;
        }
        $additionElems[] = $working;
    }
    $numericResult = 0;
    foreach($additionElems as $operand)
    {
        $numericResult += $operand;
    }

    if ($numericResult == 2001) {
        echo "{$stringResult}\n";
    }
}
  • 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-05-13T22:38:19+00:00Added an answer on May 13, 2026 at 10:38 pm

    Further down the same page you linked to…. =)

    “Paul Jungwirth wrote:

    You have the numbers 123456789, in
    that order. Between each number, you
    must insert either nothing, a plus
    sign, or a multiplication sign, so
    that the resulting expression equals
    2001. Write a program that prints all solutions. (There are two.)

    I think you meant 2002, not 2001. 🙂

    (Just correcting for anyone else like
    me who obsessively tries to solve
    little “practice” problems like this
    one, and then hit Google when their
    result doesn’t match the stated
    answer. 😉 Damn, some of those Perl
    examples are ugly.)”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.