Find squares n where n+1 is prime: Difference between revisions
m
syntax highlighting fixup automation
(Added Sidef) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 6:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<
PR read "primes.incl.a68" PR
[]BOOL prime = PRIMESIEVE 1 000; # construct a sieve of primes up to 1000 #
Line 19:
FI
OD
END</
{{out}}
<pre>
Line 27:
=={{header|AutoHotkey}}==
{{trans|FreeBASIC}}
<
while ((n2 := (n+=2)**2) < 1000)
if isPrime(n2+1)
Line 39:
return False
return True
}</
{{out}}
<pre>1, 4, 16, 36, 100, 196, 256, 400, 576, 676</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f FIND_SQUARES_N_WHERE_N+1_IS_PRIME.AWK
BEGIN {
Line 77:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 85:
=={{header|BASIC}}==
<
20 DIM C(N)
30 FOR P=2 TO SQR(N)
Line 94:
80 X=I-1: R=SQR(X)
90 IF R*R=X THEN PRINT X;
100 NEXT</
{{out}}
<pre> 1 4 16 36 100 196 256 400 576 676</pre>
=={{header|BCPL}}==
<
manifest $( MAX = 1000 $)
Line 140:
$)
wrch('*N')
$)</
{{out}}
<pre>1 4 16 36 100 196 256 400 576 676</pre>
=={{header|C}}==
<
#include <stdbool.h>
#include <math.h>
Line 173:
printf("\n");
return 0;
}</
{{out}}
<pre>1 4 16 36 100 196 256 400 576 676</pre>
=={{header|CLU}}==
<
x0: int := s/2
if x0=0 then return(s) end
Line 217:
if is_square(n) then stream$puts(po, int$unparse(n) || " ") end
end
end start_up</
{{out}}
<pre>1 4 16 36 100 196 256 400 576 676</pre>
=={{header|COBOL}}==
<
PROGRAM-ID. SQUARE-PLUS-1-PRIME.
Line 260:
CHECK-DSOR.
DIVIDE P BY DSOR GIVING DIVTEST.
IF DIVISIBLE, MOVE SPACE TO PRIME-FLAG.</
{{out}}
<pre>0001
Line 274:
=={{header|Cowgol}}==
<
const MAX := 1000;
Line 317:
end if;
i := i + 1;
end loop;</
{{out}}
<pre>1
Line 332:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<
// Find squares n where n+1 is prime. Nigel Galloway: December 17th., 2021
seq{yield 1; for g in 2..2..30 do let n=g*g in if isPrime(n+1) then yield n}|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
{{out}}
<pre>
Line 342:
=={{header|Fermat}}==
<
i:=2;
i2:=4;
Line 349:
i:+2;
i2:=i^2;
od;</
{{out}}<pre>1
4
Line 363:
=={{header|FreeBASIC}}==
<
if n<0 then return isprime(-n)
if n<2 then return false
Line 382:
n+=2
n2=n^2
wend</
{{out}}<pre> 1 4 16 36 100 196 256 400 576 676</pre>
Line 388:
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 413:
fmt.Println("There are", len(squares), "square numbers 'n' where 'n+1' is prime, viz:")
fmt.Println(squares)
}</
{{out}}
Line 422:
=={{header|GW-BASIC}}==
<
20 N = 2 : N2 = 4
30 WHILE N2 < 1000
Line 441:
180 I=I +2
190 WEND
200 RETURN</
{{out}}<pre>1
4
Line 455:
=={{header|J}}==
<
{{out}}
<pre>1 4 16 36 100 196 256 400 576 676</pre>
Line 465:
See [[Erdős-primes#jq]] for a suitable definition of `is_prime` as used here.
<
(. // infinite) as $limit
| {i:1, sq: 1}
Line 472:
| select((. + 1)|is_prime) ;
1000 | squares_for_which_successor_is_prime</
{{out}}
<pre>
Line 488:
=={{header|Julia}}==
<
isintegersquarebeforeprime(n) = isqrt(n)^2 == n && isprime(n + 1)
foreach(p -> print(lpad(last(p), 5)), filter(isintegersquarebeforeprime, 1:1000))
</
=={{header|MAD}}==
<
BOOLEAN PRIME
DIMENSION PRIME(1000)
Line 528:
VECTOR VALUES FMT = $I4*$
END OF PROGRAM</
{{out}}
<pre> 1
Line 541:
676</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
{{out}}<pre>
{1,4,16,36,100,196,256,400,576,676,1296,1600,2916,3136,4356,5476,7056,8100,8836}
Line 547:
=={{header|Modula-2}}==
<
FROM InOut IMPORT WriteCard, WriteLn;
FROM MathLib IMPORT sqrt;
Line 590:
END;
END;
END SquareAlmostPrime.</
{{out}}
<pre> 1
Line 606:
This is not terribly efficient, but it does show off the issquare and isprime functions.
<
{{out}}<pre>1
Line 621:
=={{header|Perl}}==
===Simple and Clear===
<
use strict; # https://rosettacode.org/wiki/Find_squares_n_where_n%2B1_is_prime
use warnings;
Line 627:
my @answer = grep is_square($_), map $_ - 1, @{ primes(1000) };
print "@answer\n";</
{{out}}
<pre>
Line 634:
===More Than One Way===
TMTOWTDI, right? So do it.
<
use warnings;
use feature 'say';
Line 649:
# or dispense with the module and find primes the slowest way possible
(1 x (1+$_**2)) !~ /^(11+)\1+$/ and say $_**2 for 1 .. int sqrt 1000;</
{{out}}
In all cases:
Line 664:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
Line 676:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span>
<!--</
{{out}}
<pre>
Line 682:
</pre>
Alternative, same output, but 168 iterations/tests compared to just 16 by the above:
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">sq</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #004080;">integer</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #000000;">sq</span><span style="color: #0000FF;">))</span>
<!--</
Drop the filter to get the 168 (cheekily humorous) squares-of-integers-and-non-integers result of Raku (and format/arrange them identically):
<!--<
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"%3d"</span><span style="color: #0000FF;">},</span><span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)}),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">))</span>
<!--</
=={{header|Python}}==
<
limit = 1000
print("working...")
Line 715:
print()
print("done...")
</syntaxhighlight>
{{out}}
<pre>
Line 725:
=={{header|Raku}}==
Use up to to one thousand (1,000) rather than up to one (1.000) as otherwise it would be a pretty short list...
<syntaxhighlight lang="raku"
{{out}}
<pre>(1 4 16 36 100 196 256 400 576 676)</pre>
Although, technically, there is absolutely nothing in the task directions specifying that '''n''' needs to be the square of an ''integer''. So, more accurately...
<syntaxhighlight lang="raku"
{{out}}
<pre> 1 2 4 6 10 12 16 18 22 28 30 36 40 42 46 52 58 60 66 70
Line 743:
=={{header|Ring}}==
<
load "stdlib.ring"
row = 0
Line 766:
next
return 0
</syntaxhighlight>
{{out}}
<pre>
Line 785:
=={{header|Sidef}}==
<
{{out}}
<pre>
Line 792:
=={{header|Tiny BASIC}}==
<
LET N = 2
LET M = 4
Line 808:
IF I*I < J THEN GOTO 30
LET P = 1
RETURN</
{{out}}<pre>1
4
Line 822:
=={{header|Wren}}==
{{libheader|Wren-math}}
<
var squares = []
Line 833:
}
System.print("There are %(squares.count) square numbers 'n' where 'n+1' is prime, viz:")
System.print(squares)</
{{out}}
Line 842:
=={{header|XPL0}}==
<
int N, I;
[if N <= 2 then return N = 2;
Line 857:
if IsPrime(N*N+1) then
[IntOut(0, N*N); ChOut(0, ^ )];
]</
{{out}}
|