• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to generate all possible integer values?  RSS feed

 
pravinth Ganesan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
i know for some gurus its easy.please help me!
Thank You!
 
Joe Ess
Bartender
Posts: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mr.David, Thanks for your reply.You are right.
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can do it yourself why do you need our code?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By chance, did the topic of "recursion" come up in class recently?

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!