Primality by trial division: Difference between revisions

m (→‎improved using number wheel: link to primTrial)
Line 708:
}
}</lang>
 
=={{header|Eiffel}}==
<lang Eiffel>
class
PRIMALITY_BY_TRIAL_DIVISION
feature
prime(n:INTEGER): BOOLEAN
require
positiv_input: n>0
local
i: INTEGER
max: REAL_64
math: DOUBLE_MATH
do
create math
if n=2 then
Result:=True
elseif n<=1 or n\\2=0 then
Result:=False
else
Result:=TRUE
max:= math.sqrt(n)
from
i:= 3
until
i>max
loop
if n\\i=0 then
Result := FALSE
end
i:= i+2
end
end
end
end
</lang>
Test:
<lang Eiffel>
class
APPLICATION
inherit
ARGUMENTS
create
make
feature {NONE} -- Initialization
make
local
i: INTEGER
do
create is_prime
io.put_string ("Primes up to 100:%N")
from
i:= 1
until
i>100
loop
if is_prime.prime (i)=TRUE then
io.put_integer (i)
io.new_line
end
i:= i+1
end
end
is_prime: PRIMALITY_BY_TRIAL_DIVISION
end
</lang>
<pre>
Primes up to 100:
</pre>
 
=={{header|Emacs Lisp}}==
Anonymous user