RosettaCodeData/Task/Prime-decomposition/Eiffel/prime-decomposition.e

40 lines
652 B
Text
Raw Permalink Normal View History

2015-02-20 00:35:01 -05:00
class
PRIME_DECOMPOSITION
2015-11-18 06:14:39 +00:00
2015-02-20 00:35:01 -05:00
feature
2015-11-18 06:14:39 +00:00
factor (p: INTEGER): ARRAY [INTEGER]
-- Prime decomposition of 'p'.
require
p_positive: p > 0
local
div, i, next, rest: INTEGER
do
create Result.make_empty
if p = 1 then
Result.force (1, 1)
end
div := 2
next := 3
rest := p
2015-02-20 00:35:01 -05:00
from
2015-11-18 06:14:39 +00:00
i := 1
until
rest = 1
2015-02-20 00:35:01 -05:00
loop
2015-11-18 06:14:39 +00:00
from
until
rest \\ div /= 0
loop
Result.force (div, i)
rest := (rest / div).floor
i := i + 1
end
div := next
next := next + 2
2015-02-20 00:35:01 -05:00
end
2015-11-18 06:14:39 +00:00
ensure
is_divisor: across Result as r all p \\ r.item = 0 end
is_prime: across Result as r all prime (r.item) end
2015-02-20 00:35:01 -05:00
end