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

  • SEARCH
  • Home
  • 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 7508579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:41:50+00:00 2026-05-29T22:41:50+00:00

I’ve been tasked with creating a list of all possibilities using data in 8

  • 0

I’ve been tasked with creating a list of all possibilities using data in 8 blocks.

The 8 blocks have the following number of possibilities:

*Block 1: 12 possibilities
*Block 2: 8 possibilities
*Block 3: 8 possibilities
*Block 4: 11 possibilities
*Block 5: 16 possibilities
*Block 6: 11 possibilities
*Block 7: 5 possibilities
*Block 8: 5 possibilities

This gives a potential number of 37,171,200 possibilities.

I tried simply doing and limiting only to displaying the values returned with the correct string length like so:

foreach($block1 AS $b1){
    foreach($block2 AS $b2){
        foreach($block3 AS $b3){
            foreach($block4 AS $b4){
                foreach($block5 AS $b5){
                    foreach($block6 AS $b6){
                        foreach($block7 AS $b7){
                            foreach($block8 AS $b8){
                                if (strlen($b1.$b2.$b3.$b4.$b5.$b6.$b7.$b8) == 16)
                                {
                                    echo $b1.$b2.$b3.$b4.$b5.$b6.$b7.$b8.'<br/>';
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

However the execution time was far too long to compute. I was wondering if anyone knew of a simpler way of doing this?

  • 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-29T22:41:51+00:00Added an answer on May 29, 2026 at 10:41 pm

    You could improve your algorithm by caching the string prefixes and remember their lengths. Then you don’t have to do that for each combination.

    $len = 16:
    
    // array for remaining characters per level
    $r = array($len);
    // array of level parts
    $p = array();
    foreach ($block1 AS &$b1) {
        // skip if already too long
        if (($r[0] - strlen($b1)) <= 0) continue;
        $r[1] = $r[0] - strlen($b1);
        foreach ($block2 AS &$b2) {
            if (($r[1] - strlen($b2)) <= 0) continue;
            $r[2] = $r[1] - strlen($b2);
            foreach ($block3 AS $b3) {
                // …
                foreach ($block8 AS &$b8) {
                    $r[8] = $r[7] - strlen($b8);
                    if ($r[8] == 0) {
                        echo implode('', $p).'<br/>';
                    }
                }
            }
        }
    }
    

    Additionally, using references in foreach will stop PHP using a copy of the array internally.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.