Quadrat special primes
- Task
n is smallest prime such that the difference of successive terms are the smallest quadrat numbers,
where n < 16000.
Raku
<lang perl6>my @sqp = 2, -> $previous {
my $next; for (1..∞).map: *² { $next = $previous + $_; last if $next.is-prime; } $next
} … *;
say "{+$_} matching numbers:\n", $_>>.fmt('%5d').batch(7).join: "\n" given
@sqp[^(@sqp.first: * > 16000, :k)];</lang>
- Output:
49 matching numbers: 2 3 7 11 47 83 227 263 587 911 947 983 1019 1163 1307 1451 1487 1523 1559 2459 3359 4259 4583 5483 5519 5843 5879 6203 6779 7103 7247 7283 7607 7643 8219 8363 10667 11243 11279 11423 12323 12647 12791 13367 13691 14591 14627 14771 15671
REXX
<lang rexx>/*REXX program finds the smallest primes such that the difference of successive terms */ /*─────────────────────────────────────────────────── are the smallest quadrat numbers. */ parse arg hi cols . /*obtain optional argument from the CL.*/ if hi== | hi=="," then hi= 16000 /* " " " " " " */ if cols== | cols=="," then cols= 10 /* " " " " " " */ call genP /*build array of semaphores for primes.*/ w= 10 /*width of a number in any column. */
@sqp= 'the smallest primes < ' commas(hi) " such that the" , 'difference of successive terma are the smallest quadrat numbers'
if cols>0 then say ' index │'center(@sqp , 1 + cols*(w+1) ) if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─') sqp= 0; idx= 1 /*initialize number of sqp and index.*/ op= 1 $= /*a list of nice primes (so far). */
do j=0 by 0 do k=1 until !.np; np= op + k*k /*find the next square + oldPrime.*/ if np>= hi then leave j /*Is newPrime too big? Then quit.*/ end /*k*/ sqp= sqp + 1 /*bump the number of sqp's. */ op= np /*assign the newPrime to the oldPrime*/ if cols==0 then iterate /*Build the list (to be shown later)? */ c= commas(np) /*maybe add commas to the number. */ $= $ right(c, max(w, length(c) ) ) /*add a nice prime ──► list, allow big#*/ if sqp//cols\==0 then iterate /*have we populated a line of output? */ say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */ idx= idx + cols /*bump the index count for the output*/ end /*j*/
if $\== then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ say say 'Found ' commas(sqp) " of " @sqp exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ? /*──────────────────────────────────────────────────────────────────────────────────────*/ genP: !.= 0 /*placeholders for primes (semaphores).*/
@.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. */ !.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " " " " flags. */ #=5; s.#= @.# **2 /*number of primes so far; prime². */ /* [↓] generate more primes ≤ high.*/ do j=@.#+2 by 2 to hi /*find odd primes from here on. */ parse var j -1 _; if _==5 then iterate /*J divisible by 5? (right dig)*/ if j// 3==0 then iterate /*" " " 3? */ if j// 7==0 then iterate /*" " " 7? */ /* [↑] the above five lines saves time*/ do k=5 while s.k<=j /* [↓] divide by the known odd primes.*/ if j // @.k == 0 then iterate j /*Is J ÷ X? Then not prime. ___ */ end /*k*/ /* [↑] only process numbers ≤ √ J */ #= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ end /*j*/; return</lang>
- output when using the default inputs:
index │ the smallest primes < 16,000 such that the difference of successive terma are the smallest quadrat numbers ───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── 1 │ 2 3 7 11 47 83 227 263 587 911 11 │ 947 983 1,019 1,163 1,307 1,451 1,487 1,523 1,559 2,459 21 │ 3,359 4,259 4,583 5,483 5,519 5,843 5,879 6,203 6,779 7,103 31 │ 7,247 7,283 7,607 7,643 8,219 8,363 10,667 11,243 11,279 11,423 41 │ 12,323 12,647 12,791 13,367 13,691 14,591 14,627 14,771 15,671 Found 49 of the smallest primes < 16,000 such that the difference of successive terma are the smallest quadrat numbers
Ring
<lang ring> load "stdlib.ring"
see "working..." + nl
Primes = [] limit1 = 50 oldPrime = 1 add(Primes,2)
for n = 1 to limit1
nextPrime = oldPrime + pow(n,2) if isprime(nextPrime) n = 1 add(Primes,nextPrime) oldPrime = nextPrime else nextPrime = nextPrime - oldPrime ok
next
see "prime1 prime2 Gap" + nl for n = 1 to Len(Primes)-1
diff = Primes[n+1] - Primes[n] see ""+ Primes[n] + " " + Primes[n+1] + " " + diff + nl
next
see nl + "done..." + nl </lang>
- Output:
working... prime1 prime2 Gap 2 3 1 3 7 4 7 11 4 11 47 36 47 83 36 83 227 144 227 263 36 263 587 324 587 911 324 911 947 36 947 983 36 983 1019 36 1019 1163 144 1163 1307 144 1307 1451 144 1451 1487 36 1487 1523 36 1523 1559 36 1559 2459 900 2459 3359 900 3359 4259 900 4259 4583 324 4583 5483 900 5483 5519 36 5519 5843 324 5843 5879 36 5879 6203 324 6203 6779 576 6779 7103 324 7103 7247 144 7247 7283 36 7283 7607 324 7607 7643 36 7643 8219 576 8219 8363 144 8363 10667 2304 10667 11243 576 11243 11279 36 11279 11423 144 11423 12323 900 12323 12647 324 12647 12791 144 12791 13367 576 13367 13691 324 13691 14591 900 14591 14627 36 14627 14771 144 14771 15671 900 done...