Ludic numbers: Difference between revisions

m
Line 619:
class
LUDIC_NUMBERS
 
create make
create
make
 
feature
 
make(n: INTEGER)
make (n: INTEGER)
-- make an Initial Array filled with the numbers from 1 to n
-- make anInitial Array for ludic_numbers filled with anthe initialnumbers from 1 to n and Ludic_numbers filled with 1's.
require
n_positive: n > 0
local
i: INTEGER
do
create initial.make_filled (0, 1, n - 1)
create ludic_numbers.make_filled (1, 1, 1)
from i:= 2
until i> n:= 2
loop until
initial.put ( i, i-1)> n
i:= i+1loop
initial.put (i, i - 1)
i := i + 1
end
find_ludic_numbers
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
countlast:= 1INTEGER
untildo
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
new_array:= delete_i_elements(new_array)
count:= count+1
end
end
 
delete_i_elements (ar: ARRAY [INTEGER]): ARRAY [INTEGER]
--- deleteArray with all multiples of 'ar[1]' from the array ar (Eiffel starts indexing at 1)deleted.
require
ar_not_empty: ar.count > 0
local
s_array: ARRAY [INTEGER]
i, k: INTEGER
length: INTEGER
do
do
create s_array.make_empty
create s_array.make_empty
from
ilength := 1ar.count
k:= 1from
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
if s_array.count = 0 then
i:= i+1
Result := ar
else
Result := s_array
end
ensure
not_empty: not Result.is_empty
end
 
if s_array.count=0 then
Result:= ar
else
Result:= s_array
end
ensure
not_empty : Result.count>0
end
end
</lang>
Line 700 ⟶ 711:
class
APPLICATION
 
inherit
ARGUMENTS
create
make
 
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:= 1loop
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 +1across
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)
end
 
ludic: LUDIC_NUMBERS
 
end
</lang>
Anonymous user