Palindromic primes in base 16: Difference between revisions
Content added Content deleted
(Added Wren) |
(Added Go) |
||
Line 4: | Line 4: | ||
<br><br> |
<br><br> |
||
=={{header|Go}}== |
|||
{{libheader|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> |
|||
{{out}} |
|||
<pre> |
|||
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. |
|||
</pre> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |
Revision as of 14:57, 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 in base 16, where n < 500
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.
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.