Find squares n where n+1 is prime: Difference between revisions
Content added Content deleted
(Realize in F#) |
SqrtNegInf (talk | contribs) (→{{header|Perl}}: alternate solutions) |
||
Line 135: | Line 135: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
===Simple and Clear=== |
|||
<lang perl>#!/usr/bin/perl |
<lang perl>#!/usr/bin/perl |
||
use strict; # https://rosettacode.org/wiki/Find_squares_n_where_n%2B1_is_prime |
use strict; # https://rosettacode.org/wiki/Find_squares_n_where_n%2B1_is_prime |
||
use warnings; |
use warnings; |
||
Line 147: | Line 147: | ||
1 4 16 36 100 196 256 400 576 676 |
1 4 16 36 100 196 256 400 576 676 |
||
</pre> |
</pre> |
||
===More Than One Way=== |
|||
TMTOWTDI, right? So do it. |
|||
<lang perl>use strict; |
|||
use warnings; |
|||
use feature 'say'; |
|||
use ntheory 'is_prime'; |
|||
my $a; is_prime $_ and $a = sqrt $_-1 and $a == int $a and say $_-1 for 1..1000; # backwards approach |
|||
my $b; do { say $b**2 if is_prime 1 + ++$b**2 } until $b > int sqrt 1000; # do/until |
|||
my $c; while (++$c < int sqrt 1000) { say $c**2 if is_prime 1 + $c**2 } # while/if |
|||
say for map $_**2, grep is_prime 1 + $_**2, 1 .. int sqrt 1000; # for/map/grep |
|||
for (1 .. int sqrt 1000) { say $_**2 if is_prime 1 + $_**2 } # for/if |
|||
say $_**2 for grep is_prime 1 + $_**2, 1 .. int sqrt 1000; # for/grep |
|||
is_prime 1 + $_**2 and say $_**2 for 1 .. int sqrt 1000; # and/for |
|||
is_prime 1+$_**2&&say$_**2for 1..31; # and/for golf, FTW |
|||
# 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;</lang> |
|||
{{out}} |
|||
In all cases: |
|||
<pre>1 |
|||
4 |
|||
16 |
|||
36 |
|||
100 |
|||
196 |
|||
256 |
|||
400 |
|||
576 |
|||
676</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |