java - Writing 2^x in PL -
we designed pl has following syntaxes:
load variablename, value inc variablename //increases value 1 loop variablename //loops number of times depending on variablename's value @ time. if variablename's value somehow altered later on in code, loop cycle runs same amount variablename's default value. end //we put corresponding end our loop.
for example here simple code computes x+x:
vm vm6 = new vm(); vm6.add("load x, 7"); vm6.add("load answer, 0"); vm6.add("loop x"); vm6.add("inc answer"); vm6.add("inc answer"); vm6.add("end");
now trying figure out how write 2^x stuck on longest time. me?
edit: figured out. used own solution of nested loops.
it seems pretty straightforward, doesn't it? compute x^y, loop y times, each time multiply answer x. answer starts off being 1 (anything 0 power 1).
load x, 2 load answer, 1 loop [power] [multiply procedure] end
the [multiply procedure]
referred above additive method of multiplication. 2 numbers multiplied (x * y
) equal number of intersections between 2 loops of length x
, y
. purposes, multiplying x * y
looks this:
load x, [x value] load y, [y value] load answer, 0 loop x loop y inc answer end end
so whole 2^x procedure series of loops:
load x, [power] load base, 2 load answer, 1 loop x load addition, 0 //reset multiplier loop answer loop base inc addition end end load answer, addition // load answer multiplied value end
of course, if can't nested loops, you're pretty out of luck.
Comments
Post a Comment