Jump to content

Find adjacent primes which differ by a square integer: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|J}}: document the algorithm)
m (syntax highlighting fixup automation)
Line 6:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">F primes_upto(limit)
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
Line 24:
V diff = pr1 - pr2
I (is_square(diff) & diff > 36)
print(pr1‘ ’pr2‘ diff = ’diff)</langsyntaxhighlight>
 
{{out}}
Line 58:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang="algol68">BEGIN # find a adjacent primes where the primes differ by a square > 36 #
INT min diff = 37;
INT max prime = 1 000 000;
Line 81:
FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 113:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f FIND_ADJACENTS_PRIMES_WHICH_DIFFERENCE_IS_SQUARE_INTEGER.AWK
# converted from FreeBASIC
Line 155:
return(q)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 188:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include<stdio.h>
#include<stdlib.h>
 
Line 222:
}
return 0;
}</langsyntaxhighlight>
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">% Integer square root
isqrt = proc (s: int) returns (int)
x0: int := s/2
Line 285:
end
end
end start_up</langsyntaxhighlight>
{{out}}
<pre> 89753 - 89689 = 64 = 8^2
Line 316:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Find adjacents primes which difference is square integer . Nigel Galloway: November 23rd., 2021
primes32()|>Seq.takeWhile((>)1000000)|>Seq.pairwise|>Seq.filter(fun(n,g)->let n=g-n in let g=(float>>sqrt>>int)n in g>6 && n=g*g)|>Seq.iter(printfn "%A")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 352:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: formatting io kernel lists lists.lazy math math.functions
math.primes.lists sequences ;
 
Line 374:
"============================" print
big-sq-adj-primes-diff [ second 1,000,000 < ] lwhile
[ "%-6d %-6d %d\n" vprintf ] leach</langsyntaxhighlight>
{{out}}
<pre>
Line 410:
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">Func Issqr( n ) = if (Sqrt(n))^2=n then 1 else 0 fi.;
i:=3;
j:=3;
Line 422:
j:=j+2;
od;
od;</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
 
function nextprime( n as uinteger ) as uinteger
Line 447:
if j-i > 36 and issquare(j-i) then print i, j, j-i
i = j
wend</langsyntaxhighlight>
{{out}}<pre>
89689 89753 64
Line 480:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 503:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 511:
 
=={{header|GW-BASIC}}==
<langsyntaxhighlight lang="gwbasic">10 P=3 : P2=0
20 GOSUB 180
30 IF P2>1000000! THEN END
Line 535:
230 GOSUB 80
240 IF Q = 1 THEN P2 = P: P = T: RETURN
250 GOTO 220</langsyntaxhighlight>
 
=={{header|J}}==
<langsyntaxhighlight Jlang="j"> #(,.-~/"1) p:0 1+/~I.(= <.)6.5>.%:2-~/\p:i.p:inv 1e6 NB. count them
26
(,.-~/"1) p:0 1+/~I.(= <.)6.5>.%:2-~/\p:i.p:inv 1e6 NB. show them
Line 566:
954763 954827 64
981823 981887 64
997813 997877 64</langsyntaxhighlight>
 
In other words: enumerate primes less than 1e6, find the pairwise differences, find where the prime pairs where maximum of their square root and 6.5 is an integer, and list those pairs with their differences.
Line 577:
 
'''Preliminaries'''
<langsyntaxhighlight lang="jq">def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
# Primes less than . // infinite
Line 584:
| if $n < 3 then empty
else 2, (range(3; $n) | select(is_prime))
end;</langsyntaxhighlight>
'''The task'''
<langsyntaxhighlight lang="jq"># Input is given to primes/0 - to determine the maximum prime to consider
# Output: stream of [$prime, $nextPrime]
def adjacentPrimesWhichDifferBySquare:
Line 611:
| "\(.[1]|l) - \(.[0]|l) = \($diff|lpad(4))" ) ;
 
1E6 | task(36)</langsyntaxhighlight>
{{out}}
As for [[#ALGOL_68]].
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function squareprimegaps(limit)
Line 640:
squareprimegaps(10_000_000_000)
 
</langsyntaxhighlight>{{out}}
<pre>
 
Line 671:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ps = Prime[Range[PrimePi[10^6]]];
ps = Partition[ps, 2, 1];
ps = {#1, #2, #2 - #1} & @@@ ps;
ps //= Select[Extract[{3}]/*GreaterThan[36]];
ps //= Select[Extract[{3}]/*Sqrt/*IntegerQ];
ps // Grid</langsyntaxhighlight>
{{out}}
<pre>89689 89753 64
Line 706:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">
for(i=3,1000000,j=nextprime(i+1);if(isprime(i)&&j-i>36&&issquare(j-i),print(i," ",j," ",j-i)))
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Find_adjacents_primes_which_difference_is_square_integer
Line 721:
(my $diff = $primeref->[$i] - $primeref->[$i - 1]) > 36 or next;
is_square($diff) and print "$primeref->[$i] - $primeref->[$i - 1] = $diff\n";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 753:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1_000_000</span>
Line 771:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 803:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
import math
print("working...")
Line 837:
 
print("done...")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 871:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
use Math::Primesieve;
 
Line 894:
say "\nGap {$p.key}: {comma @counts[$p.key]} found$ten:";
put join "\n", $p.value.batch(5)».map({"($_, {$_+ $p.key})"})».join(', ');
}</langsyntaxhighlight>
{{out}}
<pre>Adjacent primes up to 10,000,000,000 with a gap value that is a perfect square:
Line 933:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 967:
next
return 0
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,001:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require "prime"
 
Prime.each(1_000_000).each_cons(2) do |a, b|
Line 1,009:
puts "#{b} - #{a} = #{diff}" if isqrt*isqrt == diff
end
</syntaxhighlight>
</lang>
{{out}}
<pre>89753 - 89689 = 64
Line 1,039:
</pre>
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var p = 2
var upto = 1e6
 
Line 1,047:
}
p = q
})</langsyntaxhighlight>
{{out}}
<pre>
Line 1,081:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "./math" for Int
import "./fmt" for Fmt
 
Line 1,095:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,129:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if odd N > 2 is prime
int N, I;
[for I:= 3 to sqrt(N) do
Line 1,156:
N:= N+1; \step by 1+1 = 2 (for odd numbers)
];
]</langsyntaxhighlight>
 
{{out}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.