Find squares n where n+1 is prime: Difference between revisions

Add Cowgol
(Add BASIC)
(Add Cowgol)
Line 161:
{{out}}
<pre>1 4 16 36 100 196 256 400 576 676</pre>
 
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
 
const MAX := 1000;
 
sub isqrt(s: uint16): (x0: uint16) is
x0 := s>>1;
if x0 == 0 then
x0 := s;
else
loop
var x1: uint16 := (x0 + s/x0) >> 1;
if x1 >= x0 then return; end if;
x0 := x1;
end loop;
end if;
end sub;
 
var prime: uint8[MAX + 1];
MemSet(&prime[0], 1, @bytesof prime);
 
var p: uint16 := 2;
while p*p <= MAX loop
if prime[p] != 0 then
var c := p*p;
while c <= MAX loop
prime[c] := 0;
c := c + p;
end loop;
end if;
p := p + 1;
end loop;
 
var i: uint16 := 2;
while i <= MAX loop
if prime[i] != 0 then
var sq := i - 1;
var sqr := isqrt(sq);
if sqr*sqr == sq then
print_i16(sq);
print_nl();
end if;
end if;
i := i + 1;
end loop;</lang>
{{out}}
<pre>1
4
16
36
100
196
256
400
576
676</pre>
 
=={{header|F_Sharp|F#}}==
2,114

edits