Created
November 5, 2010 13:20
-
-
Save mnbi/664142 to your computer and use it in GitHub Desktop.
check each prime in primes.txt
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
#!/opt/local/bin/ruby1.9 -w | |
# -*- coding: utf-8 -*- | |
# checkprimes.rb: check each prime in primes.txt | |
PRIMES_FILE = "primes.txt" | |
$primes = [] | |
if File.exist?(PRIMES_FILE) | |
File.foreach(PRIMES_FILE) do |line| | |
$primes.push(line.to_i) | |
end | |
end | |
def prime?(n) | |
return true if n == 2 | |
return false if n.even? | |
d = 3 | |
limit = Math::sqrt(n) | |
while d <= limit | |
return false if (n % d == 0) | |
d += 2 | |
end | |
true | |
end | |
$primes.each do |p| | |
print "#{p} is not a prime number.\n" if ! prime?(p) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment