Find squares n where n+1 is prime: Difference between revisions
Content deleted Content added
Not a robot (talk | contribs) Add CLU |
|||
Line 25: | Line 25: | ||
</pre> |
</pre> |
||
=={{header|CLU}}== |
|||
<lang clu>isqrt = proc (s: int) returns (int) |
|||
x0: int := s/2 |
|||
if x0=0 then return(s) end |
|||
x1: int := (x0 + s/x0)/2 |
|||
while x1 < x0 do |
|||
x0 := x1 |
|||
x1 := (x0 + s/x0)/2 |
|||
end |
|||
return(x0) |
|||
end isqrt |
|||
sieve = proc (n: int) returns (array[int]) |
|||
prime: array[bool] := array[bool]$fill(2,n-1,true) |
|||
primes: array[int] := array[int]$predict(1,isqrt(n)) |
|||
for p: int in int$from_to(2,isqrt(n)) do |
|||
if prime[p] then |
|||
for c: int in int$from_to_by(p*p,n,p) do |
|||
prime[c] := false |
|||
end |
|||
end |
|||
end |
|||
for p: int in array[bool]$indexes(prime) do |
|||
if prime[p] then array[int]$addh(primes,p) end |
|||
end |
|||
return(primes) |
|||
end sieve |
|||
is_square = proc (n: int) returns (bool) |
|||
return(isqrt(n) ** 2 = n) |
|||
end is_square |
|||
start_up = proc () |
|||
po: stream := stream$primary_output() |
|||
primes: array[int] := sieve(1000) |
|||
for prime: int in array[int]$elements(primes) do |
|||
n: int := prime-1 |
|||
if is_square(n) then stream$puts(po, int$unparse(n) || " ") end |
|||
end |
|||
end start_up</lang> |
|||
{{out}} |
|||
<pre>1 4 16 36 100 196 256 400 576 676</pre> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)] |
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)] |
||
Line 35: | Line 78: | ||
1 4 16 36 100 196 256 400 576 676 |
1 4 16 36 100 196 256 400 576 676 |
||
</pre> |
</pre> |
||
=={{header|Fermat}}== |
=={{header|Fermat}}== |
||
<lang fermat>!!1; |
<lang fermat>!!1; |