Palindromic primes in base 16: Difference between revisions
m (→{{header|REXX}}: changed comments.) |
m (→{{header|REXX}}: added totals.) |
||
Line 127: | Line 127: | ||
11 │ 191 1b1 1c1 |
11 │ 191 1b1 1c1 |
||
───────┴─────────────────────────────────────────────────────────────────────────────────────────── |
───────┴─────────────────────────────────────────────────────────────────────────────────────────── |
||
Found 13 palindromic primes in base 16 that are < 500 |
|||
</pre> |
</pre> |
||
Revision as of 17:56, 23 June 2021
- 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
<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 ───────┴─────────────────────────────────────────────────────────────────────────────────────────── Found 13 palindromic primes in base 16 that are < 500
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
<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.