Number of operations required to reach value.

  • Thread starter Thread starter trollcast
  • Start date Start date
  • Tags Tags
    Operations Value
Click For Summary

Discussion Overview

The discussion revolves around calculating the number of operations required to transform one number into another using a limited set of operators, specifically increment and left bit shift. Participants explore various methods and algorithms to achieve this, considering efficiency and the handling of different cases.

Discussion Character

  • Homework-related
  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests starting from the lower number, repeatedly multiplying by 2 until exceeding the target, then dividing by 2 and incrementing to reach the target, questioning the efficiency for large numbers.
  • Another participant points out that the initial approach does not account for necessary increments between shifts, highlighting the need for a more nuanced algorithm.
  • A participant raises a question about how to confirm when the operations have reached the target number, suggesting the use of a compare function.
  • One participant proposes a binary tree approach, where left branches represent shifts and right branches represent increments, but expresses concern about the complexity with large target numbers.
  • Another suggestion is to work the algorithm backwards, starting from the larger number and using right shifts and decrements, which may simplify the problem.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the most efficient method to solve the problem, with multiple competing views and approaches presented throughout the discussion.

Contextual Notes

There are limitations regarding the assumptions made about the operators and the conditions under which the operations are performed. The discussion also highlights the potential inefficiencies of certain methods without resolving these concerns.

trollcast
Gold Member
Messages
282
Reaction score
13

Homework Statement


Given a limited set of operators, specifically increment and bitshift left (only 1 bit at a time) Calculate the number of operations required to get from a certain number to another.

Eg. 9 to 20 = 2 [(9++)LSHIFT]

Homework Equations



The Attempt at a Solution



I haven't started to code for it yet but I'm just wondering if my method is going to be far too inefficient at finding solutions.

If we start off with the lowest number and repeatedly multiply by 2 until it is larger than the higher number. Then Divide by 2 and repeatedly add 1 until you reach the higher number.

Go back to the lower number add 1 and repeat as above.

EG. 8 to 36

attachment.php?attachmentid=54766&stc=1&d=1358280661.png


The only thing is for very large numbers I don't think this wouldn't be very efficient?

PS. Just to clarify the code isn't restricted to only those 2 operators.
 

Attachments

  • tree.png
    tree.png
    2.6 KB · Views: 470
Physics news on Phys.org
You're not being asked to create an efficient algorithm, just one that works by producing the smallest total number of shift and increment operations. Your current approach doesn't handle the case where increments should occur between shift operations, such as shift 2, increment 1, shift 2, increment 1, shift ... .

You'll also need to handle cases like from 9 to 17, which requires 8 increments, 0 shifts.
 
Last edited:
rcgldr said:
The problem statement doesn't explain how you are supposed to confirm that you operations have reached the other number. Is there a compare function that returns separate status for less than, equal to, greater than?

If the goal is to simply find the fewest number of operations to get from a specific number to another specific number, then I assume your program can use any operators required to determine the fewest number of bit shifts and increments to reach the target number and optinally produce the list of operations. This could include a brute force method that tries various combinations and selects the one with fewest operations.

Yeah sorry, the problem basically is to work out the lowest number of left shifts and / or increments needed to get from 1 number to another.

You can use any program functions or operators needed to work that out.
 
After re-reading the problement statement, I updated my previous post, read it again.
 
rcgldr said:
After re-reading the problement statement, I updated my previous post, read it again.

Thanks,

So by extension of my previous method would a binary tree with the left branches being shifts and the right branches being increments at each node with some code to terminate branches if they overshoot the target number or to store the depth of the node if the value of that node is equal to the target.

The only issue would be if you gave it a small number say 11 and then a very large number such as 10000 as there would be a lot of branches to calculate that won't lead to even the correct answer?
 
Try working the algorithm backwards, start with the larger number, then use right shifts and decrements to get the smaller number. That may be an easier problem to optimize. Some of the steps will be more obvious when starting with the larger number, for example, if the larger number is odd, you need to decrement before right shifting.
 
Last edited:

Similar threads

  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 39 ·
2
Replies
39
Views
4K