Palindromic primes in base 16: Difference between revisions

From Rosetta Code
Content added Content deleted
m (added highlighting.)
m (→‎{{header|REXX}}: changed comments.)
Line 76: Line 76:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program finds and displays palindromic primes for all N < 1000. */
<lang rexx>/*REXX program finds and displays palindromic primes in base 16 for all N < 500. */
parse arg hi cols . /*obtain optional argument from the CL.*/
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 200 /*Not specified? Then use the default.*/
if hi=='' | hi=="," then hi= 500 /*Not specified? Then use the default.*/
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
hidec= x2d(hi) /*obtain the decimal value of hi (hex).*/
call genP /*build array of semaphores for primes.*/
call genP /*build array of semaphores for primes.*/
w= 8 /*max width of a number in any column. */
w= 8 /*max width of a number in any column. */
Line 88: Line 87:
finds= 0; idx= 1 /*define # of palindromic primes & idx.*/
finds= 0; idx= 1 /*define # of palindromic primes & idx.*/
$= /*hex palindromic primes list (so far).*/
$= /*hex palindromic primes list (so far).*/
do j=1 for hidec; if \!.j then iterate /*J (decimal) not prime? Then skip.*/
do j=1 for hi; if \!.j then iterate /*J (decimal) not prime? Then skip.*/
x= d2x(j); if x\==reverse(x) then iterate /*Hex value not palindromic? " " */
x= d2x(j); if x\==reverse(x) then iterate /*Hex value not palindromic? " " */
finds= finds + 1 /*bump the number of palindromic primes*/
finds= finds + 1 /*bump the number of palindromic primes*/
Line 106: Line 105:
lowerHex: return translate( arg(1), 'abcdef', "ABCDEF") /*convert hex chars──►lowercase.*/
lowerHex: return translate( arg(1), 'abcdef', "ABCDEF") /*convert hex chars──►lowercase.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: !.= 0; hprime= max(hidec, reverse(hidec)) /*placeholders for primes (semaphores).*/
genP: !.= 0; hip= max(hi, copies(9,length(hi))) /*placeholders for primes (semaphores).*/
@.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. */
@.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. */
!.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " " " " flags. */
#=5; s.#= @.# **2 /*number of primes so far; prime². */
#=5; s.#= @.# **2 /*number of primes so far; prime². */
/* [↓] generate more primes ≤ high.*/
/* [↓] generate more primes ≤ high.*/
do j=@.#+2 by 2 to hprime /*find odd primes from here on. */
do j=@.#+2 by 2 to hip /*find odd primes from here on. */
parse var j '' -1 _; if _==5 then iterate /*J divisible by 5? (right dig)*/
parse var j '' -1 _; if _==5 then iterate /*J divisible by 5? (right dig)*/
if j// 3==0 then iterate /*" " " 3? */
if j// 3==0 then iterate /*" " " 3? */
Line 123: Line 122:
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
index │ palindromic primes in base 16 that are < 200
index │ palindromic primes in base 16 that are < 500
───────┼───────────────────────────────────────────────────────────────────────────────────────────
───────┼───────────────────────────────────────────────────────────────────────────────────────────
1 │ 2 3 5 7 b d 11 101 151 161
1 │ 2 3 5 7 b d 11 101 151 161
11 │ 191 1b1 1c1
11 │ 191 1b1 1c1
───────┴───────────────────────────────────────────────────────────────────────────────────────────
───────┴───────────────────────────────────────────────────────────────────────────────────────────

Found 13 palindromic primes in base 16 that are < 200
</pre>
</pre>



Revision as of 17:32, 23 June 2021

Palindromic primes in base 16 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.
Task

Find palindromic primes   n   in base 16,   where   n   <   50010

Factor

<lang factor>USING: kernel math.parser math.primes prettyprint sequences sequences.extras ;

500 primes-upto [ >hex ] [ dup reverse = ] map-filter .</lang>

Output:
V{
    "2"
    "3"
    "5"
    "7"
    "b"
    "d"
    "11"
    "101"
    "151"
    "161"
    "191"
    "1b1"
    "1c1"
}

Go

Library: Go-rcu

<lang go>package main

import (

   "fmt"
   "rcu"
   "strconv"
   "strings"

)

func reverse(s string) string {

   chars := []rune(s)
   for i, j := 0, len(chars)-1; i < j; i, j = i+1, j-1 {
       chars[i], chars[j] = chars[j], chars[i]
   }
   return string(chars)

}

func main() {

   fmt.Println("Primes < 500 which are palindromic in base 16:")
   primes := rcu.Primes(500)
   count := 0
   for _, p := range primes {
       hp := strconv.FormatInt(int64(p), 16)
       if hp == reverse(hp) {
           fmt.Printf("%3s ", strings.ToUpper(hp))
           count++
           if count%5 == 0 {
               fmt.Println()
           }
       }
   }
   fmt.Println("\n\nFound", count, "such primes.")

}</lang>

Output:
Primes < 500 which are palindromic in base 16:
  2   3   5   7   B 
  D  11 101 151 161 
191 1B1 1C1 

Found 13 such primes.

REXX

<lang rexx>/*REXX program finds and displays palindromic primes in base 16 for all N < 500. */ parse arg hi cols . /*obtain optional argument from the CL.*/ if hi== | hi=="," then hi= 500 /*Not specified? Then use the default.*/ if cols== | cols=="," then cols= 10 /* " " " " " " */ call genP /*build array of semaphores for primes.*/ w= 8 /*max width of a number in any column. */ title= ' palindromic primes in base 16 that are < ' hi if cols>0 then say ' index │'center(title, 1 + cols*(w+1) ) if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─') finds= 0; idx= 1 /*define # of palindromic primes & idx.*/ $= /*hex palindromic primes list (so far).*/

   do j=1  for hi;       if \!.j  then iterate  /*J (decimal) not prime?     Then skip.*/
   x= d2x(j);  if x\==reverse(x)  then iterate  /*Hex value not palindromic?   "    "  */
   finds= finds + 1                             /*bump the number of palindromic primes*/
   if cols<0                      then iterate  /*Build the list  (to be shown later)? */
   $= $  right( lowerHex(x), w)                 /*use a lowercase version of the hex #.*/
   if finds//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.*/ if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─') say say 'Found ' finds title exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ lowerHex: return translate( arg(1), 'abcdef', "ABCDEF") /*convert hex chars──►lowercase.*/ /*──────────────────────────────────────────────────────────────────────────────────────*/ genP: !.= 0; hip= max(hi, copies(9,length(hi))) /*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 hip                 /*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  3  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 │                       palindromic primes in base 16 that are  <  500
───────┼───────────────────────────────────────────────────────────────────────────────────────────
   1   │        2        3        5        7        b        d       11      101      151      161
  11   │      191      1b1      1c1
───────┴───────────────────────────────────────────────────────────────────────────────────────────

Ring

<lang ring> load "stdlib.ring" see "working..." + nl see "Palindromic primes in base 16:" + nl row = 0 decList = 0:15 baseList = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"] limit = 500

for n = 1 to limit

   hex = hex(n)
   if ispalindrome(hex) and isprime(n)
      see "" + upper(hex) + " "
      row = row + 1
      if row%5 = 0
         see nl
      ok
   ok

next

see nl + "Found " + row + " palindromic primes in base 16" + nl see "done..." + nl </lang>

Output:
working...
Palindromic primes in base 16:
2 3 5 7 B 
D 11 101 151 161 
191 1B1 1C1 
Found 13 palindromic primes in base 16
done...

Wren

Library: Wren-math
Library: Wren-fmt

<lang ecmascript>import "/math" for Int import "/fmt" for Conv, Fmt

System.print("Primes < 500 which are palindromic in base 16:") var primes = Int.primeSieve(500) var count = 0 for (p in primes) {

   var hp = Conv.Itoa(p, 16)
   if (hp == hp[-1..0]) {
       Fmt.write("$3s ", hp)
       count = count + 1
       if (count % 5 == 0) System.print()
   }

} System.print("\n\nFound %(count) such primes.")</lang>

Output:
Primes < 500 which are palindromic in base 16:
  2   3   5   7   B 
  D  11 101 151 161 
191 1B1 1C1 

Found 13 such primes.