Skip to content

Instantly share code, notes, and snippets.

@mnbi
Created November 5, 2010 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnbi/664142 to your computer and use it in GitHub Desktop.
Save mnbi/664142 to your computer and use it in GitHub Desktop.
check each prime in primes.txt
#!/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