Skip to content

Instantly share code, notes, and snippets.

@jeevn
Created February 5, 2017 15:44
Show Gist options
  • Save jeevn/e5b3183cc0817b497a355d838ecb326c to your computer and use it in GitHub Desktop.
Save jeevn/e5b3183cc0817b497a355d838ecb326c to your computer and use it in GitHub Desktop.
Regex primality test
# 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'
@jeevn
Copy link
Author

jeevn commented Feb 5, 2017

Adapted from Abigail’s perl.
Very inefficient, but craftily concise!
Call with +ve integer as argument, e.g. $ ruby prime.rb 691

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment