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

Content added Content deleted
m (→‎{{header|PL/M}}: the inevitable type...)
(Added PROMAL)
Line 949: Line 949:


EOF
EOF
</syntaxhighlight>
{{out}}
<pre>
1 4 16 36 100 196 256 400 576 676
</pre>

=={{header|PROMAL}}==
{{Trans|ALGOL W}}
<syntaxhighlight lang="promal">
;;; Find squares n where n + 1 is prime
PROGRAM primesq
INCLUDE library

;;; returns TRUE(1) if p is prime, FALSE(0) otherwise
FUNC BYTE isPrime
ARG WORD n
WORD i
WORD f
WORD f2
WORD toNext
BYTE prime
BEGIN
IF n < 3
prime = n = 2
ELSE IF n % 3 = 0
prime = n = 3
ELSE IF n % 2 = 0
prime = 0
ELSE
prime = 1
f = 5
f2 = 25
toNext = 24 ; note: ( 2n + 1 )^2 - ( 2n - 1 )^2 = 8n
WHILE f2 <= n AND prime
prime = n % f <> 0
f = f + 2
f2 = toNext
toNext = toNext + 8
RETURN prime
END

WORD i2
WORD toNext
BEGIN

IF isPrime( ( 1 * 1 ) + 1 ) ; 1 is the only possible odd number
OUTPUT " 1"

i2 = 4
toNext = 4 ; note: ( 2n + 2 )^2 - 2n^2 = 8n + 4
WHILE i2 < 1000
IF isPrime( i2 + 1 )
OUTPUT " #W", i2
toNext = toNext + 8
i2 = i2 + toNext
END
</syntaxhighlight>
</syntaxhighlight>
{{out}}
{{out}}