Algorithm which determines required signs for arithmetical operation - algorithm

I have no idea how to Google this out so I would like to ask you guys if you know is there such an algorithm, which would determine what kind of signs you need to put between numbers in order to get given result.
For example, you input 4 numbers: 8 9 2 1. The last number is the result. So the answer would be 8-9+2=1. Is there such an algorithm, maybe you know its name so I could Google it out and read about it?

Yes, it's called Brute-force search.
You generate all possible candidates for the solution and check each of them to see whether they satisfy the constraints.

Related

How do I guess (best searching algorithm) any positive integer from a system which can only reply in a Yes or a No?

Let us say the system only gives yes or no if you guess the right number.
Lets take two examples where
1. number = 3
2. number = 134567894567
As you can see the first number can be found if you made linear call like:-
is the number = 1? No
is the number = 2? No
is the number = 3? Yes
So a linear search algorithm would be easier to implement.
However if the element is as large as 134567894567 maybe going the binary search way would make sense. how do I build a system which provides best results keeping the above constraints in mind.
You can't use binary search if the system will give you a yes/no answer.
You need to use linear search.
In order to use binary search, the conditions are:
The range must be bounded.
At each 'guess', it must tell you the direction you need to head towards.
That's the minimal requirement to use binary search.
So for your current situation, where you are not getting the required information, you must use linear search as you originally proposed.

How to solve the below recurrence relation question

Consider a recursive algorithm that break a given problem into five parts. Out of these five parts the algorithm utilizes three parts and discards two parts. The chosen parts are broken into five again and the same process is recursively repeated until the problem size is 1. Once the problem size is 1, the individual parts are recombined.
Write a recurrence relation for the above algorithm. Please state your assumptions.
Solve the recurrence relation developed in part 1 above using the Substitution Method. Specify the guess and the method you used in deciding that guess.
Please let know the answer even if you know only for the part 1.
Thank you!
This depends on what you mean by the "parts" of the problem. If the initial problem is some sort of data structure, particularly an array of numbers, then in the first part you would divide the structure into five categories based on certain properties of the numbers, and then discard two of those categories and repeat the process on the remaining 3. Just to be clear, is this the exact wording of a homework problem you were given? It would be good to have some information.

Finding a sequence of operations

This should eventually be written in JavaScript. But I feel that I should not type any code until my algorithm is clear, which it is not!
Problem Given: Starting at 1, write a function that given a number returns a sequence of operations that consist only of either "+5" or "*3" that produce the number in question.
My basic algorithm:
Get the number
if the number is 1
return 1.
else if we surpass the number
return -1.
else keep trying to "+5" or "*3" until number is reached, assuming it can be reached.
My problem is with step # 4: I see that there are two paths to take which will bring me to the number in question(target), either "+5" OR "*3", but what about the number 13 which can be found by a MIXTURE of BOTH paths?? I can only do one thing or the other!
How would I know which path to take and how many times I should take that path? How would I bounce back and forth between paths?
I agree with the concept of breadth first search in a binary tree. However, I suggest turning the problem around, and looking at the problem of using "-5" or "/3" to get from the target back to 1. That allows pruning based on the target.
For example, 13 is not divisible by 3, so the first step in the backwards problem for target 13 must be "-5", not "/3".
It does not change the complexity, but may make the algorithm faster in practice for small problems.
You essentially want to do a breadth first, binary search tree. You could use recursion, or just some while loops. Each step you take the current number and add 5 or multiply by 3. Do your tests, and if you find the input value, then return 0 or something (You did not specify).
The key here is to thing about the data structure and how to search it. Do you understand why it should be breadth first? Do you understand why it is a binary tree?
In response to comments:
First off I admire your efforts. Solving this kind of problem, independent of language, is a great way to approach a problem. It is not about stupid trick in Javascript (or any other language).
So the first concept to get down is that you "searching" for a solution, if you don't find one return -1.
Second you should do some research on binary trees. They are a very important concept!
Third you should then go breadth first search. However, that is the least important. It just makes the problem a bit more efficient.
what about the number 13 which can be found by a MIXTURE of BOTH paths?? I can only do one thing or the other!
Well, actually you can do both. As in the example in chapter 3 of the book you mention, you'll see that the function find is called twice inside itself -- the function is trying both paths at any choice point and the first correct solution is returned (you could also experiment with altering the overall function so it will return all correct paths).
How would I know which path to take and how many times I should take that path? How would I bounce back and forth between paths?
Basically, bouncing back and forth between paths is achieved by traveling both of them. You know if it's the right path if the function hits the target number.

Given a number series, finding the Check Digit Algorithm...?

Suppose I have a series of index numbers that consists of a check digit. If I have a fair enough sample (Say 250 sample index numbers), do I have a way to extract the algorithm that has been used to generate the check digit?
I think there should be a programmatic approach atleast to find a set of possible algorithms.
UPDATE: The length of a index number is 8 Digits including the check digit.
No, not in the general case, since the number of possible algorithms is far more than what you may think. A sample space of 250 may not be enough to do proper numerical analysis.
For an extreme example, let's say your samples are all 15 digits long. You would not be able to reliably detect the algorithm if it changed the behaviour for those greater than 15 characters.
If you wanted to be sure, you should reverse engineer the code that checks the numbers for validity (if available).
If you know that the algorithm is drawn from a smaller subset than "every possible algorithm", then it might be possible. But algorithms may be only half the story - there's also the case where multipliers, exponentiation and wrap-around points change even using the same algorithm.
paxdiablo is correct, and you can't guess the algorithm without making any other assumption (or just having the whole sample space - then you can define the algorithm by a look up table).
However, if the check digit is calculated using some linear formula dependent on the "data digits" (which is a very common case, as you can see in the wikipedia article), given enough samples you can use Euler elimination.

What algorithm to use to calculate a check digit?

What algorithm to use to calculate a check digit for a list of digits?
The length of the list is between 8 and 12 digits.
see also:
How to generate a verification code/number?
The Luhn algorithm is good enough for the credit card industry...
As RichieHindle points out, the Luhn algorithm is pretty good. It will detect (but not correct) any one error or transposition (except a transposition of 0 and 9).
You could also consider the algorithm for ISBN check digits, although for old-style ISBN, the check digit is sometimes "X", which may be a problem for you if you're using integer fields. New-style ISBN doesn't seem to have that problem. Wikipedia doesn't go in to the theoretical properties of the system, but I remember studying ISBN numbers in my coding theory course long ago, so I think they are pretty good :-)
I know it is a bit late (according to post dates), but first time I needed a check number algorithm was last week.
So I checked more algorithms and IMHO the best solution seems to be the Damm algorithm.
It is simple to implementation and detect most of tested errors. With default digit check table all single digit errors, all English language mishearing errors, all adjacent transposition errors, and almost all jump transpositions errors are detectable.
For me there was only a single problem, since I need to calculate check digit not only from numbers but also from characters. Unfortunately for me, there was a given rule, that the last character must be a digit; or better to say, the characters were assigned by third party authority and only fixed amount of numbers were used as manufacturer number.
There are many ways how to transcribe characters to number, but the error detection will always be lower, comparing to when only numbers are used.
For these cases you can use the ISO_6346 specification.
When there is no such limitation, use the tables for different size and assign characters and number to table values.
EDIT: updated/fixed description, added reason for digit check number for characters, and added tables for different base sizes.
Luhn algorithm
Check Digit Algorithm
Check Digit Algorithms Tutor
ISIN check digit algorithm
Verhoeff, there is nothing better IMO.

Resources