Automatic generation produced by ISE Eiffel

Classes Clusters Cluster hierarchy Chart Relations Text Flat Contracts Flat contracts Go to:
indexing description: "Prime number properties" status: "See notice at end of class" names: primes date: "$Date: 2001-11-16 20:32:23 +0000 (Fri, 16 Nov 2001) $" revision: "$Revision: 51435 $" class PRIMES inherit COUNTABLE_SEQUENCE [INTEGER] rename has as is_prime redefine is_prime, i_th end feature -- Access Smallest_prime: INTEGER is 2 Smallest_odd_prime: INTEGER is 3 higher_prime (n: INTEGER): INTEGER is -- Lowest prime greater than or equal to n do if n <= smallest_prime then Result := smallest_prime else check n > smallest_prime end from if n \\ smallest_prime = 0 then Result := n + 1 else Result := n end until is_prime (Result) loop Result := Result + smallest_prime end end end lower_prime (n: INTEGER): INTEGER is -- Greatest prime lower than or equal to n require argument_big_enough: n >= smallest_prime do if n = smallest_prime then Result := smallest_prime else from if n \\ smallest_prime = 0 then Result := n - 1 else Result := n end until is_prime (Result) loop Result := Result - smallest_prime end end end all_lower_primes (n: INTEGER): ARRAY [BOOLEAN] is -- Array of n boolean values, where the -- value at index i is true if and only if -- i is prime. local i, j: INTEGER do from create Result.make (1, n) i := 3 until i > n loop Result.put (True, i) i := i + smallest_prime end if n >= smallest_prime then Result.put (True, smallest_prime) end from i := smallest_odd_prime until i * i > n loop if Result.item (i) then from j := i * i until j > n loop Result.put (False, j) j := j + smallest_prime * i end end i := i + smallest_prime end end is_prime (n: INTEGER): BOOLEAN is -- Is n a prime number? local divisor: INTEGER do if n <= 1 then Result := False elseif n = smallest_prime then Result := True elseif n \\ smallest_prime /= 0 then from divisor := smallest_odd_prime until (n \\ divisor = 0) or else (divisor * divisor >= n) loop divisor := divisor + smallest_prime end; if divisor * divisor > n then Result := True end end end i_th (i: INTEGER): INTEGER is -- The i-th prime number local candidates: ARRAY [BOOLEAN] found: INTEGER do candidates := all_lower_primes (i * i) from Result := 2 found := 1 variant i * i - Result until found = i loop Result := Result + 1 if candidates.item (Result) then found := found + 1 end end end indexing library: "[ EiffelBase: Library of reusable components for Eiffel. ]" status: "[ Copyright 1986-2001 Interactive Software Engineering (ISE). For ISE customers the original versions are an ISE product covered by the ISE Eiffel license and support agreements. ]" license: "[ EiffelBase may now be used by anyone as FREE SOFTWARE to develop any product, public-domain or commercial, without payment to ISE, under the terms of the ISE Free Eiffel Library License (IFELL) at http://eiffel.com/products/base/license.html. ]" source: "[ Interactive Software Engineering Inc. ISE Building 360 Storke Road, Goleta, CA 93117 USA Telephone 805-685-1006, Fax 805-685-6869 Electronic mail <info@eiffel.com> Customer support http://support.eiffel.com ]" info: "[ For latest info see award-winning pages: http://eiffel.com ]" end -- class PRIMES
Classes Clusters Cluster hierarchy Chart Relations Text Flat Contracts Flat contracts Go to:

-- Generated by ISE Eiffel --
For more details: www.eiffel.com