Long primes

Revision as of 02:17, 4 August 2018 by rosettacode>Gerard Schildberger (added a new task (long primes) and two REXX language entries.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


A   long prime   (the definition that will be used here)   are primes whose reciprocals   (in decimal)   have a   period length   of one less than the prime number   (also expressed in decimal).

Long primes is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.


Long primes   are also known as:

  •   base ten cyclic numbers
  •   full reptend primes
  •   golden primes
  •   long period primes
  •   maximal period primes
  •   proper primes


Example

7   is the first long prime,   the reciprocal of seven is   1/7,   which is equal to the repeating decimal fraction   0.142857142857···

The length of the   repeating   part of the decimal fraction is six,   (the underlined part)   which is one less than the (decimal) prime number   7.
Thus   7   is a long prime.


There are other (more) general definitions of a   long prime   which include wording/verbiage for other bases other than ten.


Task
  •   Show all long primes up to   500   (preferably on one line).
  •   Show the   number   of long primes up to       500
  •   Show the   number   of long primes up to     1,000
  •   Show the   number   of long primes up to     2,000
  •   Show the   number   of long primes up to     4,000
  •   Show the   number   of long primes up to     8,000
  •   Show the   number   of long primes up to   16,000
  •   Show the   number   of long primes up to   32,000
  •   Show the   number   of long primes up to   64,000   (optional)
  •   Show all output here.


Also see



REXX

For every   doubling   of the limit, it takes about roughly   8   times longer to compute the long primes.

uses odd numbers

<lang rexx>/*REXX pgm calculates/displays base ten long primes (AKA golden primes, proper primes,*/ /*───────────────────── maximal period primes, long period primes, full reptend primes).*/ parse arg a /*obtain optional argument from the CL.*/ if a= | a="," then a= '500 -500 -100 -2000 -4000 -8000 -16000' /*use the default?*/

   do k=1  for words(a);     H=word(a, k)       /*step through the list of high limits.*/
   neg= H<1                                     /*used as an indicator to display count*/
   H= abs(H)                                    /*obtain the absolute value of  H.     */
   numeric digits max(H, 500)                   /*insure enough dec digs for periodLen.*/
   $=                                           /*the list of  long primes   (so far). */
      do j=7  to H  by 2                        /*start with 7,  just use odd integers.*/
      if .len(j) + 1 \== j  then iterate        /*period length too small?  "    "   " */
      $=$ j                                     /*add the   long prime   to the $ list.*/
      end   /*j*/
   say
   if neg  then do;  say 'number of long primes ≤ '    H     " is: "     words($);    end
           else do;  say   'list of long primes ≤ '    H":";         say strip($);    end
   end      /*k*/

exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ .len: procedure; parse arg x -1 z; y=9 /*obtain the argument from the caller. */

                if z==5  then return 0          /*if the last digit is 5,  then skip.  */
                _=1
                         do  while y//x \== 0;       y= y'9';          _= length(y)
                         end   /*while*/
                return _</lang>
list of long primes ≤  500:
7 17 19 23 29 47 59 61 97 109 113 131 149 167 179 181 193 223 229 233 257 263 269 313 337 367 379 383 389 419 433 461 487 491 499

number of long primes ≤  500  is:  35

number of long primes ≤  1000  is:  60

number of long primes ≤  2000  is:  116

number of long primes ≤  4000  is:  218

number of long primes ≤  8000  is:  390

number of long primes ≤  16000  is:  716

number of long primes ≤  32000  is:  1300

uses primes

This REXX version is about   15%   faster than the 1st REXX version   (becauses it only tests primes). <lang rexx>/*REXX pgm calculates/displays base ten long primes (AKA golden primes, proper primes,*/ /*───────────────────── maximal period primes, long period primes, full reptend primes).*/ parse arg a /*obtain optional argument from the CL.*/ if a= | a="," then a= '500 -500 -1000 -2000 -4000 -8000 -16000 -32000' /*use default?*/ m=0; aa=words(a) /* [↑] two list types of low primes. */

   do j=1  for aa;   m= max(m, abs(word(a, j))) /*find the maximum argument in the list*/
   end   /*j*/

call genP /*go and generate some primes. */

   do k=1  for aa;           H=word(a, k)       /*step through the list of high limits.*/
   neg= H<1                                     /*used as an indicator to display count*/
   H= abs(H)                                    /*obtain the absolute value of  H.     */
   numeric digits max(H, 500)                   /*insure enough dec digs for periodLen.*/
   $=                                           /*the list of  long primes   (so far). */
      do j=7  to H  by 2
      if \@.j               then iterate        /*Is  J  not a prime?    Then skip it. */
      if .len(j) + 1 \== j  then iterate        /*period length too small?  "    "   " */
      $=$ j                                     /*add the   long prime   to the $ list.*/
      end   /*j*/                               /* [↑]  some pretty weak prime testing.*/
   say
   if neg  then do;  say 'number of long primes ≤ '    H     " is: "     words($);    end
           else do;  say   'list of long primes ≤ '    H":";         say strip($);    end
   end      /*k*/

exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ genP: @.=0; @.2=1; @.3=1; @.5=1; @.7=1; @.11=1;  !.=0; !.1=2; !.2=3; !.3=5; !.4=7; !.5=11

     #=5                                        /*the number of primes  (so far).      */
         do g=!.#+2  by 2  until #>=m           /*gen enough primes to satisfy max  A. */
               do d=2  until !.d**2 > g         /*only divide up to square root of  X. */
               if g // !.d == 0  then iterate g /*Divisible?   Then skip this integer. */
               end   /*d*/                      /* [↓]  a spanking new prime was found.*/
         #=#+1;  @.g=1;  !.#=g                  /*bump P counter; assign P, add to P's.*/
         end         /*g*/
     return

/*──────────────────────────────────────────────────────────────────────────────────────*/ .len: procedure; parse arg x; _=1; y=9 /*obtain the argument from the caller. */

                         do  while y//x \== 0;       y= y'9';          _= length(y)
                         end   /*while*/
                return _</lang>
output   is identical to the 1st REXX version.