Skip to content
Snippets Groups Projects
Commit 6358bf0a authored by Amber Sprenkels's avatar Amber Sprenkels
Browse files

add prime calculating test program

parent 00c2c4ad
No related branches found
No related tags found
No related merge requests found
Pipeline #
/**
* primes.spl
*
* This program will calculate the first thousand primes and
* print them to the screen.
*/
print_list (list) :: [Int] -> Void {
putChar('[');
print_list_inner(list);
putChar(']');
putChar('
');
return;
}
print_list_inner (list) :: [Int] -> Void {
if (isEmpty(list)) {
return;
} else {
print(list.hd);
if (!isEmpty(list.tl)) {
putChar(',');
putChar(' ');
}
return print_list_inner(list.tl);
}
}
reverse (list) :: [Int] -> [Int] {
var accu = [];
while (! isEmpty (list)) {
accu = list . hd : accu;
list = list . tl;
}
return accu;
}
isDivisibleByAny(p, ps) :: Int [Int] -> Bool {
if (isEmpty(ps)) {
return False;
} else {
if (p % ps . hd == 0) {
return True;
} else {
return isDivisibleByAny(p, ps . tl);
}
}
}
len(list) :: [Int] -> Int {
if (isEmpty(list)) {
return 0;
} else {
return 1 + len(list . tl);
}
}
main() :: Int {
var x = 2;
var primes = [];
while (len(primes) < 1000) {
if (! isDivisibleByAny(x, primes)) {
primes = x : primes;
}
x = x + 1;
}
print_list(reverse(primes));
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment