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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:01:13+00:00 2026-05-29T17:01:13+00:00

Let me start with an example – I have a range of numbers from

  • 0

Let me start with an example –
I have a range of numbers from 1 to 9. And let’s say the target number that I want is 29.
In this case the minimum number of operations that are required would be (9*3)+2 = 2 operations. Similarly for 18 the minimum number of operations is 1 (9*2=18).
I can use any of the 4 arithmetic operators – +, -, / and *.

How can I programmatically find out the minimum number of operations required?
Thanks in advance for any help provided.

clarification: integers only, no decimals allowed mid-calculation. i.e. the following is not valid (from comments below): ((9/2) + 1) * 4 == 22
I must admit I didn’t think about this thoroughly, but for my purpose it doesn’t matter if decimal numbers appear mid-calculation. ((9/2) + 1) * 4 == 22 is valid. Sorry for the confusion.

  • 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-29T17:01:30+00:00Added an answer on May 29, 2026 at 5:01 pm

    For the special case where set Y = [1..9] and n > 0:

    • n <= 9 : 0 operations
    • n <=18 : 1 operation (+)
    • otherwise : Remove any divisor found in Y. If this is not enough, do a recursion on the remainder for all offsets -9 .. +9. Offset 0 can be skipped as it has already been tried.

    Notice how division is not needed in this case. For other Y this does not hold.

    This algorithm is exponential in log(n). The exact analysis is a job for somebody with more knowledge about algebra than I.

    For more speed, add pruning to eliminate some of the search for larger numbers.

    Sample code:

    def findop(n, maxlen=9999):
        # Return a short postfix list of numbers and operations
    
        # Simple solution to small numbers
        if n<=9: return [n]
        if n<=18: return [9,n-9,'+']
    
        # Find direct multiply
        x = divlist(n)
        if len(x) > 1:
            mults = len(x)-1
            x[-1:] = findop(x[-1], maxlen-2*mults)
            x.extend(['*'] * mults)
            return x
    
        shortest = 0
    
        for o in range(1,10) + range(-1,-10,-1):
            x = divlist(n-o)
            if len(x) == 1: continue
            mults = len(x)-1
    
            # We spent len(divlist) + mults + 2 fields for offset. 
            # The last number is expanded by the recursion, so it doesn't count. 
            recursion_maxlen = maxlen - len(x) - mults - 2 + 1
    
            if recursion_maxlen < 1: continue
            x[-1:] = findop(x[-1], recursion_maxlen)
            x.extend(['*'] * mults)
            if o > 0:
                x.extend([o, '+'])
            else:
                x.extend([-o, '-'])
            if shortest == 0 or len(x) < shortest:
                shortest = len(x)
                maxlen = shortest - 1
                solution = x[:]
    
        if shortest == 0:
            # Fake solution, it will be discarded
            return '#' * (maxlen+1)
        return solution
    
    def divlist(n):
        l = []
        for d in range(9,1,-1):
            while n%d == 0:
                l.append(d)
                n = n/d
        if n>1: l.append(n)
        return l
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me start by example... I have an all.proj that looks similar to this:
Let me start by saying that I do not advocate this approach, but I
Let me start out by saying that I'm not a JavaScript developer so this
Let's start from simple introduction. For better understanding my problem I have drastically simplifield
To start let me just say that I am new to jQuery as well
I want to run a simple Apache Camel example that copies files from one
To describe my dilemma, let me first start with an example problem (stolen from
Okay, let's start with an example. Keep in mind, this is only an example.
This may sound very weird, but let's start with an example: <my:MagicWidget ui:field=someFieldName fieldName=someFieldName/>
First of all let me start by saying that this question is not about

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.