Frobenius numbers: Difference between revisions

Add Cowgol
(Added Arturo implementation)
(Add Cowgol)
Line 377:
900591 919631* 937019 946719 958431 972179 986039
</pre>
 
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
 
const LIMIT := 10000;
 
sub sqrt(n: intptr): (x0: intptr) is
var x1: intptr;
if n <= 1 then
x0 := 1;
else
x0 := n >> 1;
x1 := (x0 + n/x0) >> 1;
while x1 < x0 loop
x0 := x1;
x1 := (x0 + n/x0) >> 1;
end loop;
end if;
end sub;
 
sub sieve(max: intptr, buf: [uint16]): (count: uint16) is
var sbuf := buf as [uint8] + max;
MemZero(sbuf, max);
var i: intptr := 2;
while i*i <= max loop
if [sbuf+i] == 0 then
var j := i+i;
while j <= max loop
[sbuf+j] := 1;
j := j+i;
end loop;
end if;
i := i+1;
end loop;
count := 0;
i := 2;
while i <= max loop
if [sbuf+i] == 0 then
[buf] := i as uint16;
buf := @next buf;
count := count + 1;
end if;
i := i + 1;
end loop;
end sub;
 
var primes: uint16[LIMIT + 1];
var nprimes := sieve(sqrt(LIMIT)+1, &primes[0]);
var n: uint16 := 0;
 
while n < nprimes-1 loop
print_i16(primes[n] * primes[n+1] - primes[n] - primes[n+1]);
print_nl();
n := n + 1;
end loop;</lang>
{{out}}
<pre>1
7
23
59
119
191
287
395
615
839
1079
1439
1679
1931
2391
3015
3479
3959
4619
5039
5615
6395
7215
8447
9599</pre>
 
=={{header|Factor}}==
2,114

edits