Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6931229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:33:00+00:00 2026-05-27T11:33:00+00:00

Given a set of numbers and a set of binary operations, what is the

  • 0

Given a set of numbers and a set of binary operations,
what is the fastest way to create random expression trees or exhaustively check every possible combination in Mathematica?

What I am trying to solve is given:

numbers={25,50,75,100,3,6}              (* each can ONLY be used ONCE  *)
operators={Plus,Subtract,Times,Divide}  (* each can be used repeatedly *)
target=99

find expression trees that would evaluate to target.

I have two solutions whose performances I give for the case where expression trees contain exactly 4 of the numbers and 3 operators:

  1. random sample & choice: ~25K trees / second
  2. exhaustive scan: 806400 trees in ~2.15 seconds

(timed on a laptop with: Intel(R) Core(TM)2 Duo CPU T9300 @ 2.50GHz, 3GB ram, no parallelization used yet but would be most welcome in answers)

My notebooks are a bit messy at the moment. So I would first love to pose the question and hope for original ideas and answers while I clean up my code for sharing.

Largest possible case is where every expression tree uses up all the (6) numbers and ‘Length[numbers]-1’ (5) operators.

Performance of methods in the largest case is:

  1. random sample & choice: ~21K trees / second
  2. exhaustive scan: 23224320 trees in ~100 seconds

Also I am using Mathematica 8.0.1 so I am more than all ears if there are any ways to do it in OpenCL or using compiled functions wiht CompilationTarget->”C”, etc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:33:00+00:00Added an answer on May 27, 2026 at 11:33 am

    OK, this is not elegant or fast, and it’s buggy, but it works (sometimes). It uses a monte carlo method, implementing the metropolis algorithm for a weight function that I (arbitrarily) selected just to see if this would work. This was some time ago for a similar problem; I suppose my mathematica skills have improved as it looks ugly now, but I have no time to fix it at the moment.

    Execute this (it looks more reasonable when you paste it into a notebook):

    ClearAll[swap];
    swap[lst_, {p1_, p2_}] := 
     ReplacePart[
      lst, {p1 \[Rule] lst\[LeftDoubleBracket]p2\[RightDoubleBracket], 
       p2 \[Rule] lst\[LeftDoubleBracket]p1\[RightDoubleBracket]}]
    
    ClearAll[evalops];
    (*first element of opslst is Identity*)
    
    evalops[opslst_, ord_, nums_] := 
     Module[{curval}, curval = First@nums;
      Do[curval = 
        opslst\[LeftDoubleBracket]p\[RightDoubleBracket][curval, 
         nums\[LeftDoubleBracket]ord\[LeftDoubleBracket]p\
    \[RightDoubleBracket]\[RightDoubleBracket]], {p, 2, Length@nums}];
      curval]
    
    ClearAll[randomizeOrder];
    randomizeOrder[ordlst_] := 
     swap[ordlst, RandomInteger[{1, Length@ordlst}, 2]]
    
    ClearAll[randomizeOps];
    (*never touch the first element*)
    
    randomizeOps[oplst_, allowedOps_] := 
     ReplacePart[
      oplst, {RandomInteger[{2, Length@oplst}] \[Rule] RandomChoice[ops]}]
    
    ClearAll[takeMCstep];
    takeMCstep[goal_, opslst_, ord_, nums_, allowedops_] := 
     Module[{curres, newres, newops, neword, p}, 
      curres = evalops[opslst, ord, nums];
      newops = randomizeOps[opslst, allowedops];
      neword = randomizeOrder[ord];
      newres = evalops[newops, neword, nums];
      Switch[Abs[newres - goal], 
       0, {newops, 
        neword}, _, (p = Abs[curres - goal]/Abs[newres - goal];
        If[RandomReal[] < p, {newops, neword}, {opslst, ord}])]]
    

    then to solve your actual problem, do

    ops = {Times, Plus, Subtract, Divide}
    nums = {25, 50, 75, 100, 3, 6}
    ord = Range[Length@nums]
    (*the first element is identity to simplify the logic later*)
    oplist = {Identity}~Join~RandomChoice[ops, Length@nums - 1]
    out = NestList[
      takeMCstep[
        99, #\[LeftDoubleBracket]1\[RightDoubleBracket], #\
    \[LeftDoubleBracket]2\[RightDoubleBracket], nums, ops] &, {oplist, 
       ord}, 10000]
    

    and then to see that it worked,

    ev = Map[evalops[#\[LeftDoubleBracket]1\[RightDoubleBracket], #\
    \[LeftDoubleBracket]2\[RightDoubleBracket], nums] &, out];
    ev // Last // N
    ev // ListPlot[#, PlotMarkers \[Rule] None] &
    

    giving

    enter image description here

    thus, it obtained the correct order of operators and numbers after around 2000 tries.

    As I said, it’s ugly, inefficient, and badly programmed as it was a quick-and-dirty adaptation of a quick-and-dirty hack. If you’re interested I can clean up and explain the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a known set $A$ of distinct numbers $0 ~ 2^(n+1)-1$. In binary mode,
Given a set of real numbers drawn from a unknown continuous univariate distribution (let's
I have a set of positive numbers. Given a number not in the set,
Is there any way of automating a site publish with a given set of
Given a set of java regular expression patterns separated by an OR (i.e |
I need an algorithm that identifies all possible combinations of a set of numbers
Given a set of numbers, divide the numbers into two subsets such that difference
I am looking for an algorithm which, given a set of numbers {0, 1,
I am trying to find a given set of numbers for the equation x^3
I am looking at solution for the set bit count problem (given a binary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.