some ideas for how to confront this problem?

• 01-21-2014, 05:22 PM
Luisgur
some ideas for how to confront this problem?
So i want to make a program that returns boolean. The user inputs 4 numbers, and then 1 more, the program should return true if using all 4 numbers, adding, subtracting, multipling and dividing you can form the 5th number, example:

[3,6,1,11], [5]

11-1 = 10
6/3 = 2

10/2 = 5-----> True

my question is if anyone have any idea how to make this program? recursion? many for´s?
Thanks!
• 01-21-2014, 05:38 PM
gimbal2
Re: some ideas for how to confront this problem?
What would you recurse on?

Why MANY for's? For now I read only one.
• 01-21-2014, 05:42 PM
jim829
Re: some ideas for how to confront this problem?
Are you asking how to repetively prompt for user input (easy) or how to determine success or failure of the supplied input (much harder)?

Regards,
Jim
• 01-21-2014, 06:15 PM
AlexGraal
Re: some ideas for how to confront this problem?
I don't really want to give you the answer. But, I'll give you the format for your function that will work, and I'll give a link to where I learned to do these. They are very easy once you grasp the concept of doing them. The link is: CodingBat Java Recursion-1

coding bat is an excellent website to learn specific techniques like this. Go through recursion 1 (which I linked you to) so you understand how recursion works.

Then, go through recursion 2 - it'll teach you how to actually do your problem.

Code:

`public boolean canMake(int start, double[] nums, double target, double tot)`
Nums[] will hold your numbers. Start is where you start at in your number array so that you get all of the combos. Target will be what number you want to get.

By sending back a bunch of different branches, since you have so many options, and then changing the start index in each branch and doing tot - nums[start], another branch with tot + nums[start], *, and /, then changing start, then returning true if tot == target, false if start >= nums.length, you'll have a working function. You also have to make sure you start a set of branches starting with just the next number, without using it. AKA, canMake( start + 1, nums, target, tot)

EDIT: For the other posts, I think I understand what he is asking as I've done the same problem at one point. Given a couple of numbers, find if any number of them can be combined in any way to get the target number. It MUST be recursion. It's a tricky problem until you understand recursion
• 01-21-2014, 06:39 PM
JosAH
Re: some ideas for how to confront this problem?
Quote:

Originally Posted by AlexGraal
It MUST be recursion.

No it doesn't, because what those silly computers call recursion, actually is iteration in diguise; finite stacks can be very well emulated with arrays ... to iterate is human, recursion is divine.

kind regards,

Jos ;-)
• 01-21-2014, 07:54 PM
AlexGraal
Re: some ideas for how to confront this problem?
All right. For an elegant solution, it must be recursion. I guess there are other ways to do it. I just wrote it up really quickly and it is 11 lines in total , including main method and everything. The entire program. And it works.

I don't think you could write this in 11 lines without using recursion

in fact, the method itself can be as short as 3 lines.
• 01-24-2014, 03:26 AM
Luisgur
Re: some ideas for how to confront this problem?
Thanks a lot Alex! if i have any problem i will be asking you, im new at this so bear with me. :D