Evaluatiing an algorithm

Assume we want to determine whether a number is a prime number or not. In Jasmine we could define the algorithm in a String array thus:

String[] lines = {
  "# Test to see if n is an odd prime number", 
  "root = floor(sqrt(n)); notPrime = FALSE; p = 3", 
  "while(p <= root && notPrime == FALSE)", 
  "  if(n % p == 0); notPrime = TRUE; endif", 
  "  p = p + 1", 
  "wend"
};

This demonstrates some of the features supported in algorithms. The # symbol is used at the start of a comment (continues to the end-of-line) so the first line in the algorithm is a comment describing its purpose.

The next line has three statements to initialize some variables.  The symbol ';' is used to separate statements on the same line. FALSE and TRUE are constants used by Jasmine to represent the Boolean states false and true.

Then there is the start of a while-wend loop and inside the loop there is an if-endif conditional statement, Jasmine also supports the 'else' clause.

To compile, evaluate and print the results of the algorithm we can add the statements.

Algorithm a = Compile.algorithm(lines, false);
a.eval("n", 19);
boolean isPrime = a.answer("notPrime").toBoolean();
int n = a.answer("n").toInteger();
System.out.print("The number " + n + " is");
if (isPrime) {
  System.out.print(" not");
}
System.out.println(" a prime number");

The first line will compile the algorithm, this only needs to be done once.

In line 11 we use the Algorithm object to perform the evaluation using the variable values passed in the parameters. The actual parameters are always in pairs, the variable name followed by the value. So in this case we want a variable called 'n' that has the initial value 19. If more variables were needed then we simply add another name-value.

The next two lines show how to retrieve a value stored in a variable. Finally we display the result of the evaluation –

The number 19 is a prime number

It is possible to repeat the evaluation as many times as we like using different values of n. The values stored in variables are retained after an evaluation so they can be retrieved and are not cleared on subsequent evaluations. If you want to reset all the variables to zero before an evaluation then use the statement

a. clearVariables();

If you want to see all the variables stored then use

a. showVariables();

More Algorithm examples

The following examples show more features available when evaluating algorithms.

For loop

String[] lines = {
  "# Sum of all numbers within an inclusive range ", 
  "from = min(n0, n1); to = max(n0, n1); sum = 0", 
  "for(i = from, i <= to, i = i + 1); sum = sum + i; fend",
};
Algorithm a = Compile.algorithm(lines, false);
a.eval("n0", 15, "n1", -13);
int s = a.answer("sum").toInteger();
System.out.println("Sum of range is " + s);

Repeat-until loop

String[] lines = {
  "# Show the Fibonacci  series until 'limit' is reached", 
  "f0 = 1; f1 = 1; print(f0); print(f1)", 
  "repeat", 
  "  f2 = f0 + f1; print(f2)", 
  "  f0 = f1; f1 = f2", 
  "until(f2 >= limit)",
};
Algorithm a = Compile.algorithm(lines, false);
a.eval("limit", 15);

If-then-else-endif

String[] lines = {
  "# Calculate future value of an  investement", 
  "# Interest can be simple or compound", 
  "# pv = present value, fv = future value", 
  "# r = % interest rate, t = years to invest", 
  "r = r / 100 # convert to proportion", 
  "if(simple == TRUE) then fv = pv*(1+r*t); else; fv = pv*(1+r)^t; endif", 
  "print(fv)", 
};
Algorithm a = Compile.algorithm(lines, false);
a.eval("simple", Jasmine.TRUE, "pv", 1000, "r", 10, "t", 2);
a.eval("simple", Jasmine.FALSE, "pv", 1000, "r", 10, "t", 2);
}

In this example we use the Jasmine constants TRUE and FALSE instead of the boolean primitives true and false.