Created
February 5, 2017 15:44
-
-
Save jeevn/e5b3183cc0817b497a355d838ecb326c to your computer and use it in GitHub Desktop.
Regex primality test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# REGEX PRIMALITY TEST (after a perl by abigail.be) | |
# Call with int arg >0, e.g. $ ruby prime.rb 691 | |
N = ARGV[0].to_i | |
print "#{N} is " | |
puts ('I'*N) !~ /^I?$|^(II+?)\1+$/ ? 'prime' : 'composite' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adapted from Abigail’s perl.
Very inefficient, but craftily concise!
Call with +ve integer as argument, e.g.
$ ruby prime.rb 691