Kite Programming Language

Something fun to try

Written by Mooneer Salem on Wednesday 4th of August, 2010 in Usage

Today, I found out about a pretty nifty regular expression that will match if a number is not prime. Turns out that the regex is usable unmodified in Kite:

#!/usr/bin/kite

method is_prime(number)
[
    property rgx;
    property digit_str;
    rgx = r/^1?$|^(11+?)\1+$/;
    
    digit_str = "";
    until(number == 0)
    [
        digit_str = digit_str + "1";
        number = number - 1;
    ];
    (rgx|match(digit_str) is System.null);
];

(is_prime(17))|print;
(is_prime(3))|print;
(is_prime(20))|print;

Results:

true
true
false

Unfortunately, because of the way the regular expression engine in Kite works, it took much longer than 14 seconds to run the check for the large numbers tried in the article. This will be another facet of the overall Kite optimization effort in the future as well. :)

Add comment