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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:52:16+00:00 2026-05-15T22:52:16+00:00

I hope this isn’t a dupe, but it’s hard to boil down the problem

  • 0

I hope this isn’t a dupe, but it’s hard to boil down the problem into keywords!

This is always something that I’ve wondered about. Let’s say you have a black box that takes n integers as an input (where n > 1). Given that there is a bounds on the integer values, how would you go about writing an algorithm that will push the entire sample space through the black box? (bonus points if n can be specified at runtime)

My attempt when n = 2 is as follows:

int min = 0;
int max = 9;
int a = min;
int b = min;

while(a <= max && b <= max)
{
  blackBox(a, b);
  a++;
  if(a > max)
  {
    a = min;
    b++;
  }
}

The above code is fine for two variables, but as you might guess, my algorithm gets really ugly when n approaches double-digits.

Is there a better way to do this other than nesting if statements like I have done?

I know a bad way to do it, which would be to randomly generate the values for each iteration and save the inputs of previous iterations so you don’t poke the black box with the same variables twice. However, I was hoping for a more speedy method as collisions really hurt the execution time as the number of unique black box calls approaches (max – min + 1) ^ 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-15T22:52:17+00:00Added an answer on May 15, 2026 at 10:52 pm

    Why not used nested loops? Then you just add more nested loops as necessary.

    Might not be overly efficent but you did indicate you need to cover the entire sample space, so you’re going to have to run every possible combination of values of the input variables anway – so I doubt there’s much you can do about efficency unless it’s possible to only evaluate against a portion of the state space.

    int min = 0;
    int max = 9;
    for( int a = min ; a <= max ; ++a )
        for( int b = min ; b <= max ; ++b )
            blackBox( a , b );
    

    Also, I think you’ll find the number of unique calls is (max - min + 1) ^ n, not the other way around.

    Edit:

    A different run-time version to that already suggested

    Imre L seems to have hit the nail on the head for a real-time version using the same language type as your question (something C-like), but since you’ve tagged this as language agnostic I’ve decided to try something different (also, I’m learning Python at the moment so was looking for an excuse to practice).

    Here’s a Python real-time version, in each case x will be a n-tuple, such as [1,0,3,2]. Only thing I will say is this does not include max in the state-space (in the example below it will use 0 to 2 inclusive, not 3) so you’d have to increment max before use.

    import itertools 
    
    min = 0
    max = 3
    n = 4
    
    for x in itertools.product(range(min,max), repeat=n):
            blackBox( x )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hope this isn't too vague, but I'm stuck on a problem that has
Alright, I hope this isn't too broad a question but my curiosity got the
I hope this question isn't too open ended, but a nudge in the right
I hope this isn't a duplicate, but I'm not sure even how to phrase
I hope this isn't too vague a question, but here goes. I want to
I hope this isn't knocked for being too general, but... I recently had occasion
OK I hope this isn't too specific. I have a database driven CMS that
While this isn't strictly programming related, it's something I've run into as a web
I really hope this isn't redundant, but I honestly can't find the answer in
I hope this isn't a duplicate of a question itself, but the search terms

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.