Message Boards Message Boards

0
|
5719 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

How to permute 4 operation between fixed numbers position..

Posted 10 years ago

Hi all, is there any way to permute all four operation Plus, Minus, Times, Exp between numbers that has fixed position..I created list by hand but it takes time.. please see attachment..

Attachments:
POSTED BY: Okkes Dulgerci
2 Replies

how about using string operations:

In[54]:= nums = {"4", "5", "1", "18"}

Out[54]= {"4", "5", "1", "18"}

In[55]:= ops = Permutations[{"+", "-", "*", "^"}, {3}]

Out[55]= {{"+", "-", "*"}, {"+", "-", "^"}, {"+", "*", "-"}, {"+", 
  "*", "^"}, {"+", "^", "-"}, {"+", "^", "*"}, {"-", "+", "*"}, {"-", 
  "+", "^"}, {"-", "*", "+"}, {"-", "*", "^"}, {"-", "^", "+"}, {"-", 
  "^", "*"}, {"*", "+", "-"}, {"*", "+", "^"}, {"*", "-", "+"}, {"*", 
  "-", "^"}, {"*", "^", "+"}, {"*", "^", "-"}, {"^", "+", "-"}, {"^", 
  "+", "*"}, {"^", "-", "+"}, {"^", "-", "*"}, {"^", "*", "+"}, {"^", 
  "*", "-"}}

In[56]:= d = First[Dimensions[%]]

Out[56]= 24

In[57]:= Table[StringJoin[Riffle[nums, ops[[i, All]]]], {i, d}]

Out[57]= {"4+5-1*18", "4+5-1^18", "4+5*1-18", "4+5*1^18", "4+5^1-18", \
"4+5^1*18", "4-5+1*18", "4-5+1^18", "4-5*1+18", "4-5*1^18", \
"4-5^1+18", "4-5^1*18", "4*5+1-18", "4*5+1^18", "4*5-1+18", \
"4*5-1^18", "4*5^1+18", "4*5^1-18", "4^5+1-18", "4^5+1*18", \
"4^5-1+18", "4^5-1*18", "4^5*1+18", "4^5*1-18"}

In[59]:= list = ToExpression[%]

Out[59]= {-9, 8, -9, 9, -9, 94, 17, 0, 17, -1, 17, -86, 3, 21, 37, \
19, 38, 2, 1007, 1042, 1041, 1006, 1042, 1006}
POSTED BY: Kay Herbert
Posted 10 years ago

Awesome!! Thanks a lot..

POSTED BY: Okkes Dulgerci
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract