Ambi Functions

Here are a couple of short examples of ambi functions. These may be copied and pasted into the Ambi Calculator. The first is a recursive implementation of Euclid’s algorithm for finding the Greatest Common Divisor:

 

function; gcd;
  // A B gcd ;
  // Euclid's algorithm ;
  ifelse ;
    import $b = import $a = $b 0 == ;
    $a export;
    $b $a $b % gcd export ;
  pass;

 

The second is an iterative function to check if a number is prime.

 

function ; isprime ;
  // N isprime ;
  // returns boolean;
  for ;
    import  $n = true  $isprime = 2 $i =;
    $isprime $i $n sqrt <=  and  ;
    $i ++ ;
    $n $i %  0 !=  $isprime =  ;
  $isprime export;

Speak Your Mind

*