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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:47:13+00:00 2026-06-17T22:47:13+00:00

Just to be clear, this is not a homework assignment , I study CS

  • 0

Just to be clear, this is not a homework assignment, I study CS in my own time!

I recently purchased a book entitled ’50 puzzles for logical thinking’ by Charles Phillips. I started one of them and it occurred to me that I could solve the problem using recursion. Here’s the (paraphrased) question:

Insert a mathematical operator (+, -, ÷, x) in each of the spaces to solve the equation:

6 _ 3 _ 5 _ 7 _ 4 _ 8 = 13

It is my understanding, that in order to solve this problem using recursion, I first need to identify a base case. However, I’m having trouble doing this.

So my question is, what is a possible base case and how should I begin to implement it? What could the recursive function look like (arguments, return type etc)? (code is helpful please)!

This is what I have so far: Nearly working I think

See my answer for an implementation

N.b. I’m using Java

  • 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-17T22:47:14+00:00Added an answer on June 17, 2026 at 10:47 pm

    Here is the implementation I ended up with, but first an explanation of the solution to the problem:

    • The base case (as said by larsmans and Jan Dvorak) is when all the “_” are filled with operators (such as “+”).
    • The function calls itself, adding another parameter each time until it reaches a base case that is incorrect (e.g. “6+3+5+7+4-8=13”) or it has a correct answer.
    • If the base case is incorrect, then we keep popping up levels we get to a level with an operator we can change.

    Here’s the code:

    class GapFill {
    
        private static String numbers; //E.g. 6_5_4=15
        private static String[] blank; //Array of operators to go in the blanks
    
        //Where:
        //p = plus
        //m = minus
        //d = divide
        //t = times
        private static String[] operators = {"p", "m", "d,", "t"};
    
        public static void main(String[] args) {
            numbers = args[0];
            blank = new String[numbers.split("_").length - 1];
            if(dfs(0)) { //If a solution was found
                int count = 0;
                while(numbers.indexOf("_")!=-1) {
                    int index = numbers.indexOf("_");
                    numbers = numbers.substring(0,index)+blank[count]+numbers.substring(index+1);
                    count++;
                }
                System.out.println(numbers);
            }
        }
    
        private static boolean dfs(int i) {
            if(i == blank.length) {  //base case: filled in all the blanks
                return solveEquation();
            }
            for(String op : operators) {
                blank[i] = op;
                if(dfs(i + 1)) {
                    return true;
                }
            }
            blank[i] = "_"; //restore to previous state
            return false;
        }
    
        private static boolean solveEquation() {
            String[] eachNumber = numbers.substring(0, numbers.indexOf("=")).split("_");
            String finalResult = numbers.substring(numbers.indexOf("=")+1, numbers.length());
            double currentResult = Double.parseDouble(eachNumber[0]);
            for(int i=1;i<eachNumber.length;i++) {
                String op = blank[i-1];
                if(op==operators[0]) {
                    currentResult = currentResult + Integer.parseInt(eachNumber[i]);
                } else if(op==operators[1]) {
                    currentResult = currentResult - Integer.parseInt(eachNumber[i]);
                } else if(op==operators[2]) {
                    currentResult = currentResult / Integer.parseInt(eachNumber[i]);
                } else if(op==operators[3]) {
                    currentResult = currentResult * Integer.parseInt(eachNumber[i]);
                }
            }
            return (currentResult==Integer.parseInt(finalResult));
        }
    
    }
    

    The output for java GapFill 6_3_5_7_4_8=13 is 6m3p5m7p4p8=13.

    The “p,m,d,t” symbols are used instead of “+,-,÷,×” since the terminal doesn’t like × or ÷

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

Sidebar

Related Questions

So I'll admit that this is a homework assignment, but I'm not asking you
Just to clear it up this is on an actual device and not an
Just to make this clear, I have looked at the other questions on this
EDIT: Just to make things clear, this problem was caused by a typo in
Just to be clear, I am not asking for legal advice, I am asking
I'm just starting with django. It is not quite clear to me, how should
First I want to make it clear that this is a homework based question.
i'll just get straight to the point. my database is like this every book
First off, let me say that this is not homework (I am an A-Level
Did my best to not post this question but i just can not figure

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.