Wieferich primes: Difference between revisions
Content added Content deleted
m (Corrected spelling of Wikipedia link) |
(→{{header|REXX}}: added the computer programming language REXX.) |
||
Line 195: | Line 195: | ||
{{out}} |
{{out}} |
||
<pre>Weiferich primes less than 5000: 1093, 3511</pre> |
<pre>Weiferich primes less than 5000: 1093, 3511</pre> |
||
=={{header|REXX}}== |
|||
<lang rexx>/*REXX program finds and displays Weiferich primes which are under a specified limit N*/ |
|||
parse arg n . /*obtain optional argument from the CL.*/ |
|||
if n=='' | n=="," then n= 5000 /*Not specified? Then use the default.*/ |
|||
numeric digits 3000 /*bump # of dec. digs for calculation. */ |
|||
numeric digits max(9, length(2**n) ) /*calculate # of decimal digits needed.*/ |
|||
call genP /*build array of semaphores for primes.*/ |
|||
title= ' Weiferich primes that are < ' commas(n) /*title for the output. */ |
|||
w= length(title) + 2 /*width of field for the primes listed.*/ |
|||
say ' index │'center(title, w) /*display the title for the output. */ |
|||
say '───────┼'center("" , w, '─') /* " a sep for the output. */ |
|||
wp= 0 /*initialize number of Weiferich primes*/ |
|||
do j=1 to #; p= @.j; pm= p - 1 /*search for Weiferich primes in range.*/ |
|||
if (2**pm - 1) // p**2\==0 then iterate /*P**2 not evenly divide 2**(P-1) - 1?*/ |
|||
wp= wp + 1 /*bump the counter of Weiferich primes.*/ |
|||
say center(wp, 7)'│' center(commas(p), w) /*display the Weiferich prime to term. */ |
|||
end /*j*/ |
|||
say '───────┴'center("" , w, '─') /*display a foot sep for the output. */ |
|||
say |
|||
say 'Found ' commas(wp) title |
|||
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 n-1 /*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> |
|||
{{out|output|text= when using the default input:}} |
|||
<pre> |
|||
index │ Weiferich primes that are < 5,000 |
|||
───────┼────────────────────────────────────── |
|||
1 │ 1,093 |
|||
2 │ 3,511 |
|||
───────┴────────────────────────────────────── |
|||
Found 2 Weiferich primes that are < 5,000 |
|||
</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |