Find squares n where n+1 is prime


Find squares n where n+1 is prime and n<1.000

Find squares n where n+1 is prime is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task

ALGOL 68

<lang algol68>BEGIN # find squares n where n + 1 is prime #

   PR read "primes.incl.a68" PR
   []BOOL prime = PRIMESIEVE 1 000; # construct a sieve of primes up to 1000 #
   # find the squares 1 less than a prime (ignoring squares of non-integers) #
   FOR i TO UPB prime WHILE INT i2 = i * i;
                            i2 < UPB prime
   DO
       IF prime[ i2 + 1 ] THEN
           print( ( " ", whole( i2, 0 ) ) )
       FI
   OD

END</lang>

Output:
 1 4 16 36 100 196 256 400 576 676

Fermat

<lang fermat>!!1; i:=2; i2:=4; while i2<1000 do

   if Isprime(i2+1) then !!i2 fi;
   i:+2;
   i2:=i^2;

od;</lang>

Output:
1

4 16 36 100 196 256 400 576 676

FreeBASIC

<lang freebasic>function isprime(n as integer) as boolean

   if n<0 then return isprime(-n)
   if n<2 then return false
   if n<4 then return true
   dim as uinteger i=3
   while i*i<n
       if n mod i = 0 then return false
       i+=2
   wend
   return true

end function


print 1;" "; dim as integer n=2, n2=4 while n2<1000

   if isprime(1+n2) then print n2;"  ";
   n+=2
   n2=n^2

wend</lang>

Output:
 1   4   16   36   100   196   256   400   576   676

GW-BASIC

<lang gwbasic>10 PRINT 1 20 N = 2 : N2 = 4 30 WHILE N2 < 1000 40 J = N2+1 50 GOSUB 110 60 IF PRIME = 1 THEN PRINT N2 70 N = N + 2 80 N2 = N*N 90 WEND 100 END 110 PRIME = 0 120 IF J < 2 THEN RETURN 130 PRIME = 1 140 IF J<4 THEN RETURN 150 I=5 160 WHILE I*I<J 170 IF J MOD I = 0 THEN PRIME = 0 : RETURN 180 I=I +2 190 WEND 200 RETURN</lang>

Output:
1

4 16 36 100 196 256 400 576 676

Julia

<lang julia>using Primes

isintegersquarebeforeprime(n) = isqrt(n)^2 == n && isprime(n + 1)

foreach(p -> print(lpad(last(p), 5)), filter(isintegersquarebeforeprime, 1:1000))

</lang>

Output:
    1    4   16   36  100  196  256  400  576  676 

PARI/GP

This is not terribly efficient, but it does show off the issquare and isprime functions.

<lang parigp>for(n = 1, 1000, if(issquare(n)&&isprime(n+1),print(n)))</lang>

Output:
1

4 16 36 100 196 256 400 576

676

Perl

<lang perl>#!/usr/bin/perl

use strict; # https://rosettacode.org/wiki/Find_squares_n_where_n%2B1_is_prime use warnings; use ntheory qw( primes is_square );

my @answer = grep is_square($_), map $_ - 1, @{ primes(1000) }; print "@answer\n";</lang>

Output:
1 4 16 36 100 196 256 400 576 676

Phix

with javascript_semantics
sequence res = {1}
integer sq = 4, d = 2
while sq<1000 do
    if is_prime(sq+1) then
        res &= sq
    end if
    d += 4
    sq += 2*d
end while
printf(1,"%V\n",{res})
Output:
{1,4,16,36,100,196,256,400,576,676}

The (cheekily humorous) squares-of-[non-]integers result of Raku can be replicated with

puts(1,join_by(apply(true,sprintf,{{"%3d"},sq_sub(get_primes_le(1000),1)}),1,20," "))

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... <lang perl6>say ({$++²}…^*>Ⅿ).grep: (*+1).is-prime</lang>

Output:
(1 4 16 36 100 196 256 400 576 676)

Although, technically, there is absolutely nothing in the task directions specifying that n needs to be the square of an integer. So, more accurately... <lang perl6>put (^Ⅿ).grep(*.is-prime).map(*-1).batch(20)».fmt("%3d").join: "\n"</lang>

Output:
  1   2   4   6  10  12  16  18  22  28  30  36  40  42  46  52  58  60  66  70
 72  78  82  88  96 100 102 106 108 112 126 130 136 138 148 150 156 162 166 172
178 180 190 192 196 198 210 222 226 228 232 238 240 250 256 262 268 270 276 280
282 292 306 310 312 316 330 336 346 348 352 358 366 372 378 382 388 396 400 408
418 420 430 432 438 442 448 456 460 462 466 478 486 490 498 502 508 520 522 540
546 556 562 568 570 576 586 592 598 600 606 612 616 618 630 640 642 646 652 658
660 672 676 682 690 700 708 718 726 732 738 742 750 756 760 768 772 786 796 808
810 820 822 826 828 838 852 856 858 862 876 880 882 886 906 910 918 928 936 940
946 952 966 970 976 982 990 996

Ring

<lang ring> load "stdlib.ring" row = 0 limit = 1000 see "working..." + nl

for n = 1 to limit-1

   if issquare(n) and isprime(n+1)
      row++
      see "" + n +nl
   ok

next

see "Found " + row + " numbers" + nl see "done..." + nl

func issquare(x)

    for n = 1 to sqrt(x)
        if x = pow(n,2)
           return 1
        ok
    next
    return 0

</lang>

Output:
working...
1
4
16
36
100
196
256
400
576
676
Found 10 numbers
done...

Tiny BASIC

<lang tinybasic> PRINT 1

     LET N = 2
     LET M = 4
  10 LET J = M + 1
     GOSUB 20
     IF P = 1 THEN PRINT M
     LET N = N + 2
     LET M = N*N
     IF M < 1000 THEN GOTO 10
     END
  20 LET P = 0
     LET I = 3
  30 IF (J/I)*I = J THEN RETURN
     LET I = I + 2
     IF I*I < J THEN GOTO 30
     LET P = 1
     RETURN</lang>
Output:
1

4 16 36 100 196 256 400 576

676

Wren

Library: Wren-math

<lang ecmascript>import "./math" for Int

var squares = [] var limit = 1000.sqrt.floor var i = 1 while (i <= limit) {

   var n = i * i
   if (Int.isPrime(n+1)) squares.add(n)
   i = (i == 1) ? 2 : i + 2

} System.print("There are %(squares.count) square numbers 'n' where 'n+1' is prime, viz:") System.print(squares)</lang>

Output:
There are 10 square numbers 'n' where 'n+1' is prime, viz:
[1, 4, 16, 36, 100, 196, 256, 400, 576, 676]

XPL0

<lang XPL0>func IsPrime(N); \Return 'true' if N is prime int N, I; [if N <= 2 then return N = 2; if (N&1) = 0 then \even >2\ return false; for I:= 3 to sqrt(N) do

   [if rem(N/I) = 0 then return false;
   I:= I+1;
   ];

return true; ]; \IsPrime

int N; [for N:= 1 to sqrt(1000-1) do

   if IsPrime(N*N+1) then
       [IntOut(0, N*N);  ChOut(0, ^ )];

]</lang>

Output:
1 4 16 36 100 196 256 400 576 676