Mo Logo [Home] [Lexikon] [Aufgaben] [Tests] [Kurse] [Begleitmaterial] [Hinweise] [Mitwirkende] [Publikationen]

Mathematik-Online-Aufgabensammlung: Lösung zu

Interaktive Aufgabe 1094: Siebverfahren des Eratosthenes


A B C D E F G H I J K L M N O P Q R S T U V W X Y Z


function p = prime(n)

% function p = prime(n)
% prime numbers p_k <= n^2 

% list of primes, initially empty 
p = [];

% list of numbers to test
q = [2:n.^2];

% While the first element is less than n there could be non-primes in the list. 
% Take the first as prime and delete multiples.
while q(1) <= n 
   p = [p, q(1)];
   q = q(rem(q,q(1)) ~= 0);
end;

% Now the rest of the list contains primes.
p=[p,q];

>> length(prime(444))

ans =

       17746

[Zurück zur Aufgabe]

  automatisch erstellt am 5.  7. 2007