programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

How to generate all possible integer values?

pravinth Ganesan
Greenhorn
Posts: 13
Hi Friends,
How can i generate all the possible Integer values with the 4 basic arithmatic operators +,-,/,* for a given sequence of integer numbers?
For example if the numbers are 2,3
Then the resultmust contain all the following values:
2+3=5
3-2=1
3/2=1
3*2=6
so the resulting array must contain 1,5,6
The number of integers can be of any number,
for example:{2,3,4,56,2,1}
and when finding a result you must you all the integers in each step.
Thank You!

Joe Ess
Bartender
Posts: 9439
12
The reason a "guru" may find a problem easy is because he has considered this or similar problems before. This is why we ask that folks asking questions around here Show Some Effort. The only way to learn to write code is to write code.
So what have you done so far?

marc weber
Sheriff
Posts: 11343
Originally posted by Joe Ess:
The reason a "guru" may find a problem easy is because he has considered this or similar problems before...

I'm obviously not a guru, because I don't even understand the question.

Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
I guess the question is: Given a sequence of integers, compute a + b, a - b, a * b and a / b for every combination of integers a and b from the sequence. Return only unique results (so if two computations both return the result 1, return 1 only once).

Mark Newton
Ranch Hand
Posts: 129
Just a thought, but I got the impression that it should also consider the order of the integers, so perhaps it should be:

a + b
a - b
a * b
a / b
b + a
b - a
b * a
b / a

Obviously they'll only be unique for the - and / operations.

Barry Gaunt
Ranch Hand
Posts: 7729
I did it, my program works! But I wrote it in the margin of a book I was reading which I unfortunately left on the train this morning...

pravinth Ganesan
Greenhorn
Posts: 13
Its not a matter of generating same value again.
But the problem is to use all the given Integer values at a time.I mean as you specified its right for 2 integer variable-values.But for three it should be

a+b+c
a+b*c
a+b/c
a+b-c

a*b+c
a*b-c
a*b/c
a*b*c

a-b+c
a-b-c
a-b/c
a-b*c

a/b+c
a/b-c
a/b/c
a/b*c

Okay i think i can able to do it myself now.If you can then please post here your code.Thank You! Its very knid of you!Thank You!

Ken Blair
Ranch Hand
Posts: 1078
If you can do it yourself why do you need our code?

Henry Wong
author
Sheriff
Posts: 23295
125
By chance, did the topic of "recursion" come up in class recently?

Henry