Ludic numbers: Difference between revisions

Content added Content deleted
Line 619: Line 619:
class
class
LUDIC_NUMBERS
LUDIC_NUMBERS

create make
create
make

feature
feature

make(n: INTEGER)
make (n: INTEGER)
-- make an Initial Array filled with the numbers from 1 to n
-- make an Array for ludic_numbers filled with an initial 1
-- Initial Array filled with the numbers from 1 to n and Ludic_numbers filled with 1's.
require
require
n_positive: n>0
n_positive: n > 0
local
local
i: INTEGER
i: INTEGER
do
do
create initial.make_filled (0, 1, n-1)
create initial.make_filled (0, 1, n - 1)
create ludic_numbers.make_filled(1,1,1)
create ludic_numbers.make_filled (1, 1, 1)
from i:= 2
from
until i> n
i := 2
loop
until
initial.put (i, i-1)
i > n
i:= i+1
loop
initial.put (i, i - 1)
i := i + 1
end
find_ludic_numbers
end
end
ludic
end
ludic_numbers: ARRAY[INTEGER]


ludic_numbers: ARRAY [INTEGER]
feature{NONE}
initial: ARRAY[INTEGER]


feature {NONE}
ludic

--- forces the first element (initial[1]) of the initial array into ludic_numbers
initial: ARRAY [INTEGER]
--- before deleting it and all multiples of it

local
find_ludic_numbers
count: INTEGER
-- Forces initial[1] into ludic_numbers before deleting it and all multiples of initial[1].
new_array: ARRAY[INTEGER]
local
do
count: INTEGER
create new_array.make_from_array (initial)
new_array: ARRAY [INTEGER]
from
count:= 1
last: INTEGER
until
do
create new_array.make_from_array (initial)
count> initial.count
last := initial.count
loop
from
if ludic_numbers[ludic_numbers.count]/= new_array[1] then
count := 1
ludic_numbers.force (new_array[1], count+1)
until
count > last
loop
if ludic_numbers [ludic_numbers.count] /= new_array [1] then
ludic_numbers.force (new_array [1], count + 1)
end
new_array := delete_i_elements (new_array)
count := count + 1
end
end
new_array:= delete_i_elements(new_array)
count:= count+1
end
end
end


delete_i_elements(ar: ARRAY[INTEGER]): ARRAY[INTEGER]
delete_i_elements (ar: ARRAY [INTEGER]): ARRAY [INTEGER]
--- delete all multiples of ar[1] from the array ar (Eiffel starts indexing at 1)
--- Array with all multiples of 'ar[1]' deleted.
require
require
ar_not_empty: ar.count >0
ar_not_empty: ar.count > 0
local
local
s_array: ARRAY[INTEGER]
s_array: ARRAY [INTEGER]
i,k: INTEGER
i, k: INTEGER
length: INTEGER
do
do
create s_array.make_empty
create s_array.make_empty
from
i:= 1
length := ar.count
k:= 1
from
i := 0
until
i>ar.count
k := 1
loop
until
i = length
if (i-1)\\(ar[1])/=0 then
loop
s_array.force (ar[i], k)
if (i) \\ (ar [1]) /= 0 then
k:= k+1
s_array.force (ar [i + 1], k)
k := k + 1
end
i := i + 1
end
end
if s_array.count = 0 then
i:= i+1
Result := ar
else
Result := s_array
end
ensure
not_empty: not Result.is_empty
end
end

if s_array.count=0 then
Result:= ar
else
Result:= s_array
end
ensure
not_empty : Result.count>0
end
end
end
</lang>
</lang>
Line 700: Line 711:
class
class
APPLICATION
APPLICATION

inherit
ARGUMENTS
create
create
make
make

feature
feature
make
local
k, count: INTEGER
do
create ludic.make(22000)


make
io.put_string ("%NLudic numbers up to 25. %N")
local
across ludic.ludic_numbers.subarray (1, 25) as ld loop io.put_string (ld.item.out + "%N") end
k, count: INTEGER

do
io.put_string ("%NLudic numbers from 2000 ... 2005. %N")
create ludic.make (22000)
across ludic.ludic_numbers.subarray (2000, 2005) as ld loop io.put_string (ld.item.out + "%N") end
io.put_string ("%NLudic numbers up to 25. %N")

across
io.put_string ("%NNumber of Ludic numbers smaller than 1000. %N")
ludic.ludic_numbers.subarray (1, 25) as ld
from
k:= 1
loop
io.put_string (ld.item.out + "%N")
until
end
ludic.ludic_numbers[k]>= 1000
io.put_string ("%NLudic numbers from 2000 ... 2005. %N")
loop
k:= k +1
across
ludic.ludic_numbers.subarray (2000, 2005) as ld
count:= count+1
end
loop
io.put_string (ld.item.out + "%N")
end
io.put_string ("%NNumber of Ludic numbers smaller than 1000. %N")
from
k := 1
until
ludic.ludic_numbers [k] >= 1000
loop
k := k + 1
count := count + 1
end
io.put_integer (count)
io.put_integer (count)
end
end

ludic: LUDIC_NUMBERS
ludic: LUDIC_NUMBERS

end
end
</lang>
</lang>