Wilson primes of order n: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: changed a comment.)
m (→‎{{header|REXX}}: used an IF/THEN/ELSE statement, changed some comments.)
Line 247: Line 247:
<lang rexx>/*REXX program finds and displays Wilson primes: a prime P such that P**2 divides:*/
<lang rexx>/*REXX program finds and displays Wilson primes: a prime P such that P**2 divides:*/
/*────────────────── (n-1)! * (P-n)! - (-1)**n where n is 1 ──◄ 11, and P < 18.*/
/*────────────────── (n-1)! * (P-n)! - (-1)**n where n is 1 ──◄ 11, and P < 18.*/
parse arg oLO oHI hip . /*obtain optional arguments from the CL*/
parse arg oLO oHI hip . /*obtain optional argument from the CL.*/
if oLO=='' | oLO=="," then oLO= 1 /*Not specified? Then use the default.*/
if oLO=='' | oLO=="," then oLO= 1 /*Not specified? Then use the default.*/
if oHI=='' | oHI=="," then oHI= 11 /* " " " " " " */
if oHI=='' | oHI=="," then oHI= 11 /* " " " " " " */
Line 256: Line 256:
parse value bignum 'E0' with ex 'E' ex . /*obtain possible exponent of factorial*/
parse value bignum 'E0' with ex 'E' ex . /*obtain possible exponent of factorial*/
numeric digits (max(9, ex+2) ) /*calculate max # of dec. digits needed*/
numeric digits (max(9, ex+2) ) /*calculate max # of dec. digits needed*/
call facts hip /*go & calculate a number of factorials*/
call facts hip /*calculate some memoized factorials.*/
title= ' Wilson primes P of order ' oLO " ──► " oHI', where P < ' commas(hip)
title= ' Wilson primes P of order ' oLO " ──► " oHI', where P < ' commas(hip)
w= length(title) + 1 /*width of columns of possible numbers.*/
w= length(title) + 1 /*width of columns of possible numbers.*/
say ' order │'center(title, w )
say ' order │'center(title, w )
say '───────┼'center("" , w, '─')
say '───────┼'center("" , w, '─')
do n=oLO to oHI /*search for Wilson primes of order N.*/
do n=oLO to oHI; pom= -1**n /*precalculate POM (Plus Or Minus) (±)*/
nmf= !(n-1); pom = -1**n /*precalculate a factorial and +|- */
nmf= !(n-1) /* " a particular factorial.*/
if n==1 then lim= 103 /*limit to known primes for 1st order. */
$=
lim= #; if n==1 then lim= 103 /*limit to known primes for 1st order. */
else lim= # /* " " those " " orders > 1 */
do j=1 for lim; p= @.j /*search through allowable primes. */
$= /*$: a line (output) of Wilson primes.*/
x= nmf * !(p-n) - pom /*calculate (n-1)! * (p-n)! - (-1)**n */
do j=1 for lim; p= @.j /*search through allowable primes. */
if x//sq.j \==0 then iterate /*is X ÷ prime squared? No, then skip.*/ /* ◄■■■■■■■ the filter.*/
x= nmf * !(p-n) - pom /*calculate (n-1)! * (p-n)! - (-1)**n */
$= $ ' ' commas(p) /*add a commatized prime ──► $ list.*/
if x//sq.j \==0 then iterate /*is X ÷ prime squared? No, then skip.*/ /* ◄■■■■■■■ the filter.*/
$= $ ' ' commas(p) /*add a commatized prime ──► $ list.*/
end /*p*/
end /*p*/


if $=='' then $= ' (none found within the range specified)'
if $=='' then $= ' (none found within the range specified)'
say center(n, 7)'│' substr($, 2) /*display what Wilson primes we found. */
say center(n, 7)'│' substr($, 2) /*display what Wilson primes we found. */
end /*n*/
end /*n*/
say '───────┴'center("" , w, '─')
say '───────┴'center("" , w, '─')
exit 0 /*stick a fork in it, we're all done. */
exit 0 /*stick a fork in it, we're all done. */