C
Charles A Gray
I created a class for prime numbers as so:
class Primes
def initialize
end
def prime?(number)
# Method returns true if number is prime.
limit = Math.sqrt(number).ceil
flag = true
if number % 2 == 0
flag = false
else
3.step(limit,2) {|i|
if number % i == 0
flag = false
break
end
}
end
return flag
end
def show_primes(lower, upper)
# Prints all primes between lower and upper
# Lower is incremented 1 if it is even.
# The arcane "(((lower/2).floor)*2+1)" performs this task
(((lower/2).floor)*2+1).step(upper,2) {|i|
if prime?(i) == true
print i.to_s + " "
end
}
end
end
Then when I enter
a=primes.new
a.show_primes(1000000,1000100) I get
1000003 1000033 1000037 1000039 1000081 1000099 1000001
Where is that trailing 1000001 coming from? It is not a prime number and
in fact is the lower limit.
If I enter the same methods outside of class Primes and enter
show_primes(1000000,1000100) I don't get the lower limit at the end of
the printed values.
I am running version 1.18.4 using scite in Ubuntu.
class Primes
def initialize
end
def prime?(number)
# Method returns true if number is prime.
limit = Math.sqrt(number).ceil
flag = true
if number % 2 == 0
flag = false
else
3.step(limit,2) {|i|
if number % i == 0
flag = false
break
end
}
end
return flag
end
def show_primes(lower, upper)
# Prints all primes between lower and upper
# Lower is incremented 1 if it is even.
# The arcane "(((lower/2).floor)*2+1)" performs this task
(((lower/2).floor)*2+1).step(upper,2) {|i|
if prime?(i) == true
print i.to_s + " "
end
}
end
end
Then when I enter
a=primes.new
a.show_primes(1000000,1000100) I get
1000003 1000033 1000037 1000039 1000081 1000099 1000001
Where is that trailing 1000001 coming from? It is not a prime number and
in fact is the lower limit.
If I enter the same methods outside of class Primes and enter
show_primes(1000000,1000100) I don't get the lower limit at the end of
the printed values.
I am running version 1.18.4 using scite in Ubuntu.