Primality by trial division: Difference between revisions

Add Cowgol
(Dialects of BASIC moved to the BASIC section.)
(Add Cowgol)
Line 1,751:
7 is a prime number
8 is not a prime number
 
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub prime(n: uint32): (isprime: uint8) is
isprime := 1;
 
if n < 2 then
isprime := 0;
return;
end if;
 
if n & 1 == 0 then
if n != 2 then
isprime := 0;
end if;
return;
end if;
 
var factor: uint32 := 3;
while factor * factor <= n loop
if n % factor == 0 then
isprime := 0;
return;
end if;
factor := factor + 2;
end loop;
end sub;
 
var i: uint32 := 0;
while i <= 100 loop
if prime(i) != 0 then
print_i32(i);
print_nl();
end if;
i := i + 1;
end loop;</syntaxhighlight>
{{out}}
<pre>2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97</pre>
 
=={{header|Crystal}}==
2,093

edits