Check if a Number is Prime in Larzscript
Test primality, and generate a sieve of primes up to a limit.
mathx.is_prime() does real trial division (skipping even numbers past 2); mathx.sieve() runs a real Sieve of Eratosthenes for generating many primes at once.
larzscript pkg install mathx
Code
import "mathx" as mathx print(mathx.is_prime(97)) print(mathx.is_prime(100)) print(mathx.sieve(50))
Output
true false [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
Related
larzmathSmall math helpers→FizzBuzz in LarzscriptThe classic interview warm-up: print 1 to N, but Fizz for multiples of 3, Buzz for multiples of 5, FizzBuzz for both.→Reverse a String in LarzscriptFlip a string end to end - one of the smallest real string algorithms, and a good first look at the str package.→Check if a String is a Palindrome in LarzscriptDetect whether a string reads the same forwards and backwards, ignoring case, spaces and punctuation.→